#include <fstream> #include <iostream> using namespace std; // Global constants
ID: 3623036 • Letter: #
Question
#include <fstream>#include <iostream>
using namespace std;
// Global constants
const int MAX_EMPLOYEE_NUMBER = 100;
const char CS_INPUT_FILE_NAME[] = "hours.txt";
const char CS_OUTPUT_FILE_NAME[] = "pay.txt";
const char CS_WAGES_FILE_NAME[] = "wages.txt";
// Functions
void computeAndWritePay(
const double arHours[MAX_EMPLOYEE_NUMBER + 1], const double arWages[MAX_EMPLOYEE_NUMBER +1] )
{
ofstream outputFile( CS_OUTPUT_FILE_NAME );
for ( int eNbr = 1; eNbr <= MAX_EMPLOYEE_NUMBER; ++eNbr ) {
if ( arHours[eNbr] != 0 ) {
outputFile << eNbr << ' '
<< arHours[eNbr] << ' '
<< arHours[eNbr] * arWages[eNbr]
<< endl;
}
}
outputFile.close();
}
void computeWages (double arWages[MAX_EMPLOYEE_NUMBER+1], double arOt[MAX_EMPLOYEE_NUMBER + 1]){
ifstream inputFile( CS_WAGES_FILE_NAME );
for ( int i=0; i <= MAX_EMPLOYEE_NUMBER; ++i ) {
arWages[i] = 0;
}
int eNbr = -1;
double wage = 0;
while (inputFile >> eNbr){
inputFile >> wage;
arWages[eNbr] = wage;
cout << eNbr << " " << wage << " " << arWages[eNbr] << endl;
}
inputFile.close();
}
void computeHours( double arHours[MAX_EMPLOYEE_NUMBER + 1], double arOt[MAX_EMPLOYEE_NUMBER +1] )
{
ifstream inputFile( CS_INPUT_FILE_NAME );
for ( int i=0; i <= MAX_EMPLOYEE_NUMBER; ++i ) {
arHours[i] = 0;
}
int arStartTimes[MAX_EMPLOYEE_NUMBER + 1];
// Stored as total number of minutes since midnight
for ( int i=0; i <= MAX_EMPLOYEE_NUMBER; ++i ) {
arStartTimes[i] = -1;
}
// Repeat while another time can be read
int h = -1;
int m = -1;
char timeType = ' ';
int eNbr = -1;
while ( inputFile >> h ) {
inputFile.get(); // ':'
inputFile >> m >> timeType >> eNbr;
int t = h * 60 + m; // time as minutes from midnight
if ( timeType == '+' ) { // start time
arStartTimes[eNbr] = t;
}
else { // stop time
arHours[eNbr] += ( t - arStartTimes[eNbr]) / 60.0;
arStartTimes[eNbr] = -1;
}
}
if( arStartTimes[eNbr] < 360){
arOt[eNbr] = arStartTimes[eNbr] - 360;
}else if ( arStartTimes[eNbr] > 1080){
arOt[eNbr] = arStartTimes[eNbr] - 1080;
}
inputFile.close();
}
void runPayCalculator()
{
double arHours[MAX_EMPLOYEE_NUMBER + 1];
double arWages[MAX_EMPLOYEE_NUMBER + 1];
double arOt[MAX_EMPLOYEE_NUMBER + 1];
for ( int i=0; i <= MAX_EMPLOYEE_NUMBER; ++i ) {
arHours[i] = -1;
arWages[i] = 0;
arOt[i] = 0;
}
computeHours( arHours,arOt );
computeWages (arWages, arOt);
computeAndWritePay( arHours, arWages );
}
int main()
{
runPayCalculator();
cout << "Press ``Enter'' to close program."; // For Windows
cin.get();
return 0;
}
Explanation / Answer
#include <fstream.h>
#include <iostream.h>
// Global constants
const int MAX_EMPLOYEE_NUMBER = 100;
const char CS_INPUT_FILE_NAME[] = "hours.txt";
const char CS_OUTPUT_FILE_NAME[] = "pay.txt";
const char CS_WAGES_FILE_NAME[] = "wages.txt";
// Functions
void computeAndWritePay(
const double arHours[MAX_EMPLOYEE_NUMBER + 1], const double arWages[MAX_EMPLOYEE_NUMBER +1] )
{
ofstream outputFile( CS_OUTPUT_FILE_NAME );
for ( int eNbr = 1; eNbr <= MAX_EMPLOYEE_NUMBER; ++eNbr ) {
if ( arHours[eNbr] != 0 ) {
outputFile << eNbr << ' '
<< arHours[eNbr] << ' '
<< arHours[eNbr] * arWages[eNbr]
<< endl;
}
}
outputFile.close();
}
void computeWages (double arWages[MAX_EMPLOYEE_NUMBER+1], double arOt[MAX_EMPLOYEE_NUMBER + 1]){
ifstream inputFile( CS_WAGES_FILE_NAME );
for ( int i=0; i <= MAX_EMPLOYEE_NUMBER; ++i ) {
arWages[i] = 0;
}
int eNbr = -1;
double wage = 0;
while (inputFile >> eNbr){
inputFile >> wage;
arWages[eNbr] = wage;
cout << eNbr << " " << wage << " " << arWages[eNbr] << endl;
}
inputFile.close();
}
void computeHours( double arHours[MAX_EMPLOYEE_NUMBER + 1], double arOt[MAX_EMPLOYEE_NUMBER +1] )
{
ifstream inputFile( CS_INPUT_FILE_NAME );
for ( int i=0; i <= MAX_EMPLOYEE_NUMBER; ++i ) {
arHours[i] = 0;
}
int arStartTimes[MAX_EMPLOYEE_NUMBER + 1];
// Stored as total number of minutes since midnight
for ( i=0; i <= MAX_EMPLOYEE_NUMBER; ++i ) {
arStartTimes[i] = -1;
}
// Repeat while another time can be read
int h = -1;
int m = -1;
char timeType = ' ';
int eNbr = -1;
while ( inputFile >> h ) {
inputFile.get(); // ':'
inputFile >> m >> timeType >> eNbr;
int t = h * 60 + m; // time as minutes from midnight
if ( timeType == '+' ) { // start time
arStartTimes[eNbr] = t;
}
else { // stop time
arHours[eNbr] += ( t - arStartTimes[eNbr]) / 60.0;
arStartTimes[eNbr] = -1;
}
if( arStartTimes[eNbr] < 360){
arOt[eNbr] = arStartTimes[eNbr] - 360;
}else if ( arStartTimes[eNbr] > 1080){
arOt[eNbr] = arStartTimes[eNbr] - 1080;
}
}
inputFile.close();
}
void runPayCalculator()
{
double arHours[MAX_EMPLOYEE_NUMBER + 1];
double arWages[MAX_EMPLOYEE_NUMBER + 1];
double arOt[MAX_EMPLOYEE_NUMBER + 1];
for ( int i=0; i <= MAX_EMPLOYEE_NUMBER; ++i ) {
arHours[i] = -1;
arWages[i] = 0;
arOt[i] = 0;
}
computeHours( arHours,arOt );
computeWages (arWages, arOt);
computeAndWritePay( arHours, arWages );
}
int main()
{
runPayCalculator();
cout << "Press ``Enter'' to close program."; // For Windows
cin.get();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.