4.task_4
1 #include <stdio.h> 2 #include <string.h> 3 #define N 10000 4 5 int main() 6 { 7 int count,i,t=0; 8 char x[N]; 9 FILE *fp; 10 fp=fopen("D:\\data4.txt","r"); 11 while (!feof(fp)) 12 { 13 fgets(x,N,fp); 14 count=strlen(x); 15 if (x[count-1]=='\n') 16 { 17 x[count-1]='\0'; 18 count--; 19 } 20 for (i=0;x[i]!='\0';i++) 21 { 22 if (x[i]==' ') 23 count--; 24 } 25 t+=count; 26 } 27 fclose(fp); 28 printf("data4.txt中共包含字符数(不计空白字符):%d\n",t); 29 return 0; 30 }
5.task_5
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("D:\\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("D:\\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,j; 88 for (i=0,j=0;i<n;i++) 89 { 90 st[i].sum=st[i].objective+st[i].subjective; 91 if (st[i].sum>=60) 92 { 93 strcpy(st[i].ans,"pass"); 94 st_pass[j++]=st[i]; 95 } 96 else 97 strcpy(st[i].ans,"fail"); 98 } 99 return j; 100 }
6.task_6
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #define N 100 5 6 typedef struct 7 { 8 long int id; 9 char name[N]; 10 char class[N]; 11 }STU; 12 13 void finput(STU st[N],int n); 14 void foutput(STU st[N]); 15 16 int main() 17 { 18 int i,k; 19 STU stu[N],lucky[N]; 20 finput(stu,N); 21 for (i=0;i<5;i++) 22 { 23 k=rand()%80; 24 lucky[i]=stu[k]; 25 } 26 foutput(lucky); 27 for (i=0;i<5;i++) 28 printf("%ld\t\t%s\t\t%s\n",lucky[i].id,lucky[i].name,lucky[i].class); 29 return 0; 30 } 31 32 void finput(STU st[N],int n) 33 { 34 int i; 35 FILE *fin; 36 37 fin = fopen("D:\\list.txt", "r"); 38 if (fin == NULL) { 39 printf("fail to open file\n"); 40 exit(0); 41 } 42 43 while (!feof(fin)) { 44 for (i = 0; i < n; i++) 45 fscanf(fin, "%ld %s %s",&st[i].id,st[i].name,st[i].class); 46 } 47 48 fclose(fin); 49 50 } 51 52 void foutput(STU st[N]) 53 { 54 int i; 55 FILE *fout; 56 fout = fopen("D:\\lucky.txt", "w"); 57 if (!fout) { 58 printf("fail to open or create list_pass.txt\n"); 59 exit(0); 60 } 61 for (i=0;i<5;i++) 62 fprintf(fout,"%ld\t\t%s\t\t%s\n",st[i].id,st[i].name,st[i].class); 63 fclose(fout); 64 }
标签:printf,文件,int,编程,st,STU,实验,pass,t% From: https://www.cnblogs.com/zxy2004/p/17901655.html