4.task4
1 #include <stdio.h> 2 3 int main() { 4 char ch; 5 int k=0; 6 FILE *fp; 7 8 fp = fopen("data4.txt", "r"); 9 if(fp == NULL) { 10 printf("fail to open file\n"); 11 return 1; 12 } 13 while((ch=fgetc(fp))!=EOF) 14 { 15 if(ch!=' '&&ch!='\n'&&ch!='\t'&&ch!='\v') 16 k++; 17 } 18 19 20 fclose(fp); 21 printf("data4.txt中共包含字符数(不计空白符):%d\n",k); 22 return 0; 23 }
5.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 level[10]; 14 } STU; 15 16 // 函数声明 17 void input(STU s[], int n); 18 void output(STU s[], int n); 19 void process(STU s[], int n); 20 21 int main() { 22 STU stu[N]; 23 24 printf("从文件读入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N); 25 input(stu, N); 26 27 printf("\n对考生信息进行处理: 计算总分,确定等级\n"); 28 process(stu, N); 29 30 printf("\n打印考生完整信息, 并保存到文件中"); 31 output(stu, N); 32 33 return 0; 34 } 35 36 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 37 void input(STU s[], int n) { 38 int i; 39 FILE *fin; 40 41 fin = fopen("examinee.txt", "r"); 42 if (fin == NULL) { 43 printf("fail to open file\n"); 44 exit(0); 45 } 46 47 while (!feof(fin)) { 48 for (i = 0; i < n; i++) 49 fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective); 50 } 51 52 fclose(fin); 53 } 54 55 // 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 56 // 不仅输出到屏幕上,还写到文本文件result.txt中 57 void output(STU s[], int n) { 58 FILE *fout; 59 int i; 60 61 // 输出到屏幕 62 printf("\n"); 63 printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n"); 64 for (i = 0; i < n; i++) 65 printf("%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].level); 66 67 // 保存到文件 68 fout = fopen("result.txt", "w"); 69 if (!fout) { 70 printf("fail to open or create result.txt\n"); 71 exit(0); 72 } 73 74 fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n"); 75 76 for (i = 0; i < n; i++) 77 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].level); 78 79 fclose(fout); 80 } 81 82 // 对考生信息进行处理:计算总分,排序,确定等级 83 void process(STU s[], int n) { 84 int i; 85 int j; 86 STU h; 87 for(i=0;i<N;i++) 88 { 89 s[i].sum=s[i].objective+s[i].subjective; 90 } 91 for(i=0;i<N-1;i++) 92 { 93 for(j=0;j<N-1-i;j++) 94 { 95 if(s[j].sum<s[j+1].sum) 96 { 97 h=s[j]; 98 s[j]=s[j+1]; 99 s[j+1]=h; 100 } 101 } 102 } 103 for(i=0;i<N*0.1;i++) 104 strcpy(s[i].level,"优秀"); 105 for(i=N*0.1;i<N*0.5;i++) 106 strcpy(s[i].level,"合格"); 107 for(i=N*0.5;i<N;i++) 108 strcpy(s[i].level,"不合格"); 109 110 }
6.task6
1 #include <stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #define N 5 5 #define M 20 6 typedef struct 7 { 8 long int id; 9 char name[20]; 10 char clas[20]; 11 }STU; 12 13 int main() { 14 int i; 15 int n; 16 STU s[M],r[N]; 17 18 FILE *fp,*fout; 19 20 fp=fopen("list.txt", "r"); 21 fout=fopen("lucky.txt","w"); 22 23 if(fp == NULL) 24 { 25 printf("fail to open file\n"); 26 return 1; 27 } 28 29 while(!feof(fp)) 30 { 31 for(i=0;i<M;i++) 32 { 33 fscanf(fp,"%ld%s%s",&s[i].id,s[i].name,s[i].clas); 34 } 35 } 36 37 srand(time(NULL)); 38 39 for(i=0;i<N;i++) 40 { 41 n=rand()%M; 42 r[i]=s[n]; 43 } 44 45 fclose(fp); 46 47 if(fout==NULL) 48 { 49 printf("fail to open file\n"); 50 return 1; 51 } 52 53 for (i=0;i<N;i++) 54 { 55 fprintf(fout,"%ld\t\t%s\t\t%s\n", r[i].id, r[i].name,r[i].clas); 56 printf("%ld\t\t%s\t\t%s\n", r[i].id, r[i].name, r[i].clas); 57 } 58 fclose(fout); 59 return 0; 60 }
标签:fp,ch,20,int,STU,实验,include From: https://www.cnblogs.com/joshh1230/p/17469943.html