#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct Student
{
string Sno;
string Sname;
string Ssex;
string Sclass;
Student *next;
};
class linkList
{
public:
Student *head;
Student *last;
linkList()
{
head=NULL;
last=NULL;
}
void creat();
void insert();
void del();
void show();
void lookUp();
void update();
void save();
};
void linkList::creat()
{
head=new Student;
last=new Student;
last->next=NULL;
last->Sno="abcde";
head->Sno="0";
head->next=last;
ifstream infile ("",ios::in);
if(!infile) void;
else
{
while(!())
{
Student *p1;
p1=new Student;
p1->next=NULL;
infile>>p1->Sno;
infile>>p1->Sname;
infile>>p1->Ssex;
infile>>p1->Sclass;
Student *p;
p=head;
while(p1->Sno>p->next->Sno )
{
p=p->next;
}
p1->next=p->next;
p->next=p1;
}
();
}
}
void linkList::insert()
{
//ofstream outfile("");
//if(! outfile)
//{
// cerr<<"open error!!"<<endl;
// exit(1);
// }
Student *stu1;
stu1=new Student;
stu1->next=NULL;
cout<<"请输入学生的学号:"<<endl;
cin>> stu1->Sno;
//outfile<<stu1->Sno<<endl;
cout<<"请输入学生的姓名:"<<endl;
cin>> stu1->Sname;
//outfile<<stu1->Sname<<endl;
cout<<"请输入学生的性别:"<<endl;
cin>> stu1->Ssex;
//outfile<<stu1->Ssex<<endl;
cout<<"请输入学生的班级:"<<endl;
cin>> stu1->Sclass;
//outfile<<stu1->Sclass<<endl;
Student *p;
p=head;
while(stu1->Sno>p->next->Sno )
{
p=p->next;
}
stu1->next=p->next;
p->next=stu1;
// ();
}
void linkList::lookUp()
{
Student *p;
p=head;
string p1;
cout<<"按学号查找请输入 1 "<<endl;
cout<<"按姓名查找请输入 2 "<<endl;
cout<<"按班级查找请输入 3 "<<endl;
cout<<"取消操作请输入 4 "<<endl;
int a;
cin>>a;
if(a==1)
{
cout<<"请输入学号:"<<endl;
cin>>p1;
while(p->Sno!="abcde")
{
if(p1==p->Sno) break;
else p=p->next;
}
if(p->Sno=="abcde")
cout<<"无此学生!!"<<endl;
else cout<<p->Sno<<" "<<p->Sname<<" "<<p->Ssex<<" "<<p->Sclass<<endl;
}
if(a==2)
{
cout<<"请输入姓名:"<<endl;
cin>>p1;
int count=0;
while(p->Sno!="abcde")
{
if(p->Sname==p1)
{
cout<<p->Sno<<" "<<p->Sname<<" "<<p->Ssex<<" "<<p->Sclass<<endl;
count++;
}
p=p->next;
}
if(count==0) cout<<"无此人"<<endl;
}
if(a==3)
{
cout<<"请输入班级:"<<endl;
cin>>p1;
int count=0;
while(p->Sno!="abcde")
{
if(p->Sclass==p1)
{
cout<<p->Sno<<" "<<p->Sname<<" "<<p->Ssex<<" "<<p->Sclass<<endl;
count++;
}
p=p->next;
}
if(count==0) cout<<"无此人"<<endl;
}
}
void linkList::del()
{
string p1;
cout<<"请输入您要删除学生的学号:";
cin>>p1;
Student *p;
p=head;
while(p->next!=NULL)
{
p=p->next;
if(p->next->Sno==p1)
{
int a=0;
cout<<p->next->Sno<<" "<<p->next->Sname<<" "<<p->next->Ssex<<" "<<p->next->Sclass<<endl;
cout<<"您真的要删除这个学生吗?确认请输入数字1,取消操作请输入0"<<endl;
cin>>a;
if(a==1)
{
Student *q;
q=p->next;
p->next=p->next->next;
delete q;
break;
}
if(a==0) break;
}
if(p->next==NULL && p->Sno!=p1)
cout<<"无此学生!!"<<endl;
}
}
void linkList::update()
{
int count = 0;
string p1;
Student *p;
p=head;
cout<<"请输入您要修改学生的学号:"<<endl;
cin>>p1;
while(p->next!=NULL)
{
if(p->Sno==p1)
{
count++;
int a;
cout<<p->Sno<<" "<<p->Sname<<" "<<p->Ssex<<" "<<p->Sclass<<endl;
cout<<"修改学号请入1"<<endl;
cout<<"修改姓名请输入2"<<endl;
cout<<"修改性别请输3"<<endl;
cout<<"修改班级请输入4"<<endl;
cout<<"取消操作请输入0"<<endl;
cin>>a;
if(a==1)
{
string b;
cout<<"请输入新的学号:";
cin>>b;
p->Sno=b;
}
else if(a==2)
{
string b;
cout<<"请输入新的姓名:";
cin>>b;
p->Sname=b;
}
else if(a==3)
{
string b;
cout<<"请输入新的性别:";
cin>>b;
p->Ssex=b;
}
else if(a==4)
{
string b;
cout<<"请输入新的班级:";
cin>>b;
p->Sclass=b;
}
else cout<<"输入错误!!"<<endl;
break;
}
p=p->next;
}
if(count==0) cout<<"无此数"<<endl;
}
void linkList::show()
{
Student *p;
p=head;
int count=0;
while(p->next!=last)
{
cout<<p->next->Sno<<" "<<p->next->Sname<<" "<<p->next->Ssex<<" "<<p->next->Sclass<<endl;
p=p->next;
count++;
}
if(count==1) cout<<"无数据"<<endl;
}
void linkList::save()
{
cout<<"正在存储数据…………"<<endl;
cout<<"请不要关闭窗口"<<endl;
Student *p;
p=head;
ofstream outfile("",ios::out);
if(! outfile)
{
cerr<<"打开文件失败不能存储文件!!"<<endl;
exit(1);
}
while(p->next!=last)
{
outfile<<p->next->Sno<<" ";
outfile<<p->next->Sname<<" ";
outfile<<p->next->Ssex<<" ";
outfile<<p->next->Sclass<<" ";
p=p->next;
}
();
}
int main()
{
linkList L;
();
int t1=0;
while(t1==0)
{
int t2;
cout<<"新建学生信息请输入 1 ;"<<endl;
cout<<"修改学生信息请输入 2 ;"<<endl;
cout<<"查找学生信息请输入 3 ;"<<endl;
cout<<"删除学生信息请输入 4 ;"<<endl;
cout<<"查看所有学生信息请输入 5 ;"<<endl;
cout<<"退出出请输入 6 ;"<<endl;
cin>>t2;
if(t2==1)
{
while(t2==1)
{
();
cout<<"返回主菜单请输入 0 ;"<<endl;
cout<<"继续操作请输入 1;"<<endl;
cin>>t2;
if(t2==0) t1=0;
}
}
else if(t2==2)
{
while(t2==2)
{
();
cout<<"返回主菜单请输入 0 ;"<<endl;
cout<<"继续操作请输入 1;"<<endl;
cin>>t2;
if(t2==0) t1=0;
}
}
else if(t2==3)
{
while(t2==3)
{
();
cout<<"返回主菜单请输入 0 ;"<<endl;
cout<<"继续操作请输入 1;"<<endl;
cin>>t2;
}
}
else if(t2==4)
{
while(t2==4)
{
();
cout<<"返回主菜单请输入 0 ;"<<endl;
cout<<"退出请输入 1;"<<endl;
cin>>t2;
if(t2==0) t1=0;
}
}
else if(t2==5)
{
while(t2==5)
{
();
cout<<"返回主菜单请输入 0 ;"<<endl;
cout<<"继续操作请输入 1;"<<endl;
cin>>t2;
if(t2==0) t1=0;
}
}
else if(t2==6)
{
break;
}
else
cout<<"输入错误!!"<<endl;
}
();
cout<<"存储完成"<<endl;
return 0;
}