编写一个简单的学生成绩管理系统。学生成绩管理系统有若干学生成绩记录组成,每个记录由学号、姓名、性别和成绩四部分组成,学号不超过11个字符,姓名不超过21个字符,性别为’m’(male)或’f’(female),成绩为double型数据。该系统具有以下功能:
(1) 程序开始执行时显示如下版权信息和主菜单:
*******学生成绩管理系统*************
l *1.添加学生成绩记录
l *2.显示学生成绩记录
l *3.查找学生成绩记录
l * 4.删除学生成绩记录
l * 5.保存学生成绩记录
l * 6.加载学生成绩记录
l * 7.退出学生成绩管理系统
************************************
请输入(1~7):
(2) 通过键盘输入数字1~7后,程序能够执行相应的功能,执行完后回到上述主菜单继续等待用户输入,输入数字7后退出程序。
(3) 选择“添加学生成绩记录”后,输入要插入记录的位置。按程序提示依次输入学号,姓名,性别,成绩,插入记录成功。
(4) 选择“显示学生成绩记录”,显示学生成绩表中已有的学生记录的个数,并显示出各个学生记录的相关信息。
(5) 选择“查找学生成绩记录”,按提示输入要查找的学生的学号,输入学号,若该学号存在,则输出相应的学生记录,如该学号不存在,则按任意键返回主菜单。
(6) 选择“删除学生成绩记录”,按提示输入要删除的学生的学号,输入学号,若改学号存在,则删除相应的学生记录,如果该学号不存在,则按任意键返回主菜单。
(7) 选择“保存学生成绩记录”,系统会将已有的学生记录保存在默认的文件名为fname.txt的文件中。
(8) 选择“加载学生成绩记录”,系统会提示输入文件名,输入默认的文件名为fname.txt,则可将文件中的相关信息导出。
(9) 选择“退出学生成绩管理系统”,按任意键退出系统。
#include <iostream>
#include<string>
using namespace std;
class node
{
public:
int num;
string name;
int age;
node *prev;
node *next;
/*~node()
{
this->next=NULL;
this->prev=NULL;
this->num=NULL;
this->=NULL;
this->age=NULL;
}*/
};
class list
{public:
node *head;
node *tail;
int length;
list():head(NULL),tail(NULL){}
node * creat( );
void show(node *);
void show_node(node *);
void show_stack(node *,int);
node * find(node *);
void add(node *);
void del(node *);
void change(node *);
};
node* list::creat()
{
int n;
node *head=new node;
tail=head;
cout<<"输入建立链表的长度\n";
cin>>n;length=n;
cout<<"请输入第1个人员信息:"<<endl;
node *p=new node;
cout<<"学号:";cin>>p->num;
cout<<"姓名:";cin>>p->name;
cout<<"年龄:";cin>>p->age;
tail->next=p;
tail=p;
head->prev=tail;
for(int i=1;i<n;i++)
{
cout<<"请输入第"<<i+1<<"个人员信息:"<<endl;
node *p=new node;
cout<<"学号:";cin>>p->num;
cout<<"姓名:";cin>>p->name;
cout<<"年龄:";cin>>p->age;
tail->next=p;
p->prev=tail;
tail=p;
head->prev=tail;
}
tail->next=NULL;
return head;
}
void list::show_stack(node *h,int ln)
{
node *hs;
int le=ln;
hs=h->prev;
//he->prev=tail;
for(int i=0;i<le;i++)
{
cout <<"学号:"<<"姓名:"<<"年龄:"<<endl;
cout<<hs->num<<hs->name<<hs->age<<endl;
hs=hs->prev;
}
}
void list::show(node *h)
{
node *he;
he=h->next;
while (he->next) //含两个元素及以上
{ cout<<"学号:"<<he->num<<endl;
cout<<"姓名:"<<he->name<<endl;
cout<<"年龄:"<<he->age<<endl;
he=he->next;
}
cout<<"学号:"<<he->num<<endl;
cout<<"姓名:"<<he->name<<endl;
cout<<"年龄:"<<he->age<<endl;
cout<<"输出结束\n";
}
node * list::find(node *h)
{
int n;
cout<<"输入要查找学生的学号:"<<endl;
cin>>n;
node *p,*q;
q=h;
p=q->next;
while(p->num!=n)
{
q=q->next;
p=q->next;
}
return q;
}
void list::show_node(node *q)
{
node *p;
p=q->next;
cout<<"学号:"<<p->num<<endl;
cout<<"姓名:"<<p->name<<endl;
cout<<"年龄:"<<p->age<<endl;
}
void list::add(node *h)
{
int b;
node *q,*he;
he=h;
node *p=new node;
cout<<"输入要插入的学生信息:\n";
cout<<"学号:";cin>>p->num;
cout<<"姓名:";cin>>p->name;
cout<<"年龄:";cin>>p->age;
cout<<"1.在表尾部插入\n";
cout<<"2.在表头部插入\n";
cout<<"3.在其他位置插入\n";
cout<<"请选择要进行的操作:";
cin>>b;
switch(b)
{
case 1:
he->prev->next=p;
p->prev=he->prev;
he->prev=p;
p->next=NULL;
length++;
cout<<"插入成功!"<<endl;
break;
case 2:
he->next->prev=p;
p->next=he->next;
he->next=p;
length++;
cout<<"插入成功!"<<endl;
break;
case 3:
cout<<"请查找需插入位置前面的学生:";
q=find(h);
q->next->prev=p;
p->next=q->next;
q->next=p;
p->prev=q;
length++;
cout<<"插入成功!"<<endl;
break;
default:cout<<"输入错误\n";
}
}
void list::del(node *h)
{
node *q,*p;
int m;
cout<<"请先查找需要删除的学生!\n";
q=find(h);
p=q->next;
cout<<"该学生信息:\n";
cout<<"学号:"<<p->num<<endl;
cout<<"姓名:"<<p->name<<endl;
cout<<"年龄:"<<p->age<<endl;
cout<<"确定要删除吗?确定请输入1";
cin>>m;
if(m)
{
if(q==h)
q->next=p->next;
else if(p->next)
{
q->next=p->next;
p->next->prev=q;
}
else
{
q->next=NULL;
h->prev=q;
}
}
delete (p);
length--;
}
void list::change(node *h)
{
node *q;
cout<<"请查找需要修改的人员\n";
q=find(h);
show_node(q);
cout<<"请输入修改后的信息:\n";
cout<<"学号:";cin>>q->next->num;
cout<<"姓名:";cin>>q->next->name;
cout<<"年龄:";cin>>q->next->age;
cout<<"修改成功!";
}
class set:public list
{
public:
set *operator-(list &);
set *operator+(list &);
set *and(list &);
};
set*set::operator-(list &g)
{
set *c=new set;
c->head=c->tail=NULL;
node *q=c->head;
node *f=this->head;
c->length=0;
node*d;
while(f->next)//for(int i=1;i<=this->length;i++)//
{
f=f->next;
int flag=0;
d=g.head;
int i=g.length;
while(d->next) //for(int j=1;j<=i;j++)
{
d=d->next;
if((f->num==d->num)&&(f->name==d->name)&&(f->age==d->age))
flag=1;
}
if(!flag) //f的第i个结点在w中没有相同结点时
{
node *p=new node;
p->num=f->num;
p->name=f->name;
p->age=f->age;
q->next=p;
q=q->next;
c->length++;
}
}
q->next=NULL;
c->tail=q;
q=c->head->next;
for(int i=1;i<=c->length;i++)
{
cout<<222;
}
return c;
}
int main()
{
cout<<"---------------学生信息管理系统---------------"<<endl;
int c,k=1;
node*h,*q,*hw;
list pre,w;
set *l,s;
h=pre.creat();
s.head=h;
cout<<"表的长度为:"<<pre.length<<endl;
while(k)
{
cout<<"\t\t\t\t1.查找信息\t\t\t\t\n";
cout<<"\t\t\t\t2.插入信息\t\t\t\t\n";
cout<<"\t\t\t\t3.删除信息\t\t\t\t\n";
cout<<"\t\t\t\t4.修改信息\t\t\t\t\n";
cout<<"\t\t\t\t5.队列输出全部信息\t\t\t\t\n";
cout<<"\t\t\t\t6.栈输出全部信息\t\t\t\t\n";
cout<<"\t\t\t\t7.显示表长\t\t\t\t\n";
cout<<"\t\t\t\t8.实现两链表差\t\t\t\t\n";
cout<<"\t\t\t\t0.退出\t\t\t\t\n";
cin>>c;
switch(c)
{
case 1:q=pre.find(h);pre.show_node(q);break;
case 2:pre.add(h);break;
case 3:pre.del(h);break;
case 5:pre.show(h);break;
case 4:pre.change(h);break;
case 6:pre.show_stack(h,pre.length);break;
case 7:cout<<"表的长度为:"<<pre.length<<endl;
case 8:hw=w.creat();l=s-w;break;
case 0:k=0;break;
default: cout<<"请输入有效选择!\n";
}
}
return 0;
}