求助C/C++语言设计

功能要求:工资项目有:部门、职工号、姓名、基本工资、职务补贴、工龄工资、水电费、实发工资等。设计一个简单的工资管理系统,要求有简单的界面,程序段要分清模块,有必要的说明。输入各部门职工的名单及各项工资,统计应发工资,按部门排序规范格式显示工资列表,在指定位置插入职工工资,也可以修改、删除指定职工的工资。(用文件完成)

# include <iostream>
# include <fstream>
# include <string>
# include <iomanip>
# include <stdlib.h>
using namespace std;

struct worker_inf
{
int month; //月份
int code; //工人编号
string name; //姓名
float get[4]; //基本工资,津贴,房帖,交通补贴
float pay[4]; //房租,储蓄,交通费,会费
float tax; //个人所得税
float theory_num; //应发书
float reduce_num; //应扣数
float practice_num; //实发数
worker_inf *next;
};
/////////////////////////////////////////////////////////////////
class worker //定义职工类
{
private:
worker_inf *head;
void print(worker_inf *); //输出一条指定职工的工资记录,并返回该记录的指针
worker_inf *find(int); //查找条例条件的记录,并返回该记录的指针

public:
worker(){head=NULL;}
worker_inf *get_head(){return head;}
int listcount(); //统计当前链表的记录总数,并返回一个整数
void additem(int month,int code,string name,float get[4],float pay[4]); //添加一条工资记录表尾
void removeitem(int); //删除一条指定职工的工资记录
int menu(); //修改某职工工资的菜单
void changemonth(); //修改月份
void changeitem(int); //修改职工的工资信息
void list(); //输出当月全体职工的工资信息
void search(int); //输出指定编号职工的工资信息
float tax_num(); //计算职工个人所得税
float theorynumber(); //计算应发工资
float reducenumber(); //计算应扣工资
float practicenumber(); //计算实发工资
};
//////////////////////////////////////////////////////////////////
int worker::listcount() //统计当前链表数,并返回一个整数
{

if(!head)return 0;
worker_inf *p=head;
int n=0;
while(p)
{n++;p=p->next;}
return n;
}
//////////////////////////////////////////////////////////////////
void worker::additem(int month,int code,string name,float get[4],float pay[4]) //添加一条工资记录到表尾
{
if(!head)
{
head=new worker_inf;
for(int i=0;i<4;i++)
{
head->get[i]=get[i];
head->pay[i]=pay[i];
}
head->code=code;
head->month=month;
head->name=name;
head->next=NULL;
return;
}
worker_inf *t=head;
while(t && t->code!=code)
t=t->next;
if(t)
{
cout<<"操作失败:编号为"<<code<<"的记录已经存在!"<<endl;
return;
}
worker_inf *p=head;
while(p->next)p=p->next;
worker_inf *p1=new worker_inf;
p1->code=code;
for(int i=0;i<4;i++)
{
p1->get[i]=get[i];
p1->pay[i]=pay[i];
}
p1->code=code;
p1->month=month;
p1->name=name;
p1->next=NULL;
p->next=p1;
return;
}
////////////////////////////////////////////////////////////////////
void worker::removeitem(int code) //删除一条指定职工的工资记录
{
worker_inf *t=find(code);
if(!t)return;
worker_inf *p=head;//如果要删除的记录位于表头
if(head==t)
{
head=head->next;
delete p;
cout<<"成功删除编号为"<<code<<"的记录!"<<endl<<endl;
return;
}
while(p->next!=t)p=p->next;
worker_inf *p1=p->next;
p->next=p1->next;
delete p1;
cout<<"成功删除编号为"<<code<<"的记录!"<<endl<<endl;
return;
}
////////////////////////////////////////////////////////////////
int worker::menu() //修改某一职工信息的菜单
{
int select=-1;
cout<<"\t\t\t\t\t\t**************修改菜单**************"<<endl<<endl;
cout<<"1.基本工资"<<endl<<endl;
cout<<"2.津贴"<<endl<<endl;
cout<<"3.房帖"<<endl<<endl;
cout<<"4.交通补贴"<<endl<<endl;
cout<<"5.房租"<<endl<<endl;
cout<<"6.储蓄"<<endl<<endl;
cout<<"7.交通费"<<endl<<endl;
cout<<"8.会费"<<endl<<endl;
cout<<"0.退出修改系统"<<endl<<endl;
cout<<"[请选择(输入相应数字)]:";
cin>>select;
if(select<0||select>9)
{
cout<<"对不起您输入错误!请重新输入【0-9】:"<<endl;
cin>>select;
}
return select;
}
/////////////////////////////////////////////////////////////////
int menu();
void worker::changeitem(int code) //修改某职工部分工资信息
{
worker_inf *p=find(code);
if(!p){cout<<"不存在职工编号为"<<code<<"的职工工资信息"<<endl;return;}
int select;
while(1)
{
float m;
select=menu();
if(select==0){system("cls");break;}
cout<<"请输入修改后的值";
cin>>m;
int n;
if(select<=4){
n=select-1;
p->get[n]=m;}
else{
n=select-5;
p->pay[n]=m;}
tax_num();
theorynumber();
reducenumber();
practicenumber();
cout<<"修改成功"<<endl;
}
}
////////////////////////////////////////////////////////////////////
void worker::changemonth() //修改月份
{
worker_inf *p=head;
while(p)
{
if(p->month==12)p->month=1;
else
p->month++;
p=p->next;
}
}
//////////////////////////////////////////////////////////////////////
void worker::print(worker_inf *p)//输出worker_inf制定的记录
{
cout.precision(0);
cout<<p->month<<" ";
cout<<p->code<<" ";
cout<<p->name<<"\t";
for(int i=0;i<4;i++)
{cout<<setiosflags(ios::fixed)<<p->get[i]<<"\t";}
for(int j=0;j<4;j++)
{cout<<p->pay[j]<<"\t";}
cout<<p->tax<<"\t";
cout<<p->theory_num<<"\t";
cout<<p->reduce_num<<"\t";
cout<<p->practice_num<<endl<<endl;
return;
}

