//********************************************************
//
// Assignment 8 - Structures and Strings and Pointers
//
// Name: <replace with your name>
//
// Class: C Programming, <replace with Semester and Year>
//
// Date: <replace with the current date>
//
// Description: Program which determines overtime and
// gross pay for a set of employees with outputs sent
// to standard output (the screen).
//
// This assignment also adds the employee name, their tax state,
// and calculates the state tax, federal tax, and net pay. It
// also calculates totals, averages, minimum, and maximum values.
//
// Array and Structure references are to be replaced with
// pointer references to speed up the processing of this code.
//
// Call by Reference design (using pointers)
//
//********************************************************
#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);
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, 51.0},
{{"Mary", "Apl"}, "NH", 526488, 9.75, 42.5},
{{"Frank", "Fortran"}, "VT", 765349, 10.50, 37.0},
{{"Jeff", "Ada"}, "NY", 34645, 12.25, 45.0},
{{"Anton", "Pascal"}, "CA", 127615, 8.35, 40.0}
};
struct totals employeeTotals = {0, 0, 0, 0, 0, 0, 0};
struct min_max employeeMinMax = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
struct employee *emp_ptr = employeeData;
struct totals *emp_totals_ptr = &employeeTotals;
struct min_max *emp_minMax_ptr = &employeeMinMax;
// Call functions to read and calculate employee data
getHours(emp_ptr, SIZE);
calcOvertimeHrs(emp_ptr, SIZE);
calcGrossPay(emp_ptr, SIZE);
calcStateTax(emp_ptr, SIZE);
calcFedTax(emp_ptr, SIZE);
calcNetPay(emp_ptr, SIZE);
calcEmployeeTotals(emp_ptr, emp_totals_ptr, SIZE);
calcEmployeeMinMax(emp_ptr, emp_minMax_ptr, SIZE);
// Print headers and employee details
printHeader();
printEmp(emp_ptr, SIZE);
printEmpStatistics(emp_totals_ptr, emp_minMax_ptr, SIZE);
return 0;
}
// Function Definitions
void getHours(struct employee *emp_ptr, int theSize) {
for (int i = 0; i < theSize; ++i) {
printf("\nEnter hours worked by emp # %06li: ", emp_ptr
->clockNumber
); scanf("%f", &emp_ptr
->hours
); ++emp_ptr;
}
}
void printHeader(void) {
printf("\n\n*** Pay Calculator ***\n"); printf("---------------------------------------------------------------------------------\n"); printf("Name Tax Clock# Wage Hours OT Gross State Fed Net\n"); printf(" State Pay Tax Tax Pay\n"); printf("---------------------------------------------------------------------------------\n"); }
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("%-20.20s %-2.2s %06li %5.2f %5.1f %5.1f %8.2f %7.2f %7.2f %8.2f\n", 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;
}
printf("---------------------------------------------------------------------------------\n"); }
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) {
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) {
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;
for (int 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;
}
}
void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr, int theSize) {
printf("Totals: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n", 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("Averages: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n", 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("Minimum: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n", 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("Maximum: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n", 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);
}