这是一个基础的信息管理系统,有最基本的输入,删除,更改,查询功能。
#include <iostream>
#include <string>
using namespace std;
class Empolyee{
public:
int id;
string name;
string sex;
long long phonenumber;
string section;
int salary;
void AddEmpolyee();
void DeleteEmpolyee();
void UpdateEmpolyee();
void ReseachByName();
void ReseachByOffice();
void add();
void SaveInfo();
void Display();
} o[10];
void Empolyee::AddEmpolyee(){
int choice;
do{
cout << endl;
cout << "请输入员工编号" << endl;
cin >> id;
o[id].id=id;
cout << "请输入员工名字" << endl;
cin >> o[id].name;
cout << "请输入员工性别" << endl;
cin >> o[id].sex;
cout << "请输入员工电话" << endl;
cin >> o[id].phonenumber;
cout << "请输入员工科室" << endl;
cin >> o[id].section;
cout << "请输入员工薪水" << endl;
cin >> o[id].salary;
cout << "\n\n请确认以下信息:\n编号:00" << o[id].id << "\n名字:" << o[id].name << "\n性别:" << o[id].sex << "\n电话:" << o[id].phonenumber << "\n科室:" << o[id].section << "\n薪水:" << o[id].salary <<endl;
cout << "输入正确按1,输入有误按0" << endl;
cin >> choice;
}while (choice==0);
}
void Empolyee::DeleteEmpolyee(){
int choice,i,j;
int temp_id;
cout << endl;
cout << "输入你要删除的员工编号:" << endl;
cin >> temp_id;
for(j=1;temp_id!=o[j].id;j++)
{
;
}
cout << "确认要删除00" << temp_id << "号吗?确认输入1,不想删了输入0" << endl;
cin >> choice;
if(choice==1)
{
for(i=j;o[i].id!=(-1);i++)
{
o[i]=o[i+1];
}
}
}
void Empolyee::UpdateEmpolyee(){
int choice,i;
int temp_id;
cout << endl;
cout << "输入你要修改的员工编号:" ;
cin >> temp_id;
for(i=1;temp_id!=o[i].id;i++)
{
;
}
cout << " 1 修改名字\n 2 修改性别\n 3 修改科室\n 4 修改电话\n 5 修改薪水\n 输入编号操作:" ;
cin >> choice;
switch(choice)
{
case 1:{
cout << "输入更改后的名字:" ;
cin >> o[i].name;
break;
}
case 2:{
cout << "输入更改后的性别:" ;
cin >> o[i].sex;
break;
}
case 3:{
cout << "输入更改后的科室:" ;
cin >> o[i].section;
break;
}
case 4:{
cout << "输入更改后的电话:" ;
cin >> o[i].phonenumber;
break;
}
case 5:{
cout << "输入更改后的薪水:" ;
cin >> o[i].salary;
break;
}
}
cout << "修改成功!修改完的信息如下所示:\n";
cout << "编号:00" << o[i].id << "\n名字:" << o[i].name << "\n性别:" << o[i].sex << "\n电话:" << o[i].phonenumber << "\n科室:" << o[i].section << "\n薪水:" << o[i].salary << endl;
}
void Empolyee::ReseachByName(){
string sname;
int i;
cout << "输入你要查询的名字:";
cin >> sname;
for(i=1;sname!=o[i].name;i++)
;
cout << "你要找的是:" << o[i].name << endl;
cout << "编号:00" << o[i].id << "\n性别:" << o[i].sex << "\n电话:" << o[i].phonenumber << "\n科室:" << o[i].section << "\n薪水:" << o[i].salary << endl;
}
void Empolyee::ReseachByOffice(){
string ssection;
int i;
cout << "输入你要查询的科室:";
cin >> ssection;
cout << "该科室有如下员工:\n";
for(i=1;i<10;i++)
{
if(ssection==o[i].section)
cout << "编号:00" << o[i].id << " 名字:" << o[i].name << endl;
}
}
void Empolyee::add(){
string ssection;
int i,num,sum;
double ave;
num=0;
sum=0;
cout << "输入你要查询的科室:";
cin >> ssection;
cout << "该科室有如下员工:\n";
for(i=1;i<10;i++)
{
if(ssection==o[i].section)
{
num=num+1;
sum=sum+o[i].salary;
cout << "编号:00" << o[i].id << " 名字:" << o[i].name << " 薪水:" << o[i].salary << endl;}
}
ave=sum/num;
cout << "科室" << ssection << "的平均薪水为:" << ave;
}
void Empolyee::Display(){
for(int i=1;i<10;i++)
{
cout << "id:" << o[i].id << endl;
cout << "name:" << o[i].name << endl;
cout << "sex:" << o[i].sex << endl;
cout << "sl:" << o[i].salary << "\n\n\n" << endl;
}
}
int main()
{
for(int i=1;i<11;i++)
{
o[i].id=(-1);
}
int choice;
do{
cout << "******************公司员工信息管理系统******************" << endl;
cout << " 1 增加职工信息\n 2 删除职工信息\n 3 修改职工信息\n 4 按姓名查询职工信息\n 5 按科室查询职工信息\n 6 按科室统计员工平均工资\n 0 退出" << endl;
cout << "********************************************************" << endl;
cout << "输入0-6选项:" ;
cin >> choice;
switch(choice)
{
case 1:o[0].AddEmpolyee();break;
case 2:o[0].DeleteEmpolyee();break;
case 3:o[0].UpdateEmpolyee();break;
case 4:o[0].ReseachByName();break;
case 5:o[0].ReseachByOffice();break;
case 6:o[0].add();break;
case 0:cout << "ok" << endl;break;
case 7:o[0].Display();break;
default:
cout << "你输入的数据不合法" << endl;
}
cout << endl;
system("pause");
}while (choice!=0);
return 0;
}
这是功能1 增加职工信息
这是修改信息
这是按科室查询信息
其余基本功能不再赘述
我在该程序基础上新增了文件写入读取功能,不再关闭程序后数据清零。
以下是代码
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
template<typename type>
void diywrite(ofstream &stream,type &x)//写入文件
{
stream.write((char*)&x,sizeof(type));
}
void diywrite(ofstream& stream, string& str) //写入文件
{
int len = str.length();
stream.write((char*)&len, sizeof(int));
stream.write(str.c_str(), len);
}
ofstream fout("Empolyee.dat",ios::binary);
class Empolyee{
public:
static int p; //
int id;
string name;
string sex;
long long phonenumber;
string section;
int salary;
void AddEmpolyee();
void DeleteEmpolyee();
void UpdateEmpolyee();
void ReseachByName();
void ReseachByOffice();
void add();
void SaveInfo();
void Display();
friend void diysave(ofstream& stream, Empolyee &m);
} o[10];
void diysave(ofstream& stream, Empolyee &m) //保存文件
{
diywrite(stream, m.id);
diywrite(stream, m.name);
diywrite(stream, m.sex);
diywrite(stream, m.phonenumber);
diywrite(stream, m.section);
diywrite(stream, m.salary);
cout<<"****************"<<endl;
cout<<"***保存成功!***"<<endl;
cout<<"****************"<<endl;
}
int Empolyee::p=1;//初始化静态成员
void Empolyee::AddEmpolyee(){
int choice;
do{
cout << endl;
cout << "请输入员工编号" << endl;
cin >> id;
o[p].id=id;//类中静态成员p,以及本函数下面的 p++语句确保数组 o[] 按顺序被赋值
cout << "请输入员工名字" << endl;
cin >> o[p].name;
cout << "请输入员工性别" << endl;
cin >> o[p].sex;
cout << "请输入员工电话" << endl;
cin >> o[p].phonenumber;
cout << "请输入员工科室" << endl;
cin >> o[p].section;
cout << "请输入员工薪水" << endl;
cin >> o[p].salary;
cout << "\n\n请确认以下信息:\n编号:00" << o[p].id << "\n名字:" << o[p].name << "\n性别:" << o[p].sex << "\n电话:" << o[p].phonenumber << "\n科室:" << o[p].section << "\n薪水:" << o[p].salary <<endl;
cout << "输入正确按1,输入有误按0" << endl;
cin >> choice;
}while (choice==0);
void diysave(ofstream& stream, Empolyee &m);
diysave(fout,o[p]);//保存
p++;//使得数组o[]往后偏移一个位置
}
void Empolyee::DeleteEmpolyee(){
fout.close();//流fout传给文件
int choice,i,j;
int temp_id;
cout << endl;
cout << "输入你要删除的员工编号:" << endl;
cin >> temp_id;
for(j=1;temp_id!=o[j].id;j++)
{
;
}
cout << "确认要删除00" << temp_id << "号吗?确认输入1,不想删了输入0" << endl;
cin >> choice;
if(choice==1)
{
for(i=j;o[i].id!=(-1);i++)
{
o[i]=o[i+1];
}
p--; //删除一个,那增加时o[p] 要往前偏移
}
fout.open("Empolyee.dat",ios::binary);//打开文件并重置
for(i=1;o[i].id!=(-1);i++){
diysave(fout,o[i]);//重新保存文件
}
}
void Empolyee::UpdateEmpolyee(){
fout.close();//流fout传给文件
int choice,i;
int temp_id;
cout << endl;
cout << "输入你要修改的员工编号:" ;
cin >> temp_id;
for(i=1;temp_id!=o[i].id;i++)
{
;
}
cout << " 1 修改名字\n 2 修改性别\n 3 修改科室\n 4 修改电话\n 5 修改薪水\n 输入编号操作:" ;
cin >> choice;
switch(choice)
{
case 1:{
cout << "输入更改后的名字:" ;
cin >> o[i].name;
break;
}
case 2:{
cout << "输入更改后的性别:" ;
cin >> o[i].sex;
break;
}
case 3:{
cout << "输入更改后的科室:" ;
cin >> o[i].section;
break;
}
case 4:{
cout << "输入更改后的电话:" ;
cin >> o[i].phonenumber;
break;
}
case 5:{
cout << "输入更改后的薪水:" ;
cin >> o[i].salary;
break;
}
}
cout << "修改成功!修改完的信息如下所示:\n";
cout << "编号:00" << o[i].id << "\n名字:" << o[i].name << "\n性别:" << o[i].sex << "\n电话:" << o[i].phonenumber << "\n科室:" << o[i].section << "\n薪水:" << o[i].salary << endl;
fout.open("Empolyee.dat",ios::binary);//打开文件并重置
for(i=1;o[i].id!=(-1);i++){
diysave(fout,o[i]);//重新保存
}
}
void Empolyee::ReseachByName(){
string sname;
int i;
cout << "输入你要查询的名字:";
cin >> sname;
for(i=1;sname!=o[i].name;i++)
;
cout << "你要找的是:" << o[i].name << endl;
cout << "编号:00" << o[i].id << "\n性别:" << o[i].sex << "\n电话:" << o[i].phonenumber << "\n科室:" << o[i].section << "\n薪水:" << o[i].salary << endl;
}
void Empolyee::ReseachByOffice(){
string ssection;
int i;
cout << "输入你要查询的科室:";
cin >> ssection;
cout << "该科室有如下员工:\n";
for(i=1;i<10;i++)
{
if(ssection==o[i].section)
cout << "编号:00" << o[i].id << " 名字:" << o[i].name << endl;
}
}
void Empolyee::add(){
string ssection;
int i,num,sum;
double ave;
num=0;
sum=0;
cout << "输入你要查询的科室:";
cin >> ssection;
cout << "该科室有如下员工:\n";
for(i=1;i<10;i++)
{
if(ssection==o[i].section)
{
num=num+1;
sum=sum+o[i].salary;
cout << "编号:00" << o[i].id << " 名字:" << o[i].name << " 薪水:" << o[i].salary << endl;}
}
ave=sum/num;
cout << "科室" << ssection << "的平均薪水为:" << ave;
}
void Empolyee::Display(){
for(int i=1;i<10;i++)
{
cout << "id:" << o[i].id << endl;
cout << "name:" << o[i].name << endl;
cout << "sex:" << o[i].sex << endl;
cout << "sl:" << o[i].salary << "\n\n\n" << endl;
}
}
int main()
{
for(int i=1;i<10;i++)//
{
o[i].id=(-1);
}
int choice;
do{
cout << "******************公司员工信息管理系统******************" << endl;
cout << " 1 增加职工信息\n 2 删除职工信息\n 3 修改职工信息\n 4 按姓名查询职工信息\n 5 按科室查询职工信息\n 6 按科室统计员工平均工资\n 0 退出" << endl;
cout << "********************************************************" << endl;
cout << "输入0-6选项:" ;
cin >> choice;
switch(choice)
{
case 1:o[0].AddEmpolyee();break;
case 2:o[0].DeleteEmpolyee();break;
case 3:o[0].UpdateEmpolyee();break;
case 4:o[0].ReseachByName();break;
case 5:o[0].ReseachByOffice();break;
case 6:o[0].add();break;
case 0:cout << "ok" << endl;break;
case 7:o[0].Display();break;
default:
cout << "你输入的数据不合法" << endl;
}
cout << endl;
system("pause");
system("cls");//清屏
}while (choice!=0);
fout.close();// 流fout传给文件
return 0;
}
以下是功能演示:
在退出程序后,程序根目录生成文件 ,这就是储存数据的文件。用记事本打开显示
。