第七次实验
实验三:正确输出了排序信息且生成file3.dat文件。打开后数据准确且可读。
实验四:
子任务一:正确输出了排序信息且生成file4.dat文件但数据不可直观读取。
子任务二:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define N 10 5 6 7 typedef struct student { 8 int num; 9 char name[20]; 10 int score; 11 }STU; 12 13 int main() { 14 FILE *fin; 15 STU st[N]; 16 int i; 17 18 19 fin = fopen("file4.dat", "r"); 20 if( !fin ) { 21 printf("fail to open file4.dat\n"); 22 exit(0); 23 } 24 fread(st, sizeof(STU), N, fin); 25 for(i=0;i<10;i++){ 26 printf("%d %s %d\n",st[i].num,st[i].name,st[i].score); 27 } 28 29 return 0; 30 }

实验五:
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 const int N = 10; 5 6 // 定义结构体类型struct student,并定义其别名为STU 7 typedef struct student { 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打印考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级\n"); 31 output(stu, N); 32 33 return 0; 34 } 35 36 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分 37 void input(STU s[], int n) 38 { 39 int i; 40 FILE *a; 41 STU stu[N]; 42 a = fopen("examinee.txt", "r"); 43 if( !a ) 44 { 45 printf("fail to open examinee.txt\n"); 46 exit(0); 47 } 48 for(i=0;i<n;i++) 49 { 50 fscanf(a,"%d %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective); 51 } 52 fclose(a); 53 } 54 55 // 输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级 56 // 不仅输出到屏幕上,还写到文本文件result.txt中 57 void output(STU s[], int n) 58 { 59 int i; 60 FILE *b; 61 b=fopen("result.txt","w"); 62 if(!b) 63 { 64 printf("fail to open result.txt"); 65 exit(0); 66 } 67 for(i=0;i<n;i++) 68 { 69 printf("%d %s %f %f %f %s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); 70 fprintf(b,"%d %s %f %f %f %s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); 71 } 72 fclose(b); 73 } 74 75 // 对考生信息进行处理:计算总分,排序,确定等级 76 void process(STU s[], int n) 77 { 78 int i,j,p; 79 STU temp; 80 for(i=0;i<n;i++) 81 { 82 s[i].sum=s[i].objective+s[i].subjective; 83 } 84 for(i=0;i<n;i++) 85 { 86 for(j=i+1;j<n;j++) 87 { 88 if(s[i].sum<s[j].sum) 89 { 90 temp=s[i]; 91 s[i]=s[j]; 92 s[j]=temp; 93 } 94 } 95 } 96 for(i=0;i<n;i++){ 97 p=i+1; 98 if(p<=n*0.1){ 99 strcpy(s[i].level,"优秀"); 100 } 101 else if(p>n*0.1&&p<=n*0.5){ 102 strcpy(s[i].level,"合格"); 103 } 104 else{ 105 strcpy(s[i].level,"不合格"); 106 } 107 } 108 109 }


浙公网安备 33010602011771号