实验7
实验任务4
1 #include <stdio.h> 2 int main() { 3 FILE *fp; 4 char ch; 5 int line_count = 0; 6 int char_count = 0; 7 8 fp = fopen("data4.txt", "r"); 9 if (fp == NULL) { 10 printf("文件打开失败!\n"); 11 return 1; 12 } 13 14 while ((ch = fgetc(fp)) != EOF) { 15 if (ch == '\n') { 16 line_count++; 17 } 18 if (!(ch == ' ' || ch == '\t' || ch == '\n' || 19 ch == '\r' || ch == '\f' || ch == '\v')) { 20 char_count++; 21 } 22 } 23 printf("data4.txt统计结果:\n"); 24 printf("行数:%d\n", line_count); 25 printf("字符数(不计空白符):%d\n", char_count); 26 27 fclose(fp); 28 return 0; 29 }

实验任务5
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 for (i = 0; i < n; i++) 64 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 65 66 fclose(fin); 67 } 68 69 // 把通过考试的考生完整信息写入文件list_pass.txt 70 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 71 void write(STU s[], int n) { 72 FILE *passtxt; 73 passtxt = fopen("list_pass.txt", "w"); 74 if (!passtxt) { 75 printf("Open file failed(list_pass.txt)"); 76 return ; 77 } 78 79 fprintf(passtxt, "准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 80 for (int i = 0; i < n; i++) { 81 fprintf(passtxt, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", 82 s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 83 } 84 85 fclose(passtxt); 86 } 87 88 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 89 int process(STU st[], int n, STU st_pass[]) { 90 int j=0; 91 for(int i=0; i<n; i++) { 92 st[i].sum = st[i].objective + st[i].subjective; 93 strcpy(st[i].result, st[i].sum > 60 ? "通过" : "未通过"); 94 if(st[i].sum > 60) 95 st_pass[j++] = st[i]; 96 } 97 return j; 98 }

实验任务6
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include<time.h> 5 #define N 100 6 int main(){ 7 srand((unsigned int)time(NULL)); 8 int flag[N]={0}; 9 char s[N][N],st[N][N]; 10 FILE *fp; 11 FILE *fin; 12 int n,j,i=0; 13 int count=0; 14 long int num; 15 fp=fopen("list.txt","r"); 16 if(!fp){ 17 printf("fail to open file"); 18 return 1; 19 } 20 while(fgets(s[i],N,fp)!=NULL){ 21 i++; 22 } 23 n=i; 24 for(i=0;i<5;){ 25 num=0+rand()%(79-0+1); 26 if(flag[num]==0){ 27 28 flag[num]=1; 29 printf("%s",s[num]); 30 strcpy(st[count],s[num]); 31 count++; 32 i++; 33 } 34 } 35 36 fclose(fp); 37 fin=fopen("20251225.txt","w"); 38 if(!fin){ 39 printf("fail to open file"); 40 return 1; 41 } 42 for(i=0;i<5;i++){ 43 fputs(st[i],fin); 44 45 } 46 fclose(fin); 47 return 0; 48 }


浙公网安备 33010602011771号