2025.12.23 C语言程序设计上机实习八

2025.12.23 C语言程序设计上机实习八

必做题

image-20251223190120471

选做题

题目源文件实验八.pdf

题目1:文件中字符串的模糊匹配

函数FuzzyMatchLine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
int FuzzyMatchLine(const char *filename, int i, const char *pattern)
{
if (pattern[0] == '\0') return 1;
if (i < 1) return 0;
char line[100];
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL) return 0;
for (int j = 1; j <= i; j++)
{
if (fgets(line, sizeof(line), fp) == NULL)
{
fclose(fp);
return 0;
}
}
for (int k = 0; line[k] != '\0'; k++)
{
line[k] = tolower((unsigned char)line[k]);
}
char *pattern_temp;
pattern_temp = NULL;
pattern_temp = (char *)malloc(strlen(pattern) * sizeof(char));
memcpy(pattern_temp, pattern, strlen(pattern));
for(int l = 0; pattern_temp[l] != '\0'; l++)
{
pattern_temp[l] = tolower((unsigned char)pattern_temp[l]);
}
if (strstr(line, pattern_temp) != NULL)
{
free(pattern_temp);
fclose(fp);
return 1;
}
free(pattern_temp);
fclose(fp);
return 0;
}

用相应测试例测试的运行结果截图

image-20251223195329876

题目2:学生信息文件排序

函数cmpByNameThenId

1
2
3
4
5
6
7
8
9
10
11
12
13
int cmpByNameThenId(const void *a, const void *b)
{
const struct Student *s1 = (const struct Student *)a;
const struct Student *s2 = (const struct Student *)b;

int nameCmp = strcmp(s1->name, s2->name);
if (nameCmp != 0)
{
return nameCmp;
}

return s1->id - s2->id;
}

函数cmpByAgeThenId

1
2
3
4
5
6
7
8
9
10
11
12
int cmpByAgeThenId(const void *a, const void *b)
{
const struct Student *s1 = (const struct Student *)a;
const struct Student *s2 = (const struct Student *)b;

if (s1->age != s2->age)
{
return s1->age - s2->age;
}

return s1->id - s2->id;
}

函数cmpById

1
2
3
4
5
6
7
int cmpById(const void *a, const void *b)
{
const struct Student *s1 = (const struct Student *)a;
const struct Student *s2 = (const struct Student *)b;

return s1->id - s2->id;
}

函数LoadAndSortStudents

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void LoadAndSortStudents(char mode, struct Student **stus, int *n)
{
FILE *fp;
fp = fopen("stuInfo.dat", "rb");
if (fp == NULL) return;
fread(n, sizeof(int), 1, fp);
*stus = (struct Student *)malloc(sizeof(struct Student) * (*n));
if (*stus == NULL)
{
fclose(fp);
return;
}
fread(*stus, sizeof(struct Student), *n, fp);

switch(mode) {
case 'n': goto n;
case 'a': goto a;
case 'i': goto i;
default: fclose(fp); free(*stus); return;
}
n:
qsort(*stus, *n, sizeof(struct Student), cmpByNameThenId);
fclose(fp);
return;
a:
qsort(*stus, *n, sizeof(struct Student), cmpByAgeThenId);
fclose(fp);
return;
i:
qsort(*stus, *n, sizeof(struct Student), cmpById);
fclose(fp);
return;
}

用相应测试例测试的运行结果截图

image-20251223203152130

附:完整代码

FuzzyMatch.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

// 你需要实现的函数
int FuzzyMatchLine(const char *filename, int i, const char *pattern);

int main()
{
int i;
char pattern[51]; // 题目说明 pattern 长度 ≤ 50

if (scanf("%d", &i) != 1)
{
printf("No\n");
return 0;
}

// 跳过换行符
getchar();

if (fgets(pattern, sizeof(pattern), stdin) == NULL)
{
printf("No\n");
return 0;
}

// 去除 fgets 读入的换行符(如果存在)
size_t len = strlen(pattern);
if (len > 0 && pattern[len - 1] == '\n')
{
pattern[len - 1] = '\0';
}

int result = FuzzyMatchLine("strings.txt", i, pattern);

if (result == 1)
{
printf("Yes\n");
}
else
{
printf("No\n");
}

return 0;
}


int FuzzyMatchLine(const char *filename, int i, const char *pattern)
{
if (pattern[0] == '\0') return 1;
if (i < 1) return 0;
char line[100];
FILE *fp;
fp = fopen(filename, "r");
if (fp == NULL) return 0;
for (int j = 1; j <= i; j++)
{
if (fgets(line, sizeof(line), fp) == NULL)
{
fclose(fp);
return 0;
}
}
for (int k = 0; line[k] != '\0'; k++)
{
line[k] = tolower((unsigned char)line[k]);
}
char *pattern_temp;
pattern_temp = NULL;
pattern_temp = (char *)malloc(strlen(pattern) * sizeof(char));
memcpy(pattern_temp, pattern, strlen(pattern));
for(int l = 0; pattern_temp[l] != '\0'; l++)
{
pattern_temp[l] = tolower((unsigned char)pattern_temp[l]);
}
if (strstr(line, pattern_temp) != NULL)
{
free(pattern_temp);
fclose(fp);
return 1;
}
free(pattern_temp);
fclose(fp);
return 0;
}

SortStudents.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Student
{
char name[21]; // 姓名,最多20字符 + '\0'
int age; // 年龄
int id; // 学号
};

// 你需要实现
void LoadAndSortStudents(char mode, struct Student **stus, int *n);

int main()
{
char mode = getchar();
struct Student *students;
int n;
LoadAndSortStudents(mode, &students, &n);
for (int i = 0; i < n; i++)
{
printf("id: %d, name: %s, age: %d\n", students[i].id, students[i].name, students[i].age);
}
return 0;
}

int cmpByNameThenId(const void *a, const void *b)
{
const struct Student *s1 = (const struct Student *)a;
const struct Student *s2 = (const struct Student *)b;

int nameCmp = strcmp(s1->name, s2->name);
if (nameCmp != 0)
{
return nameCmp;
}

return s1->id - s2->id;
}

int cmpByAgeThenId(const void *a, const void *b)
{
const struct Student *s1 = (const struct Student *)a;
const struct Student *s2 = (const struct Student *)b;

if (s1->age != s2->age)
{
return s1->age - s2->age;
}

return s1->id - s2->id;
}

int cmpById(const void *a, const void *b)
{
const struct Student *s1 = (const struct Student *)a;
const struct Student *s2 = (const struct Student *)b;

return s1->id - s2->id;
}


void LoadAndSortStudents(char mode, struct Student **stus, int *n)
{
FILE *fp;
fp = fopen("stuInfo.dat", "rb");
if (fp == NULL) return;
fread(n, sizeof(int), 1, fp);
*stus = (struct Student *)malloc(sizeof(struct Student) * (*n));
if (*stus == NULL)
{
fclose(fp);
return;
}
fread(*stus, sizeof(struct Student), *n, fp);

switch(mode) {
case 'n': goto n;
case 'a': goto a;
case 'i': goto i;
default: fclose(fp); free(*stus); return;
}
n:
qsort(*stus, *n, sizeof(struct Student), cmpByNameThenId);
fclose(fp);
return;
a:
qsort(*stus, *n, sizeof(struct Student), cmpByAgeThenId);
fclose(fp);
return;
i:
qsort(*stus, *n, sizeof(struct Student), cmpById);
fclose(fp);
return;
}