#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Stuff
{
char num[20];//职工号
char name[20];//姓名
float base;//基本工资
float more;//补贴
float reward;//奖励
float sub;//扣除
float sum;//实发
};
int menu()
{
int choice;//记录选择
system("cls");//清屏
printf("1:输入职工数据\n");
printf("2:查询职工数据\n");
printf("3:修改职工数据\n");
printf("4:统计所有职工平均工资\n");
printf("5:退出程序\n");
printf("请输入选择:");
scanf("%d",&choice);
while(choice<1||choice>5)//避免数据输入不正确
{
printf("请重新选择:");//错误就重新输入
scanf("%d",&choice);
}
return choice;
}
int Add_New_Info(struct Stuff stu[],int n)
{
system("cls");//清屏
printf("请输入新增职工号:");
scanf("%s",stu[n].num);
printf("请输入职工姓名:");
scanf("%s",stu[n].name);
stu[n].sum=0;//实发金额=0
printf("请输入基本工资:");
scanf("%f",&stu[n].base);
stu[n].sum += stu[n].base;//实发金额=基本工资
printf("请输入补贴金额:");
scanf("%f",&stu[n].more);
stu[n].sum += stu[n].more;//实发金额=基本工资+补贴金额
printf("请输入奖励金额:");
scanf("%f",&stu[n].reward);
stu[n].sum += stu[n].reward;//实发金额=基本工资+补贴金额+奖励金额
printf("请输入扣除金额:");
scanf("%f",&stu[n].sub);
stu[n].sum -= stu[n].sub;//实发金额=基本工资+补贴金额+奖励金额-stu[n].sub
printf("添加成功!\n");
system("pause");
return n+1;
}
void Search_Stu_Info(struct Stuff stu[],int n)
{
int i;
char temp[20];
system("cls");//清屏
printf("请输入要查询的职工号:");
scanf("%s",temp);
for(i=0;i<n;i++)//遍历结构体数组查找
{
if(!strcmp(temp,stu[i].num))//如果查找到职工号相同
break;//跳出
}
if(i==n)//如果i==n说明没有找到
{
printf("无该职工信息!\n");
system("pause");
return;
}
// 找到输出信息
printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n","职工号","姓名","基本工资","补贴金额","奖励金额","扣除金额","实发工资");
printf("%-10s",stu[i].num);
printf("%-10s",stu[i].name);
printf("%-10.2f",stu[i].base);
printf("%-10.2f",stu[i].more);
printf("%-10.2f",stu[i].reward);
printf("%-10.2f",stu[i].sub);
printf("%-10.2f\n",stu[i].sum);
system("pause");
}
void Modify_Stu_Info(struct Stuff stu[],int n)
{
int i;
char temp[20];
system("cls");//清屏
printf("请输入要查询的职工号:");
scanf("%s",temp);
for(i=0;i<n;i++)//遍历结构体数组查找
{
if(!strcmp(temp,stu[i].num))//如果查找到职工号相同
break;//跳出
}
if(i==n)//如果i==n说明没有找到
{
printf("无该职工信息!\n");
system("pause");
return;
}
// 找到输出信息
printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n","职工号","姓名","基本工资","补贴金额","奖励金额","扣除金额","实发工资");
printf("%-10s",stu[i].num);
printf("%-10s",stu[i].name);
printf("%-10.2f",stu[i].base);
printf("%-10.2f",stu[i].more);
printf("%-10.2f",stu[i].reward);
printf("%-10.2f",stu[i].sub);
printf("%-10.2f\n\n",stu[i].sum);
stu[i].sum=0;//实发金额=0
printf("请修改基本工资:");
scanf("%f",&stu[i].base);
stu[i].sum += stu[i].base;//实发金额=基本工资
printf("请修改补贴金额:");
scanf("%f",&stu[i].more);
stu[i].sum += stu[i].more;//实发金额=基本工资+补贴金额
printf("请修改奖励金额:");
scanf("%f",&stu[i].reward);
stu[i].sum += stu[i].reward;//实发金额=基本工资+补贴金额+奖励金额
printf("请修改扣除金额:");
scanf("%f",&stu[i].sub);
stu[i].sum -= stu[i].sub;//实发金额=基本工资+补贴金额+奖励金额-stu[n].sub
printf("修改成功!\n");
system("pause");
}
void Total_Stu_Info(struct Stuff stu[],int n)
{
int i;
float sum=0;
system("cls");//清屏
printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n","职工号","姓名","基本工资","补贴金额","奖励金额","扣除金额","实发工资");
for(i=0;i<n;i++)//遍历结构体数组显示
{
printf("%-10s",stu[i].num);
printf("%-10s",stu[i].name);
printf("%-10.2f",stu[i].base);
printf("%-10.2f",stu[i].more);
printf("%-10.2f",stu[i].reward);
printf("%-10.2f",stu[i].sub);
printf("%-10.2f\n",stu[i].sum);
sum+=stu[i].sum;
}
printf("\n");
printf("共有职工:%d人\n",n);
printf("平均工资为%.2f元\n",sum/n);
system("pause");
}
int main()
{
int choice;
struct Stuff stu[100];
int NUM=0;
do
{
choice=menu();
switch(choice)
{
case 1:
NUM=Add_New_Info(stu,NUM);//添加员工数据
break;
case 2:
Search_Stu_Info(stu,NUM);//查询员工数据
break;
case 3:
Modify_Stu_Info(stu,NUM);//修改员工信息
break;
case 4:
Total_Stu_Info(stu,NUM);//统计员工信息
break;
}
}while(choice!=5);
return 0;
}
11-16