task4
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main() { 5 FILE *fp = fopen("data4.txt", "r"); 6 if (fp == NULL) { 7 perror("无法打开文件"); 8 return 1; 9 } 10 11 int lines = 0; 12 int count = 0; 13 int has_char = 0; 14 int c; 15 16 while ((c = fgetc(fp)) != EOF) { 17 if (c == '\n') { 18 lines++; 19 has_char = 0; 20 } else if (c == ' ' || c == '\t' || c == '\r') { 21 has_char = 1; 22 } else { 23 count++; 24 has_char = 1; 25 } 26 } 27 28 if (has_char) { 29 lines++; 30 } 31 32 fclose(fp); 33 34 printf("data4.txt统计结果:\n"); 35 printf("行数:%21d\n", lines); 36 printf("字符数(不计空白符):%6d\n", count); 37 38 return 0; 39 }

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 15 // 函数声明 16 void read(STU st[], int n); 17 void write(STU st[], int n); 18 void output(STU st[], int n); 19 int process(STU st[], int n, STU st_pass[]); 20 21 int main() { 22 STU stu[N], stu_pass[N]; 23 int cnt; 24 double pass_rate; 25 26 printf("从文件读入%d个考生信息...\n", N); 27 read(stu, N); 28 29 printf("\n对考生成绩进行统计...\n"); 30 cnt = process(stu, N, stu_pass); 31 32 printf("\n通过考试的名单:\n"); 33 output(stu, N); // 输出所有考生完整信息到屏幕 34 write(stu, N); // 输出考试通过的考生信息到文件 35 36 pass_rate = 1.0 * cnt / N; 37 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 38 39 return 0; 40 } 41 42 // 把所有考生完整信息输出到屏幕上 43 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 44 void output(STU st[], int n) { 45 int i; 46 47 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 48 for (i = 0; i < n; i++) 49 printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result); 50 } 51 52 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 53 void read(STU st[], int n) { 54 int i; 55 FILE *fin; 56 57 fin = fopen("examinee.txt", "r"); 58 if (!fin) { 59 printf("fail to open file\n"); 60 return; 61 } 62 63 while (!feof(fin)) { 64 for (i = 0; i < n; i++) 65 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 66 } 67 68 fclose(fin); 69 } 70 71 void write(STU s[], int n) { 72 FILE *fout = fopen("list_pass.txt", "w"); 73 if (!fout) { 74 printf("fail to create file\n"); 75 return; 76 } 77 78 fprintf(fout, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t 结果\n"); 79 80 int i; 81 for (i = 0; i < n; i++) { 82 fprintf(fout, "%ld\t %s\t %.2f\t %.2f\t %.2f\t %s\n", 83 s[i].id, s[i].name, s[i].objective, 84 s[i].subjective, s[i].sum, s[i].result); 85 } 86 87 fclose(fout); 88 printf("考试通过的考生信息已写入文件 list_pass.txt\n"); 89 } 90 91 int process(STU st[], int n, STU st_pass[]) { 92 int pass_count = 0; 93 int i; 94 95 for (i = 0; i < n; i++) { 96 st[i].sum = st[i].objective + st[i].subjective; 97 98 if (st[i].sum >= 60) { 99 strcpy(st[i].result, "通过"); 100 st_pass[pass_count++] = st[i]; 101 } else { 102 strcpy(st[i].result, "不通过"); 103 } 104 } 105 106 return pass_count; 107 }


task 6:
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <time.h> 5 6 #define MAX_PARTICIPANTS 100 7 #define WINNER_COUNT 5 8 9 typedef struct { 10 char id[20]; // 学号 11 char name[20]; // 姓名 12 char class[50]; // 班级 13 } Participant; 14 15 int main() { 16 FILE *inputFile = fopen("list.txt", "r"); 17 if (!inputFile) { 18 printf("无法打开文件 list.txt\n"); 19 return 1; 20 } 21 22 Participant participants[MAX_PARTICIPANTS]; 23 int count = 0; 24 int i; 25 26 while (fscanf(inputFile, "%s %s %[^\n]", participants[count].id, 27 participants[count].name, participants[count].class) == 3) { 28 count++; 29 if (count >= MAX_PARTICIPANTS) break; 30 } 31 fclose(inputFile); 32 33 if (count < WINNER_COUNT) { 34 printf("参与者数量不足,无法抽取 %d 个获奖者\n", WINNER_COUNT); 35 return 1; 36 } 37 38 srand(time(NULL)); 39 40 int selected[count]; 41 for (i = 0; i < count; i++) { 42 selected[i] = 0; 43 } 44 45 int winners[WINNER_COUNT]; 46 int winnerCount = 0; 47 48 while (winnerCount < WINNER_COUNT) { 49 int index = rand() % count; 50 51 if (!selected[index]) { 52 selected[index] = 1; 53 winners[winnerCount] = index; 54 winnerCount++; 55 } 56 } 57 58 printf("\n------------中奖名单------------\n"); 59 for (i = 0; i < WINNER_COUNT; i++) { 60 int idx = winners[i]; 61 printf("%s\t%s\t%s\n", participants[idx].id, 62 participants[idx].name, participants[idx].class); 63 } 64 65 char outputFilename[100]; 66 printf("\n------------保存到文件------------\n"); 67 printf("输入文件名:") ; 68 scanf("%s", outputFilename); 69 70 FILE *outputFile = fopen(outputFilename, "w"); 71 if (!outputFile) { 72 printf("无法创建文件 %s\n", outputFilename); 73 return 1; 74 } 75 76 77 for (i = 0; i < WINNER_COUNT; i++) { 78 int idx = winners[i]; 79 fprintf(outputFile, "%s\t%s\t%s\n", participants[idx].id, 80 participants[idx].name, participants[idx].class); 81 } 82 fclose(outputFile); 83 84 printf("\n保存成功!\n", outputFilename); 85 86 return 0; 87 }

浙公网安备 33010602011771号