// necessary header files
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// define constants
#define SIZE 5
#define STD_HOURS 40.0
#define OT_RATE 1.5
#define MA_TAX_RATE 0.05
#define NH_TAX_RATE 0.0
#define VT_TAX_RATE 0.06
#define CA_TAX_RATE 0.07
#define DEFAULT_TAX_RATE 0.08
#define NAME_SIZE 20
#define TAX_STATE_SIZE 3
#define FED_TAX_RATE 0.25
#define FIRST_NAME_SIZE 10
#define LAST_NAME_SIZE 10
// Define a structure type to store an employee name
struct name
{
char firstName[FIRST_NAME_SIZE];
char lastName [LAST_NAME_SIZE];
};
// Define a structure type to pass employee data between functions
struct employee
{
struct name empName;
char taxState [TAX_STATE_SIZE];
long int clockNumber;
float wageRate;
float hours;
float overtimeHrs;
float grossPay;
float stateTax;
float fedTax;
float netPay;
};
// this structure type defines the totals of all floating point items
struct totals
{
float total_wageRate;
float total_hours;
float total_overtimeHrs;
float total_grossPay;
float total_stateTax;
float total_fedTax;
float total_netPay;
};
// this structure type defines the min and max values of all floating point items
struct min_max
{
float min_wageRate;
float min_hours;
float min_overtimeHrs;
float min_grossPay;
float min_stateTax;
float min_fedTax;
float min_netPay;
float max_wageRate;
float max_hours;
float max_overtimeHrs;
float max_grossPay;
float max_stateTax;
float max_fedTax;
float max_netPay;
};
// function prototypes
void getHours (struct employee *emp_ptr, int theSize);
void printEmp (struct employee *emp_ptr, int theSize);
void calcEmployeeTotals (struct employee *emp_ptr,
struct totals *emp_totals_ptr,
int theSize);
void calcEmployeeMinMax (struct employee *emp_ptr,
struct min_max *emp_minMax_ptr,
int theSize);
void printHeader (void);
// Transition these functions to pointer references
// Transitioned from array to pointer references
void calcOvertimeHrs (struct employee *emp_ptr, int theSize)
{
int i; // loop index
for (i = 0; i < theSize; ++i)
{
if (emp_ptr->hours >= STD_HOURS)
emp_ptr->overtimeHrs = emp_ptr->hours - STD_HOURS;
else
emp_ptr->overtimeHrs = 0;
emp_ptr++; // move to next employee
}
}
// Transitioned from array to pointer references
void calcGrossPay (struct employee *emp_ptr, int theSize)
{
int i;
float theNormalPay;
float theOvertimePay;
for (i = 0; i < theSize; ++i)
{
theNormalPay = emp_ptr->wageRate * (emp_ptr->hours - emp_ptr->overtimeHrs);
theOvertimePay = emp_ptr->overtimeHrs * (OT_RATE * emp_ptr->wageRate);
emp_ptr->grossPay = theNormalPay + theOvertimePay;
emp_ptr++; // move to next employee
}
}
// Transitioned from array to pointer references
void calcStateTax (struct employee *emp_ptr, int theSize)
{
int i;
for (i = 0; i < theSize; ++i)
{
// Ensure tax state letters are uppercase
emp_ptr
->taxState
[0] = toupper(emp_ptr
->taxState
[0]); emp_ptr
->taxState
[1] = toupper(emp_ptr
->taxState
[1]);
if (strcmp(emp_ptr
->taxState
, "MA") == 0) emp_ptr->stateTax = emp_ptr->grossPay * MA_TAX_RATE;
else if (strcmp(emp_ptr
->taxState
, "VT") == 0) emp_ptr->stateTax = emp_ptr->grossPay * VT_TAX_RATE;
else if (strcmp(emp_ptr
->taxState
, "NH") == 0) emp_ptr->stateTax = emp_ptr->grossPay * NH_TAX_RATE;
else if (strcmp(emp_ptr
->taxState
, "CA") == 0) emp_ptr->stateTax = emp_ptr->grossPay * CA_TAX_RATE;
else
emp_ptr->stateTax = emp_ptr->grossPay * DEFAULT_TAX_RATE;
emp_ptr++; // move to next employee
}
}
// Transitioned from array to pointer references
void calcFedTax (struct employee *emp_ptr, int theSize)
{
int i;
for (i = 0; i < theSize; ++i)
{
emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
emp_ptr++; // move to next employee
}
}
// Transitioned from array to pointer references
void calcNetPay (struct employee *emp_ptr, int theSize)
{
int i;
float theTotalTaxes;
for (i = 0; i < theSize; ++i)
{
theTotalTaxes = emp_ptr->stateTax + emp_ptr->fedTax;
emp_ptr->netPay = emp_ptr->grossPay - theTotalTaxes;
emp_ptr++; // move to next employee
}
}
// Transitioned printEmpStatistics to use pointer references
void printEmpStatistics (struct totals *emp_totals_ptr,
struct min_max *emp_MinMax_ptr,
int theSize)
{
printf("\n--------------------------------------------------------------"); printf("-------------------"); printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", emp_totals_ptr->total_wageRate,
emp_totals_ptr->total_hours,
emp_totals_ptr->total_overtimeHrs,
emp_totals_ptr->total_grossPay,
emp_totals_ptr->total_stateTax,
emp_totals_ptr->total_fedTax,
emp_totals_ptr->total_netPay);
if (theSize > 0)
{
printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", emp_totals_ptr->total_wageRate/theSize,
emp_totals_ptr->total_hours/theSize,
emp_totals_ptr->total_overtimeHrs/theSize,
emp_totals_ptr->total_grossPay/theSize,
emp_totals_ptr->total_stateTax/theSize,
emp_totals_ptr->total_fedTax/theSize,
emp_totals_ptr->total_netPay/theSize);
}
printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", emp_MinMax_ptr->min_wageRate,
emp_MinMax_ptr->min_hours,
emp_MinMax_ptr->min_overtimeHrs,
emp_MinMax_ptr->min_grossPay,
emp_MinMax_ptr->min_stateTax,
emp_MinMax_ptr->min_fedTax,
emp_MinMax_ptr->min_netPay);
printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", emp_MinMax_ptr->max_wageRate,
emp_MinMax_ptr->max_hours,
emp_MinMax_ptr->max_overtimeHrs,
emp_MinMax_ptr->max_grossPay,
emp_MinMax_ptr->max_stateTax,
emp_MinMax_ptr->max_fedTax,
emp_MinMax_ptr->max_netPay);
}
// main function
int main ()
{
// Initialize employee data
struct employee employeeData[SIZE] = {
{ {"Connie", "Cobol"}, "MA", 98401, 10.60},
{ {"Mary", "Apl"}, "NH", 526488, 9.75 },
{ {"Frank", "Fortran"}, "VT", 765349, 10.50 },
{ {"Jeff", "Ada"}, "NY", 34645, 12.25 },
{ {"Anton", "Pascal"}, "CA", 127615, 8.35 }
};
// Setup totals and min/max structures and their pointers
struct totals employeeTotals = {0,0,0,0,0,0,0};
struct totals *emp_totals_ptr = &employeeTotals;
struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
struct min_max *emp_minMax_ptr = &employeeMinMax;
// Prompt for hours worked (with scanf return value checked)
getHours(employeeData, SIZE);
// Calculate various pay components
calcOvertimeHrs(employeeData, SIZE);
calcGrossPay(employeeData, SIZE);
calcStateTax(employeeData, SIZE);
calcFedTax(employeeData, SIZE);
calcNetPay(employeeData, SIZE);
// Calculate running totals and min/max values
calcEmployeeTotals(employeeData, emp_totals_ptr, SIZE);
calcEmployeeMinMax(employeeData, emp_minMax_ptr, SIZE);
// Print header and employee data
printHeader();
printEmp(employeeData, SIZE);
// Print overall statistics using pointer references
printEmpStatistics(emp_totals_ptr, emp_minMax_ptr, SIZE);
return 0;
}
// Function: getHours
// Purpose: Reads hours worked from the user for each employee.
void getHours (struct employee *emp_ptr, int theSize)
{
int i;
for (i = 0; i < theSize; ++i)
{
printf("\nEnter hours worked by emp # %06li: ", emp_ptr
->clockNumber
); if (scanf("%f", &emp_ptr
->hours
) != 1) {
printf("Error reading hours.\n"); }
emp_ptr++;
}
}
// Function: printHeader
// Purpose: Prints the initial table header.
void printHeader (void)
{
printf ("\n\n*** Pay Calculator ***\n"); printf("\n--------------------------------------------------------------"); printf("-------------------"); printf("\nName Tax Clock# Wage Hours OT Gross "); printf("\n--------------------------------------------------------------"); printf("-------------------"); }
// Function: printEmp
// Purpose: Prints out the information for each employee.
void printEmp (struct employee *emp_ptr, int theSize)
{
int i;
char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
for (i = 0; i < theSize; ++i)
{
strcpy(name
, emp_ptr
->empName.
firstName); strcat(name
, emp_ptr
->empName.
lastName);
printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f", name, emp_ptr->taxState, emp_ptr->clockNumber,
emp_ptr->wageRate, emp_ptr->hours,
emp_ptr->overtimeHrs, emp_ptr->grossPay,
emp_ptr->stateTax, emp_ptr->fedTax,
emp_ptr->netPay);
emp_ptr++;
}
}
// Function: calcEmployeeTotals
// Purpose: Sums up the floating point members for all employees.
void calcEmployeeTotals (struct employee *emp_ptr,
struct totals *emp_totals_ptr,
int theSize)
{
int i;
for (i = 0; i < theSize; ++i)
{
emp_totals_ptr->total_wageRate += emp_ptr->wageRate;
emp_totals_ptr->total_hours += emp_ptr->hours;
emp_totals_ptr->total_overtimeHrs += emp_ptr->overtimeHrs;
emp_totals_ptr->total_grossPay += emp_ptr->grossPay;
emp_totals_ptr->total_stateTax += emp_ptr->stateTax;
emp_totals_ptr->total_fedTax += emp_ptr->fedTax;
emp_totals_ptr->total_netPay += emp_ptr->netPay;
emp_ptr++;
}
}
// Function: calcEmployeeMinMax
// Purpose: Updates the min and max values for each floating point member.
void calcEmployeeMinMax (struct employee *emp_ptr,
struct min_max *emp_minMax_ptr,
int theSize)
{
int i;
// Initialize min and max with first employee's data
emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
emp_minMax_ptr->min_hours = emp_ptr->hours;
emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
emp_minMax_ptr->min_netPay = emp_ptr->netPay;
emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
emp_minMax_ptr->max_hours = emp_ptr->hours;
emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
emp_minMax_ptr->max_netPay = emp_ptr->netPay;
for (i = 1; i < theSize; ++i)
{
emp_ptr++;
if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate)
emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate)
emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
if (emp_ptr->hours < emp_minMax_ptr->min_hours)
emp_minMax_ptr->min_hours = emp_ptr->hours;
if (emp_ptr->hours > emp_minMax_ptr->max_hours)
emp_minMax_ptr->max_hours = emp_ptr->hours;
if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay)
emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay)
emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax)
emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax)
emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax)
emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax)
emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
if (emp_ptr->netPay < emp_minMax_ptr->min_netPay)
emp_minMax_ptr->min_netPay = emp_ptr->netPay;
if (emp_ptr->netPay > emp_minMax_ptr->max_netPay)
emp_minMax_ptr->max_netPay = emp_ptr->netPay;
}
}