实验7
task4
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <ctype.h> 4 void count_lines_and_characters(const char *filename); 5 int main() { 6 const char *filename = "data4.txt"; 7 count_lines_and_characters(filename); 8 return 0; 9 } 10 void count_lines_and_characters(const char *filename) { 11 FILE *file = fopen(filename, "r"); 12 if (file == NULL) { 13 printf("无法打开文件:%s\n", filename); 14 return; 15 } 16 int line_count = 0; 17 int char_count = 0; 18 int ch; 19 while ((ch = fgetc(file)) != EOF) { 20 if (ch == '\n') { 21 line_count++; 22 } else if (!isspace(ch)) { 23 char_count++; 24 } 25 } 26 fclose(file); 27 printf("%s统计结果:\n", filename); 28 printf("行数:%d\n", line_count + 1); 29 printf("字符数(不计空白符):%d\n", char_count); 30 }

task5
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 10 5 6 typedef struct { 7 long id; 8 char name[20]; 9 float objective; 10 float subjective; 11 float sum; 12 char result[10]; 13 } STU; 14 void read(STU st[], int n); 15 void write(STU st[], int n); 16 int process(STU st[], int n, STU st_pass[]); 17 void output(STU st[], int n); 18 int main() { 19 STU stu[N], stu_pass[N]; 20 int cnt; 21 double pass_rate; 22 printf("从文件读入%d个考生信息...\n", N); 23 read(stu, N); 24 printf("\n对考生成绩进行统计...\n"); 25 cnt = process(stu, N, stu_pass); 26 printf("\n通过考试的名单:\n"); 27 output(stu, N); 28 write(stu_pass, cnt); 29 pass_rate = 1.0 * cnt / N; 30 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate * 100); 31 return 0; 32 } 33 void write(STU st[], int n) { 34 FILE *fout = fopen("list_pass.txt", "w"); 35 if (!fout) { 36 printf("fail to open file\n"); 37 return; 38 } 39 for (int i = 0; i < n; i++) { 40 fprintf(fout, "%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 41 } 42 fclose(fout); 43 } 44 int process(STU st[], int n, STU st_pass[]) { 45 int cnt = 0; 46 for (int i = 0; i < n; i++) { 47 st[i].sum = st[i].objective + st[i].subjective; 48 if (st[i].sum > 60) { 49 strcpy(st[i].result, "通过"); 50 cnt++; 51 memcpy(&st_pass[i - cnt + 1], &st[i], sizeof(STU)); 52 } else { 53 strcpy(st[i].result, "未通过"); 54 } 55 } 56 return cnt; 57 } 58 void output(STU st[], int n) { 59 int i; 60 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\n"); 61 for (i = 0; i < n; i++) 62 printf("%ld\t%s\t%.2f\t%.2f\t%.2f\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 63 } 64 void read(STU st[], int n) { 65 int i; 66 FILE *fin; 67 fin = fopen("examinee.txt", "r"); 68 if (!fin) { 69 printf("fail to open file\n"); 70 return; 71 } 72 for (i = 0; i < n; i++) 73 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 74 fclose(fin); 75 }

task6
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <time.h> 5 6 #define N 100 7 #define M 5 8 9 int main() { 10 char s[N][N]; 11 FILE *fp; 12 int i, n; 13 int flag[N] = {0}; 14 fp = fopen("list.txt", "r"); 15 if (!fp) { 16 perror("list.txt"); 17 return 1; 18 } 19 i = 0; 20 while (fgets(s[i], N, fp) != NULL) { 21 n = i; 22 i++; 23 } 24 fclose(fp); 25 srand((unsigned int)time(NULL)); 26 printf("------中奖名单------\n"); 27 for (int k = 0; k < M; k++) { 28 int r; 29 do { 30 r = rand() % (n + 1); 31 } while (flag[r]); 32 flag[r] = 1; 33 printf("%s", s[r]); 34 } 35 printf("\n"); 36 37 fp = fopen("20251222.txt", "w"); 38 if (!fp) { 39 perror("20251222.txt"); 40 return 1; 41 } 42 for (int k = 0; k < M; k++) { 43 int r; 44 do { 45 r = rand() % (n + 1); 46 } while (flag[r]); 47 flag[r] = 1; 48 fprintf(fp, "%s\n", s[r]); 49 } 50 fclose(fp); 51 printf("文件保存成功!\n"); 52 53 printf("------20251222中奖名单------\n"); 54 char temp[N]; 55 for (int k = 0; k < M; k++) { 56 for (int j = k + 1; j < M; j++) { 57 if (strncmp(s[k], s[j], 9) > 0) { 58 strcpy(temp, s[k]); 59 strcpy(s[k], s[j]); 60 strcpy(s[j], temp); 61 } 62 } 63 } 64 for (int k = 0; k < M; k++) { 65 printf("%s", s[k]); 66 } 67 printf("\n"); 68 69 fp = fopen("20251222.txt", "a"); 70 if (!fp) { 71 perror("20251222.txt"); 72 return 1; 73 } 74 for (int k = 0; k < M; k++) { 75 fprintf(fp, "%s\n", s[k]); 76 } 77 fclose(fp); 78 printf("文件保存成功!\n"); 79 80 return 0; 81 }

浙公网安备 33010602011771号