实验7
任务4:
源代码:
1 #include<stdio.h> 2 #include<string.h> 3 #define N 100 4 5 int main(){ 6 char s[N]; 7 int i=0,j,cl=0,cc=0; 8 FILE *fp; 9 fp = fopen("data4.txt","r"); 10 11 while(!feof(fp)){ 12 fgets(s,N,fp); 13 cl++; 14 i=0; 15 while(s[i]!='\0'){ 16 if(s[i]!=' '&&s[i]!='\n') 17 cc++; 18 19 i++; 20 } 21 } 22 printf("统计结果:\n"); 23 printf("行数:%d\n字符数:%d\n",cl,cc); 24 25 return 0; 26 }

任务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 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 // 把通过考试的考生完整信息写入文件list_pass.txt 72 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 73 void write(STU s[], int n) { 74 // 待补足 75 // xxx 76 int i; 77 FILE *fp; 78 fp = fopen("list_pass.txt","w"); 79 80 if(!fp) 81 printf("failed to open file"); 82 83 fprintf(fp,"准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 84 for(i=0;i<N;i++){ 85 if(s[i].sum>=60) 86 fprintf(fp,"%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].result); 87 } 88 89 90 91 } 92 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 93 int process(STU st[], int n, STU st_pass[]) { 94 // 待补足 95 // xxx 96 int cnt=0,i; 97 for(i=0;i<N;i++){ 98 st[i].sum=st[i].objective+st[i].subjective; 99 if(st[i].sum>=60){ 100 strcpy(st[i].result,"通过"); 101 st_pass[cnt++]=st[i]; 102 } 103 if(st[i].sum<60) 104 strcpy(st[i].result,"不通过"); 105 } 106 return cnt; 107 108 }


任务6:
源代码:
1 #include<stdio.h> 2 #include<string.h> 3 #include<time.h> 4 #include<stdlib.h> 5 #define N 80 6 typedef struct STUINFO{ 7 char number[20]; 8 char name[20]; 9 char classes[20]; 10 }STU; 11 12 int main(){ 13 char s[N]; 14 srand((unsigned)time(NULL)); 15 STU x[N]; 16 FILE *fp,*fin; 17 int i,j,n[5]; 18 fp=fopen("list.txt","r"); 19 20 for(i=0;i<N;i++) 21 fscanf(fp,"%s %s %s",x[i].number,x[i].name,x[i].classes); 22 23 fclose(fp); 24 25 26 for(i=0;i<5;){ 27 n[i]=rand()%80; 28 for(j=0;j<i;j++) 29 if(n[i]==n[j]) 30 break; 31 if(j==i) 32 i++; 33 } 34 35 printf("----------随机抽点名单----------\n"); 36 for(i=0;i<5;i++) 37 printf("%-20s %-10s %-20s\n",x[n[i]].number,x[n[i]].name,x[n[i]].classes); 38 39 printf("----------保存到文件----------\n"); 40 printf("输入文件名:\n"); 41 scanf("%s",&s); 42 43 fin=fopen(s,"w"); 44 for(i=0;i<5;i++){ 45 fprintf(fin,"%-20s %-10s %-20s\n",x[n[i]].number,x[n[i]].name,x[n[i]].classes); 46 } 47 48 fclose(fin); 49 50 printf("保存成功!"); 51 52 return 0; 53 }


浙公网安备 33010602011771号