资源描述
C++语言课程设计设计报告
二○一二~二○一三学年第二学期
信息科学与工程学院
面向对象C++语言课程设计报告
☆课程名称: 面向对象C++语言课程设计
目 录
1.需求分析……………………………………………………3
2.总体设计……………………………………………………4
3.详细设计……………………………………………………5
4.调试测验……………………………………………………6
5.测试结果……………………………………………………6
6.心得体会……………………………………………………12
7.相关附录……………………………………………………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 <iostream>
#include<string>
#include<stdlib.h>
#include<fstream>
using namespace std;
#define NULL 0
class 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<<"*************** ☆ 学 生 信 息 管 理 系 统 ☆ *************** " <<endl;// goto指向的位置
cout<<"********** ★★★★ ★★★★★★★ ★★★★★ ******** "<<endl;
cout<<"***************★ ☆ 1.输入学生信息 ☆ ★***************"<<endl;
cout<<"***************★ ☆ 2.插入学生信息 ☆ ★***************"<<endl;
cout<<"***************★ ☆ 3.修改学生信息 ☆ ★***************"<<endl;
cout<<"***************★ ☆ 4.删除学生信息 ☆ ★***************"<<endl;
cout<<"***************★ ☆ 5.显示学生信息 ☆ ★***************"<<endl;
cout<<"***************★ ☆ 6.查询学生信息 ☆ ★***************"<<endl;
cout<<"***************★ ☆ 7.退出 ☆ ★***************"<<endl;
cout<<" 请输入您的选择(1--7):"<<endl;
cin>>choose;
switch(choose) //选择结构,选择要进行的操作
{case 1: system("cls"); //系统清屏
cout<<"输入学生信息:"<<endl;
head=creat();//调用函数,建立链表
system("cls");
goto start;//返回目录
break;
case 2: system("cls");
cout<<endl<<"输入要插入的学生信息:";
stu=new student; //新建结点
cout<<"学生的学号:";
cin>>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<<endl<<"输入要继续插入的学生信息:";
stu=new student;
cout<<"学生的学号:";
cin>>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<<endl<<"输入要修改信息学生的学号/姓"<<endl;
cout<<"按学号查询输入1,姓名查询输入"<<endl;
cin>>a;
if(a==1)
{cout<<endl<<"要修改信息学生的学号:";
cin>>correct_numb ;
head=correct_num(head,correct_numb); //按学号修改学生信息
save(head);
}
if(a==2)
{cout<<endl<<"要修改信息学生的姓名:";
cin>>correct_name;
head=correct_nam(head,correct_name);
// 按姓名修改学生信息
save(head);}
system("cls");
goto start;
break;
case 4: system("cls");
cout<<endl<<"输入要删除学生的学号:";
cin>>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<<"学生信息如下:"<<endl;
system("cls");
print(head);
system("pause");//运行停在当前页面
system("cls");
goto start;
break;
case 6: system("cls");
cout<<endl<<"输入要查询信息学生的学号/姓名:"<<endl;
cout<<"按学号查询输入1,姓名查询输入2:"<<endl;
cin>>a;
system("cls");
if(a==1)
{cout<<endl<<"要查询信息学生的学号:";
cin>>search_numb;
head=search_num(head,search_numb);}
//调用按学号查询函数
if(a==2)
{cout<<endl<<"要查询信息学生的姓名:";
cin>>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<<endl<<"请重新选择1--7中的数字!";
//输入非选项数字时,返回主菜单
system("pause");
system("cls");
goto start;
break;
}
return 0;
}
//创建链表的函数
student *creat(void)
{ofstream outfile("f1.dat",ios::out);//打开文件
if(!outfile)
{cerr<<"打开失败"<<endl;
exit(0);}
student *head;
student *p1,*p2;
n=0;
p1=p2=new student;//新建对象
cout<<"学生的学号:";
cin>>p1->num;
outfile<<p1->num<<" "; //将数据存入文件
cout<<"学生的姓名:" ;
cin>>p1->name;
outfile<<p1->name<<" ";
cout<<"学生的年龄:";
cin>>p1->age;
outfile<<p1->age<<" ";
cout<<"学生的性别:" ;
cin>>p1->sex;
outfile<<p1->sex<<" ";
cout<<"学生的住址:" ;
cin>>p1->address;
outfile<<p1->address<<" ";
cout<<"学生的电脑成绩:" ;
cin>>p1->computer_score;
outfile<<p1->computer_score<<" ";
cout<<"学生的数学成绩:" ;
cin>>p1->math_score;
outfile<<p1->math_score<<" ";
cout<<"学生的英语成绩:" ;
cin>>p1->English_score;
outfile<<p1->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<<p1->num<<" ";
cout<<"学生的姓名:" ;
cin>>p1->name;
outfile<<p1->name<<" ";
cout<<"学生的年龄:";
cin>>p1->age;
outfile<<p1->age<<" ";
cout<<"学生的性别:" ;
cin>>p1->sex;
outfile<<p1->sex<<" ";
cout<<"学生的住址:" ;
cin>>p1->address;
outfile<<p1->address<<" ";
cout<<"学生的电脑成绩:" ;
cin>>p1->computer_score;
outfile<<p1->computer_score<<" ";
cout<<"学生的数学成绩:" ;
cin>>p1->math_score;
outfile<<p1->math_score<<" ";
cout<<"学生的英语成绩:" ;
cin>>p1->English_score;
outfile<<p1->English_score<<" ";
}
p2->next=NULL;
outfile.close();//关闭文件
return(head);
}
//保存文件的函数
int save(student *head)
{ofstream outfile("f1.dat",ios::out);
if(!outfile)
{cerr<<"打开文件出错!"<<endl;
exit(0);}
student *p1=head,*p2;
if(head==NULL)
{cout<<"列表为空!"<<endl;return 0;};
do{outfile<<p1->num<<" "<<p1->name<<" "<<p1->age<<" "<<p1->sex<<" "<<p1->address<<" "<<p1->computer_score<<" "<<p1->math_score<<" "<<p1->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<<"列表为空!"<<endl;return(head);}
p1=head;
while(num!=p1->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<<"删除的学号:"<<num<<endl;
n=n-1;
}
else cout<<"不能找到此学号的学生:"<<num;
return(head);
}
//插入学生信息的函数
student *insert(student *head,student *stud)
{student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{head=p0;p0->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<<"列表为空!"<<endl;return(head);}
p=head;
while(num!=p->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<<"不能找到此学号的学生:"<<num;
return(head);
}
//按姓名修改学生信息的函数
student *correct_nam(student*head,string name)
{student *p;
if(head==NULL)
{cout<<"列表为空!"<<endl;return(head);}
p=head;
while(name!=p->name&&p->next!=NULL)
{p=p->next;}
if(p->name==name)
{cout<<endl<<"请重新输入此学生的信息";
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;
return(head);
}
//按学号查询学生信息的函数
student *search_num(student *head,long num)
{student *p;
if(head==NULL)
{cout<<"列表为空!"<<endl;return(head);}
p=head;
while(num!=p->num&&p->next!=NULL)
p=p->next;
if(num==p->num) //找到后输出学生信息
{cout<<"学生的学号:";
cout<<p->num<<endl;
cout<<"学生的姓名:" ;
cout<<p->name<<endl;
cout<<"学生的年龄:";
cout<<p->age<<endl;
cout<<"学生的性别:" ;
cout<<p->sex<<endl;
cout<<"学生的住址:" ;
cout<<p->address<<endl;
cout<<"学生的电脑成绩:" ;
cout<<p->computer_score<<endl;
cout<<"学生的数学成绩:" ;
cout<<p->math_score<<endl;
cout<<"学生的英语成绩:" ;
cout<<p->English_score<<endl;}
else cout<<"不能找到此学号的学生:"<<num;
return(head);}
//按姓名查询学生信息的函数
student *search_nam(student *head,string name)
{student *p;
if(head==NULL)
{cout<<"列表为空!"<<endl;return(head);}
p=head;
while(name!=p->name&&p->next!=NULL)
p=p->next;
if(name==p->name)
{cout<<"学生的学号:";
cout<<p->num<<endl;
cout<<"学生的姓名:" ;
cout<<p->name<<endl;
cout<<"学生的年龄:";
cout<<p->age<<endl;
cout<<"学生的性别:" ;
cout<<p->sex<<endl;
cout<<"学生的住址:" ;
cout<<p->address<<endl;
cout<<"学生的电脑成绩:" ;
cout<<p->computer_score<<endl;
cout<<"学生的数学成绩:" ;
cout<<p->math_score<<endl;
cout<<"学生的英语成绩:" ;
cout<<p->English_score<<endl;}
else cout<<"不能找到此学号的学生:"<<name;
return(head);}
//输出函数
void print(student *head)
{student *p;
if(head==NULL) //头指针为空时提示重新录入
cout<<"还没有录入学生信息"<<endl;
if(head!=NULL) //头指针不为空时输出学生信息
{ cout<<"这"<<n<<"个学生的信息如下:" <<endl;
p=head;
do
{ cout<<"学生的学号:";
cout<<p->num<<endl;
cout<<"学生的姓名:" ;
cout<<p->name<<endl;
cout<<"学生的年龄:";
cout<<p->age<<endl;
cout<<"学生的性别:" ;
cout<<p->sex<<endl;
cout<<"学生的住址:" ;
cout<<p->address<<endl;
cout<<"学生的电脑成绩:" ;
cout<<p->computer_score<<endl;
cout<<"学生的数学成绩:" ;
cout<<p->math_score<<endl;
cout<<"学生的英语成绩:"
cout<<p->English_score<<endl;
p=p->next; //指向下一个结点
}while(p!=NULL);} //结点不为空时,继续输出
}
®版权所有,仿冒必究©如有雷同,不胜荣幸。
33
展开阅读全文