//********************************************************
//
// Assignment 10 - Linked Lists, Typedef, and Macros
//
// Name: Emma Carbone
//
// Class: C Programming, Spring 2025
//
// Date: 4/15/2025
//
// 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 have all been replaced with
// pointer references to speed up the processing of this code.
// A linked list has been created and deployed to dynamically
// allocate and process employees as needed.
//
// It will also take advantage of the C Preprocessor features,
// in particular with using macros, and will replace all
// struct type references in the code with a typedef alias
// reference.
//
// Call by Reference design (using pointers)
//
//********************************************************
// necessary header files
#include <stdio.h>
#include <string.h>
#include <ctype.h> // for char functions
#include <stdlib.h> // for malloc
// define constants
#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_STATE_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 macros
#define CALC_OT_HOURS(theHours) ((theHours > STD_HOURS) ? theHours - STD_HOURS : 0)
#define CALC_STATE_TAX(thePay,theStateTaxRate) (thePay * theStateTaxRate)
#define CALC_FED_TAX(thePay,theFedTaxRate) (thePay * theFedTaxRate)
#define CALC_NET_PAY(thePay,theStateTax,theFedTax) (thePay - ((theStateTax) + (theFedTax)))
#define CALC_NORMAL_PAY(theWageRate,theHours,theOvertimeHrs) \
((theWageRate) * ((theHours) - (theOvertimeHrs)))
#define CALC_OT_PAY(theWageRate,theOvertimeHrs) ((theOvertimeHrs) * (OT_RATE * (theWageRate)))
#define CALC_MIN(theValue, currentMin) ((theValue) < (currentMin) ? (theValue) : (currentMin))
#define CALC_MAX(theValue, currentMax) ((theValue) > (currentMax) ? (theValue) : (currentMax))
// Define a global structure type to store an employee name
// ... note how one could easily extend this to other parts
// parts of a name: Middle, Nickname, Prefix, Suffix, etc.
struct name
{
char firstName[FIRST_NAME_SIZE];
char lastName [LAST_NAME_SIZE];
};
// Define a global structure type to pass employee data between functions
// Note that the structure type is global, but you don't want a variable
// of that type to be global. Best to declare a variable of that type
// in a function like main or another function and pass as needed.
// Also note the "next" member has been added as a pointer to structure employee.
// This allows us to point to another data item of this same type,
// allowing us to set up and traverse through all the linked
// list nodes, with each node containing the employee information below.
typedef 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 employee * next;
} EMPLOYEE;
// This structure type defines the totals of all floating point items
// so they can be totaled and used also to calculate averages
typedef struct totals
{
float total_wageRate;
float total_hours;
float total_overtimeHrs;
float total_grossPay;
float total_stateTax;
float total_fedTax;
float total_netPay;
} TOTALS;
// This structure type defines the min and max values of all floating
// point items so they can be display in our final report
// Also note the use of typedef to create an alias for struct min_max
typedef 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;
} MIN_MAX;
// Function prototypes
EMPLOYEE * getEmpData (void);
int isEmployeeSize (EMPLOYEE * head_ptr);
void calcOvertimeHrs (EMPLOYEE * head_ptr);
void calcGrossPay (EMPLOYEE * head_ptr);
void printHeader (void);
void printEmp (EMPLOYEE * head_ptr);
void calcStateTax (EMPLOYEE * head_ptr);
void calcFedTax (EMPLOYEE * head_ptr);
void calcNetPay (EMPLOYEE * head_ptr);
void calcEmployeeTotals (EMPLOYEE * head_ptr,
TOTALS * emp_totals_ptr);
void calcEmployeeMinMax (EMPLOYEE * head_ptr,
MIN_MAX * emp_minMax_ptr);
void printEmpStatistics (TOTALS * emp_totals_ptr,
MIN_MAX * emp_minMax_ptr,
int theSize);
int main ()
{
EMPLOYEE * head_ptr; // always points to first linked list node
int theSize;
// set up structure to store totals and initialize all to zero
TOTALS employeeTotals = {0,0,0,0,0,0,0};
TOTALS * emp_totals_ptr = &employeeTotals;
// set up structure to store min and max values and initialize all to zero
MIN_MAX employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
MIN_MAX * emp_minMax_ptr = &employeeMinMax;
// read input and build linked list
head_ptr = getEmpData ();
theSize = isEmployeeSize (head_ptr);
if (theSize <= 0)
{
// print a user friendly message
printf("\n\n**** There was no employee input to process ***\n"); }
else
{
// process all calculations
calcOvertimeHrs (head_ptr);
calcGrossPay (head_ptr);
calcStateTax (head_ptr);
calcFedTax (head_ptr);
calcNetPay (head_ptr);
calcEmployeeTotals (head_ptr, emp_totals_ptr);
calcEmployeeMinMax (head_ptr, emp_minMax_ptr);
// output results
printHeader();
printEmp (head_ptr);
printEmpStatistics (emp_totals_ptr, emp_minMax_ptr, theSize);
}
// program end message
printf ("\n\n *** End of Program *** \n"); return 0;
} // main
//**************************************************************
// Function: getEmpData
//
// Purpose: Obtains input from user: employee name (first an last),
// tax state, clock number, hourly wage, and hours worked
// in a given week.
//
// Information in stored in a dynamically created linked
// list for all employees.
//
// Parameters: void
//
// Returns:
//
// head_ptr - a pointer to the beginning of the dynamically
// created linked list that contains the initial
// input for each employee.
//
//**************************************************************
EMPLOYEE * getEmpData (void)
{
char answer[80]; // user prompt response
int more_data = 1; // continue flag
char value; // response char
EMPLOYEE *current_ptr, *head_ptr;
// allocate first node
head_ptr
= (EMPLOYEE
*) malloc (sizeof(EMPLOYEE
)); current_ptr = head_ptr;
while (more_data)
{
// read names
printf ("\nEnter employee first name: "); scanf ("%s", current_ptr
->empName.
firstName); printf ("\nEnter employee last name: "); scanf ("%s", current_ptr
->empName.
lastName);
// read other data
printf ("\nEnter employee two character tax state: "); scanf ("%s", current_ptr
->taxState
); printf("\nEnter employee clock number: "); scanf("%li", ¤t_ptr
->clockNumber
); printf("\nEnter employee hourly wage: "); scanf("%f", ¤t_ptr
->wageRate
); printf("\nEnter hours worked this week: "); scanf("%f", ¤t_ptr
->hours
);
// ask to add another
printf("\nWould you like to add another employee? (y/n): "); if (value != 'Y')
{
current_ptr->next = NULL;
more_data = 0;
}
else
{
current_ptr
->next
= (EMPLOYEE
*) malloc (sizeof(EMPLOYEE
)); current_ptr = current_ptr->next;
}
} // while
return head_ptr;
} //getEmpData
//*************************************************************
// Function: isEmployeeSize
//
// Purpose: Traverses the linked list and keeps a running count
// on how many employees are currently in our list.
//
// Parameters:
//
// head_ptr - pointer to the initial node in our linked list
//
// Returns:
//
// theSize - the number of employees in our linked list
//
//**************************************************************
int isEmployeeSize (EMPLOYEE * head_ptr)
{
EMPLOYEE * current_ptr;
int theSize = 0;
// check for at least one entry
if (head_ptr->empName.firstName[0] != '\0')
{
for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
++theSize;
} // if
return theSize;
} // isEmployeeSize
//**************************************************************
// Function: printHeader
//
// Purpose: Prints the initial table header information.
//
// Parameters: none
//
// Returns: void
//
//**************************************************************
void printHeader (void)
{
printf ("\n\n*** Pay Calculator ***\n"); printf("\n--------------------------------------------------------------"); printf("-------------------"); printf("\nName Tax Clock# Wage Hours OT Gross State Fed Net"); printf("\n State Pay Tax Tax Pay"); printf("\n--------------------------------------------------------------"); printf("-------------------");
} // printHeader
//*************************************************************
// Function: printEmp
//
// Purpose: Prints out all the information for each employee
// in a nice and orderly table format.
//
// Parameters:
//
// head_ptr - pointer to the beginning of our linked list
//
// Returns: void
//
//**************************************************************
void printEmp (EMPLOYEE * head_ptr)
{
char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 2];
EMPLOYEE * current_ptr;
for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
{
strcpy(name
, current_ptr
->empName.
firstName); strcat(name
, current_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,
current_ptr->taxState,
current_ptr->clockNumber,
current_ptr->wageRate,
current_ptr->hours,
current_ptr->overtimeHrs,
current_ptr->grossPay,
current_ptr->stateTax,
current_ptr->fedTax,
current_ptr->netPay);
} // for
} // printEmp
//*************************************************************
// Function: printEmpStatistics
//
// Purpose: Prints out the summary totals and averages of all
// floating point value items for all employees
// that have been processed. It also prints
// out the min and max values.
//
// Parameters:
//
// emp_totals_ptr - pointer to a structure containing a running total
// of all employee floating point items
//
// emp_minMax_ptr - pointer to a structure containing
// the minimum and maximum values of all
// employee floating point items
//
// tjeSize - the total number of employees processed, used
// to check for zero or negative divide condition.
//
// Returns: void
//
//**************************************************************
void printEmpStatistics (TOTALS * emp_totals_ptr, MIN_MAX * emp_minMax_ptr, int theSize)
{
// separator
printf("\n--------------------------------------------------------------"); printf("-------------------");
// totals
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);
// averages
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);
} // if
// min & max
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);
// count
printf ("\n\nThe total employees processed was: %i\n", theSize
);
} // printEmpStatistics
//*************************************************************
// Function: calcOvertimeHrs
//
// Purpose: Calculates the overtime hours worked by an employee
// in a given week for each employee.
//
// Parameters:
//
// head_ptr - pointer to the beginning of our linked list
//
// Returns: void (the overtime hours gets updated by reference)
//
//**************************************************************
void calcOvertimeHrs (EMPLOYEE * head_ptr)
{
for (EMPLOYEE * current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
current_ptr->overtimeHrs = CALC_OT_HOURS(current_ptr->hours);
} // calcOverTimeHrs
//*************************************************************
// Function: calcGrossPay
//
// Purpose: Calculates the gross pay based on the the normal pay
// and any overtime pay for a given week for each
// employee.
//
// Parameters:
//
// head_ptr - pointer to the beginning of our linked list
//
// Returns: void (the gross pay gets updated by reference)
//
//**************************************************************
void calcGrossPay (EMPLOYEE * head_ptr)
{
for (EMPLOYEE * current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
{
float normal = CALC_NORMAL_PAY(current_ptr->wageRate, current_ptr->hours, current_ptr->overtimeHrs);
float otPay = CALC_OT_PAY(current_ptr->wageRate, current_ptr->overtimeHrs);
current_ptr->grossPay = normal + otPay;
} // for
} // calcGrossPay
//*************************************************************
// Function: calcStateTax
//
// Purpose: Calculates the State Tax owed based on gross pay
// for each employee. State tax rate is based on the
// the designated tax state based on where the
// employee is actually performing the work. Each
// state decides their tax rate.
//
// Parameters:
//
// head_ptr - pointer to the beginning of our linked list
//
// Returns: void (the state tax gets updated by reference)
//
//**************************************************************
void calcStateTax (EMPLOYEE * head_ptr)
{
for (EMPLOYEE * current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
{
if (islower(current_ptr
->taxState
[0])) current_ptr
->taxState
[0] = toupper(current_ptr
->taxState
[0]); if (islower(current_ptr
->taxState
[1])) current_ptr
->taxState
[1] = toupper(current_ptr
->taxState
[1]);
if (strcmp(current_ptr
->taxState
, "MA") == 0) current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, MA_TAX_RATE);
else if (strcmp(current_ptr
->taxState
, "VT") == 0) current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, VT_TAX_RATE);
else if (strcmp(current_ptr
->taxState
, "NH") == 0) current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, NH_TAX_RATE);
else if (strcmp(current_ptr
->taxState
, "CA") == 0) current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, CA_TAX_RATE);
else
current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, DEFAULT_STATE_TAX_RATE);
} // for
} // calcStateTax
//*************************************************************
// Function: calcFedTax
//
// Purpose: Calculates the Federal Tax owed based on the gross
// pay for each employee
//
// Parameters:
//
// head_ptr - pointer to the beginning of our linked list
//
// Returns: void (the federal tax gets updated by reference)
//
//**************************************************************
void calcFedTax (EMPLOYEE * head_ptr)
{
for (EMPLOYEE * current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
current_ptr->fedTax = CALC_FED_TAX(current_ptr->grossPay, FED_TAX_RATE);
} // calcFedTax
//*************************************************************
// Function: calcNetPay
//
// Purpose: Calculates the net pay as the gross pay minus any
// state and federal taxes owed for each employee.
// Essentially, their "take home" pay.
//
// Parameters:
//
// head_ptr - pointer to the beginning of our linked list
//
// Returns: void (the net pay gets updated by reference)
//
//**************************************************************
void calcNetPay (EMPLOYEE * head_ptr)
{
for (EMPLOYEE * current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
current_ptr->netPay = CALC_NET_PAY(current_ptr->grossPay, current_ptr->stateTax, current_ptr->fedTax);
} // calcNetpay
//*************************************************************
// Function: calcEmployeeTotals
//
// Purpose: Performs a running total (sum) of each employee
// floating point member item stored in our linked list
//
// Parameters:
//
// head_ptr - pointer to the beginning of our linked list
// emp_totals_ptr - pointer to a structure containing the
// running totals of each floating point
// member for all employees in our linked
// list
//
// Returns:
//
// void (the employeeTotals structure gets updated by reference)
//
//**************************************************************
void calcEmployeeTotals (EMPLOYEE * head_ptr, TOTALS * emp_totals_ptr)
{
for (EMPLOYEE * current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
{
emp_totals_ptr->total_wageRate += current_ptr->wageRate;
emp_totals_ptr->total_hours += current_ptr->hours;
emp_totals_ptr->total_overtimeHrs+= current_ptr->overtimeHrs;
emp_totals_ptr->total_grossPay += current_ptr->grossPay;
emp_totals_ptr->total_stateTax += current_ptr->stateTax;
emp_totals_ptr->total_fedTax += current_ptr->fedTax;
emp_totals_ptr->total_netPay += current_ptr->netPay;
} // for
} // calcEmployeeTotals
//*************************************************************
// Function: calcEmployeeMinMax
//
// Purpose: Accepts various floating point values from an
// employee and adds to a running update of min
// and max values
//
// Parameters:
//
// head_ptr - pointer to the beginning of our linked list
// emp_minMax_ptr - pointer to the min/max structure
//
// Returns:
//
// void (employeeMinMax structure updated by reference)
//
//**************************************************************
void calcEmployeeMinMax (EMPLOYEE * head_ptr, MIN_MAX * emp_minMax_ptr)
{
EMPLOYEE * current_ptr = head_ptr;
// initialize with first employee
emp_minMax_ptr->min_wageRate = emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
emp_minMax_ptr->min_hours = emp_minMax_ptr->max_hours = current_ptr->hours;
emp_minMax_ptr->min_overtimeHrs = emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
emp_minMax_ptr->min_grossPay = emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
emp_minMax_ptr->min_stateTax = emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
emp_minMax_ptr->min_fedTax = emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
emp_minMax_ptr->min_netPay = emp_minMax_ptr->max_netPay = current_ptr->netPay;
// traverse remaining
current_ptr = current_ptr->next;
for (; current_ptr; current_ptr = current_ptr->next)
{
emp_minMax_ptr->min_wageRate = CALC_MIN(current_ptr->wageRate, emp_minMax_ptr->min_wageRate);
emp_minMax_ptr->max_wageRate = CALC_MAX(current_ptr->wageRate, emp_minMax_ptr->max_wageRate);
emp_minMax_ptr->min_hours = CALC_MIN(current_ptr->hours, emp_minMax_ptr->min_hours);
emp_minMax_ptr->max_hours = CALC_MAX(current_ptr->hours, emp_minMax_ptr->max_hours);
emp_minMax_ptr->min_overtimeHrs = CALC_MIN(current_ptr->overtimeHrs, emp_minMax_ptr->min_overtimeHrs);
emp_minMax_ptr->max_overtimeHrs = CALC_MAX(current_ptr->overtimeHrs, emp_minMax_ptr->max_overtimeHrs);
emp_minMax_ptr->min_grossPay = CALC_MIN(current_ptr->grossPay, emp_minMax_ptr->min_grossPay);
emp_minMax_ptr->max_grossPay = CALC_MAX(current_ptr->grossPay, emp_minMax_ptr->max_grossPay);
emp_minMax_ptr->min_stateTax = CALC_MIN(current_ptr->stateTax, emp_minMax_ptr->min_stateTax);
emp_minMax_ptr->max_stateTax = CALC_MAX(current_ptr->stateTax, emp_minMax_ptr->max_stateTax);
emp_minMax_ptr->min_fedTax = CALC_MIN(current_ptr->fedTax, emp_minMax_ptr->min_fedTax);
emp_minMax_ptr->max_fedTax = CALC_MAX(current_ptr->fedTax, emp_minMax_ptr->max_fedTax);
emp_minMax_ptr->min_netPay = CALC_MIN(current_ptr->netPay, emp_minMax_ptr->min_netPay);
emp_minMax_ptr->max_netPay = CALC_MAX(current_ptr->netPay, emp_minMax_ptr->max_netPay);
} // for
} // calcEmployeeMinMax