实验6
实验结论
Part1: 结构体类型及编程应用
1.补足程序ex1_2.cpp,附上补足后的程序源码,输入不同测试数据,给出运行结果截图
#include <stdio.h> const int N = 5; 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("please enter %d students' information\n", N); input(stu, N); printf("\nadding the number of students in the lowest scores and their information...\n"); count = findMinlist(stu, minlist, N); printf("\n there are %d lowest score,here are the information:\n", count); output(minlist, count); return 0; } 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); } 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); } int findMinlist(STU s[], STU t[], int n) { int i,k=0,min=s[0].score; for(i=0;i<n;i++){ if(s[i].score<min) min=s[i].score; } for(i=0;i<n;i++){ if(s[i].score==min) t[k++]=s[i];} return k; }
2.补足程序ex1_3.cpp,附上补足后的程序源码,输入不同测试数据,给出运行结果截图
#include <stdio.h> #include <string.h> const int N = 10; 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("enter %d candidates' information: admission ticket,name,objective(<=40),subjective(<=60)\n", N); input(stu, N); printf("\n manage the candidates' information:caclulate total scores,set the level\n"); process(stu, N); printf("\nprint the candidates' information: admission ticket,name,objective,subjective\n"); output(stu, N); 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("%ld %s %f %f %f %s\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+s[i].subjective; 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; } } } strcpy(s[0].level,"A" ); for(i=1;i<=4;i++){ strcpy(s[i].level,"B"); } for(i=5;i<=9;i++){ strcpy(s[i].level,"C"); } }
Part2: 共用体类型及编程示例
总结: 共用体与结构体类型的区别?
共同体:共同占用同段内存
结构体:有独立内存空间
Part3: 枚举类型及编程示例
总结:
1.枚举类型用于描述哪一类数据?
整型常量;
2.枚举变量使用过程中的注意事项
能否直接输入输出? 能否把一个int型数值赋值给一个枚举类型的变量?反过来呢?
不能;不能,需显式转换;反过来可以
实验总结和体会
心力交瘁有点困