实验7
task4
代码
1 #include<stdio.h> 2 3 int main(){ 4 char ch; 5 int count=0; 6 FILE *fp; 7 8 fp=fopen("C:\\Users\\admin\\Desktop\\博客园\\实验7\\data4.txt","r"); 9 if(fp==NULL){ 10 printf("error\n"); 11 return 1; 12 } 13 14 while(!(feof(fp))){ 15 ch=fgetc(fp); 16 if(ch==' '||ch=='\n'){ 17 continue; 18 } 19 if(ch==EOF){ 20 break; 21 } 22 count++; 23 }
24 fclose(fp); 25 printf("data4.txt中共包含字符数(不计空白字符):%d",count); 26 return 0; 27 }
结果

task5
代码
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define N 10 6 7 typedef struct { 8 long int id; 9 char name[20]; 10 float objective; 11 float subjective; 12 float sum; 13 char ans[10]; 14 } STU; 15 16 void finput(STU st[], int n); 17 void foutput(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 finput(stu, N); 28 29 printf("\n对考生成绩进行统计...\n"); 30 cnt = process(stu, N, stu_pass); 31 32 printf("\n通过考试的名单:\n"); 33 output(stu, N); 34 foutput(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 void output(STU st[], int n) { 43 int i; 44 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].ans); 48 } 49 50 void finput(STU st[], int n) { 51 int i; 52 FILE *fin; 53 54 fin = fopen("examinee.txt", "r"); 55 if (fin == NULL) { 56 printf("fail to open file\n"); 57 exit(0); 58 } 59 60 while (!feof(fin)) { 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 65 fclose(fin); 66 } 67 68 void foutput(STU s[], int n) { 69 FILE *fout; 70 int i; 71 72 fout = fopen("list_pass.txt", "w"); 73 if (!fout) { 74 printf("fail to open or create list_pass.txt\n"); 75 exit(0); 76 } 77 78 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 79 80 for (i = 0; i < n; i++) 81 fprintf(fout, "%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].ans); 82 83 fclose(fout); 84 } 85 86 int process(STU st[], int n, STU st_pass[]) { 87 int i,cnt=0; 88 for(i=0;i<n;i++){ 89 st[i].sum=st[i].objective+st[i].subjective; 90 91 if(st[i].sum>=60){ 92 strcpy(st[i].ans,"pass"); 93 cnt++; 94 }else{ 95 strcpy(st[i].ans,"fail"); 96 } 97 } 98 return cnt; 99 }
结果

task6(必做和选做(❁´◡`❁))
代码
1 #include<stdio.h> 2 #include<string.h> 3 #include<stdlib.h> 4 #include<time.h> 5 #define N 80 6 7 typedef struct{ 8 int hao; 9 char name[N]; 10 char ban[N]; 11 }stu; 12 13 void finput(stu st[],int n); 14 void foutput(stu s[]); 15 void sui(stu st[],stu s[]); 16 void sort(stu s[]); 17 18 int main(){ 19 stu st[N],s[5]; 20 finput(st,N); 21 sui(st,s); 22 sort(s); 23 foutput(s); 24 25 return 0; 26 } 27 28 void finput(stu st[],int n){ 29 FILE *fp; 30 int i; 31 32 fp=fopen("list.txt","r"); 33 if(fp==NULL){ 34 printf("fail to open\n"); 35 exit(0); 36 } 37 while(!feof(fp)){ 38 for(i=0;i<n;i++){ 39 fscanf(fp,"%d %s %s",&st[i].hao,st[i].name,st[i].ban); 40 } 41 } 42 43 fclose(fp); 44 } 45 46 void foutput(stu s[]){ 47 FILE *fout; 48 int i; 49 char filename[20]; 50 struct tm *local_time; 51 time_t now; 52 53 time(&now); 54 local_time = localtime(&now); 55 56 strftime(filename,sizeof(filename),"%Y%m%d.txt",local_time); 57 58 fout = fopen(filename,"w"); 59 if(!fout){ 60 printf("fail"); 61 exit(0); 62 } 63 64 for(i=0;i<5;i++){ 65 fprintf(fout,"%d\t\t%s\t%s\n",s[i].hao,s[i].name,s[i].ban); 66 printf("%d\t\t%s\t%s\n",s[i].hao,s[i].name,s[i].ban); 67 } 68 69 fclose(fout); 70 } 71 72 void sui(stu st[],stu s[]){ 73 srand(time(0)); 74 int total[N]; 75 int i; 76 for(i=0;i<5;i++){ 77 int t=rand()%80+1; 78 if(total[t]==1){ 79 i--; 80 continue; 81 } 82 total[t]=1; 83 s[i]=st[t]; 84 } 85 86 } 87 88 void sort(stu s[]){ 89 int i,j; 90 stu temp; 91 char p[N]; 92 for(i=0;i<5;i++){ 93 for(j=i+1;j<5;j++){ 94 if(s[j].hao<s[i].hao){ 95 temp=s[i]; 96 s[i]=s[j]; 97 s[j]=temp; 98 } 99 } 100 } 101 }
结果


浙公网安备 33010602011771号