#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
struct name {
char firstName[FIRST_NAME_SIZE];
char lastName[LAST_NAME_SIZE];
};
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;
};
struct totals {
float total_wageRate;
float total_hours;
float total_overtimeHrs;
float total_grossPay;
float total_stateTax;
float total_fedTax;
float total_netPay;
};
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;
};
// Updated prototypes with pointers
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);
// Transitioned to pointers
void calcOvertimeHrs(struct employee *emp_ptr, int theSize);
void calcGrossPay(struct employee *emp_ptr, int theSize);
void calcStateTax(struct employee *emp_ptr, int theSize);
void calcFedTax(struct employee *emp_ptr, int theSize);
void calcNetPay(struct employee *emp_ptr, int theSize);
void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_MinMax_ptr, int theSize);
int main() {
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}
};
struct totals employeeTotals = {0};
struct min_max employeeMinMax = {0};
// Get hours worked with proper input validation
getHours(employeeData, SIZE);
// Calculate all payroll components
calcOvertimeHrs(employeeData, SIZE);
calcGrossPay(employeeData, SIZE);
calcStateTax(employeeData, SIZE);
calcFedTax(employeeData, SIZE);
calcNetPay(employeeData, SIZE);
// Calculate totals and min/max
calcEmployeeTotals(employeeData, &employeeTotals, SIZE);
calcEmployeeMinMax(employeeData, &employeeMinMax, SIZE);
// Print results
printHeader();
printEmp(employeeData, SIZE);
printEmpStatistics(&employeeTotals, &employeeMinMax, SIZE);
return 0;
}
// Updated getHours with robust input validation
void getHours(struct employee *emp_ptr, int theSize) {
for (int i = 0; i < theSize; ++i) {
int valid = 0;
do {
printf("Enter hours worked by emp #%06li: ", emp_ptr
->clockNumber
); if (scanf("%f", &emp_ptr
->hours
) != 1) { printf("Invalid input. Please enter a number.\n"); while(getchar() != '\n'); // Clear input buffer } else if (emp_ptr->hours < 0 || emp_ptr->hours > 168) {
printf("Hours must be between 0 and 168.\n"); } else {
valid = 1;
}
} while (!valid);
emp_ptr++;
}
}
void printHeader(void) {
printf("\n\n*** Pay Calculator ***\n"); printf("\n--------------------------------------------------------------"); printf("-------------------"); printf("\nName Tax Clock# Wage Hours OT Gross "); printf("\n--------------------------------------------------------------"); printf("-------------------"); }
void printEmp(struct employee *emp_ptr, int theSize) {
char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
for (int i = 0; i < theSize; ++i) {
snprintf(name
, sizeof(name
), "%s %s", emp_ptr
->empName.
firstName, 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++;
}
}
// Updated functions with pointer references
void calcOvertimeHrs(struct employee *emp_ptr, int theSize) {
for (int i = 0; i < theSize; ++i) {
emp_ptr->overtimeHrs = (emp_ptr->hours > STD_HOURS) ? (emp_ptr->hours - STD_HOURS) : 0;
emp_ptr++;
}
}
void calcGrossPay(struct employee *emp_ptr, int theSize) {
for (int i = 0; i < theSize; ++i) {
float normalPay = emp_ptr->wageRate * (emp_ptr->hours - emp_ptr->overtimeHrs);
float overtimePay = emp_ptr->overtimeHrs * (OT_RATE * emp_ptr->wageRate);
emp_ptr->grossPay = normalPay + overtimePay;
emp_ptr++;
}
}
void calcStateTax(struct employee *emp_ptr, int theSize) {
for (int i = 0; i < theSize; ++i) {
// Convert state code to 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++;
}
}
void calcFedTax(struct employee *emp_ptr, int theSize) {
for (int i = 0; i < theSize; ++i) {
emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
emp_ptr++;
}
}
void calcNetPay(struct employee *emp_ptr, int theSize) {
for (int i = 0; i < theSize; ++i) {
emp_ptr->netPay = emp_ptr->grossPay - (emp_ptr->stateTax + emp_ptr->fedTax);
emp_ptr++;
}
}
void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize) {
for (int 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++;
}
}
void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_MinMax_ptr, int theSize) {
// Initialize with first employee
emp_MinMax_ptr->min_wageRate = emp_MinMax_ptr->max_wageRate = emp_ptr->wageRate;
emp_MinMax_ptr->min_hours = emp_MinMax_ptr->max_hours = emp_ptr->hours;
emp_MinMax_ptr->min_overtimeHrs = emp_MinMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
emp_MinMax_ptr->min_grossPay = emp_MinMax_ptr->max_grossPay = emp_ptr->grossPay;
emp_MinMax_ptr->min_stateTax = emp_MinMax_ptr->max_stateTax = emp_ptr->stateTax;
emp_MinMax_ptr->min_fedTax = emp_MinMax_ptr->max_fedTax = emp_ptr->fedTax;
emp_MinMax_ptr->min_netPay = emp_MinMax_ptr->max_netPay = emp_ptr->netPay;
emp_ptr++;
// Check remaining employees
for (int i = 1; i < theSize; ++i) {
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;
emp_ptr++;
}
}
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);
}