第3个回答 推荐于2016-03-17
完成,改注释的地方也给你写了。C++版本的,如果要变成C的自己把输入输出换一下就OK。调试已经通过。等分。。。。。。。。。。。。。。。。。。。。。。。
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define FILENAME "staff.txt" //数据文件
/////////////////////////////////////////////
struct Staff //职工机构体
{
char CarNumber[10]; //卡号
char Name[10]; //姓名
int Month; //月份
float SPWages; //应发工资
float APWages; //实发工资
float Water; //水费
float Electrical; //电费
float Tax; //税金
};
////////////////////////////////////////////// 文件操作模块
FILE *FP; //全局文件指针
FILE * FileOpen(char FileName[]) //文件打开函数
{
FILE *fp;
if((fp=fopen(FileName,"r"))==NULL)
{
fp=fopen(FileName,"w");
cout<<"文件打开失败重新创建记录文件";
return fp;
}
fp=fopen(FileName,"r+");
return fp;
}
void FileClose(FILE *fp)
{
if(fclose(fp)==0)
cout<<"安全关闭"<<endl;
else
cout<<"文件关闭失败"<<endl;
}
////////////////////////////////////////////////
void Increase() //添加职工信息
{
FP=FileOpen(FILENAME);
Staff temp;
cout<<endl;
cout<<"请输入姓名:";
cin>>temp.Name;
cout<<"请输入卡号:";
cin>>temp.CarNumber;
cout<<"请输入月份:";
cin>>temp.Month;
cout<<"请输入应发工资:";
cin>>temp.SPWages;
cout<<"请输入水费:";
cin>>temp.Water;
cout<<"请输入电费:";
cin>>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(temp),1,FP);
cout<<"信息添加成功,请选择浏览工资信息选项进行查看"<<endl;
FileClose(FP);
}
//////////////////////////////////////////////
void PrintInformation() //浏览工资信息
{
FP=FileOpen(FILENAME);
rewind(FP);
Staff temp;
while(fread(&temp,sizeof(Staff),1,FP)==1)
{
cout<<"姓名:"<<temp.Name<<endl;
cout<<"卡号:"<<temp.CarNumber<<endl;
cout<<"月份:"<<temp.Month<<endl;
cout<<"应发工资:"<<temp.SPWages<<endl;
cout<<"水费:"<<temp.Water<<endl;
cout<<"电费:"<<temp.Electrical<<endl;
cout<<"税金:"<<temp.Tax<<endl;
cout<<"实发工资:"<<temp.APWages<<endl;
cout<<endl;
}
FileClose(FP);
}
//////////////////////////////////////////////////
void Statistics() //统计工资信息
{
Staff temp;
char nametemp[10];
float sum=0;
int monthstart=0,monthover=0;
cout<<"请输入统计的人员姓名:"<<endl;
cin>>nametemp;
cout<<"请输入统计时间段的起始月份(如:3)";
cin>>monthstart;
cout<<"请输入统计时间段的终止月份(如:3)";
cin>>monthover;
FP=FileOpen(FILENAME);
while(fread(&temp,sizeof(Staff),1,FP)==1)
{
if(strcmp(temp.Name,nametemp)==0)
{
if(temp.Month>=monthstart&&temp.Month<=monthover)
{
sum=sum+temp.APWages;
}
}
}
cout<<"职工"<<nametemp<<"从"<<monthstart<<"月至"<<monthover<<"月合计"<<sum<<"元。"<<endl;
}
////////////////////////////////////////////////
void NameSearch() //按姓名查询
{
char tempname[10];
Staff temp;
cout<<endl;
cout<<"请输入要查询的职工的名称:";
cin>>tempname;
FP=FileOpen(FILENAME);
while(fread(&temp,sizeof(Staff),1,FP)==1)
{
if(strcmp(temp.Name,tempname)==0)
{
cout<<"姓名:"<<temp.Name<<endl;
cout<<"卡号:"<<temp.CarNumber<<endl;
cout<<"月份:"<<temp.Month<<endl;
cout<<"应发工资:"<<temp.SPWages<<endl;
cout<<"水费:"<<temp.Water<<endl;
cout<<"电费:"<<temp.Electrical<<endl;
cout<<"税金:"<<temp.Tax<<endl;
cout<<"实发工资:"<<temp.APWages<<endl;
cout<<endl;
}
}
FileClose(FP);
}
void CardSearch() // 按卡号进行查询
{
char tempcard[10];
Staff temp;
cout<<endl;
cout<<"请输入要查询的工资卡号:";
cin>>tempcard;
FP=FileOpen(FILENAME);
while(fread(&temp,sizeof(Staff),1,FP)==1)
{
if(strcmp(temp.CarNumber,tempcard)==0)
{
cout<<"姓名:"<<temp.Name<<endl;
cout<<"卡号:"<<temp.CarNumber<<endl;
cout<<"月份:"<<temp.Month<<endl;
cout<<"应发工资:"<<temp.SPWages<<endl;
cout<<"水费:"<<temp.Water<<endl;
cout<<"电费:"<<temp.Electrical<<endl;
cout<<"税金:"<<temp.Tax<<endl;
cout<<"实发工资:"<<temp.APWages<<endl;
cout<<endl;
}
}
FileClose(FP);
}
int Search() //查询模块目录
{
int Choose=0;
while(1)
{
cout<<endl;
cout<<"请选择查询方式"<<endl;
cout<<"1、按照卡号查询"<<endl;
cout<<"2、按照姓名查询"<<endl;
cout<<"0、返回上级目录"<<endl;
cout<<"请输入查询方式:"<<endl;
cin>>Choose;
switch(Choose)
{
case 1:;CardSearch();break;
case 2:NameSearch();break;
case 0:return 0;break;
}
}
}
//////////////////////////////////////////////
void ShowMenu() //目录显示函数
{
int Choose=0;
while(1)
{
cout<<endl;
cout<<"工资信息管理系统"<<endl;
cout<<"1、添加工资信息。"<<endl;
cout<<"2、浏览工资信息。"<<endl;
cout<<"3、统计工资信息。"<<endl;
cout<<"4、查询工资信息。"<<endl;
cout<<"0、退出系统。"<<endl;
cout<<"请输入服务类型:";
cin>>Choose;
switch(Choose)
{
case 1:Increase();break;
case 2:PrintInformation();break;
case 3:Statistics();break;
case 4:Search();break;
case 0:exit(0);break;
}
}
}
void main()
{
ShowMenu();
}本回答被提问者采纳