#include <iostream>
#include <fstream>
#include <string>
#define NUM 10
using namespace std;
ofstream fsimtab("SimTale.txt",ios::out);
ofstream fdettab("DetTab.txt",ios::out);
ifstream fdata("xinxi.txt",ios::in);
class worker
//基类
{
public:
string num;
string name;
char sex;
int age;
virtual void display() {}
};
class sworker: public worker //派生类
{
public:
virtual void display();
};
class dworker: public worker
//派生类
{
public:
virtual void display();
};
int menu();
float add(dworker a[], int &n);
float produce(dworker det[], sworker sim[],int &n);
float printer(worker *pt, int &n);
void Read(dworker a[],int &n)
{
ifstream Re("date.txt",ios::in);
Re>>n;
for(int i=0;i<n;i++)
{
Re>>a[i].num;
Re>>a[i].name;
Re>>a[i].sex;
Re>>a[i].age;
}
Re.close();
}
void Write(dworker a[],int &n)
{
ofstream Re("date.txt",ios::out);
Re<<n<<endl;
for(int i=0;i<n;i++)
{
Re<<a[i].num<<endl;
Re<<a[i].name<<endl;
Re<<a[i].sex<<endl;
Re<<a[i].age<<endl;
}
Re.close();
}
int main()
{
sworker simtab[NUM];
dworker dettab[NUM];
int lastnum=0;
string input;
system("title 职工信息管理系统");
Read(dettab,lastnum);
produce(dettab, simtab,lastnum);
bool exit=false;
do{
switch(menu())
{
case 1:char ch;
system("cls");
do
{
if (add(dettab,lastnum))
{cout<<"\t\t\t增加成功,是否再添加(y/n)?";
cin>>ch;}
else
cout<<"\t\t\t可能数组已满,生成信息简表失败!";
}while(ch=='y');
break;
case 2: system("cls");
if (produce(dettab,simtab,lastnum))
{
cout<<"\t\t\t
------生成信息简表成功------"<<endl;
}
else
cout<<"生成信息简表失败!"<<endl;
cout<<"输入任意键返回主菜单:";
cin>>input;
//cin>>hold;
break;
case 3: system("cls");
cout<<"\t\t\t
**********************"<<endl;
cout<<"\t\t\t
-----显示原始记录-----"<<endl;
cout<<"\t\t\t
**********************"<<endl;
if (printer(dettab,lastnum))
printer(dettab,lastnum);
else
cout<<"输入任意返回主菜单!"<<endl;
//cin>>hold;
cin>>input;
break;
case 4: system("cls");
if (printer(simtab,lastnum))
cout<<"输入任意返回主菜单!"<<endl;
else
cout<<"输入任意返回主菜单!"<<endl;
cin>>input;
break;
case 5:
exit=true;
Write(dettab,lastnum);
break;
}
}while(exit==false);
cout<<"谢谢使用!"<<endl;
fsimtab.close();
fdettab.close();
fdata.close();
return 0;
}
char f,m;
int menu()
{
int a=5;
do{
system("cls");
cout<<"********************************************************************************"<<endl;
cout<<"\t\t\t--------职工信息管理系统--------"<<endl;
cout<<"********************************************************************************"<<endl;
cout<<"\t\t\t------------菜单选项------------"<<endl;
cout<<endl;
cout<<"\t\t***********************************************"<<endl;
cout<<"\t\t\t*\t1.增加职工记录""
""*"<<endl;
cout<<"\t\t\t*\t2.生成信息简表""
""*"<<endl;
cout<<"\t\t\t*\t3.显示原始记录""
""*"<<endl;
cout<<"\t\t\t*\t4.显示简表记录""
""*"<<endl;
cout<<"\t\t\t*\t5.结束程序运行""
""*"<<endl;
cout<<"\t\t***********************************************"<<endl;
cout<<"\t\t-----------------------------------------------"<<endl;
cout<<"\t\t***********************************************"<<endl;
cout<<"\t\t\t
请选择菜单(Enter):1-5:";
cin>>a;
} while(a<1||a>5);
return a;
}
void sworker::display ()
{cout<<"
姓名:"<<name <<"\t\t\t年龄:"<<age<<endl;
;
fsimtab<<"
姓名: "<<name <<"\t\t\t年龄:"<<age<<endl;
;
//cout<<"姓名:"<<name<<endl;
//cout<<"年龄:"<<age<<endl;
//fsimtab<<"姓名:"<<name<<endl;
//fsimtab<<"年龄:"<<age<<endl;
fsimtab<<"\t\t\t********************"<<endl;
}
void dworker::display ()
{
cout<<"\t\t\t
编号:"<<num<<endl;
cout<<"\t\t\t
姓名:"<<name<<endl;
cout<<"\t\t\t
性别:"<<sex<<endl;
cout<<"\t\t\t
年龄:"<<age<<endl;
fdettab<<"\t\t\t
编号:"<<num<<endl;
fdettab<<"\t\t\t
姓名:"<<name<<endl;
fdettab<<"\t\t\t
性别:"<<sex<<endl;
fdettab<<"\t\t\t
年龄:"<<age<<endl;
fdettab<<"*********************"<<endl;
}
float add(dworker a[],int &n)
{
if (n<10)
{
cout<<"\t\t\t您选择的是:1.增加职工记录"<<endl;
bool c;
do{
c=true;
cout<<"\t\t\t输入编号:";
cin>>a[n].num;
for(int k=0;k<n;k++)
{
if(a[n].num==a[k].num)
{
cout<<"\t\t\t输入编号重复!"<<endl;
c=false;}
}
}while(c==false);
bool d;
do{
d=true;
cout<<"\t\t\t输入姓名:";
cin>>a[n].name;
for(int k=0;k<n;k++)
{
if(a[n].name==a[k].name)
{
cout<<"\t\t\t输入姓名重复!"<<endl;
d=false;}
}
}while(d==false);
cout<<"\t\t\t请输入性别(g为女,m为男):";
cin>>a[n].sex;
while(a[n].sex!='g'&&a[n].sex!='m')
{cout<<"\t\t\t性别输入错误";
cout<<"\t\t\t请输入性别(g为女,m为男):";
cin>>a[n].sex;}
cout<<"\t\t\t请输入年龄:";
bool e;
do{
e=true;
cin>>a[n].age;
if(a[n].age<=16&&a[n].age>=50)
{cout<<"年龄小于16岁或大于50岁,请检查是否输入正确,重新输入:";
e=false;}
}
while(e==false);
//
while(cin>>a[n].age)
//
{
//
if(a[n].age >=16&&a[n].age<=50)
//
{
//
break;
//
}
//
cout <<"\t\t\t年龄小于16或大于50,不符合要求,请重新输入."<<endl;
//}
n++;
return 1;
}
else
return 0;
}
float produce(dworker det[], sworker sim[],int &n)
{
if(n<10)
{
for (int i=0;i<=n;i++)
{
sim[i].name=det[i].name;
sim[i].age =det[i].age;
}
return 1;
}
else return 0;
}
float printer(worker *pt, int &n)
{
for(int i=0;i<n;i++)
{
cout<<"********************************************************************************"<<endl;
(pt+i)->display();
}
cout<<"********************************************************************************"<<endl;
return 0;
}
能否修改一下,加上搜索和删除功能。谢谢!!!