资源描述
#include <iomanip>
#include <iostream>
#include <fstream>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define NULL 0
#define LEN sizeof(struct student)
int const N=20;
void Menu();
void Pass();
int n=0; //定义一个全局变量统计职员人数
//——--------->定义一个职员信息结构体
struct student
{
char name[N]; //用来存放姓名
char sex[N]; //用来存放性别
long id; //用来存放编号
float paid[3]; //用来存放工资
int total; //用来存放总工资
struct student *next;
};
//-------------->职员类
class Information
{
public:
Information() ; //结构函数.
~Information() ; //析构函数.
student *creat(); //建立链表
void output(student *head); //显示职员信息
int count(student *head); //定义函数count()统计职员总数
student *insert(student*head); //指针函数*insert()用来添加职员信息.
student *cancel(student *head,long id); //指针函数*cancel()用来删除职员信息.
student *find(student *head,long id); //指针函数*find()用来查找职员信息.
student *modify(student *head,long id); //指针函数*modife()用来修改职员信息.
void paixu(student *head); //定义paixu()函数将职员总额从大到小排列并输出
void average(student *head); //定义职职员资平均值函数
void save(student *head); //保留文件信息
student *Read(); //读取文件信息
private:
student *p1,*p2,*p3,*head,st;
};
Information::Information()
{cout<<" ******************************************************************************\n";
cout<<" ------------------------<<欢迎您使用职员工资管理系统>>------------------------\n";
cout<<" ******************************************************************************\n\n";
}
Information::~Information()
{ cout<<" ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤\n";
cout<<" \n";
cout<<" 本系统管理员 \n";
cout<<" \n";
cout<<" \n";
cout<<" ------------------------<<谢谢您使用职员工资管理系统>>------------------------\n";
cout<<" \n";
cout<<" \n";
cout<<" 欢迎下次使用 \n";
cout<<" \n";
cout<<" \n";
cout<<" 再见 \n";
cout<<" \n";
cout<<" ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤\n";
}
//------------>建立链表信息
student *Information::creat(void)
{//定义一个指向struct student结构体指针函数*creat()用来录入职员信息.
char ch[N];n=0; //用来存放职员姓名
p1=p2=(student *)malloc(LEN);//调用malloc()函数用来开辟一个新存放单元
cout<<" -------------<<请建立职员信息表,在姓名处键以 # 结束输入!>>--------------"<<endl;
cout<<" 姓名:";
cin>>ch;
head=NULL; //给指针head赋初值
while (strcmp(ch,"#")!=0)
{ //调用字符比较函数strcmp()用来判定是否继续输入
p1=(student *)malloc(LEN); //调用malloc()函数用来开辟一个新存放单元
strcpy(p1->name,ch); //将循环结构前面输入姓名复制到结构体名为p1数组name中
cout<<" 性别:";
cin>>p1->sex;
cout<<" 编号:";
cin>>p1->id;
while((p1->id)<0||(p1->id)>100000) //判定输入编号是否有效(100000个)
{
cout<<" 对不起您输入错误!请重新输入(>0<1000000): ";
cin>>p1->id;
}
cout<<" 基础工资:";
cin>>p1->paid[0];
while((p1->paid[0])<0||(p1->paid[0])>100000) //判定输入分数是否有效(>=0 <=100000)
{
cout<<" 对不起您输入错误!请重新输入(>0<100000): ";
cin>>p1->paid[0];
}
cout<<" 加班工资:";
cin>>p1->paid[1];
while((p1->paid[1])<0||(p1->paid[1])>100000) //判定输入分数是否有效(>=0 <=100000)
{
cout<<" 对不起您输入错误!请重新输入(>0<100000): ";
cin>>p1->paid[1];
}
cout<<" 其它奖金:";
cin>>p1->paid[2];
while((p1->paid[2])<0||(p1->paid[2])>100000) //判定输入分数是否有效(>=0 <=100000)
{
cout<<" 对不起您输入错误!请重新输入(>0<100000): ";
cin>>p1->paid[2];
}
p1->total=p1->paid[0]+p1->paid[1]+p1->paid[2]; //计算总额
if(n==0)head=p1; //假如是输入第一组职员信息就将指针p1赋给指针head
else p2->next=p1; //不然将p1赋给p2所指结构体next指针
p2=p1; //将指针p1赋给指针p2
n++; //将职员人数n值加1
cout<<"\n 姓名:";
cin>>ch; //将输入姓名存放到字符数组ch中
}
p2->next=NULL; //将p2所指结构体next指针重新赋空值
return (head);//将输入第一组职员信息返回
}
//--------------->定义output()函数将职员信息从头指针所指内容开始输出
void Information::output(student *head)
{
system("cls");
if(head==NULL) cout<<" 这是一个空表,请先输入职员信息!\n";
else{
cout<<"-------------------------------------------------------------------------------\n";
cout<<" *职职员资信息表*\n";
cout<<"-------------------------------------------------------------------------------\n";
cout<<"|编 号| |姓 名| |性别| |基础工资| |加班工资| |其它奖金| |总额|\n";
cout<<"-------------------------------------------------------------------------------\n";
p1=head; //将头指针赋给p
do
{
cout<<setw(6)<<p1->id
<<setw(10)<<p1->name
<<setw(10)<<p1->sex
<<setw(10)<<p1->paid[0]
<<setw(10)<<p1->paid[1]
<<setw(12)<<p1->paid[2]
<<setw(12)<<p1->total<<endl;
cout<<"-------------------------------------------------------------------------------\n";
p1=p1->next; //将下一组职员信息next指针赋给p
}while(p1!=NULL); //若指针p非空则继续,目标是把全部职员信息全部传给指针p然后输出.
}
}
//------------>统计职员人数函数
int Information::count(struct student *head) //定义函数count()统计职员总数
{
if(head==NULL)return(0); //若指针head为空返回值为0
else return(1+count(head->next)); //函数递归调用
}
//----------->添加职员成绩函数
student *Information::insert( student *head)
//插入新结点定义一个指向struct student结构体指针函数*insert()用来添加职员信息.
{
system("cls");
cout<<"\t----------------<<请输入新增职员信息>>----------------\n"<<endl;
p1=(student *)malloc(LEN); //使p1指向插入新结点
cout<<" 编号:";
cin>>p1->id;
while((p1->id)<0||(p1->id)>100000)
{
cout<<" 对不起您输入错误!请重新输入(>0<100000): ";
cin>>p1->id; //将输入编号存放到p1所指结构体数组id中
}
cout<<" 姓名:";
cin>>p1->name; //将输入姓名存放到结构体名为p1数组name中
cout<<" 性别:";
cin>>p1->sex;
cout<<" 基础工资:";
cin>>p1->paid[0];
while((p1->paid[0])<0||(p1->paid[0])>100000)
{
cout<<" 对不起您输入错误!请重新输入(>0<100000): ";
cin>>p1->paid[0]; //将输入基础工资存放到p1所指结构体数组paid中
}
cout<<" 加班工资:";
cin>>p1->paid[1];
while((p1->paid[1])<0||(p1->paid[1])>100000)
{
cout<<" 对不起您输入错误!请重新输入(>0<100000): ";
cin>>p1->paid[1]; //将输入加班工资存放到p1所指结构体数组paid中
}
cout<<" 其它奖金:";
cin>>p1->paid[2];
while((p1->paid[2])<0||(p1->paid[2])>100000)
{
cout<<" 对不起您输入错误!请重新输入(>0<100000): ";
cin>>p1->paid[2]; //将输入其它奖金存放到p1所指结构体数组paid中
}
p1->total=p1->paid[0]+p1->paid[1]+p1->paid[2];//计算总分
p2=head; //将头指针赋给p2
if(head==NULL) //若没调用次函数以前头指针head为空
{
head=p1;p1->next=NULL;
} //则将p1赋给头指针head并将p1所指结构体组员指针next赋空值
else
{
while((p1->id>p2->id)&&(p2->next!=NULL))
{
p3=p2; //p3指向原p2指向结点
p2=p2->next;
} //p2后移一个结点
if(p1->id<=p2->id)
{
if(head==p2)
{
p1->next=head;
head=p1;
} //插入到第一个结点之前
else
{
p3->next=p1;
p1->next=p2;
} //插入到p3所指结点以后
}
else
{
p2->next=p1;
p1->next=NULL;
} //插入到尾结点以后
}
n++; //将职员人数加1
cout<<"\t----------------<<你输入职员信息已经成功插入>>----------------"<<endl;
return (head);
}
//------------>删除职员信息
student *Information::cancel(student *head,long id) //定义一个指向struct student结构体指针函数*delete()用来删除考生信息.
{
system("cls");
if(head==NULL) //若调用次函数以前头指针head为空
{
return(head);
}
else
{
p1=head; //不然将头指针赋给p1
while(id!=p1->id&&p1->next!=NULL) //寻求要删除结点当p1所指职员编号不是输入职员编号而且p1所指next指针不为空
{
p2=p1;
p1=p1->next;
} //p2指向原p1指向结点p1后移一个结点
if(id==p1->id) //假如输入职员编号是p1所指职员编号//结点找到后删除
{
if(p1==head) head=p1->next; //假如head指针和p1指针相等则将下一个结点赋给指针head
else
p2->next=p1->next; //不然将p1所指结点赋给p2所指结点将要删除职员信息跳过去
cout<<" 删除编号为"<<id<<"职员\n";
n--; //将职员人数减1
}
return(head); //将头指针返回
}
}
/**************************修改职员数据**************************/
student *Information::modify(student *head,long id)
{
system("cls");
cout<<"\t----------------<<请输入须修改职员工资信息>>----------------\n"<<endl;
p1=(student *)malloc(LEN); //使p1指向输入结点
p1=head; //不然将头指针赋给p1
while(id!=p1->id&&p1->next!=NULL)
//寻求结点当p1所指职员编号不是输入职员编号而且p1所指next指针不为空
{
p1=p1->next; //p2指向原p1指向结点p1后移一个结点
}
if(id==p1->id) //假如要查找职员编号是p1所指职员编号
{
cout<<"你需要修改职员信息以下:\n";
cout<<"------------------------------------------------------------------------------\n";
cout<<"|编 号| |姓 名| |性别| |基础工资| |加班工资| |其它奖金| |总额|\n";
cout<<"------------------------------------------------------------------------------\n";
cout<<setw(6)<<p1->id
<<setw(10)<<p1->name
<<setw(10)<<p1->sex
<<setw(10)<<p1->paid[0]
<<setw(10)<<p1->paid[1]
<<setw(12)<<p1->paid[2]
<<setw(12)<<p1->total<<endl;
cout<<"------------------------------------------------------------------------------\n";
cout<<" 编号:";
cin>>p1->id;
while((p1->id)<0||(p1->id)>100000)
{
cout<<" 对不起您输入错误!请重新输入(>0<100000): ";
cin>>p1->id; //将输入编号存放到p1所指结构体数组id中
}
cout<<" 姓名:";
cin>>p1->name; //将输入姓名存放到结构体名为p1数组name中
cout<<" 性别:";
cin>>p1->sex;
cout<<" 基础工资:";
cin>>p1->paid[0];
while((p1->paid[0])<0||(p1->paid[0])>100000)
{
cout<<" 对不起您输入错误!请重新输入(>0<100000): ";
cin>>p1->paid[0]; //将输入基础工资存放到p1所指结构体数组paid中
}
cout<<" 加班工资:";
cin>>p1->paid[1];
while((p1->paid[1])<0||(p1->paid[1])>100000)
{
cout<<" 对不起您输入错误!请重新输入(>0<100000): ";
cin>>p1->paid[1]; //将输入加班工资存放到p1所指结构体数组paid中
}
cout<<" 其它奖金:";
cin>>p1->paid[2];
while((p1->paid[2])<0||(p1->paid[2])>100000)
{
cout<<" 对不起您输入错误!请重新输入(>0<100000): ";
cin>>p1->paid[2]; //将输入其它奖金存放到p1所指结构体数组paid中
}
p1->total=p1->paid[0]+p1->paid[1]+p1->paid[2]; //计算总分
}
else
cout<<" 需要修改信息中没有编号为"<<id<<"职员.\n\n"; //没有想要修改结点信息
getchar();
return(head);
}
//------------>查找职员信息
student *Information::find(student *head,long id)
//定义一个指向struct student结构体指针函数*find()用来查找职员信息.
{
system("cls");
if(head==NULL) //若调用次函数以前头指针head为空
{
cout<<" 这是一个空表,请先输入职员信息!\n";
return(head);
}
else
{
p1=head; //不然将头指针赋给p1
while(id!=p1->id&&p1->next!=NULL)
//寻求结点当p1所指职员编号不是输入职员编号而且p1所指next指针不为空
{
p1=p1->next; //p2指向原p1指向结点p1后移一个结点
}
if(id==p1->id) //假如要查找职员编号是p1所指职员编号
{
cout<<"------------------------------------------------------------------------------\n";
cout<<"|编 号| |姓 名| |性别| |基础工资| |加班工资| |其它奖金| |总额|\n";
cout<<"------------------------------------------------------------------------------\n";
cout<<setw(6)<<p1->id
<<setw(10)<<p1->name
<<setw(10)<<p1->sex
<<setw(10)<<p1->paid[0]
<<setw(10)<<p1->paid[1]
<<setw(12)<<p1->paid[2]
<<setw(12)<<p1->total<<endl;
cout<<"------------------------------------------------------------------------------\n";
}
else
cout<<"信息中没有编号为"<<id<<"职员.\n"; //结点没找到
return(head);
}
}
//------------定义paixu()函数将职员工资总额从大到小排列并输出
void Information::paixu(student *head)
{
system("cls");
int i,k,m=0,j;
student *p[N];//定义一个指向struct student结构体指针数组p
if(head!=NULL)//假如头指针是空则继续
{ m=count(head);
cout<<"------------------------------------------------------------------------------\n";
cout<<" *职员工资统计表*\n";
cout<<"------------------------------------------------------------------------------\n";
cout<<"|编号| |姓名| |性别| |基础工资| |加班工资| |其它奖金| |总额| |名次|\n";
cout<<"------------------------------------------------------------------------------\n";
p1=head;
for(k=0;k<m;k++)
{
p[k]=p1;
p1=p1->next;
}
for(k=0;k<m-1;k++) //选择排序法
for(j=k+1;j<m;j++)
if(p[k]->total<p[j]->total)
{
p2=p[k];
p[k]=p[j];
p[j]=p2;
} //从大到小排列指针
for(i=0;i<m;i++)
{
cout<<setw(6)<<p[i]->id
<<setw(8)<<p[i]->name
<<setw(9)<<p[i]->sex
<<setw(10)<<p[i]->paid[0]
<<setw(10)<<p[i]->paid[1]
<<setw(10)<<p[i]->paid[2]
<<setw(10)<<p[i]->total
<<setw(10)<<i+1<<endl;
cout<<"------------------------------------------------------------------------------\n";
}
}
}
//------------>求各工资平均值函数
void Information::average(student *head)
{
int k,m;
float arg1=0,arg2=0,arg3=0;
if(head==NULL)//假如头指针是空则继续
{
cout<<" 这是一个空表,请先输入职员信息!\n";
}
else
{
m=count(head);
p1=head;
for(k=0;k<m;k++)
{
arg1+=p1->paid[0];
arg2+=p1->paid[1];
arg3+=p1->paid[2];
p1=p1->next;
}
arg1/=m;arg2/=m;arg3/=m;
cout<<" *各项工资平均值*\n";
cout<<"------------------------------------------------------------------------------\n";
cout<<"\t\t基础工资平均值: "<<setw(4)<<arg1
<<"\n"<<"\t\t加班工资平均值: "<<setw(4)<<arg2
<<"\n"<<"\t\t奖金平均值: "<<setw(4)<<arg3<<"\n";
cout<<"------------------------------------------------------------------------------\n";
}
}
//------------------->保留函数.
void Information::save(student *head)
{
system("cls");
ofstream out("data.txt",ios::out);
out<<count(head)<<endl;
while(head!=NULL)
{ out<<head->name<<"\t"
<<head->id<<"\t"<<"\t"
<<head->sex<<"\t"
<<head->paid[0]<<"\t"
<<head->paid[1]<<"\t"
<<head->paid[2]<<"\t"
<<head->total<<endl;
head=head->next;
}
}
//———————————>读取文件信息
student *Information::Read()
{
system("cls");
int i=0;
p1=p2=( student *)malloc(LEN);
head=NULL;
ifstream in("data.txt",ios::in);
in>>i;
if(i==0){cout<<" data 文件中数据为空,请先输入数据!"<<endl; return 0;}
else {
cout<<"\n原文件已保留信息以下:\n";
cout<<" ………………………………………………………………………………………………"<<endl;
cout<<"|姓 名| |编 号| |性别| |基础工资| |加班工资| |其它奖金| |总额|\n";
cout<<" ………………………………………………………………………………………………"<<endl;
for(;i>0;i--)
{
p1=(student *)malloc(LEN);
in>>st.name>>st.id>>st.sex
>>st.paid[0]>>st.paid[1]>>st.paid[2]>>st.total;
strcpy(p1->name,st.name);
p1->id=st.id;
strcpy(p1->sex,st.sex);
p1->paid[0]=st.paid[0];
p1->paid[1]=st.paid[1];
p1->paid[2]=st.paid[2];
p1->total=st.total;
if(n==0)head=p1; //假如是输入第一组职员信息就将指针p1赋给指针head
else p2->next=p1; //不然将p1赋给p2所指结构体next指针
p2=p1; //将指针p1赋给指针p2
n++; //将n值加1
//显示读入数据
cout<<" "<<p1->name<<"\t"
<<p1->id<<" \t"
<< p1->sex <<" \t"
<< p1->paid[0] <<" \t"
<< p1->paid[1] <<" \t"
<< p1->paid[2] <<" \t"
<< p1->total<<endl;
cout<<" ………………………………………………………………………………………………"<<endl;
}
cout<<" 数据已经成功读取完成!\n\n"<<endl;
p2->next=NULL;
in.close();
return (head);
}
}
//-------------------->菜单
void Menu()
{
Information person;
student *head=NULL;
int choice;
long i;
head=person.Read();
do{
cout<<"\t※※※※※※※※※※※※※※※※※※※※※※※※※"<<endl;
cout<<"\t※ "<<endl;
cout<<"\t※ _ _ _ 欢迎进入职员信息统计管理 _ _ _ "<<endl;
cout<<"\t※ "<<endl;
cout<<"\t※※※※※※※※※※※※※※※※※※※※※※※※※"<<endl;
cout<<"\t "<<endl;
cout<<"\t
展开阅读全文