二○一二~二○一三学年第二学期信息科学与工程学院面向对象C++语言课程设计报告☆课程名称: 面向对象C++语言课程设计 目 录1.需求分析……………………………………………………32.总体设计……………………………………………………43.详细设计……………………………………………………54.调试测验……………………………………………………65.测试结果……………………………………………………66.心得体会……………………………………………………127.相关附录……………………………………………………13Ⅰ.需求分析(1)问题描述:设计一个学生信息管理系统,能录入、修改、添加、删除、查询、显示学生信息并将学生信息在文件中保存2)应用价值:能作为一个有效管理学生信息的系统可以储存学生的姓名、学号、性别、年龄、住址、各科成绩这在现实中对学生信息的管理也有极其重要的应用价值可以有效的保存学生信息,应用于教务管理3)限制条件:因为刚接触C语言不到,两个学期所以所做程序难免会有一些瑕疵在次列举其中的一些限制条件。
我所设置的录入学生信息的函数必须由学号由小到大录入,这是为了配合后面的修改、添加、删除查询功能因为后面几个功能实现的方法就是由学号从小到达找所要修改的那一个结点另一个问题就是学号的首位不可以为零,否则的话就无法显示首位对于这一问题,我还没有想到更好的解决方法不过在现实应用中,很少出现学号首位为零的情况因此也就不会太影响使用价值最后一个问题,就是如果输入的数据类型与定义的不符,就可能引起系统崩溃因此,用户在录入信息时应特别注意数据类别Ⅱ总体设计(1) 程序设计组成框图1、输入学生的信息:姓名、性别、学号、地址、成绩4、输入要删除的学生的学号,删除学生信息 菜单1. 输入学生信息2. 插入学生信息3. 修改学生信息4. 删除学生信息5. 显示学生信息6. 查询学生信息7. 退出5、显示学生的姓名、性别、学号、地址、成绩2、输入要插入学生的学号,填加信息6、输入你要查找学生姓名或学号,修改学生信息3、按学生的学号或姓名来修改学生的相关信息7、退出系统学生信息管理系统功能模块图(2) 流程图开 始输入学生信息息生生信息修 改插 入删 除查 询退 出显示结 束Ⅲ详细设计函数功能student *creat(void)用于创建链表,不需要参数,返回链表头指针,在输入学生信息时调用。
student *del(student*,long)用于删除结点,参数为头指针及要删除学生的学号,返回头指针在删除学生信息时调用 student *insert(student *,student *)用于插入结点,参数为头指针及类的指针,返回头指针插入学生信息时调用student *correct_num(student *,long)用于修改结点,参数为头指针及学号,返回头指针按学号修改学生信息时调用student *correct_nam(student *,string)按姓名修改学生信息时调用student *search_num(student *,long)用于查找结点,参数为头指针及学号,返回头指针按学号查找学生信息时调用student *search_nam(student *,string) 用于查找结点,参数为头指针及学号,返回头指针按姓名查找学生信息时调用int save(student *head)用于将指针数据存放到文件中 void print(student*)参数为头指针,用于输出各结点数据Ⅳ调试测验通过输入学生信息,再修改、删除、添加、查询,最后输出。
检查是否满足预期结果,不满足则重新完善过程中也遇到许多问题其中之一就是在执行查询、添加等功能是出现程序停止工作的情况最后才发现在进行判断时,将head==NULL误输为head=NULL,将链表转化为了空链表,当然无法工作啦在其他细节方面,也出现了类似的错误因此,编程要特别仔细,一个小小的错误,都可能使整个程序毁于一旦Ⅴ测试结果(1)首页(2)输入非选项中的的数字(3)录入信息(4)插入信息(5)显示信息(6)删除信息(7)修改信息(8)查询信息(9)查询学生学号不存在时(10)显示信息(11)退出系统Ⅵ心得体会耗时将近两周的C++程序设计即将画上句号这也是我接触过的最复杂的程序要想一步完成如此繁琐的程序是不可能的因此必须有合理的规划,列出框架再分别用函数来实现各个功能然后就是细化的工作,要分别设计各个函数,这也是最关键的一步既要注意各个函数的独立性,又要注意他们之间的关系和在整个程序中的作用最后将各不分有机结合为宜各整体再通过不断的调试、完善,最后达到预期的效果当然,最困难的部分就是修改面对一个个问题,你需要不断的检查,在繁杂的程序中发现那一个错误这过程虽然是困难的,必须非常仔细,绞尽脑汁想各种错误的原因,最后解决问题。
当问题被解决的那一刻,心中会由然而生出一种成就感这也是程序设计带给我的快乐在今后的学习生活中,我将投入更多时间到C++程序设计中,这样设计程序将会更加得心应手程序设计就是这样一门神奇的课程,只有勤于思考,刻苦钻研,勇于创新才能,才能设计出满意的程序Ⅶ相关附录#include #include#include#includeusing namespace std;#define NULL 0class student //定义类{public: long num; char name[20]; int age; char sex[20]; char address[30]; float computer_score; float math_score; float English_score; student *next;};int n;//定义主函数int main(){student *creat(void); student *del(student*,long); student *insert(student *,student *); student *correct_num(student *,long); student *correct_nam(student *,string); student *search_num(student *,long); student *search_nam(student *,string); int save(student *head); void print(student*); student *head=NULL,*stu; long del_numb,correct_numb,search_numb; int choose,a; string search_name,correct_name; start:cout<<"*************** ☆ 学 生 信 息 管 理 系 统 ☆ *************** " <>choose; switch(choose) //选择结构,选择要进行的操作 {case 1: system("cls"); //系统清屏 cout<<"输入学生信息:"<>stu->num; cout<<"学生的姓名:" ; cin>>stu->name; cout<<"学生的年龄:"; cin>>stu->age; cout<<"学生的性别:" ; cin>>stu->sex; cout<<"学生的住址:" ; cin>>stu->address; cout<<"学生的电脑成绩:" ; cin>>stu->computer_score; cout<<"学生的数学成绩:" ; cin>>stu->math_score; cout<<"学生的英语成绩:" ; cin>>stu->English_score; while(stu->num!=0) //结点学号不为空时插入 {head=insert(head,stu);//调用函数,插入结点 save(head);//将数据储存在文件中 cout<>stu->num; //输入结点相关信息 cout<<"学生的姓名:" ; cin>>stu->name; cout<<"学生的年龄:"; cin>>stu->age; cout<<"学生的性别:" ; cin>>stu->sex; cout<<"学生的住址:" ; cin>>stu->address; cout<<"学生的电脑成绩:" ; cin>>stu->computer_score; cout<<"学生的数学成绩:" ; cin>>stu->math_score; cout<<"学生的英语成绩:" ; cin>>stu->English_score;} system("cls"); goto start; break; case 3: system("cls"); cout<>a; if(a==1) {cout<>correct_numb ; head=correct_num(head,correct_numb); //按学号修改学生信息 save(head); } if(a==2) {cout<>correct_name; head=correct_nam(head,correct_name);// 按姓名修改学生信息 save(head);} system("cls"); goto start; break; case 4: system("cls"); cout<>del_numb; while(del_numb!=0) {head=del(head,del_numb); save(head); cout<<"请输入要继续删除学生的学号:"; cin>>del_numb; system("cls"); goto start; } break; case 5: system("cls"); cout<<"学生信息如下:"<>a; system("cls"); if(a==1) {cout<>search_numb; head=search_num(head,search_numb);}//调用按学号查询函数 if(a==2) {cout<>search_name; head=search_nam(head,search_name);} //按姓名查询学生信息 system("pause"); system("cls"); goto start; break; case 7: system("cls"); cout<<"按任意键退出程序!"; exit(0);break;//调用系统函数exit,运行结束 default:cout<>p1->num; outfile<num<<" "; //将数据存入文件 cout<<"学生的姓名:" ; cin>>p1->name; outfile<name<<" "; cout<<"学生的年龄:"; cin>>p1->age; outfile<age<<" "; cout<<"学生的性别:" ; cin>>p1->sex; outfile<sex<<" "; cout<<"学生的住址:" ; cin>>p1->address; outfile<address<<" "; cout<<"学生的电脑成绩:" ; cin>>p1->computer_score; outfile<computer_score<<" "; cout<<"学生的数学成绩:" ; cin>>p1->math_score; outfile<math_score<<" "; cout<<"学生的英语成绩:" ; cin>>p1->English_score; outfile<English_score<<" "; head=NULL; while(p1->num!=0) {n=n+1; if(n==1) head=p1; else p2->next=p1;//p1指向下一个结点 p2=p1; p1=new student; cout<<"学生的学号:"; cin>>p1->num; outfile<num<<" "; cout<<"学生的姓名:" ; cin>>p1->name; outfile<name<<" "; cout<<"学生的年龄:"; cin>>p1->age; outfile<age<<" "; cout<<"学生的性别:" ; cin>>p1->sex; outfile<sex<<" "; cout<<"学生的住址:" ; cin>>p1->address; outfile<address<<" "; cout<<"学生的电脑成绩:" ; cin>>p1->computer_score; outfile<computer_score<<" "; cout<<"学生的数学成绩:" ; cin>>p1->math_score; outfile<math_score<<" "; cout<<"学生的英语成绩:" ; cin>>p1->English_score; outfile<English_score<<" "; } p2->next=NULL; outfile.close();//关闭文件 return(head); }//保存文件的函数 int save(student *head) {ofstream outfile("f1.dat",ios::out); if(!outfile) {cerr<<"打开文件出错!"<num<<" "<name<<" "<age<<" "<sex<<" "<address<<" "<computer_score<<" "<math_score<<" "<English_score;//将链表数据存入文件 p2=p1;p1=p1->next;} while(p2->next!=NULL); outfile.close(); return 0; }//删除数据的函数 student *del(student *head,long num) {student *p1,*p2; if(head==NULL) {cout<<"列表为空!"<num&&p1->next!=NULL)//按学号从小到大查找 {p2=p1;p1=p1->next;} if(num==p1->num) {if(p1=head)head=p1->next;//如果找到 else p2->next=p1->next; cout<<"删除的学号:"<next=NULL;} else {while((p0->num>p1->num)&&(p1->next!=NULL)) {p2=p1; p1=p1->next;} if(p0->num<=p1->num) {if(head==p1)head=p0;//p1学号最小,则作为头指针 else p2->next=p0;//否则插入中间 p0->next=p1;} else {p1->next=p0;p0->next=NULL;}//如果最大,则放在最后 } n=n+1;//记录结点数 return(head); }//按学号修改学生信息的函数 student *correct_num(student *head,long num) {student *p; if(head==NULL) {cout<<"列表为空!"<num&&p->next!=NULL) {p=p->next;} if(num==p->num)//遭到后重新录入学生的信息 {cout<<"学生的学号:"; cin>>p->num; cout<<"学生的姓名:" ; cin>>p->name; cout<<"学生的年龄:"; cin>>p->age; cout<<"学生的性别:" ; cin>>p->sex; cout<<"学生的住址:" ; cin>>p->address; cout<<"学生的电脑成绩:" ; cin>>p->computer_score; cout<<"学生的数学成绩:" ; cin>>p->math_score; cout<<"学生的英语成绩:" ; cin>>p->English_score; } else cout<<"不能找到此学号的学生:"<name&&p->next!=NULL) {p=p->next;} if(p->name==name) {cout<>p->num; cout<<"学生的姓名:" ; cin>>p->name; cout<<"学生的年龄:"; cin>>p->age; cout<<"学生的性别:" ; cin>>p->sex; cout<<"学生的住址:" ; cin>>p->address; cout<<"学生的电脑成绩:" ; cin>>p->computer_score; cout<<"学生的数学成绩:" ; cin>>p->math_score; cout<<"学生的英语成绩:" ; cin>>p->English_score; } else cout<<"不能找到此学号的学生:"<num&&p->next!=NULL) p=p->next; if(num==p->num) //找到后输出学生信息 {cout<<"学生的学号:"; cout<num<name<age<sex<address<computer_score<math_score<English_score<name&&p->next!=NULL) p=p->next; if(name==p->name) {cout<<"学生的学号:"; cout<num<name<age<sex<address<computer_score<math_score<English_score<num<name<age<sex<address<computer_score<math_score<English_score<next; //指向下一个结点 }while(p!=NULL);} //结点不为空时,继续输出 }®版权所有,仿冒必究©如有雷同,不胜荣幸。