您好,欢迎访问三七文档
云南大学物理实验教学中心实验报告课程名称:计算机软件技术基础实验项目:实验二、线性表(顺序存储)及其应用学生姓名:学号:学院系级专业指导教师:实验时间:年日时分至时分实验地点:实验类型:教学(演示□验证□综合█设计□)学生科研□课外开放□测试□其它□成绩1一、实验目的:掌握顺序表的建立及基本操作。二、问题:建立一个顺序表,表中元素为学生,每个学生信息包含姓名、学号和成绩三部分,对该表实现:①输出、②插入、③删除、④查找功能,并计算出平均成绩和总成绩。三、程序的编写与调试1、原程序:#includeiostreamusingnamespacestd;typedefstruct{longdoublenum;charname[10];intscore;}STUDENT;classsq_LList{private:intmm;intnn;STUDENT*v;public:sq_LList(int);voidprt_sq_LList();voidins_sq_LList(int,STUDENT);voiddel_sq_LList(int);voidsea_num_sq_LList(int);voidsea_name_sq_LList(int,char批注[A2]);2voidcal_sq_LList(int);};/*输出*/sq_LList::sq_LList(intm){mm=m;v=newSTUDENT[mm];v[0].num=970156;strcpy(v[0].name,张小明);v[0].score=87;v[1].num=970157;strcpy(v[1].name,李小青);v[1].score=96;v[2].num=970158;strcpy(v[2].name,刘华);v[2].score=85;v[3].num=970159;strcpy(v[3].name,王伟);v[3].score=93;v[4].num=970160;strcpy(v[4].name,李启明);v[4].score=88;nn=5;}voidsq_LList::prt_sq_LList(){inti;for(i=0;inn;i++){cout学号:v[i].num姓名:v[i].name分数:v[i].scoreendl;}}/*插入*/voidsq_LList::ins_sq_LList(inti,STUDENTb)3{intk;if(nn==mm){coutoverflow;return;}if(inn)i=nn+1;if(i1)i=1;for(k=nn;k=i;k--)v[k]=v[k-1];v[i-1]=b;nn=nn+1;}/*删除*/voidsq_LList::del_sq_LList(inti){intk;if(nn==0){coutunderflowendl;return;}if((i1)||(inn)){coutNotthiselementinthelist!endl;return;}for(k=i;knn;k++)v[k-1]=v[k];nn=nn-1;}4/*按学号查找*/voidsq_LList::sea_num_sq_LList(inti){intk,t;____t=0;for(i=0;inn;i++){if(v[i].num==k){t=t+1;cout学号:v[i].num姓名:v[i].name分数:v[i].scoreendl;}}if(t==0)coutNothisstudentinthelist!endl;}/*按姓名查找*/voidsq_LList::sea_name_sq_LList(inti,chary[]){intt;____t=0;for(i=0;inn;i++){if(strcmp(y,v[i].name)=0)5{t=t+1cout学号:v[i].num姓名:v[i].name分数:v[i].scoreendl;}}if(t==0)coutNothisstudentinthelist!endl}/*计算*/voidsq_LList::cal_sq_LList(intm){inti;floatsum,avr;{sum=0;for(i=0;inn;i++){sum=sum+v[i].score;avr=sum/(i+1);}}cout总分:sumendl;cout平均分:avrendl;}intmain(){intmx;sq_LLists1(100);6while(1){cout1.输出2.插入3.删除4.查找5.计算0.退出\n;cout输入0-5:;cinmx;switch(mx){case1:s1.prt_sq_LList();break;case2:inti;STUDENTb;cout输入插入点位置和插入元素值:;cinib.numb.nameb.score;s1.ins_sq_LList(i,b);s1.prt_sq_LList();break;case3:cout请输入删除学生的位置:;cini;s1.del_sq_LList(i);s1.prt_sq_LList();break;case4:intmain(){intmx;while(1){cout1.按学号查找2.按姓名查找0.返endl;cout输入0-2:;cinmx;switch(mx){7case1:longdoublek;批注[A1]:删除cout请输入要查找学生的学号:;cink;批注[A1]:删除s1.sea_num_sq_LList(i);break;case2:chary[10];批注[A3]cout请输入要查找学生的姓名:;ciny;批注[A3]s1.sea_name_sq_LList();break;case0:cout返回endl;return;}}return0;}break;case5:s1.cal_sq_LList();break;case0:cout程序结束endl;return0;}}return0;}82、正确程序:#includeiostreamusingnamespacestd;typedefstruct{longdoublenum;charname[10];intscore;}STUDENT;classsq_LList{private:intmm;intnn;STUDENT*v;public:sq_LList(int);voidprt_sq_LList();voidins_sq_LList(int,STUDENT);voiddel_sq_LList(int);voidsea_num_sq_LList(int);voidsea_name_sq_LList();voidcal_sq_LList(int);};/*输出*/sq_LList::sq_LList(intm){mm=m;9v=newSTUDENT[mm];v[0].num=970156;strcpy(v[0].name,张小明);v[0].score=87;v[1].num=970157;strcpy(v[1].name,李小青);v[1].score=96;v[2].num=970158;strcpy(v[2].name,刘华);v[2].score=85;v[3].num=970159;strcpy(v[3].name,王伟);v[3].score=93;v[4].num=970160;strcpy(v[4].name,李启明);v[4].score=88;nn=5;}voidsq_LList::prt_sq_LList(){inti;for(i=0;inn;i++){cout学号:v[i].num姓名:v[i].name分数:v[i].scoreendl;}}/*插入*/voidsq_LList::ins_sq_LList(inti,STUDENTb){intk;if(nn==mm){coutoverflow;return;}if(inn)i=nn+1;if(i1)i=1;10for(k=nn;k=i;k--)v[k]=v[k-1];v[i-1]=b;nn=nn+1;}/*删除*/voidsq_LList::del_sq_LList(inti){intk;if(nn==0){coutunderflowendl;return;}if((i1)||(inn)){coutNotthiselementinthelist!endl;return;}for(k=i;knn;k++)v[k-1]=v[k];nn=nn-1;}/*按学号查找*/voidsq_LList::sea_num_sq_LList(inti){intk,t;cink;t=0;11for(i=0;inn;i++){if(v[i].num==k){t=t+1;cout学号:v[i].num姓名:v[i].name分数:v[i].scoreendl;}}if(t==0)coutNothisstudentinthelist!endl;}/*按姓名查找*/voidsq_LList::sea_name_sq_LList(){chary[10];inti,t;ciny;t=0;for(i=0;inn;i++){if(strcmp(y,v[i].name)==0){t=t+1;cout学号:v[i].num姓名:v[i].name分数:v[i].scoreendl;}}12if(t==0)coutNothisstudentinthelist!endl;}/*计算*/voidsq_LList::cal_sq_LList(intm){inti;floatsum,avr;{sum=0;for(i=0;inn;i++){sum=sum+v[i].score;avr=sum/(i+1);}}cout总分:sumendl;cout平均分:avrendl;}intmain(){intmx;sq_LLists1(100);while(1){cout1.输出2.插入3.删除4.查找5.计算0.退出\n;cout输入0-5:;cinmx;13switch(mx){case1:s1.prt_sq_LList();break;case2:inti;STUDENTb;cout输入插入点位置和插入元素值:;cinib.numb.nameb.score;s1.ins_sq_LList(i,b);s1.prt_sq_LList();break;case3:cout请输入删除学生的位置:;cini;s1.del_sq_LList(i);s1.prt_sq_LList();break;case4:{intmx;while(1){cout1.按学号查找2.按姓名查找0.返endl;cout输入0-2:;cinmx;switch(mx){case1:cout请输入要查找学生的学号:;s1.sea_num_sq_LList(i);break;case2:cout请输入要查找学生的姓名:;s1.sea_name_sq_LList();break;case0:cout返回endl;return0;14}}return0;}break;case5:s1.cal_sq_LList(i);break;case0:cout程序结束endl;return0;}}return0;}四、实验总结
本文标题:建立一个顺序表,表中元素为学生,每个学生信息包含姓名、学号和成绩三部分,对该表实现输出、插入、删除、
链接地址:https://www.777doc.com/doc-6222411 .html