用c++写的一个学生信息管理程序

#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 ("stu.dat",ios::in);

if(!infile) void;

else

{

while(!infile.eof())

{   

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;

 

}

infile.close ();

 

 

}

}

void linkList::insert()

{   

//ofstream outfile("stu.dat");

//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;

 

 

// outfile.close();

}

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("stu.dat",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;

 

}

outfile.close();

}

 

int main()

{

 

linkList L;

L.creat();

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)

{

L.insert();

cout<<"返回主菜单请输入 0 ;"<<endl;

cout<<"继续操作请输入 1;"<<endl;

cin>>t2;

if(t2==0) t1=0;

}

}

else if(t2==2)

{

while(t2==2)

{

L.update();

cout<<"返回主菜单请输入 0 ;"<<endl;

cout<<"继续操作请输入 1;"<<endl;

cin>>t2;

if(t2==0) t1=0;

}

}

else if(t2==3)

{

while(t2==3)

{

 

L.lookUp();

cout<<"返回主菜单请输入 0 ;"<<endl;

cout<<"继续操作请输入 1;"<<endl;

cin>>t2;

}

}

else if(t2==4)

{

while(t2==4)

{

 

L.del();

cout<<"返回主菜单请输入 0 ;"<<endl;

cout<<"退出请输入 1;"<<endl;

cin>>t2;

if(t2==0) t1=0;

 

}

}

else if(t2==5)

{

while(t2==5)

{

 

L.show();

cout<<"返回主菜单请输入 0 ;"<<endl;

cout<<"继续操作请输入 1;"<<endl;

cin>>t2;

if(t2==0) t1=0;

}

}

else if(t2==6)

{

break;

}

else

cout<<"输入错误!!"<<endl;

 

 

 

}

L.save();

cout<<"存储完成"<<endl;

return 0;

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值