- #include <stdio.h>
- #include <stdlib.h>
- const int N=5;
- // 定义结构体类型 struct student, 并定义 STU 为其别名
- typedef struct student {
- long no;
- char name[20];
- int score;
- }STU;
- // 函数声明
- void input(STU s[], int n);
- int findMinlist(STU s[], STU t[], int n);
- void output(STU s[], int n);
- int main() {
- STU stu[N], minlist[N];
- int count;
- printf("录入 %d 个学生信息 \ n", N);
- input(stu, N);
- printf("\n 统计最低分人数和学生信息...\n");
- count = findMinlist(stu, minlist, N);
- printf("\n 一共有 %d 个最低分, 信息如下:\n", count);
- output(minlist, count);
- system("pause");
- return 0;
- }
- // 输入 n 个学生信息, 存放在结构体数组 s 中
- void input(STU s[], int n) {
- int i;
- for(i=0; i<n; i++)
- scanf("%ld %s %d", &s[i].no, s[i].name, &s[i].score);
- }
- // 输出结构体 s 中 n 个元素信息
- void output(STU s[], int n) {
- int i;
- for(i=0; i<n; i++)
- printf("%ld %s %d\n", s[i].no, s[i].name, s[i].score);
- }
- // 在结构体数组 s 中, 查找最低分学生的记录, 将其存入结构体数组 s 中
- // 形参 n 是结构体数组 s 中元素个数
- // 函数返回最低分的学生人数
- int findMinlist(STU s[], STU t[], int n) {
- // 补足函数实现
- int a,b,count=0;
- b=s[0].score;
- for(a=0;a<n;a++)
- {
- if(s[a].score<b)
- {
- b=s[a].score;
- }
- }
- for(a=0;a<n;a++)
- {
- if(s[a].score==b)
- t[count++]=s[a];
- }
- return count;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- const int N = 10;
- // 定义结构体类型 struct student, 并定义其别名为 STU
- typedef struct student {
- long int id;
- char name[20];
- float objective; /* 客观题得分 */
- float subjective; /* 操作题得分 */
- float sum;
- char level[10];
- }STU;
- // 函数声明
- void input(STU s[], int n);
- void output(STU s[], int n);
- void process(STU s[], int n);
- int main() {
- STU stu[N];
- printf("录入 %d 个考生信息: 准考证号, 姓名, 客观题得分 (<=40), 操作题得分 (<=60)\n", N);
- input(stu, N);
- printf("\n 对考生信息进行处理: 计算总分, 确定等级 \ n");
- process(stu, N);
- printf("\n 打印考生完整信息: 准考证号, 姓名, 客观题得分, 操作题得分, 总分, 等级 \ n");
- output(stu, N);
- system("pause");
- return 0;
- }
- // 录入考生信息: 准考证号, 姓名, 客观题得分, 操作题得分
- void input(STU s[], int n) {
- // 补足代码
- int i;
- for(i=0;i<n;i++)
- scanf("%ld %s %f %f",&s[i].id,s[i].name,&s[i].objective,s[i].subjective);
- }
- // 输出考生完整信息: 准考证号, 姓名, 客观题得分, 操作题得分, 总分, 等级
- void output(STU s[], int n) {
- // 补足代码
- int i;
- for(i=0;i<n;i++)
- printf("%8ld %-16s %.2f %.2f %.2f %-4s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].level);
- }
- // 对考生信息进行处理: 计算总分, 排序, 确定等级
- void process(STU s[], int n) {
- // 补足代码
- int i,j;STU temp;
- for(i=0;i<n;i++)
- {
- s[i].sum=s[i].objective*0.4+s[i].subjective*0.6;
- }
- for(i=0;i<n;i++)
- for(j=0;j<n-1-i;j++)
- if(s[j].sum<s[j+1].sum)
- {
- temp = s[j];
- s[j] = s[j+1];
- s[j+1] = temp;
- }
- for(i=0;i<n;i++)
- {
- if((i+1)<=n*0.1)
- strcpy(s[i].level,"优秀");
- else if((i+1)>n*0.1&&(i+1)<=n*0.5)
- strcpy(s[i].level,"合格");
- else
- strcpy(s[i].level,"不合格");
- }
- }
Part2
共用体与结构体类型的区别: 共用体与结构体类型的区别在于它们的表示方法不同. 在一个结构体变量里, 结构体的各成员顺序排列存储, 每个成员都有自己独立的存储位置. 共用体的情况不是这样, 一个共用体变量的所有成员共享同一片存储区.
Part3
枚举类型用于描述整型数据, 枚举变量不可以直接输入输出, 不能把一个 int 类型数值赋值给一个枚举类型的变量, 但是一个枚举类型的变量可以赋值给一个 int 类型的数值.
来源: http://www.bubuko.com/infodetail-3344951.html