实验六
实验任务1
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define N 3 5 6 typedef struct student { 7 int id; 8 char name[20]; 9 char subject[20]; 10 float perf; 11 float mid; 12 float final; 13 float total; 14 char level[10]; 15 } STU; 16 17 void input(STU[], int ); 18 void calc(STU[], int); 19 int fail(STU[], STU [],int); 20 void sort(STU[], int); 21 void print(STU[], int); 22 23 int main() { 24 STU st[N], fst[N]; 25 int k; 26 printf("录入学生成绩信息:\n"); 27 input(st,N); 28 29 printf("\n成绩处理...\n"); 30 calc(st,N); 31 32 k = fail(st, fst, N); 33 sort(st, N); 34 printf("\n学生成绩排名情况:\n"); 35 print(st, N); 36 37 printf("\n不及格学生信息:\n"); 38 print(fst, k); 39 40 return 0; 41 } 42 43 void input(STU s[],int n) { 44 int i; 45 for(i=0;i<n;i++) 46 scanf("%d %s %s %f %f %f",&s[i].id,s[i].name,s[i].subject,&s[i].perf,&s[i].mid,&s[i].final); 47 } 48 49 void calc(STU s[], int n) { 50 int i; 51 for(i = 0; i < n; i++) { 52 s[i].total=s[i].perf*0.2+s[i].mid*0.2+s[i].final*0.6; 53 54 if(s[i].total >= 90) 55 strcpy(s[i].level,"优"); 56 else if(s[i].total >= 80 && s[i].total < 90) 57 strcpy(s[i].level,"良"); 58 else if(s[i].total >= 70 && s[i].total < 80) 59 strcpy(s[i].level,"中"); 60 else if(s[i].total >= 60 && s[i].total < 70) 61 strcpy(s[i].level,"及格"); 62 else 63 strcpy(s[i].level,"不及格"); 64 } 65 } 66 67 int fail(STU s[],STU t[],int n) { 68 int i, k = 0; 69 70 for(i = 0; i < n; i++) 71 if(s[i].total < 60) 72 t[k++] = s[i]; 73 74 return k; 75 } 76 77 void sort(STU s[], int n) { 78 int i, j; 79 STU temp; 80 81 for(i = 0; i < n - 1; i++) 82 for(j = 0; j < n - 1 - i; j++) 83 if(s[j].total < s[j+1].total) { 84 temp = s[j]; 85 s[j] = s[j+1]; 86 s[j+1] = temp; 87 } 88 } 89 90 void print(STU s[], int n) { 91 int i; 92 93 printf("-----------------\n"); 94 printf("学号 姓名 考试科目 平时成绩 期中成绩 期末成绩 总评成绩 成绩等级\n"); 95 for(i = 0; i < n; i++) 96 printf("%5d %10s%20s %5.1f %5.1f %5.1f %5.1f %10s\n",s[i].id,s[i].name,s[i].subject,s[i].perf,s[i].mid,s[i].final,s[i].total,s[i].level); 97 }
答:等级是字符数组,赋值时要用strcpy函数。
实验任务2
1 #include <stdio.h> 2 3 const int N=5; 4 5 typedef struct student { 6 long no; 7 char name[20]; 8 int score; 9 }STU; 10 11 void input(STU s[], int n); 12 int findMinlist(STU s[], STU t[], int n); 13 void output(STU s[], int n); 14 15 int main() { 16 STU stu[N], minlist[N]; 17 int count; 18 19 printf("录入%d个学生信息\n", N); 20 input(stu, N); 21 22 printf("\n统计最低分人数和学生信息...\n"); 23 count = findMinlist(stu, minlist, N); 24 25 printf("\n一共有%d个最低分,信息如下:\n", count); 26 output(minlist, count); 27 28 return 0; 29 } 30 31 void input(STU s[], int n) { 32 int i; 33 for(i=0; i<n; i++) 34 scanf("%ld %s %d", &s[i].no, s[i].name, &s[i].score); 35 } 36 37 void output(STU s[], int n) { 38 int i; 39 for(i=0; i<n; i++) 40 printf("%ld %s %d\n", s[i].no, s[i].name, s[i].score); 41 } 42 43 int findMinlist(STU s[], STU t[], int n) { 44 int i, w = s[0].score, num; 45 for(i = 0; i <n ; i++) { 46 w = w >= s[i].score ? s[i].score : w; 47 } 48 for(i = 0; i < n; i ++) 49 if(s[i].score == w) 50 t[num++] = s[i]; 51 return num; 52 53 }
实验任务3
1 #include <stdio.h> 2 #include <string.h> 3 const int N = 10; 4 5 typedef struct student { 6 long int id; 7 char name[20]; 8 float objective; 9 float subjective; 10 float sum; 11 char level[10]; 12 }STU; 13 14 void input(STU s[], int n); 15 void output(STU s[], int n); 16 void process(STU s[], int n); 17 18 int main() { 19 STU stu[N]; 20 21 printf("录入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N); 22 input(stu, N); 23 24 printf("\n对考生信息进行处理: 计算总分,确定等级\n"); 25 process(stu, N); 26 27 printf("\n打印考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级\n"); 28 output(stu, N); 29 30 return 0; 31 } 32 33 void input(STU s[], int n) { 34 int i; 35 for(i = 0; i < n; i++) 36 scanf("%ld %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective); 37 } 38 39 void output(STU s[], int n) { 40 int i; 41 printf("\n"); 42 printf("准考证号 姓名 客观题得分 操作题得分 总分 等级"); 43 printf("\n"); 44 for(i = 0; i < n; i++) 45 printf("%-11ld %-8s %-14.1f %-13.1f %-7.1f %s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level); 46 } 47 48 void process(STU s[], int n) { 49 int i, j, k, grade[n]; 50 STU temp; 51 for(i = 0; i < n; i++) 52 s[i].sum = s[i].objective + s[i].subjective; 53 for(i = 0; i < n; i++) 54 { 55 for(j = 0, k = 0; j < n; j++) 56 { 57 if(s[i].sum >= s[j].sum) 58 k++; 59 } 60 grade[i]=k>(n*0.9)?0:k>(n*0.5)?1:2; 61 switch(grade[i]){ 62 case 0:strcpy(s[i].level,"优秀");break; 63 case 1:strcpy(s[i].level,"合格");break; 64 case 2:strcpy(s[i].level,"不合格");break; 65 default:strcpy(s[i].level,"不合格");break; 66 } 67 } 68 for(i = 0; i < n - 1; i++) 69 for(j = 0; j < n-1-i; j++) 70 if(s[j].sum < s[j+1].sum){ 71 temp = s[j]; 72 s[j] = s[j+1]; 73 s[j+1] = temp; 74 } 75 }