task4
1 #include<stdio.h> 2 int main() 3 {int i=0;char ch; 4 FILE *fp; 5 6 fp=fopen("c:/aa/data4.txt","r"); 7 if(fp==NULL){ 8 printf("fail to open file\n"); 9 return 1; 10 } 11 while(1){ 12 ch=fgetc(fp); 13 if(ch==EOF) 14 break; 15 if(ch!=' '&&ch!='\n'&&ch!='\r') 16 i++; 17 } 18 19 printf("data4.txt中共包含字符数(不计空白符):%d",i); 20 fclose(fp); 21 22 23 return 0; 24 }
task5.c
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 // 函数声明 17 void finput(STU st[], int n); 18 void foutput(STU st[], int n); 19 void output(STU st[], int n); 20 int process(STU st[], int n, STU st_pass[]); 21 22 int main() { 23 STU stu[N], stu_pass[N]; 24 int cnt; 25 double pass_rate; 26 27 printf("从文件读入%d个考生信息...\n", N); 28 finput(stu, N); 29 30 printf("\n对考生成绩进行统计...\n"); 31 cnt = process(stu, N, stu_pass); 32 33 printf("\n通过考试的名单:\n"); 34 output(stu, N); // 输出到屏幕 35 foutput(stu, N); // 输出到文件 36 37 pass_rate = 1.0 * cnt / N; 38 printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100); 39 40 return 0; 41 } 42 43 // 把通过考试的考生完整信息输出到屏幕上 44 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 45 void output(STU st[], int n) { 46 int i; 47 48 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 49 for (i = 0; i < n; i++) 50 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); 51 } 52 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 54 void finput(STU st[], int n) { 55 int i; 56 FILE *fin; 57 58 fin = fopen("examinee.txt", "r"); 59 if (fin == NULL) { 60 printf("fail to open file\n"); 61 exit(0); 62 } 63 64 while (!feof(fin)) { 65 for (i = 0; i < n; i++) 66 fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective); 67 } 68 69 fclose(fin); 70 } 71 72 // 把通过考试的考生完整信息写入文件list_pass.txt 73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果 74 void foutput(STU s[], int n) { 75 FILE *fout; 76 int i; 77 78 // 保存到文件 79 fout = fopen("list_pass.txt", "w"); 80 if (!fout) { 81 printf("fail to open or create list_pass.txt\n"); 82 exit(0); 83 } 84 85 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n"); 86 87 for (i = 0; i < n; i++) 88 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); 89 90 fclose(fout); 91 } 92 93 94 95 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数 96 int process(STU st[], int n, STU st_pass[]) { 97 int i,j=0; 98 FILE *fpro; 99 fpro=fopen("examinee.txt", "r"); 100 if(fpro==NULL){ 101 printf("fail to open examinee.txt"); 102 return 1; 103 } 104 for(i=0;i<n;i++) 105 { fscanf(fpro,"%ld%s%f%f",&st[i].id,st[i].name,&st[i].objective,&st[i].subjective); 106 //把文件的内容写进结构体数组 107 st[i].sum = st[i].objective + st[i].subjective; 108 109 if(st[i].sum>=60) 110 {strcpy(st[i].ans,"通过"); 111 st_pass[j]=st[i]; 112 j++; 113 } 114 else 115 strcpy(st[i].ans,"不通过"); 116 } 117 return j; 118 119 // 待补足 120 // ××× 121 }
task6.c
必做
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 id; 9 char name[10]; 10 char class_name[20]; 11 }STU; 12 13 int main(){ 14 FILE *fp; 15 int i,m; 16 STU st[N]; 17 18 fp=fopen("c:/aa/list.txt","r"); 19 20 if(fp==NULL){ 21 printf("fail to open\n"); 22 23 return 1;} 24 25 for(i=0;i<N;i++) 26 fscanf(fp,"%ld%s%s",&st[i].id,st[i].name,st[i].class_name); 27 28 fclose(fp); 29 30 fp=fopen("c:/aa/lucky.txt.txt","w"); 31 int num; 32 printf("请输入抽点人数:"); 33 scanf("%d",&num); 34 srand((int)time(0)); 35 for(i=0;i<num;i++){ 36 37 m=rand()%80; 38 fprintf(fp,"%ld\t%s\t%s\n",st[m].id,st[m].name,st[m].class_name); 39 printf("%ld\t%s\t%s\n",st[m].id,st[m].name,st[m].class_name); 40 } 41 42 fclose(fp); 43 44 45 return 0; 46 }
标签:文件,int,pass,st,STU,实验,printf,t% From: https://www.cnblogs.com/0924lr/p/17909749.html