#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct staff//职员工资信息结构体
{
char Name[10];
char CardNumber[10];
int Month;
float SPWages;
float APWages;
float Water;
float Electrical;
float Tax;
};
FILE *fp;//定义文件指针
struct staff temp;
void printf_face()
{
printf("\n 姓名 银行卡号 月份 应发工资 水费 电费 税 实发工资 \n ");
printf("%s %s %d %.2f %.2f %.2f %.2f %.2f\n",temp.Name,temp.CardNumber,temp.Month,
temp.SPWages,temp.Water,temp.Electrical,temp.Tax,temp.APWages);
}
void PrintInformation()//浏览输出到屏幕
{
if((fp=fopen("wages","rb"))==NULL)
{
printf("cannot open the file!");
exit(0);
}
while(fread(&temp,sizeof(struct staff),1,fp)==1) printf_face();
fclose(fp);
}
void Increase()//添加职工工资信息
{
if((fp=fopen("wages","ab+"))==NULL)
{
printf("cannot open the file!");
exit(0);
}
printf("\n 请输入要==添加的工资信息: \n");
printf("\n 姓名 银行卡号 月份 应发工资 水费 电费 \n ");
scanf("%s%s%d%f%f%f",temp.Name,temp.CardNumber,
&temp.Month,&temp.SPWages,&temp.Water,
&temp.Electrical);
if(temp.SPWages<=800) temp.Tax=0;
if((temp.SPWages>800.0)&&(temp.SPWages<1400.0))
temp.Tax=(temp.SPWages-800)*0.05;
if(temp.SPWages>1400){temp.Tax=(temp.SPWages-1400)*0.1;}
temp.APWages=temp.SPWages-temp.Water-temp.Electrical-temp.Tax;
fwrite(&temp,sizeof(struct staff),1,fp);
fclose(fp);
}
void Statistics()//统计某职工工资总合
{
struct staff temp;
char tempname[10];
float sum=0;
int monthstart=0,monthover=0;
printf("\n 输入姓名 起始月份 终止月份 \n:");
scanf("%s%d%d", tempname, &monthstart,&monthover);
if((fp=fopen("wages","rb"))==NULL)
{
printf("cannot open the file!");
exit(0);
}
while(fread(&temp,sizeof(struct staff),1,fp)==1)
{
if(strcmp(temp.Name,tempname)==0)
{
if(temp.Month>=monthstart&&temp.Month<=monthover)
{
sum+=temp.APWages;
}
}
}
printf("%d 月到 %d 月的工资总和 %f",monthstart,monthover,sum);
fclose(fp);
}
void NameSearch()//按名字查询某职工工资信息
{
char tempname[10];
printf("\n 输入姓名:");
scanf("%s",tempname);
if((fp=fopen("wages","rb"))==NULL)
{
printf("cannot open the file!");
exit(0);
}
while(fread(&temp,sizeof(struct staff),1,fp)==1)
{
if(strcmp(temp.Name,tempname)==0)
{
printf_face();
}
}
fclose(fp);
}
void CardNumberSearch()//按卡号查询某职工工资信息
{
char tempCardNumber[10];
printf("\n 输入卡号:");
scanf("%s",tempCardNumber);
if((fp=fopen("wages","rb"))==NULL)
{
printf("cannot open the file!");
exit(0);
}
while(fread(&temp,sizeof(struct staff),1,fp)==1)
{
if(strcmp(temp.CardNumber,tempCardNumber)==0)
{
printf_face();
}
}
fclose(fp);
}
int Search()
{
int n;
while(1)
{
printf("\n1. 按照卡号查询: \n ");
printf("\n2. 按照姓名查询: \n ");
printf("\n0. 返回上级目录: \n");
scanf("%d",&n);
switch(n)
{
case 1:CardNumberSearch();break;
case 2:NameSearch();break;
case 0:return 0;break;
}
}
}
void ShowMenu()//功能选择
{
int n;
while(1)
{
printf("1. 添加工资信息: \n");
printf("2. 浏览工资信息: \n" );
printf("3. 统计工资信息: \n" );
printf("4. 查询工资信息: \n");
printf("0. 退出系统: " );
scanf("%d",&n);
switch(n)
{
case 1:Increase();break;
case 2:PrintInformation();break;
case 3:Statistics();break;
case 4:Search();break;
case 0:exit(0);break;
}
}
}
void main()
{
ShowMenu();
}