实验六

1.由于代码和运算结果比较显然,这里不再搬运

此做法的优点即在各个常用函数确定的情况下,录入输出的信息会显得条理清晰。

2.代码

 1 #include <stdio.h>
 2 
 3 const int N=5;
 4 
 5 // 定义结构体类型struct student,并定义STU为其别名 
 6 typedef struct student {
 7     long no;
 8     char name[20];
 9     int score;     
10 }STU;
11 
12 // 函数声明 
13 void input(STU s[], int n);
14 int findMinlist(STU s[], STU t[], int n);
15 void output(STU s[], int n);
16 
17 int main() {
18     STU stu[N], minlist[N];
19     int count;
20     
21     printf("录入%d个学生信息\n", N);
22     input(stu, N);
23     
24     printf("\n统计最低分人数和学生信息...\n");
25     count = findMinlist(stu, minlist, N);
26     
27     printf("\n一共有%d个最低分,信息如下:\n", count);
28     output(minlist, count);
29      
30     return 0;
31 } 
32 
33 // 输入n个学生信息,存放在结构体数组s中 
34 void input(STU s[], int n) {
35     int i;
36     for(i=0; i<n; i++) 
37         scanf("%ld %s %d", &s[i].no, s[i].name, &s[i].score);
38 } 
39 
40 // 输出结构体s中n个元素信息
41 void output(STU s[], int n) {
42     int i;
43     for(i=0; i<n; i++)
44         printf("%ld %s %d\n", s[i].no, s[i].name, s[i].score); 
45 } 
46 
47 // 在结构体数组s中,查找最低分学生的记录,将其存入结构体数组t中
48 // 形参n是结构体数组s中元素个数
49 // 函数返回最低分的学生人数 
50 int findMinlist(STU s[], STU t[], int n) {
51     int i,c,a=0;
52     int k=0;
53 for(i=0;i<n;i++){
54 if(s[i].score<s[k].score){
55         k=i;}
56 }
57 for(i=0;i<n;i++){
58 if(s[i].score==s[k].score){
59       t[a]=s[i];
60       a++;}
61       }
62       return a;
63 }

结果

显然函数本身是个很基本的东西,但数据一多我就把两个数组看反了,呜呜呜......

3.代码

 1 #include <stdio.h> 
 2 #include <string.h>
 3 const int N = 10;
 4 
 5 // 定义结构体类型struct student,并定义其别名为STU 
 6 typedef struct student {
 7     long int id;
 8     char name[20];
 9     float objective;    /*客观题得分*/
10     float subjective;    /*操作题得分*/
11     float sum;
12     char level[10];    
13 }STU; 
14 
15 // 函数声明
16 void input(STU s[], int n);
17 void output(STU s[], int n);
18 void process(STU s[], int n);
19 
20 int main() {
21     STU stu[N];
22     
23     printf("录入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N); 
24     input(stu, N);
25     
26     printf("\n对考生信息进行处理: 计算总分,确定等级\n");
27     process(stu, N);
28     
29     printf("\n打印考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级\n");
30     output(stu, N); 
31     
32     return 0;
33 } 
34 
35 // 录入考生信息:准考证号,姓名,客观题得分,操作题得分
36 void input(STU s[], int n) {
37      int i;
38     for(i=0;i<n;i++){
39     scanf("%ld %s %f %f", &s[i].id, s[i].name, &s[i].objective, &s[i].subjective);
40     }
41 }
42 
43 //输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
44 void output(STU s[], int n) {
45     int i;
46      printf("准考证号 姓名 客观题得分 操作题得分 总分  等级\n");
47        for(i=0;i<10;i++)
48          printf("%-10ld %-11s %-14.2f %-12.2f %-12.2f %-10s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
49 }
50 
51 // 对考生信息进行处理:计算总分,排序,确定等级
52 void process(STU s[], int n) {
53     int i,t;
54      STU temp;
55      for(i=0;i<n;i++){
56          s[i].sum = s[i].objective + s[i].subjective;
57      }
58      
59      for(i=0;i<n;i++){
60          if(s[i].sum<60)
61               strcpy(s[i].level,"不及格");
62             else if(s[i].sum>95)
63              strcpy(s[i].level,"优秀");
64          else
65              strcpy(s[i].level,"及格");
66      }
67      
68      for(i=0;i<n;i++){
69          for(t=i+1;t<n;t++){
70              if(s[t].sum>s[i].sum){
71                  temp=s[i];
72                  s[i]=s[t];
73                  s[t]=temp;
74              }
75          }
76      }
77     
78 }

结果

总结哈

这章几乎需要前面所有章的知识,你看看这一个个程序都可能是最基本的,但综合起来就显得很有意思。像冒泡排序啊,输入输出格式啊等等

总之你感觉自己能独立处理一个问题就很有成就感

 

posted @ 2020-12-20 19:57  togeplane  阅读(66)  评论(2)    收藏  举报