?? payroll.cpp
字號:
//***************************************************
// Filename: payroll.cpp
//1,Check data in two files and report the error;
//2,Calculate the pay and report.
//***************************************************
#include<iostream>
#include<fstream> //for file I/O
#include<string>
#include<iomanip>
using namespace std;
struct TransRec
{
string id; // 3 digit , for example 002
int jobSite; // 1 for New York, 2 for Washington, 3 for Chicago
int workHour; // from 0 to 72
};
struct MasterRec
{
string id;
string name;
float payRate; // Pay rate per hour
int dependent; // Number of dependent
int employeeType; // 1 for management, 0 for union
int jobSite;
char sex; // M for male, F for female
};
void OpenForInput( ifstream& ); // open input file
void OpenForOutput( ofstream& ); // open output file
void InTransRec(ifstream&,TransRec&); // read in data in Transaction file
void InMasterRec(ifstream&,MasterRec&);// read in data in Master file
float GrossPay(int,float,int); // Calculate the gross pay for every employee
float Tax(float,int); // Calculate the net pay for every employee
int main()
{
ifstream trans; // Input stream for transaction file
ifstream master; // Input stream for master file
ofstream errControl; // Output stream for error and control report
ofstream payroll; // Output stream for payroll report
TransRec rec1; // Record in transaction file
MasterRec rec2; // Record in master file
string SiteName; // Name of job site
int corrRec=0; // Num of correct record
float gPay; // Gross pay of every employee
float nPay; // Net pay of every employee
float totGrossPay=0; // Total gross pay
float totNetPay=0; // Total net pay
// Open and check Transaction File
cout << " Processing Transaction File Begins:" << endl;
if(!trans)
{
cout<<"Cannot open transaction file. Program terminated."<<endl;
return 1;
}
// Open and check Master File
cout << " Processing Master File Begins:" << endl;
OpenForInput(master);
if(!master)
{
cout<<"Cannot open master file. Program terminated."<<endl;
return 1;
}
// Open output files, set output format and print heading
cout << "(File for Error and Control Report)" << endl;
OpenForOutput(errControl);
cout << "(File for Payroll Report)" << endl;
OpenForOutput(payroll);
errControl << fixed << showpoint << setprecision(2);
payroll << fixed << showpoint << setprecision(2);
payroll << " Name ID Job-site Gross-pay Net-pay" << endl;//heading
// 12345678901234567890123456789012345678901234567890123
// Read in data and process
InTransRec(trans, rec1);
InMasterRec(master, rec2);
while(trans && master)
if(rec1.id != rec2.id)
{
errControl << rec1.id << "'s record isn't found in master file." << endl;
InTransRec(trans, rec1); // read in new data and update loop condition
}
else if(rec1.jobSite != rec2.jobSite)
{
errControl << rec1.id << "'s job site does not match that in master file."<<endl;
//read in new data and update loop condition
InTransRec(trans, rec1);
InMasterRec(master, rec2);
}
else
{
corrRec++; // count the number of records processed correctly
gPay=GrossPay(rec1.workHour,rec2.payRate,rec2.employeeType);
totGrossPay +=gPay; // add every grosspay to total grosspay
nPay=gPay-Tax(gPay,rec2.dependent);
totNetPay +=nPay; // add every netpay to total netpay
if (rec2.jobSite == 1) // change job site number to city name
SiteName="New York";
else if (rec2.jobSite == 2)
SiteName="Washington";
else
SiteName="Chicago";
// output data to payroll file
payroll<<rec2.name
<<setw(12-rec2.name.length())<<' '<<left<<rec2.id
<<setw(5)<<' '<<left<<SiteName
<<setw(14-SiteName.length())<<' '<<left<<gPay
<<setw(6)<<' '<< left <<nPay<< endl;
//read in new data and update loop condition
InTransRec(trans, rec1);
InMasterRec(master, rec2);
}
// Output total number
errControl<<endl<<"The total number of employee records that were processed correctly is "<<corrRec
<<"." <<endl;
payroll<<endl
<<"The total amount of gross pay is "<<totGrossPay<<"." <<endl
<<"The total amount of net pay is "<<totNetPay<<"." <<endl;
return 0;
}
//******************************************************************
void OpenForInput(/* inout*/ifstream& someFile)// File to be opened
{
string fileName; // User-specified file name
cout << "Input file name for reading in data: ";
cin >> fileName;
someFile.open(fileName.c_str());
if ( !someFile )
cout << "** Can't open " << fileName << " **" << endl;
}
//******************************************************************
void OpenForOutput( ofstream& someFile)// File to be opened
{
string fileName; // User-specified file name
cout << "Input file name for saving data: ";
cin >> fileName;
someFile.open(fileName.c_str());
}
//********************************************************************
void InTransRec(ifstream &inFile,TransRec &rec)
// Input the transaction record
{
inFile>>rec.id
>>rec.jobSite
>>rec.workHour;
}
//********************************************************************
void InMasterRec(ifstream &inFile,MasterRec &rec)
// Input the master record
{
inFile>>rec.id
>>rec.name
>>rec.payRate
>>rec.dependent
>>rec.employeeType
>>rec.jobSite
>>rec.sex;
}
//********************************************************************
float GrossPay(int workHour,float payRate,int employeeType)
// If the employee type is union and work hour is larger than 40
// the pay rate is 1.5*previous payrate
{
if(employeeType==1)
return workHour*payRate;
else if (workHour<=40 && employeeType==0)
return workHour*payRate;
else
return 40*payRate + (workHour-40)*payRate*1.5;
}
//********************************************************************
float Tax(float grossPay,int dependent)
{
double taxRate;
double temp;
if(dependent==1)
taxRate=0.15;
else
{
temp=(1-dependent/(dependent+6.0))*0.15;
taxRate=(temp>0.025)? temp : 0.025 ;
}
return grossPay*taxRate;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -