C语言实现简单员工工资管理系统
#include
#include
#include
typedef struct employee{
int num; /* 工号 */
char name[20]; /* 姓名 */
int sex; /* 性别 1 man 2 madam*/
int department; /* 部门 */
int post; /* 1为经理、2为技术员、3为销售员、4为销售经理 */
int lengthservice; /* 工龄 */
int hour; /* 工作时间 */
int hoursalary; /* 时薪*/
int sales; /* 销售额*/
int basicsalary; /* 底薪*/
int salary; /* 工资总数*/
struct employee *next;
}employee;
struct Employee{
int num;
char name[20]; /* 姓名 */
int sex; /* 性别 1 man 2 madam*/
int department; /* 部门 */
int post; /* 1为经理、2为技术员、3为销售员、4为销售经理 */
int lengthservice; /* 工龄 */
int hour; /* 工作时间 */
int hoursalary; /* 时薪*/
int sales; /* 销售额*/
int basicsalary; /* 底薪*/
int salary; /* 工资总数*/
};
void main()
{
int num=0,flag,a,NUM;
char name[20];
employee *list,*rare;//头节点list无数据 尾指针rare有数据
int increase(employee *rare,int *num);
void DecendOrder(employee *list);
void Statistics(employee *list);
void queryAll(employee *list);
void querybasic(employee *list,int num);
void Delete(employee *list,int num);
void DeleteSalary(employee *list,int num);
void querysalary(employee *list,int num);
void change(employee *list,int num);
void changesalary(employee *list,int num);
void Statistics2(employee *list);
void querynum(employee *list,int num);
employee *import(employee *list,int *num);
void salary(employee *list);
void Statistics3(employee *list);
void Statistics4(employee *list);
void save(employee *list);
void queryname(employee *list,char name[20]);
void querydepart(employee *list,int num);
void querypost(employee *list);
void DecendOrder2(employee *list);
list=(employee *)malloc(sizeof(employee));
list->next=NULL;
rare=import(list,&num);
if(num==0)
increase(list,&num);
salary(list);
while(1)
{
begin:printf("菜单:添加数据:1 修改数据:2 删除数据:3 查询数据:4 保存数据:5 统计数据:6 数据排序:7 退出:0
");
scanf("%d",&flag);
if(flag==0){
save(list);
exit(0);}
switch(flag){
case 1:increase(rare,&num);salary(list);save(list);break;
case 2:printf("修改基本信息:1 修改工资信息:2 返回:0
");scanf("%d",&a);
switch(a){
case 0:goto begin;break;
case 1:printf("请输入要修改的工号
");scanf("%d",&NUM);change(list,NUM);break;
case 2:printf("请输入要修改的工号
");scanf("%d",&NUM);changesalary(list,NUM);break;
}break;
case 3:printf("删除基本信息:1 删除工资信息:2 返回:0
");scanf("%d",&a);
switch(a){
case 0:goto begin;break;
case 1:printf("请输入要删除的工号
");scanf("%d",&NUM);Delete(list,NUM);break;
case 2:printf("请输入要删除的工号
");scanf("%d",&NUM);DeleteSalary(list,NUM);break;
}break;
case 4:printf("查询基本信息:1 查询工资信息:2 查询全部信息:3 查询部门信息:4 查询各个岗位信息:5 工号查询信息:6 姓名查询信息:7 返回:0
");scanf("%d",&a);
switch(a){
case 0:goto begin;break;
case 1:printf("请输入要查询的工号
");scanf("%d",&NUM);querybasic(list,NUM);break;
case 2:printf("请输入要查询的工号
");scanf("%d",&NUM);querysalary(list,NUM);break;
case 3:queryAll(list);break;
case 4:printf("请输入要查询的部门号
");scanf("%d",&NUM);querydepart(list,NUM);break;
case 5:querypost(list);break;
case 6:printf("请输入要查询的工号
");scanf("%d",&NUM);querynum(list,NUM);break;
case 7:printf("请输入想查询的姓名
");scanf("%s",name);queryname(list,name);break;
}break;
case 5:save(list);break;
case 6:printf("统计部门平均工资,最低,最高工资:1 统计部门超过平均工资的人数与员工信息:2
统计所有员工最低工资和最高工资员工的信息:3 统计所有员工超出平均工资的人数与员工信息:4
返回:0
");scanf("%d",&a);
switch(a){
case 0:goto begin;break;
case 1:Statistics(list);break;
case 2:Statistics2(list);break;
case 3:Statistics3(list);break;
case 4:Statistics4(list);break;
}break;
case 7:DecendOrder2(list);break;
default:printf("输入错误");goto begin;}}
}
int increase(employee *rare,int *num)//添加数据
{
employee *p,*q;
char name[20];
int sex,department,post,lengthservice,hour,hoursalary,sales,basicsalary;
q=rare;
printf("请输入姓名,性别(1为男,2为女),部门,职位(1为经理、2为技术员、3为销售员、4为销售经理),工龄,工作时间,时薪,销售额,底薪:(工时为-1时停止输入)
");
scanf("%s%d%d%d%d%d%d%d%d",name,&sex,&department,&post,&lengthservice,&hour,&hoursalary,&sales,&basicsalary);
while(hour!=-1)
{
num++;
p=(employee *)malloc(sizeof(employee));
strcpy(p->name,name);
p->num=*num;
p->sex=sex;
p->department=department;
p->post=post;
p->lengthservice=lengthservice;
p->hour=hour;
p->hoursalary=hoursalary;
p->sales=sales;
p->basicsalary=basicsalary;
if(*num==1)
rare->next=p;
else
q->next=p;
q=p;
p->next=NULL;
scanf("%s%d%d%d%d%d%d%d%d",name,&sex,&department,&post,&lengthservice,&hour,&hoursalary,&sales,&basicsalary);
}
return *num;
}
void salary(employee *list)//计算工资
{
employee *p=list->next,*q=list->next;
int sales=0;
while(p!=NULL)
{
if(p->hour==-1)p=p->next;
if(p->post==3)
p->salary=p->sales*4/100+35*p->lengthservice;
else if(p->post==2)
p->salary=100*p->hour+35*p->lengthservice;
else if(p->post==1)
p->salary=8000+p->lengthservice*35;
else if(p->post==4){
while(q!=NULL)
{
if(q->post==3&&q->department==p->department)
sales+=q->sales;
q=q->next;
}
p->salary=sales*5/1000+8000+p->lengthservice*35;
q=list->next;}
p=p->next;
}
}
void querybasic(employee *list,int num)//查询基本信息
{
employee *p;
p=list->next;
while(p->num!=num&&p!=NULL)
p=p->next;
if(p->num==num){
printf("工号 名字 性别 部门 职位 工龄
");
printf("%-5d%-5s%-5d%-5d%-5d%-5d
",p->num,p->name,p->sex,p->department,p->post,p->lengthservice);}
if(p==NULL)
printf("该工号不存在
");
}
void querysalary(employee *list,int num)//查询工资信息
{
employee *p;
p=list->next;
while(p->num!=num&&p!=NULL)
p=p->next;
if(p->num==num){
printf("工号 工时 时薪 销售额 底薪 工资
");
printf("%-6d%-6d%-6d%-8d%-6d%-6d
",p->num,p->hour,p->hoursalary,p->sales,p->basicsalary,p->salary);}
if(p==NULL)
printf("该工号不存在
");
}
void Delete(employee *list,int num)//删除基本信息
{
employee *p,*q;
p=list->next;
q=p->next;
while(p->next->num!=num)
{
p=p->next;
q=p->next;
}
if(q->num==num)
p->next=q->next;
else printf("职工号在范围外");
q=q->next;
while(q!=NULL)
{
q->num--;
q=q->next;
}
}
void DeleteSalary(employee *list,int num)//删除工资信息
{
employee *p;
p=list->next;
while(p->num!=num)
p=p->next;
if(p->num==num){
p->hour=-1;
p->hoursalary=-1;
p->sales=-1;
p->basicsalary=-1;}
else printf("该工号超出范围");
}
employee *import(employee *list,int *num)//读取文件
{
FILE *fp;
struct Employee stu;
employee *q,*p;
if((fp=fopen("employee.dat","rb"))==NULL){
printf("文件不存在,输入数据以建立一个文件存储信息
");
return 0;}
q=(employee *)malloc(sizeof(employee));
while(!feof(fp))
{
if((fread(&stu,sizeof(struct Employee),1,fp))!=1)break;
*num=*num+1;
p=(employee *)malloc(sizeof(employee));
strcpy(p->name,stu.name);
p->num=*num;
p->sex=stu.sex;
p->department=stu.department;
p->post=stu.post;
p->lengthservice=stu.lengthservice;
p->hour=stu.hour;
p->hoursalary=stu.hoursalary;
p->sales=stu.sales;
p->basicsalary=stu.basicsalary;
p->salary=stu.salary;
if(*num==1)
list->next=p;
else
q->next=p;
q=p;
p->next=NULL;
}
fclose(fp);
return q;
}
void save(employee *list)//存储到文件
{
struct Employee stu;
FILE *fp;
employee *p;
p=list->next;
if((fp=fopen("employee.dat","wb"))==NULL){
printf("error");
exit(0);}
while(p!=NULL)
{
stu.num=p->num;
strcpy(stu.name,p->name);
stu.sex=p->sex;
stu.department=p->department;
stu.post=p->post;
stu.lengthservice=p->lengthservice;
stu.hour=p->hour;
stu.hoursalary=p->hoursalary;
stu.sales=p->sales;
stu.basicsalary=p->basicsalary;
stu.salary=p->salary;
if((fwrite(&stu,sizeof(struct Employee),1,fp))!=1)printf("error");
p=p->next;
}
fclose(fp);
}
void change(employee *list,int num)//改变基本数据
{
char name[20];
int sex,department,post,lengthservice;
employee *p=list->next;
while(p->num!=num&&p->next!=NULL)
p=p->next;
if(p->num==num){
printf("请输入修改后的姓名 性别 部门 岗位 工龄
");
scanf("%s%d%d%d%d",name,&sex,&department,&post,&lengthservice);
strcpy(p->name,name);
p->sex=sex;
p->department=department;
p->post=post;
p->lengthservice=lengthservice;}
else printf("职工号超出范围");
}
void changesalary(employee *list,int num)//改变工资数据
{
int hour,hoursalary,sales,basicsalary;
employee *p=list->next;
while(p->num!=num)
p=p->next;
if(p->num==num){
printf("请输入修改后的工作时间 小时工资 销售额 底薪
");
scanf("%d%d%d%d",&hour,&hoursalary,&sales,&basicsalary);
p->hour=hour;
p->hoursalary=hoursalary;
p->sales=sales;
p->basicsalary=basicsalary;}
else printf("职工号超出范围");
}
void queryAll(employee *list)//分页查询所有信息
{
employee *p,*page[100];
int n,i,top=0,flag=0,max;
p=list->next;
while(p!=NULL)
{
page[++top]=p;
i=1;
while(i<=10)
{
i++;
p=p->next;
if(p==NULL)break;
}
}
max=top;
top=0;
p=page[++top];
printf("工号 姓名 性别 部门 岗位 工龄 工时 时薪 销售额 底薪 工资
");
while(1)
{
i=1;
while(i<=10)
{
i++;
printf("%-5d%-5s%-5d%-5d%-5d%-5d%-5d%-5d%-7d%-5d%-5d
",p->num,p->name,p->sex,p->department,p->post,p->lengthservice,p->hour,p->hoursalary,p->sales,p->basicsalary,p->salary);
p=p->next;
if(p==NULL)break;
}
begin:printf("当前页码%d 首页->1 上一页->2 下一页->3 尾页->4 退出->0
",top);
scanf("%d",&n);
if(n==0)break;
else if(n==1)top=1,p=page[1];
else if(n==2){
if(top==1){
printf("当前已经是第一页
");
goto begin;}
else p=page[--top];}
else if(n==3){
if(top==max){
printf("当前已经是最后一页
");
goto begin;}
else p=page[++top];}
else if(n==4)top=max,p=page[max];
else goto begin;
}
}
void querydepart(employee *list,int num)//输出该部门全部员工信息
{
employee *p=list->next;
printf("%d部门的全部员工信息为:
",num);
printf("工号 姓名 性别 部门 岗位 工龄 工时 时薪 销售额 底薪 工资
");
while(p!=NULL)
{
if(p->department==num)
printf("%-5d%-5s%-5d%-5d%-5d%-5d%-5d%-5d%-7d%-5d%-5d
",p->num,p->name,p->sex,p->department,p->post,p->lengthservice,p->hour,p->hoursalary,p->sales,p->basicsalary,p->salary);
p=p->next;
}
}
void querypost(employee *list)//分类显示四个岗位的员工信息
{
employee *p;
int i=0;
while(i<4)
{
switch(i){
case 0:printf("经理岗位的信息为(若为空则无):
");break;
case 1:printf("技术员岗位的信息为(若为空则无):
");break;
case 2:printf("销售员岗位的信息为(若为空则无):
");break;
case 3:printf("销售经理岗位的信息为(若为空则无):
");break;
}
p=list->next;
i++;
while(p!=NULL)
{
if(p->post==i)
printf("%d %s %d %d %d %d %d %d %d %d %d
",p->num,p->name,p->sex,p->department,p->post,p->lengthservice,p->hour,p->hoursalary,p->sales,p->basicsalary,p->salary);
p=p->next;
}
}
}
void querynum(employee *list,int num)//工号查询个人全部信息
{
employee *p;
p=list->next;
while(p!=NULL)
{
if(p->num==num)
break;
p=p->next;
}
if(p->num==num){
printf("工号 姓名 性别 部门 岗位 工龄 工时 时薪 销售额 底薪 工资
");
printf("%-5d%-5s%-5d%-5d%-5d%-5d%-5d%-5d%-7d%-5d%-5d
",p->num,p->name,p->sex,p->department,p->post,p->lengthservice,p->hour,p->hoursalary,p->sales,p->basicsalary,p->salary);}
else printf("工号超出范围");
}
void queryname(employee *list,char name[20])//用姓名查询数据
{
employee *p;
p=list->next;
while(p!=NULL)
{
if(strcmp(p->name,name)==0)
break;
p=p->next;
}
if(strcmp(p->name,name)==0){
printf("工号 姓名 性别 部门 岗位 工龄 工时 时薪 销售额 底薪 工资
");
printf("%-5d%-5s%-5d%-5d%-5d%-5d%-5d%-5d%-7d%-5d%-5d
",p->num,p->name,p->sex,p->department,p->post,p->lengthservice,p->hour,p->hoursalary,p->sales,p->basicsalary,p->salary);}
else printf("该姓名不存在");
}
void Statistics(employee *list)
{
employee *p=list->next,*q=list->next,*max=list->next,*k; //p,q作为循环指针,max指针指向最大的
employee *headis=NULL; //用来保存部门的信息
employee *Max,*Min; //最大和最小值的成员
int i=0; //计算部门的人数;
int sum=0,avar=0; //总值,平均值
int department; //用来接收想要统计的部门
printf("请输入你想要统计的部门:
");
scanf("%d",&department);
//提取所要统计的部门的成员
while(p)
{
if(p->num==-1)
{
p=p->next;
continue;
}
if(department==p->department)
{
q=(employee *)malloc(sizeof(employee));
*q=*p;
q->next=NULL;
if(!headis)
headis=q;
else
k->next=q;
k=q;
}
p=p->next;
}
//计算最大、最小和平均值
p=headis;
q=headis;
Max=headis;
Min=headis;
while(p)
{
if(p->num==-1)
{
p=p->next;
continue;
}
if(p->salary>Max->salary)
Max=p;
else if(p->salary
Min=p;
sum+=p->salary ;
p=p->next;
i++;
}
avar=sum/i;
printf("部门:%d
",department);
printf("平均工资:%d
",avar);
printf("最低工资:%d
",Min->salary);
printf("最高工资:%d
",Max->salary);
}
void Statistics2(employee *list)
{
employee *p=list->next,*q=list->next,*max=list->next,*k; //p,q作为循环指针,max指针指向最大的
employee *headis=NULL; //用来保存部门的信息
int j=0,i=0; //用j来计算人数,i计算工资大于平均工资的人数
int sum=0,avar=0; //平均值
int department; //用来接收想要统计的部门
printf("请输入你想要统计的部门:
");
scanf("%d",&department);
//提取所要统计的部门的成员
while(p!=NULL)
{
if(p->num==-1)
{
p=p->next;
continue;
}
if(department==p->department)
{
j++;
q=(employee*)malloc(sizeof(employee));
*q=*p;
q->next=NULL;
if(!headis)
headis=q;
else
k->next=q;
k=q;
}
p=p->next;
}
//计算最大、最小和平均值
p=headis;
q=headis;
while(p!=NULL)
{
if(p->num==-1)
{
p=p->next;
continue;
}
sum+=p->salary ;
p=p->next;
}
avar=sum/j;
p=q;
headis=0;
//提取超过平均工资的部门的成员
while(p!=NULL)
{
if(p->num==-1)
{
p=p->next;
continue;
}
if((p->salary)>avar)
{
i++;
q=(employee *)malloc(sizeof(employee));
*q=*p;
q->next=NULL;
if(!headis)
headis=q;
else
k->next=q;
k=q;
}
p=p->next;
}
p=headis;
printf("部门:%d
",department);
printf("超出平均工资的人数为:%d
",i);
if(i!=0){
printf("这些成员的信息如下:
");
printf("工号 工时 时薪 销售额 底薪 工资
");
while(p!=NULL)
{
if(p->num==-1)
{
p=p->next;
continue;
}
printf("%-6d%-6d%-6d%-8d%-6d%-6d
",p->num,p->hour,p->hoursalary,p->sales,p->basicsalary,p->salary);
p=p->next ;
}}
}
void Statistics3(employee *list)
{
employee *Max,*Min; //指向最大和最小值的成员
employee*p=list->next; //p作为循环指针
Max=list->next;
Min=list->next;
while(p)
{
if(p->num==-1)
{
p=p->next;
continue;
}
if(p->salary >Max->salary )
Max=p;
if(p->salary
Min=p;
p=p->next;
}
p=Min;
printf("在所有员工中:");
printf("最低工资的员工信息:
");
printf("工号 工时 时薪 销售额 底薪 工资
");
printf("%-6d%-6d%-6d%-8d%-6d%-6d
",p->num,p->hour,p->hoursalary,p->sales,p->basicsalary,p->salary);
p=Max;
printf("最高工资的员工信息:
");
printf("工号 工时 时薪 销售额 底薪 工资
");
printf("%-6d%-6d%-6d%-8d%-6d%-6d
",p->num,p->hour,p->hoursalary,p->sales,p->basicsalary,p->salary);
}
void Statistics4(employee *list)
{
employee *p=list->next,*q=list->next,*k; //p,q作为循环指针
employee *headis=0; //用来保存工资超过平均工资的成员
int i=0; //用i来计算人数
double sum=0,avar=0; //总值,平均值
//求平均值
while(p)
{
if(p->num==-1)
{
p=p->next;
continue;
}
i++;
sum+=p->salary ;
p=p->next;
}
avar=sum/i;
//提取出超过平均工资的成员,并且计算人数
i=0;
p=list;
while(p)
{
if(p->num==-1)
{
p=p->next;
continue;
}
if(p->salary >avar)
{
i++;
q=(employee *)malloc(sizeof(employee));
*q=*p;
q->next=NULL;
if(!headis)
headis=q;
else
k->next=q;
k=q;
}
p=p->next;
}
p=headis;
printf("超出平均工资的人数:%d
",i);
printf("工号 工时 时薪 销售额 底薪 工资
");
while(p)
{
if(p->num==-1)
{
p=p->next;
continue;
}
printf("%-6d%-6d%-6d%-8d%-6d%-6d
",p->num,p->hour,p->hoursalary,p->sales,p->basicsalary,p->salary);
p=p->next;
}
}
void DecendOrder(employee *list)
{
employee *p,*q,*a,*b,*s;
int j=0;
p=list->next;
q=p->next;
a=list;
b=a->next;
while(q!=NULL)
{
a=list;
b=a->next;
j=0;
while(a!=p)
{
if(q->salary>b->salary)
{
p->next=q->next;
q->next=b;
a->next=q;
j=1;
break;
}
a=b;
b=b->next;
}
if(j==1)
q=p->next;
else{
p=q;
q=q->next;}
}
s=list->next;
while(s!=NULL){
printf("工号 姓名 性别 部门 岗位 工龄 工时 时薪 销售额 底薪 工资
");
printf("%-5d%-5s%-5d%-5d%-5d%-5d%-5d%-5d%-7d%-5d%-5d
",s->num,s->name,s->sex,s->department,s->post,s->lengthservice,s->hour,s->hoursalary,s->sales,s->basicsalary,s->salary);
s=s->next;
}
}
void DecendOrder2(employee *list)
{
employee *p,*q,*a,*b,*s,*z,*x=NULL,*v,*head;
int j=0,post;
printf("请输入岗位
");
scanf("%d",&post);
p=list->next;
head=(employee *)malloc(sizeof(employee));
while(p!=NULL)
{
if(p->post==post){
z=(employee *)malloc(sizeof(employee));
*z=*p;
z->next=NULL;
if(x==NULL)
x=z;
else
v->next=z;
v=z;}
p=p->next;
}
head->next=x;
p=x;
q=p->next;
while(q!=NULL)
{
a=head;
b=a->next;
j=0;
while(a!=p)
{
if(q->salary>b->salary)
{
p->next=q->next;
q->next=b;
a->next=q;
j=1;
break;
}
a=b;
b=b->next;
}
if(j==1)
q=p->next;
else{
p=q;
q=q->next;}
}
s=head->next;
while(s!=NULL){
printf("工号 姓名 性别 部门 岗位 工龄 工时 时薪 销售额 底薪 工资
");
printf("%-5d%-5s%-5d%-5d%-5d%-5d%-5d%-5d%-7d%-5d%-5d
",s->num,s->name,s->sex,s->department,s->post,s->lengthservice,s->hour,s->hoursalary,s->sales,s->basicsalary,s->salary);
s=s->next;
}
}