task4
1 #include <stdio.h> 2 #include <string.h> 3 int main(){ 4 5 FILE *fp; 6 fp=fopen("d:\\data4.txt","r"); 7 8 if(fp==NULL){ 9 printf("fail to open file to write\n"); 10 return; 11 } 12 13 int a=0,c=0,i=0; 14 char b[100]; 15 16 while(!feof(fp)){ 17 fgets(b,101,fp); 18 a++; 19 while(b[i]!=0){ 20 if(b!='\n'&&b!=' ') 21 c++; 22 i++; 23 } 24 } 25 26 27 fclose(fp); 28 29 30 31 printf("data4.txt统计结果:\n"); 32 printf("行数:%d\n",n); 33 printf("字符数(不计算空白符):%d\n",); 34 35 return 0; 36 }
task5
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 72 void write(STU s[], int n) { 73 int i; 74 FILE *fp; 75 76 fp=fopen("d:\\list_pass.txt","w"); 77 if(fp=NULL){ 78 printf("fail to open file to read\n"); 79 return ; 80 } 81 82 fprintf(fp,"准考证号\t姓名\t客观题得分\t操作题得分\t总分\t结果\t"); 83 for(i=0;i<n;++i){ 84 if(s[i].sum>=60) 85 fprintf(fp,"%d\t%s\t%.2f\t%.2f\t%.2f\t%s\n",s[i].id,s[i].name,s[i].objective,s[i].subjectives[i].sum,s[i].result); 86 } 87 fclose(fp);} 88 89 int process(STU st[], int n, STU st_pass[]) { 90 int i,j=0,sum=0; 91 92 for(i=0;i<n;++i){ 93 st[i].sum=st[i].objective+st[i].subjective; 94 if(st[i].sum>=60){ 95 strcpy(st[i].result,"通过"); 96 st_pass[j++]=st[i]; 97 sum++; 98 } 99 else 100 strcpy(st[i].result,"不通过"); 101 } 102 103 return sum;} 104 105 }
task6
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 # define N 80 5 6 typedef struct{ 7 int id; 8 char name[50]; 9 char class[50]; 10 }STU; 11 12 void read(STU x[] ){ 13 int i; 14 FILE *fp; 15 fp=fopen("d:\\list.txt","r"); 16 if(fp==NULL){ 17 printf("fail to open file to read"); 18 return ;} 19 20 while(!feof(fp)){ 21 for(i=0;i<N;++i) 22 fscanf(fp,"%d %s %s",&x[i].id,x[i].name,x[i].class); 23 } 24 25 fclose(fp); 26 } 27 28 void write(STU st[],char *a ){ 29 int i; 30 FILE *fp; 31 fp=fopen(a,"w"); 32 if(fp==NULL){ 33 printf("fail to open file to write"); 34 return ;} 35 36 while(!feof(fp)){ 37 for(i=0;i<5;++i) 38 fprintf(fp,"%d %s %s",&st[i].id,st[i].name,st[i].class); 39 } 40 41 fclose(fp); 42 } 43 44 45 int main(){ 46 STU x[N],st[5]; 47 48 read(x); 49 50 int b[5],i,j,sum=0; 51 srand(time(0)); 52 for(i=0;i<5;++i){ 53 sum=rand()%80+1; 54 b[i]=sum; 55 } 56 57 58 for(i=0;i<5;++i) 59 for(j=0;j<N;++j){ 60 if(x[j].id%100==x[i]){ 61 st[i]=x[j]; 62 continue; 63 } 64 } 65 66 for(i=0;i<5;++i) 67 printf("%-10d %-10s\n",st[j].id,st[j].name,st[j].class) 68 69 printf("输入文本名:"); 70 char a[100]; 71 scanf("%99s",a); 72 printf("保存成功"); 73 write(st,a); 74 return 0; 75 76 77 }
标签:fp,int,st,STU,实验,printf,t% From: https://www.cnblogs.com/wuyu33/p/18642167