代码改变世界

实验六

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

 1 #include <stdio.h>
 2 #include <string.h>
 3 const int N = 5;
 4 // 定义结构体类型struct student,并定义其别名为STU
 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 int main() {
18 STU stu[N];
19 printf("录入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n",
20 N);
21 input(stu, N);
22 printf("\n对考生信息进行处理: 计算总分,确定等级\n");
23 process(stu, N);
24 printf("\n打印考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级\n");
25 output(stu, N);
26 return 0;
27 }
28 // 录入考生信息:准考证号,姓名,客观题得分,操作题得分
29 void input(STU s[], int n) {
30 int i;
31 for(i=0;i<n;i++)
32 scanf("%d %s %f %f",&s[i].id,s[i].name,&s[i].objective,&s[i].subjective); 
33 }
34 //输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
35 void output(STU s[], int n) {
36 int i;
37     printf("\n准考证号 姓名 客观题得分 操作题得分 总分 等级\n");
38     for(i=0;i<n;i++)
39     printf("%ld %s %.2f %.2f %.2f %s\n ",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
40 }
41 // 对考生信息进行处理:计算总分,排序,确定等级
42 void process(STU s[], int n) {
43 int i,j,k; 
44     STU temp;
45     for(i=0;i<n;i++){
46         s[i].sum=s[i].objective+s[i].subjective;
47     } 
48     for(j=0;j<n-1;j++){
49         for(k=0;k<n-i-1;k++){
50             if(s[k].sum<s[k+1].sum){
51                 temp=s[k];
52                 s[k]=s[k+1];
53                 s[k+1]=temp;
54             }
55         }
56     }
57     strcpy(s[0].level,"优秀");
58     for(i=1;i<5;i++){
59         strcpy(s[i].level,"合格"); 
60     } 
61     for(i=5;i<n;i++){
62         strcpy(s[i].level,"不及格"); 
63     }
64 }

 总结

1、结构体与共用体的区别

共用体的所有成员都在一段内存中存放,起始地址一样,并且同一时刻只能使用其中的一个成员变量每个结构体成员拥有自己的独立内存而结构体总空间大小,等于各成员总长度共用体空间等于最大成员占据的空间。

2、用于声明一组命名的常数,整形常量。

枚举变量使用过程中的注意事项:(1)枚举类型不能直接输入输出。

(2)枚举类型可以隐含转换为整型,但整型转换为枚举类型必须显式转换。

 https://www.cnblogs.com/tedyoung1/p/10990428.html

https://www.cnblogs.com/shauifan/p/11000699.html

https://www.cnblogs.com/1752223012cxy/p/11000415.html