实验七
1.实验任务4
1 #include<stdio.h> 2 int main() 3 { 4 char s[100]; 5 FILE *fp; 6 int i = 0,j,k = 0; 7 char ch; 8 fp = fopen("data4.txt","rb"); 9 10 if(fp == NULL) 11 { 12 printf("fail to open file\n"); 13 return 1; 14 } 15 ch = fgetc(fp); 16 while(ch!=EOF) 17 { 18 s[i++] = ch; 19 ch = fgetc(fp); 20 21 22 } 23 24 for(j = 0;j<i-1;j++) 25 if(s[j]!=' ') 26 k++; 27 28 fclose(fp); 29 printf("data4.txt中共包含字符数(不计空白符):%d",k-1); 30 return 0; 31 }
2.实验任务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 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 // 补足代码 85 // ××× 86 int i,j; 87 STU temp; 88 for(i = 0;i < n;i++) 89 s[i].sum = s[i].objective + s[i].subjective; 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 { 95 temp = s[j]; 96 s[j] = s[j+1]; 97 s[j+1] = temp; 98 } 99 100 101 for(i = 0;i < n;i++) 102 { 103 if(i<(n/10)) 104 strcpy(s[i].level,"优秀"); 105 if(i>=(n/10)&&i<(n/2)) 106 strcpy(s[i].level,"合格"); 107 if(i>=(n/2)) 108 strcpy(s[i].level,"不合格"); 109 110 } 111 112 113 114 }
3.实验任务6
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #define N 80 5 int main() 6 { 7 typedef struct { 8 long int id; 9 char name[20]; 10 char class[40]; 11 } STU; 12 STU s[N]; 13 FILE *fin; 14 int i,n; 15 16 fin = fopen("list.txt","r"); 17 if(fin == NULL) 18 { 19 printf("fail to open\n"); 20 exit(0); 21 } 22 23 while (!feof(fin)) 24 { 25 for(i = 0;i < N;i++) 26 fscanf(fin, "%ld %s %s", &s[i].id, s[i].name, s[i].class); 27 } 28 29 srand(time(NULL)); 30 int x[5]; 31 for(i = 0;i < 5;i++) 32 x[i] = rand()% 80 + 1; 33 34 for(i =0;i < 5;i++) 35 { 36 printf("%ld\t%s\t%s\n",s[x[i]].id,s[x[i]].name,s[x[i]].class); 37 } 38 39 FILE *fout; 40 fout = fopen("lucky.txt", "w"); 41 if (!fout) { 42 printf("fail to open or create result.txt\n"); 43 exit(0); 44 } 45 46 for (i = 0; i < 5; i++) 47 fprintf(fout,"%ld\t%s\t%s\n",s[x[i]].id,s[x[i]].name,s[x[i]].class); 48 49 fclose(fout); 50 51 return 0; 52 }