程序代码:
#ifndef PERSON_H
#define PERSON_H
#include <iostream>
using namespace std;
#include <string.h>
#include <string>
#include <list>
#include <fstream>
class Birth
{
private:
int year;
int month;
int day;
public:
Birth():year(0),month(0),day(0){};
void setYear(int a){ year=a;}
void setMonth(int a){ month=a;}
void setDay(int a){ day=a;}
int getYear(){ return year;}
int getMonth(){ return month;}
int getDay(){ return day;}
};
class Person : public Birth
{
private:
string name;
string id;
string worknum;
string tel_num;
string sex;
string apartment;
string salary;
string post;
public:
int a;
string b;
Person():name(),id(),worknum(),tel_num(),sex(),apartment(),salary(),post(){};
void setName(string a){ name=a; }
void setId(string a){ id=a; }
void setWorknum(string a){ worknum=a; }
void setTel_num(string a){ tel_num=a; }
void setSex(string a){ sex=a;}
void setApartment(string a){ apartment=a; }
void setSalary(string a){ salary=a; }
void setPost(string a){ post=a; }
void setBirth();
string getName(){ return name;}
string getSex(){ return sex;}
string getTel_num(){ return tel_num;}
string getApartment(){ return apartment;}
string getPost(){ return post;}
string getId(){ return id;}
string getWorknum(){ return worknum;}
string getSalary(){ return salary;}
friend ostream &operator <<(ostream &,Person &);
void set();
};
void Person::setBirth()
{
cout<<"输入出生年份:1900~2012";
cin>>a;
cin.ignore();
cout<<endl;
while(a<1900||a>2012)
{
cout<<"超出范围,请重新输入出生年份:1900~2012:\t";
cin>>a;
cin.ignore();
cout<<endl;
}
setYear(a);
cout<<"输入出生月份:1~12";
cin>>a;
cin.ignore();
cout<<endl;
while(a<1||a>12)
{
cout<<"超出范围,请重新输入出生月份:1~12:\t";
cin>>a;
cin.ignore();
cout<<endl;
}
setMonth(a);
cout<<"输入出生天:1~31:\t";
cin>>a;
cin.ignore();
cout<<endl;
while(a<1||a>31)
{
cout<<"超出范围,请重新输入出生天:1~31";
cin>>a;
cin.ignore();
cout<<endl;
}
setDay(a);
}
ostream &operator <<(ostream &strm,Person &obj)
{
strm<<"姓名:"<<obj.getName()<<endl<<"性别:"<<obj.getSex()
<<endl<<"生日:"<<obj.getYear()<<"年"<<obj.getMonth()<<"月"<<obj.getDay()<<"日"<<endl<<"身份证号码:"<<obj.getId()<<endl
<<"部门:"<<obj.getApartment()<<endl<<"职务:"<<obj.getPost()<<endl
<<"薪水:"<<obj.getSalary()<<"/月"<<endl<<"电话号码:"<<obj.getTel_num()<<endl;
return strm;
}
void Person::set()
{
cout<<"请输入姓名:";
getline(cin,b);
setName(b);
cout<<endl;
cout<<"请输入性别:";
getline(cin,b);
setSex(b);
cout<<endl;
cout<<"请输入生日:";
setBirth();
cout<<endl;
cout<<"请输入身份证号码:";
getline(cin,b);
setId(b);
cout<<endl;
cout<<"请输入部门:";
getline(cin,b);
setApartment(b);
cout<<endl;
cout<<"请输入职务:";
getline(cin,b);
setPost(b);
cout<<endl;
cout<<"请输入工作号:";
getline(cin,b);
setWorknum(b);
cout<<endl;
cout<<"请输入薪水:";
getline(cin,b);
setSalary(b);
cout<<endl;
cout<<"请输入电话号码:";
getline(cin,b);
setTel_num(b);
cout<<endl;
}
#endif
#ifndef MANAMENU_H
#define MANAMENU_H
#include "Person.h"
list <Person> stu;
list <Person>::iterator iter;
list <Person>::iterator it;
Person s,q;
void open()
{ fstream file;
file.open("stu.dat",ios::in|ios::binary);
if(file.fail())
cout<<"打开文件失败!";
file.read((char *)&s,sizeof(Person));
while(!file.eof())
{
stu.clear();
stu.push_back(s);
cout<<s;
file.read((char *)&s,sizeof(Person));
}
cout<<"文件读取完毕!"<<endl;
system("cls");
file.close();
}
void save()
{
fstream file;
it=stu.begin();
file.open("stu.dat",ios::out|ios::binary);
if(file.fail())
{ cout<<"文件打开失败!"<<endl;}
while(it!=stu.end())
{
q=*it;
file.write((char *)&q,sizeof(Person));
cout<<q<<endl;
++it;
}
it=stu.begin();
cout<<"文件保存成功!感谢使用!"<<endl;
exit(getchar());
}
void Managemenu()
{
char choice;
void open();
void insert();
void delone();
void delall();
void revise();
void display();
void search();
void mainmenu();
cout<<" "<<endl
<<" 1.添加员工信息"<<endl
<<" 2.删除员工信息"<<endl
<<" 3.清空所有信息"<<endl
<<" 4.修改员工信息"<<endl
<<" 5.显示所有信息"<<endl
<<" 6.查找员工信息"<<endl
<<" 7.返回主菜单 "<<endl;
choice=getchar();
cin.ignore();
while(choice<'1'||choice>'7')
{
cout<<"选择无效,请重新选择:"<<endl;
cin>>choice;
cin.ignore();
}
switch(choice)
{
case '1':system("cls");insert();
case '2':system("cls");delone();
case '3':system("cls");delall();
case '4':system("cls");revise();
case '5':system("cls");display();
case '6':system("cls");search();
case '7':system("cls");mainmenu();
}
}
void mainmenu()
{
char choice;
cout<<" 1.进入管理菜单 "<<endl;
cout<<" 2.保存并退出程序 "<<endl;
choice=getchar();
cin.ignore();
system("cls");
switch(choice)
{
case '1':{ Managemenu();}
case '2':{ save();}
}
}
void insert()
{
string m;
s.set();
stu.push_back(s);
cout<<"是否继续添加员工?y表示'是',其他任意键表示'否'";
getline(cin,m);
while(m=="y")
{
s.set();
stu.push_back(s);
cout<<"是否继续添加员工?y表示'是',其他任意键表示'否'";
getline(cin,m);
}
system("cls");
Managemenu();
}
void delone()
{
string m;
iter=stu.begin();
cout<<"请输入要删除的员工姓名:";
getline(cin,m);
while(iter->getName()!=m&&iter!=stu.end())
{
iter++;
}
cout<<*iter;
cout<<"确认删除此员工信息?y表示'是',其他任意键表示'否'";
getline(cin,m);
if(m=="y")
{
stu.erase(iter);
cout<<"信息删除成功!";
}
cout<<"按任意键返回管理菜单:";
system("pause");
system("cls");
Managemenu();
}
void delall()
{
string m;
cout<<"确认删除所有员工信息?y表示'是',其他任意键表示'否";
getline(cin,m);
if(m=="y")
{
stu.clear();
cout<<"信息删除成功!";
}
cout<<"按任意键返回管理菜单:";
system("pause");
system("cls");
Managemenu();
}
void revise()
{
string m;
cout<<"请输入要修改的员工姓名:";
iter=stu.begin();
getline(cin,m);
while(iter->getName()!=m&&iter!=stu.end())
{
iter++;
}
cout<<*iter;
cout<<"确认修改此员工信息?y表示'是',其他任意键表示'否'";
getline(cin,m);
if(m=="y")
{
s.set();
stu.insert(iter,s);
stu.erase(iter);
cout<<"信息修改成功!";
}
cout<<"按任意键返回管理菜单:";
system("pause");
system("cls");
Managemenu();
}
void display()
{
iter=stu.begin();
while(iter!=stu.end())
{
cout<<*iter;
iter++;
}
cout<<"信息显示完毕!";
cout<<"按任意键返回管理菜单"<<endl;
system("pause");
system("cls");
Managemenu();
}
void search()
{
string m;
iter=stu.begin();
cout<<"输入要查找的员工姓名:";
getline(cin,m);
while(iter->getName()!=m&&iter!=stu.end())
{
iter++;
}
cout<<*iter;
cout<<"信息显示完毕!";
cout<<"按任意键返回管理菜单"<<endl;
system("pause");
system("cls");
Managemenu();
}
#endif
#include "Manamenu.h"
int main()
{
open();
mainmenu();
return 0;
}
这是代码。。求帮忙看下。。