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

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

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


浙公网安备 33010602011771号