task4.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #define N 100 5 int main() 6 { 7 char s; 8 int i=0; 9 FILE *fp; 10 11 fp=fopen("data4.txt","r"); 12 13 if (fp==NULL){ 14 printf("fail to open"); 15 return 1; 16 17 } 18 while ((s=fgetc(fp))!=EOF) 19 { 20 if (s!=' '&&s!='\t'&&s!='\n') 21 { 22 i++; 23 } 24 25 } 26 27 printf("data4.txt中包含字符数(不计空白符):%d",i); 28 return 0;}
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 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,j; 85 STU temp; 86 for (i=0;i<n;i++) 87 { 88 s[i].sum=s[i].objective+s[i].subjective; 89 90 } 91 for(i=0;i<n-1;i++) 92 for (j=0;j<n-1;j++){ 93 if (s[j].sum<s[j+1].sum) { 94 temp=s[j]; 95 s[j]=s[j+1]; 96 s[j+1]=temp; 97 } 98 } 99 100 for (i=0;i<n;i++) 101 { 102 if (i+1<=n*0.1) strcpy(s[i].level,"优秀"); 103 else if (i+1>0.1*n&&i+1<=0.5*n) strcpy (s[i].level,"合格") ; 104 else strcpy (s[i].level,"不合格"); 105 106 } 107 }
task6.c
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 #define N 80 5 #define M 5 6 typedef struct{ 7 long int id; 8 char name[20]; 9 char classic[50]; 10 }STU; 11 12 int main() 13 { STU s[N],r[M]; 14 int i=0,n; 15 FILE *fp1,*fp2; 16 if ((fp1=fopen("list.txt","r"))==NULL){ 17 printf("fail to open file\n"); 18 return 1; 19 } 20 while (!feof(fp1)){ 21 fscanf(fp1,"%d%s%s",&s[i].id,s[i].name,s[i].classic); 22 i++;} 23 srand(time(0)); 24 for (i=0;i<M;i++){ 25 n=rand ()%N; 26 27 r[i]=s[n]; 28 } 29 fclose(fp1); 30 if ((fp2=fopen("lucky.txt","w"))==NULL){ 31 printf("fail to open the fail_2\n"); 32 return 1;} 33 34 for(i=0;i<M;i++){ 35 printf("%ld\t%s\t%s\n",r[i].id,r[i].name,r[i].classic); 36 fprintf(fp2,"%ld\t%s\t%s\n",r[i].id,r[i].name,r[i].classic);} 37 38 fclose(fp2); 39 40 return 0; 41 }
标签:10,20,int,char,STU,实验,include From: https://www.cnblogs.com/shaobky/p/17467354.html