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

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

任务6》
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <time.h> 5 #define NUM 80 6 #define SELECT 5 7 #define N 100 8 int main(){ 9 FILE *fp; 10 fp=fopen("list.txt","r"); 11 if(!fp){ 12 perror("list.txt"); 13 return 1; 14 } 15 char s[NUM][N]; 16 int count=0; 17 while(fgets(s[count],N,fp)!=NULL&&count<NUM) { 18 count++; 19 } 20 fclose(fp); 21 srand((unsigned int)time(NULL)) ; 22 int flag[NUM]={0}; 23 int selected[SELECT]; 24 int i=0,random; 25 while(i<SELECT){ 26 random=rand()%count; 27 if(flag[random]==0){ 28 flag[random]=1; 29 selected[i]=random; 30 i++; 31 } 32 } 33 printf("------------中奖名单------------\n"); 34 int j; 35 for(j=0;j<SELECT;++j){ 36 printf("%s",s[selected[j]]); 37 } 38 char file[N]; 39 printf("------------保存到文件------------\n"); 40 printf("输入文件名:") ; 41 scanf("%s",file) ; 42 fp=fopen(file,"w"); 43 if(!fp){ 44 perror("file"); 45 return 1; 46 } 47 for(j=0;j<SELECT;++j){ 48 fprintf(fp,"%s\n",s[selected[j]]); 49 } 50 fclose(fp); 51 printf("保存成功!\n"); 52 return 0; 53 }


浙公网安备 33010602011771号