/*
Name: Assignment 2
Author: muguangde
Date: 2014-07-21-12.34
Description: This is a simple program to read a text file with
pay info for employees in Hollywood, net income were calculated
according to their working hours and age, no matter they like it not.
PS, maybe easier to use structure or class.
*/
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
float workHour,payRate,grossIncome;
float tax(float,int);
float grossPay(float,float);
int age,maxAge=0,minAge=100;
string firstName,lastName;
string maxFirst,maxLast,minFirst,minLast;
ifstream infile;
ofstream outfile;
infile.open("misercorp.txt");
outfile.open("output.txt");
outfile.setf(ios::fixed,ios::floatfield);
if(!infile.is_open()) {
//if the file can't be opened
cout<<endl<<"Error: unable to open file."<<endl;
system("PAUSE");
exit(1);
}
//To read from file
infile>>firstName>>lastName;
outfile<<"\t***********************************************"<<endl;
outfile<<"\t**********Miser Corporation Payroll************"<<endl;
outfile<<"\t***********************************************"<<endl;
//Once read to the EOF, terminate the loop.
while(!infile.eof()) {
outfile<<firstName<<' '<<lastName<<endl;
infile>>workHour>>payRate>>age;
outfile.precision(2);
outfile<<workHour<<'\t'<<payRate<<'\t'<<age;
//To calculate gross income and max and min age
grossIncome=grossPay(workHour,payRate);
maxAge=(age>maxAge)? age:maxAge;
minAge=(age<minAge)? age:minAge;
outfile.precision(2);
outfile<<endl<<"\nFor employee "<<firstName<<' '<<lastName
<<", aged "<<age<<','<<" the rate of pay is "<<payRate
<<"$/hrs,"<<endl<<"the working hours are "<<workHour
<<" hours"<<", the base pay is $"
<<grossPay(workHour,payRate)<<'.';
outfile<<"\nThe tax he/she should pay is $"
<<tax(grossIncome,age)
<<", and the net pay is $"
<<grossPay(workHour,payRate)-tax(grossIncome,age)<<endl;
outfile<<"*********************************************"<<endl;
//To get the name of oldest/youngest ones
if(age==maxAge) {
maxFirst=firstName;
maxLast=lastName;
}
if(age==minAge) {
minFirst=firstName;
minLast=lastName;
}
infile>>firstName>>lastName;
}//end of while
//To print oldest and youngest employees
outfile<<"\nThe oldest employee is "<<maxFirst<<' '<<
maxLast<<", aged "<<maxAge<<endl;
outfile<<"The youngest employee is "<<minFirst<<' '<<
minLast<<", aged "<<minAge<<endl;
outfile<<"\nJob done. End of the payroll."<<endl;
infile.close();
outfile.close();
system("PAUSE");
return 0;
}
float grossPay(float workHour,float payRate)
{
float gross;
if(workHour<=40)
gross=workHour*payRate;
else
gross=(workHour-40)*payRate*1.5+40.0*payRate;
return gross;//Return the grossPay
}
float tax(float grossIncome,int age)
{
float tax;
//Calculate grossPay first, pass it to here
if(age>=55)
tax=grossIncome*0.5;
else
tax=grossIncome*0.1;
return tax;
}
回到主页
回到目录