//********************************************************
//
// Assignment 10 - Linked Lists, Typedef, and Macros
//
// Name: John Semenuk
//
// Class: C Programming, <replace with Semester and Year>
//
// Date: April 17, 2026
//
// 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>
#include <stdlib.h>
// define constants
#define STD_HOURS 40.0
#define OT_RATE 1.5
#define MA_TAX_RATE 0.05
#define NH_TAX_RATE 0.00
#define VT_TAX_RATE 0.06
#define CA_TAX_RATE 0.07
#define DEFAULT_STATE_TAX_RATE 0.08
#define FED_TAX_RATE 0.25
#define FIRST_NAME_SIZE 10
#define LAST_NAME_SIZE 10
#define TAX_STATE_SIZE 3
// define macros
#define CALC_OT_HOURS(h) ((h > STD_HOURS) ? (h - STD_HOURS) : 0)
#define CALC_STATE_TAX(pay,rate) (pay * rate)
#define CALC_FED_TAX(pay) (pay * FED_TAX_RATE)
#define CALC_NET_PAY(pay,state,fed) (pay - (state + fed))
#define CALC_NORMAL_PAY(rate,hours,ot) (rate * (hours - ot))
#define CALC_OT_PAY(rate,ot) (ot * (OT_RATE * rate))
#define CALC_MIN(val,min) ((val < min) ? val : min)
#define CALC_MAX(val,max) ((val > max) ? val : max)
// 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.
// 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.
// Also note the use of typedef to create an alias for struct employee
typedef struct employee {
struct name empName;
char taxState[TAX_STATE_SIZE];
long clockNumber;
float wageRate;
float hours;
float overtimeHrs;
float grossPay;
float stateTax;
float fedTax;
float netPay;
struct employee *next;
} EMPLOYEE;
// 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 totals {
float total_wageRate;
float total_hours;
float total_overtimeHrs;
float total_grossPay;
float total_stateTax;
float total_fedTax;
float total_netPay;
} TOTALS;
typedef struct min_max {
float min_wageRate, min_hours, min_overtimeHrs, min_grossPay, min_stateTax, min_fedTax, min_netPay;
float max_wageRate, max_hours, max_overtimeHrs, max_grossPay, max_stateTax, max_fedTax, max_netPay;
} MIN_MAX;
// Define prototypes here for each function except main
//
// Note the use of the typedef alias values throughout
// the rest of this program, starting with the fucntions
// prototypes
//
// EMPLOYEE instead of struct employee
// TOTALS instead of struct totals
// MIN_MAX instead of struct min_max
EMPLOYEE *getEmpData(void);
int isEmployeeSize(EMPLOYEE *head);
void calcOvertimeHrs(EMPLOYEE *head);
void calcGrossPay(EMPLOYEE *head);
void calcStateTax(EMPLOYEE *head);
void calcFedTax(EMPLOYEE *head);
void calcNetPay(EMPLOYEE *head);
void calcEmployeeTotals(EMPLOYEE *head, TOTALS *t);
void calcEmployeeMinMax(EMPLOYEE *head, MIN_MAX *m);
void printHeader(void);
void printEmp(EMPLOYEE *head);
void printEmpStatistics(TOTALS *t, MIN_MAX *m, int size);
// ******************************************************************
// Set up head pointer in the main function to point to the
// start of the dynamically allocated linked list nodes that will be
// created and stored in the Heap area.
// ******************************************************************
int main()
{
EMPLOYEE *head_ptr; // always points to first linked list node
int size; // number of employees processed
// set up structure to store totals and initialize all to zero
TOTALS totals = {0};
MIN_MAX minmax = {0};
head_ptr = getEmpData();
size = isEmployeeSize(head_ptr);
if (size <= 0)
{
printf("\nNo employees found.\n"); return 0;
}
calcOvertimeHrs(head_ptr);
calcGrossPay(head_ptr);
calcStateTax(head_ptr);
calcFedTax(head_ptr);
calcNetPay(head_ptr);
calcEmployeeTotals(head_ptr, &totals);
calcEmployeeMinMax(head_ptr, &minmax);
printHeader();
printEmp(head_ptr);
printEmpStatistics(&totals, &minmax, size);
printf("\n\n*** End of Program ***\n"); return 0;
}
// ********************************************************************
// Read the employee input and dynamically allocate and set up our
// linked list in the Heap area. The address of the first linked
// list item representing our first employee will be returned and
// its value is set in our head_ptr. We can then use the head_ptr
// throughout the rest of this program anytime we want to get to get
// to the beginning of our linked list.
// ********************************************************************
EMPLOYEE *getEmpData(void)
{
EMPLOYEE *head, *curr;
char ans[5];
head
= malloc(sizeof(EMPLOYEE
)); curr = head;
while (1)
{
scanf("%s", curr
->empName.
firstName);
scanf("%s", curr
->empName.
lastName);
scanf("%s", curr
->taxState
);
scanf("%li", &curr
->clockNumber
);
scanf("%f", &curr
->wageRate
);
scanf("%f", &curr
->hours
);
{
curr->next = NULL;
break;
}
curr
->next
= malloc(sizeof(EMPLOYEE
)); curr = curr->next;
}
return head;
}
// ********************************************************************
// With the head_ptr now pointing to the first linked list node, we
// can pass it to any function who needs to get to the starting point
// of the linked list in the Heap. From there, functions can traverse
// through the linked list to access and/or update each employee.
//
// Important: Don't update the head_ptr ... otherwise, you could lose
// the address in the heap of the first linked list node.
//
// ********************************************************************
// determine how many employees are in our linked list
int isEmployeeSize(EMPLOYEE *head)
{
int count = 0;
while (head)
{
count++;
head = head->next;
}
return count;
}
// *********************************************************
// Perform calculations and print out information as needed
// *********************************************************
void calcOvertimeHrs(EMPLOYEE *h) // Calculate the overtime hours
{
for (; h; h = h->next)
h->overtimeHrs = CALC_OT_HOURS(h->hours);
}
void calcGrossPay(EMPLOYEE *h) // Calculate the weekly gross pay
{
for (; h; h = h->next)
h->grossPay =
CALC_NORMAL_PAY(h->wageRate, h->hours, h->overtimeHrs)
+ CALC_OT_PAY(h->wageRate, h->overtimeHrs);
}
void calcStateTax(EMPLOYEE *h) // Calculate the state tax
{
for (; h; h = h->next)
{
if (strcmp(h
->taxState
, "MA") == 0) h->stateTax = CALC_STATE_TAX(h->grossPay, MA_TAX_RATE);
else if (strcmp(h
->taxState
, "NH") == 0) h->stateTax = CALC_STATE_TAX(h->grossPay, NH_TAX_RATE);
else if (strcmp(h
->taxState
, "VT") == 0) h->stateTax = CALC_STATE_TAX(h->grossPay, VT_TAX_RATE);
else if (strcmp(h
->taxState
, "CA") == 0) h->stateTax = CALC_STATE_TAX(h->grossPay, CA_TAX_RATE);
else
h->stateTax = CALC_STATE_TAX(h->grossPay, DEFAULT_STATE_TAX_RATE);
}
}
void calcFedTax(EMPLOYEE *h) // Calculate the federal tax
{
for (; h; h = h->next)
h->fedTax = CALC_FED_TAX(h->grossPay);
}
void calcNetPay(EMPLOYEE *h)
{
for (; h; h = h->next)
h->netPay =
CALC_NET_PAY(h->grossPay, h->stateTax, h->fedTax);
}
//*************************************************************
// 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 calcEmployeeTotals(EMPLOYEE *h, TOTALS *t)
{
for (; h; h = h->next)
{
t->total_wageRate += h->wageRate;
t->total_hours += h->hours;
t->total_overtimeHrs += h->overtimeHrs;
t->total_grossPay += h->grossPay;
t->total_stateTax += h->stateTax;
t->total_fedTax += h->fedTax;
t->total_netPay += h->netPay;
}
}
//================ MIN / MAX =================
void calcEmployeeMinMax(EMPLOYEE *h, MIN_MAX *m)
{
EMPLOYEE *p = h;
m->min_wageRate = m->max_wageRate = p->wageRate;
m->min_hours = m->max_hours = p->hours;
m->min_overtimeHrs = m->max_overtimeHrs = p->overtimeHrs;
m->min_grossPay = m->max_grossPay = p->grossPay;
m->min_stateTax = m->max_stateTax = p->stateTax;
m->min_fedTax = m->max_fedTax = p->fedTax;
m->min_netPay = m->max_netPay = p->netPay;
p = p->next;
for (; p; p = p->next)
{
m->min_wageRate = CALC_MIN(p->wageRate, m->min_wageRate);
m->max_wageRate = CALC_MAX(p->wageRate, m->max_wageRate);
m->min_hours = CALC_MIN(p->hours, m->min_hours);
m->max_hours = CALC_MAX(p->hours, m->max_hours);
m->min_overtimeHrs = CALC_MIN(p->overtimeHrs, m->min_overtimeHrs);
m->max_overtimeHrs = CALC_MAX(p->overtimeHrs, m->max_overtimeHrs);
m->min_grossPay = CALC_MIN(p->grossPay, m->min_grossPay);
m->max_grossPay = CALC_MAX(p->grossPay, m->max_grossPay);
m->min_stateTax = CALC_MIN(p->stateTax, m->min_stateTax);
m->max_stateTax = CALC_MAX(p->stateTax, m->max_stateTax);
m->min_fedTax = CALC_MIN(p->fedTax, m->min_fedTax);
m->max_fedTax = CALC_MAX(p->fedTax, m->max_fedTax);
m->min_netPay = CALC_MIN(p->netPay, m->min_netPay);
m->max_netPay = CALC_MAX(p->netPay, m->max_netPay);
}
}// 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 printHeader(void)
{
printf("\n---------------------------------------------------------------------------------"); printf("\nName Tax Clock# Wage Hours OT Gross State Fed Net"); printf("\n State Pay Tax Tax Pay"); printf("\n---------------------------------------------------------------------------------"); }
void printEmp(EMPLOYEE *h)
{
char name[25];
for (; h; h = h->next)
{
strcpy(name
, h
->empName.
firstName); strcat(name
, h
->empName.
lastName);
printf("\n%-20.20s %-2s %06li %6.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", name,
h->taxState,
h->clockNumber,
h->wageRate,
h->hours,
h->overtimeHrs,
h->grossPay,
h->stateTax,
h->fedTax,
h->netPay);
}
}// 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 *t, MIN_MAX *m, int size)
{
printf("\n---------------------------------------------------------------------------------");
printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", t->total_wageRate,
t->total_hours,
t->total_overtimeHrs,
t->total_grossPay,
t->total_stateTax,
t->total_fedTax,
t->total_netPay);
if (size > 0)
{
printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", t->total_wageRate / size,
t->total_hours / size,
t->total_overtimeHrs / size,
t->total_grossPay / size,
t->total_stateTax / size,
t->total_fedTax / size,
t->total_netPay / size);
}
printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", m->min_wageRate,
m->min_hours,
m->min_overtimeHrs,
m->min_grossPay,
m->min_stateTax,
m->min_fedTax,
m->min_netPay);
printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", m->max_wageRate,
m->max_hours,
m->max_overtimeHrs,
m->max_grossPay,
m->max_stateTax,
m->max_fedTax,
m->max_netPay);
printf("\n\nThe total employees processed was: %d\n", size
); }