完整word版c++项目设计.docx
《完整word版c++项目设计.docx》由会员分享,可在线阅读,更多相关《完整word版c++项目设计.docx(29页珍藏版)》请在冰点文库上搜索。
完整word版c++项目设计
//学生管理系统代码
#include
#include
#include
#include
usingnamespacestd;
enum{SUBJECT=5};//一共五门
typedefstruct
{
charsubject[10];//科目名称
intscore;//科目成绩
}markinfo;
typedefstructstudentnode
{
markinfomark[SUBJECT];
inttotalmark;
charname[10];//学生姓名
studentnode*next;
}studentnode;
classstudent
{
studentnode*head;
public:
student();
intaddstudent();
~student();
intcountmark();
intsortbymark();
intsave();
intshow();
intdisplay();
intreadfiletolist();
intsearchbyname();
};
student:
:
student()//用构造函数来初始化。
{
head=newstudentnode;
head->next=NULL;
}
//1.输入学生姓名、成绩等数据,并保存在链表中。
intstudent:
:
addstudent()
{
studentnode*p;
inti;
charcheck;
system("cls");
cout<<"**********************"<cout<<"请输入学生信息:
"<do
{
p=newstudentnode;
cin.ignore();
cout<<"姓名:
";
gets(p->name);
i=0;
p->totalmark=0;
do
{
cout<<"科目:
";
gets(p->mark[i].subject);
cout<<"成绩(0--100):
";
do
{
cin>>p->mark[i].score;
}
while(p->mark[i].score>100||p->mark[i].score<0);
p->totalmark=p->totalmark+p->mark[i].score;
getchar();
}
while(++i!
=SUBJECT);
if(head->next==NULL)
{
head->next=p;p->next=NULL;
}
else
{
p->next=head->next;
head->next=p;
}
cout<<"继续添加?
yorn:
";
check=getchar();
}
while(check!
='n'&&check!
='N');
return0;
}
//2.计算每位学生总成绩。
intstudent:
:
countmark()
{
studentnode*p=head->next;
if(p==NULL)
{
cout<<"没有学生,请重新输入"<}
else
{
cout<<"***************"<cout<<"学生成绩汇总:
"<while(p)
{
cout<<"姓名:
"<name<<"总成绩:
"<totalmark<p=p->next;
}
}
system("pause");
return0;
}
//4.输出所有学生成绩到一个文件中。
intstudent:
:
save()
{
charaddress[35];
inti;
studentnode*p=head->next;
cout<<"请输入保存的地址"<cin.ignore();
gets(address);
ofstreamfout;
fout.open(address,ios:
:
app|ios:
:
out);
while(p)
{
fout<<"*";
fout<name<<"*";
i=0;
while(i!
=SUBJECT)
{
fout<mark[i].subject<<"*";
fout<mark[i].score;
i++;
}
//fout<<"*";
p=p->next;
}
fout.flush();
fout.close();
cout<<"已经保存,请查阅";
system("pause");
return0;
}
student:
:
~student()//析构函数
{
studentnode*p,*s;
p=head->next;
while(p)
{
s=p->next;
deletep;
p=s;
}
deletehead;
}
//3.按照总成绩大小对记录进行排序
intstudent:
:
sortbymark()
{
studentnode*move1=head->next;
studentnode*move2,*max,*pre1,*pre2,*maxpre,*s=move1;
if(head->next==NULL)
{
cout<<"没有记录,请添加"<}
for(pre1=head,max=move1,maxpre=pre1;move1->next!
=NULL;pre1=move1,maxpre=pre1,move1=move1->next,max=move1)
{
for(pre2=move1,move2=move1->next;move2!
=NULL;pre2=move2,move2=move2->next)
if(move2->totalmark>max->totalmark)
{
maxpre=pre2;
max=move2;
}
if(move1->next==max)//交换max和move1。
{
pre1->next=max;
move1->next=max->next;
max->next=move1;
move1=max;
}
else
{
s=move1->next;
move1->next=max->next;
max->next=s;
maxpre->next=move1;
pre1->next=max;
move1=max;
}
}
cout<<"已经按照从大到小排序"<system("pause");
return0;
}
//5输出输入的信息
intstudent:
:
show()
{
studentnode*p=head->next;
inti;
if(head->next==NULL){cout<<"没有学生记录,请添加"<else
{
while(p)
{
cout<<"姓名:
"<name;
i=1;
while(i!
=SUBJECT+1)
{
cout<<"科目:
"<mark[i-1].subject;
cout<<"成绩:
"<mark[i-1].score;
i++;
}
cout<p=p->next;
}
}
system("pause");
return0;
}
//6:
从文件按读取记录
intstudent:
:
display()
{
ifstreamfin;
charbuf[100];
charstr[25];
cout<<"请输入路径及文件名:
"<cin.ignore();
gets(str);
fin.open(str);
if(!
fin)
{
cout<<"没有此文件"<system("pause");
return0;
}
while(fin)
{
fin.getline(buf,sizeof(buf));
cout<}
system("pause");
return0;
}
//8从文件中读取数据,并将数据保存在链表中
intstudent:
:
readfiletolist()
{
ifstreamfin;
inti;
charstr[25];
cout<<"请输入路径及文件名:
"<cin.ignore();
gets(str);
fin.open(str);
if(!
fin)
{
cout<<"没有此文件"<system("pause");
return0;
}
studentnode*p;
fin.ignore(100,'*');
while(fin)
{
p=newstudentnode;
p->totalmark=0;
fin.getline(p->name,100,'*');
i=0;
while(i!
=SUBJECT)
{
fin.getline(p->mark[i].subject,100,'*');
fin>>p->mark[i].score;
p->totalmark+=p->mark[i].score;
i++;
}
if(head->next==NULL)
{
head->next=p;
p->next=NULL;
}
else
{
p=head->next;
head->next=p;
}
}
cout<<"信息已经保存在链表中"<system("pause");
return0;
}
//9根据姓名进行查找
intstudent:
:
searchbyname()
{
if(head->next==NULL)
{
cout<<"没有学生,请添加或者从文件中读取"<system("pause");
return0;
}
studentnode*p=head->next;
charfindname[10];
inti;
cout<<"请输入姓名:
";
cin.ignore();
gets(findname);
while(p)
{
if(!
strcmp(p->name,findname))
{
cout<<"经查找,找到该生信息如下:
"<cout<<"姓名:
"<name;
i=1;
while(i!
=SUBJECT+1)
{
cout<<"科目:
"<mark[i-1].subject;
cout<<"成绩:
"<mark[i-1].score;
i++;
}
cout<system("pause");
return1;
}
p=p->next;
}
cout<<"没有此学生,请添加或者从文件中读取"<system("pause");
return0;
}
intshowmenu()
{
intchoice;
char*menu[9]={
"1:
输入学生成绩保存到链表\n",
"2:
计算每位学生总成绩\n",
"3:
按照总成绩大小对记录进行排序\n",
"4:
输出所有学生成绩到一个文件中\n",
"5:
显示新输入的学生信息\n",
"6:
从文件中读取信息\n",
"7:
将文件信息保存在链表中\n",
"8:
根据姓名查找学生记录\n",
"9:
结束程序\n"
};
cout<<""<<"*****************************************************"<cout<<"*"<<""<<"学生成绩管理系统"<<"*"<cout<<""<<"*****************************************************"<for(choice=0;choice<9;choice++)
cout<<""<