当前位置:首页 > 实验一
数据结构实验指导书
计算机系数据结构实验报告(1)
姓名: 叶超 学号: 6103113037 专业班级: 计算机科学131班
实验目的:对C语言的复习,增强学生对结构体数组和指针的学习,尤以结构体的应用和指针的操作作为重点。
问题描述:
1、构造一个学生结构体数组,成员包括学号,姓名,四门成绩,以及平均成绩; 2、从键盘上输入学生的学号,姓名和四门成绩; 3、找出学生中考试没有通过的学生姓名并输出;找出考试在90分以上的学生并输出。 实验要求:
1、必须达到的最少程序行数为100行;
2、在实验过程中,分析算法的时间复杂度和空间复杂度进行分析。 定义结构: struct student {
char name[20];//name
float score[4];//all subjects' score char ID[20];//school ID float avesc;//average score }stu[20];
算法分析:本算法是通过设立学生的结构体,然后通过switch结构不断调用各函数达到对结构体的操作。其中主要是通过结构体指针来达到定位结构体的各项,然后将输入以及打印都独立为一个函数,要用的时候调用就好。对输出以及处理结构体的时候要用到循环,而是循环停下来一般注意道德是要用到null。 算法:
void printOver(struct student *p,struct student *q) {
for(;p for(int i=0;i<4;i++) { if(p->score[i]>=90) { print(p); break; } } } - 0 - 数据结构实验指导书 } void printLow(struct student *p,struct student *q) { for(;p for(int i=0;i<4;i++) { if(p->score[i]<90) { print(p); break; } } } } 代码: #include char name[20];//name float score[4];//all subjects' score char ID[20];//school ID float avesc;//average score }stu[20]; struct student *Per = stu,*FirP = stu;//the place of FirP never change,the first place void addScore(struct student *p);//the function to add score void printScore(struct student *p,struct student *q);//print the scores of students; void printAll(struct student *p,struct student *q);//print the scores of all void printOver(struct student *p,struct student *q);//print the part of scores that are over 90 void printLow(struct student *p,struct student *q);//print the part of scores that didn't pass void print(struct student *p); void cin_score(int i,struct student *p); void flag();//output the head of table void aveScore(struct student *p,struct student *q); int main() { loop: { int sel;//sel is the selection of user - 1 - 数据结构实验指导书 cout << \exit\ cin >> sel; switch(sel) { case 1: addScore(Per); Per++; break; case 2: printScore(FirP,Per); break; case 3: goto endUp; default: cout << \ } goto loop; } endUp: { cout << \ } return 0; } //to use the function request that p //cin_score to ensure the strong of code cout << \ cin >> p->ID ; cout << \ cin >> p->name ; cout << \ cout << \ cin_score(0,p); cout << \ cin_score(1,p); cout << \ cin_score(2,p); cout << \ cin_score(3,p); } void printScore(struct student *p,struct student *q) - 2 - 数据结构实验指导书 { cout << \part of nopass\ char sel_2; cin >> sel_2; switch(sel_2) { case 'A': cout << \ flag(); printAll(p,q); break; case 'B': cout << \ flag(); printOver(p,q); break; case 'C': cout << \ flag(); printLow(p,q); break; default: cout << \ } } void printOver(struct student *p,struct student *q) { for(;p for(int i=0;i<4;i++) { if(p->score[i]>=90) { print(p); break; } } } } void printLow(struct student *p,struct student *q) { for(;p - 3 -
共分享92篇相关文档