///////////////////////////////////////////////////////////////////////
void worker::list() //列出当前链表中的所有记录
{
if(listcount==0)
{
cout<<"错误:当前的列表为空!"<<endl;
return;
}
worker_inf *p=head;
cout<<"共有记录:"<<listcount()<<endl;
cout<<"月份\t编号\t姓名\t基本工资\t津贴\t房帖\t交通补贴\t房租\t储蓄\t交通费\t会费\t个人所得税\t应发工资\t应扣工资\t实际工资"<<endl;
while(p)
{
print(p);
p=p->next;
}
cout<<endl;
return;
}
/////////////////////////////////////////////////////////////////////////
void worker::search(int code) //在当前链表查找指定记录并输出
{
cout<<"searching....."<<endl;
worker_inf *p=find(code);
if(p)
{
cout<<"月份\t编号\t姓名\t基本工资\t津贴\t房帖\t交通补贴\t房租\t储蓄\t交通费\t会费\t个人所得税\t应发工资\t应扣工资\t实际工资"<<endl;
print(p);
}
cout<<endl;
}
//////////////////////////////////////////////////////////////////////////
worker_inf *worker::find(int code) //查找条例条件的记录,并返回该指针
{
if(listcount==0)
{
cout<<"错误:当前列表为空!"<<endl;
return NULL;
}
worker_inf *p=head;
while(p)
{
if(p->code==code)break;
p=p->next;
}
if(!p)
{
cout<<"错误:找不到该记录!\n";
return NULL;
}
return p;
}
//////////////////////////////////////////////////////////////////////////
float worker::theorynumber() //计算应发数
{
int i;
if(listcount()==0)
{
cout<<"错误:当前的列表为空!"<<endl;
return -1;
}
float sum;
worker_inf *p=head;
while(p)
{
sum=0;
for(i=0;i<4;i++)
sum+=p->get[i];
p->theory_num=sum;
p=p->next;
}
return 0;
}
//////////////////////////////////////////////////////////////////
float worker::tax_num() //计算个人所得税
{
if(listcount==0)
{
cout<<"错误:当前的列表为空!"<<endl;
return -1;
}
worker_inf *p=head;
while(p)
{
float s;
s=p->theory_num;
if(s<=800)
p->theory_num=0;
else if(s<=2000) p->theory_num=(s-800)*0.05;
else if(s<=5000)
p->theory_num=(s-2000)*0.1+60;
else p->theory_num=(s-5000)*0.2+360;
p=p->next;
}
return 0;
}
///////////////////////////////////////////////////////////////////////
float worker::reducenumber() //计算应扣数
{
int i;
if(listcount==0)
{
cout<<"错误:当前的列表为空!"<<endl;
}
float sum;
worker_inf *p=head;
while(p)
{
sum=0;
for(i=0;i<4;i++)
sum+=p->pay[i];
p->reduce_num=p->tax+sum;
p=p->next;
}
return 0;
}
/////////////////////////////////////////////////////////////////////////
float worker::practicenumber() //计算实发数
{
if(listcount()==0)
{
cout<<"错误:当前的列表为空!"<<endl;
return -1;
}
worker_inf *p=head;
while(p)
{
float a,b;
a=p->theory_num;
b=p->reduce_num;
p->practice_num=a-b;
p=p->next;
}
return 0;
}
////////////////////////////////////////////////////////////////////////////
worker worker; //定义全局变量
int menu()
{
int select=-1;
cout<<"*****************主菜单***********************"<<endl<<endl;
cout<<"1.添加职工信息;"<<endl<<endl;
cout<<"2.删除职工信息;"<<endl<<endl;
cout<<"3.修改职工的工资信息;"<<endl<<endl;
cout<<"4.按职工编号查找记录;"<<endl<<endl;
cout<<"5.列出所有记录;"<<endl<<endl;
cout<<"6.从数据文件导入当月工资信息;"<<endl<<endl;
cout<<"7.将当月工资信息导出到磁盘文件;"<<endl<<endl;
cout<<"0.安全退出系统;"<<endl<<endl;
cout<<"[请选择(输入相应的数字)]:";
cin>>select;
return select;
}
/////////////////////////////////////////////////////////////////////////////
char exit()
{
char s;
cout<<"确定要退出程序吗?[Y/N]:";
cin>>s;
return s;
}
//////////////////////////////////////////////////////////////////////////////
void input(int *month,int*code,string *name,float get[4],float pay[4]) //输入职工信息
{
cout<<"请输入月份 编号 姓名 基本工资 津贴 房帖 交通补贴 房租 储蓄 交通费 会费:"<<endl;
cin>>*month;
cin>>*code;
if(*code==-1)return;
cin>>*name>>get[0]>>get[1]>>get[2]>>get[3]>>pay[0]>>pay[1]>>pay[2]>>pay[3];
return;
}
///////////////////////////////////////////////////////////////////////////////
void addnew() //增加记录
{
int month=0,code=0;float get[4]={0},pay[4]={0};
string name="";
cout<<endl<<"当输入的职工编号为-1时表示输入结束。"<<endl;
input(&month,&code,&name,get,pay);
while(code!=-1)
{
worker.additem(month,code,name,get,pay);
worker.tax_num();
worker.theorynumber();
worker.reducenumber();
worker.practicenumber();
input(&month,&code,&name,get,pay);
}
return;
}
////////////////////////////////////////////////////////////////////////////////
void dofind() //按职工编号查找
{
int code;
cout<<endl<<"当输入的编号为-1时表示结束输入."<<endl;
do
{
cout<<"请输入要查找的职工的编号:";
cin>>code;
if(code==-1)continue;
worker.search(code);
}while(code!=-1);
return;
}
/////////////////////////////////////////////////////////////////////////////////
void dodelete() //删除职工信息
{
cout<<endl<<"当输入的编号为-1时表示输入结束."<<endl;
int code;
do
{
cout<<"请输入要删除的职工的编号:";
cin>>code;
if(code==-1)continue;
worker.removeitem(code);
}while(code!=-1);
return;
}
///////////////////////////////////////////////////////////////////////////////////
void domodify() //修改职工信息
{
int code;
cout<<"当输入职工编号为-1时表示结束修改"<<endl;
while(1){
cout<<"请输入所需修改职工编号";
cin>>code;
if(code==-1)return;
else
worker.changeitem(code);
}
return;
}
///////////////////////////////////////////////////////////////////////////////////
void SaveFilethism()//将当月工资信息写入文件
{
worker_inf *p;
char name[20];
fstream iofile;
int i=0;
iofile.open("Worker_5th.dat",ios::out|ios::binary);
if(!iofile)
{
cerr<<"open error!"<<endl;
abort();
}
p=worker.get_head();
while(p)
{
p->name.copy(name,20,0);
name[p->name.length()]=0;
iofile.write((char *) &p->code,sizeof(int));
iofile.write((char *) &p->month,sizeof(int));
iofile.write((char *) name,20);
for(int i=0;i<4;i++)
{
iofile.write((char *) &p->get[i],sizeof(float));
}
for(int j=0;j<4;j++)
{
iofile.write((char *) &p->pay[j],sizeof(float));
}
p=p->next;
}
iofile.close();
cout<<"成功将工资信息存入文件"<<endl;
}
////////////////////////////////////////////////////////////////////////
void Loadfilethism() //读取当月全体职工的工资信息文件
{
int month,code;
char name[20]="";
float get[4],pay[4];
fstream iofile;
int i=0;
iofile.open("Worker_5th.dat",ios::in|ios::binary);
if(!iofile)
{
cout<<"数据文件不存在,请先建立该文件"<<endl;
return;
}
if(iofile.eof())
{
cout<<"数据库为空,请先添加数据"<<endl;
iofile.close();
}
else
{
while(iofile.peek()!=EOF)//peek()是取文件当前指针,EOF是文件尾标符
{
iofile.read((char *) &code,sizeof(int));
iofile.read((char *) &month,sizeof(int));
iofile.read((char *) name,20);
for(int i=0;i<4;i++)
{
iofile.read((char *) &get[i],sizeof(float));
} for(int j=0;j<4;j++)
{
iofile.read((char *) &pay[j],sizeof(float));
}
worker.additem(code,month,name,get,pay);
}
worker.tax_num();
worker.theorynumber();
worker.reducenumber();
worker.practicenumber();
iofile.close();
cout<<"成功导入工资信息"<<endl;
}
}
/////////////////////////////////////////////////////////////////////////
void list()
{
worker.list();
}
/////////////////////////////////////////////////////////////////////////
int main()
{
cout<<"******************欢迎进入职工工资管理系统*******************"<<endl<<endl;
int select;
char s;
while(1)
{
select=menu();
switch(select)
{
case 0: //退出程序
s=exit();
if(s=='y'||s=='Y') return 0;
break;
case 1: //增加新记录
addnew();
break;
case 2: //删除记录
dodelete();
break;
case 3: //修改记录
domodify();
break;
case 4: //按条件查找
dofind();
break;
case 5: //列出全部记录
list();
break;
case 6: //导入当月职工记录
Loadfilethism();
break;
case 7: //将职工记录存入磁盘
SaveFilethism();
break;
default:
cout<<"此输入无效!"<<endl;
}
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考
相似回答