
//********************************************************
//
// Assignment 10 - Linked Lists, Typedef, and Macros
//
// Name: <Adrian>
//
// Class: C Programming, <Fall 2026>
//
// Date: <November 16th 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>   // 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) ((thePay) * FED_TAX_RATE)

#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
struct name
{
    char firstName[FIRST_NAME_SIZE];
    char lastName [LAST_NAME_SIZE];
};

// Define a global structure type to pass employee data between functions
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
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
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;

// Define prototypes here for each function

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 size);

int main ()
{

    // ******************************************************************
    // 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.
    // ******************************************************************
    EMPLOYEE * head_ptr; // always points to first linked list node

    int theSize; // number of employees processed
    
    // set up structure to store totals and initialize all to zero
    TOTALS employeeTotals  = {0,0,0,0,0,0,0};

    // pointer to the employeeTotals structure
    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};
    
    // pointer to the employeeMinMax structure
    MIN_MAX * emp_minMax_ptr = &employeeMinMax;

    // ********************************************************************
    // Read the employee input and dynamically allocate and set up our 
    // linked list in the Heap area.
    // ********************************************************************
    
    head_ptr = getEmpData ();

    // determine how many employees are in our linked list
    
    theSize = isEmployeeSize (head_ptr);
    
    // Skip all the function calls to process the data if there
    // was no employee information to read in the input
    if (theSize <= 0)
    {
        // print a user friendly message and skip the rest of the processing
        printf("\n\n**** There was no employee input to process ***\n");
    }
    
    else // there are employees to be processed
    {    

        // Calculate the overtime hours
        calcOvertimeHrs (head_ptr);
       
        // Calculate the weekly gross pay
        calcGrossPay (head_ptr); 

        // Calculate the state tax                                       
        calcStateTax (head_ptr);

        // Calculate the federal tax                                                   
        calcFedTax (head_ptr);
 
        // Calculate the net pay after taxes   
        calcNetPay (head_ptr); 
        
        // Keep a running sum of the employee totals
        calcEmployeeTotals (head_ptr, 
                            &employeeTotals);
                            
        // Keep a running update of the employee minimum and maximum values
        calcEmployeeMinMax (head_ptr, 
                            &employeeMinMax);
                        
        // Print the column headers
        printHeader();

        // print out final information on each employee
        printEmp (head_ptr); 
    
        // print the totals and averages for all float items
        printEmpStatistics (&employeeTotals, 
                            &employeeMinMax, 
                            theSize);
    }
    
    // indicate that the program completed all processing
    printf ("\n\n *** End of Program *** \n");

    return (0); // success

} // main

//**************************************************************
// Function: getEmpData 
//**************************************************************

EMPLOYEE * getEmpData (void)
{
 
    char answer[80];
    int more_data = 1;
    char value;
 
    EMPLOYEE *current_ptr,
             *head_ptr;
 
    // Set up storage for first node   
    head_ptr = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
    current_ptr = head_ptr;
 
    // process while there is still input
    while (more_data)
    {
 
        // read in employee first and last name
        printf ("\nEnter employee first name: ");
        scanf ("%s", current_ptr->empName.firstName);
        printf ("\nEnter employee last name: ");
        scanf ("%s", current_ptr->empName.lastName);
 
        // read in employee tax state
        printf ("\nEnter employee two character tax state: ");
        scanf ("%s", current_ptr->taxState);
 
        // read in employee clock number
        printf("\nEnter employee clock number: ");
        scanf("%li", & current_ptr -> clockNumber);
 
        // read in employee wage rate
        printf("\nEnter employee hourly wage: ");
        scanf("%f", & current_ptr -> wageRate);
 
        // read in employee hours worked
        printf("\nEnter hours worked this week: ");
        scanf("%f", & current_ptr -> hours);
 
        // ask user if they would like to add another employee
        printf("\nWould you like to add another employee? (y/n): ");
        scanf("%s", answer);
 
        // check first character for a 'Y' for yes
        if ((value = toupper(answer[0])) != 'Y')
        {
            // no more employees to process
            current_ptr->next = (EMPLOYEE *) NULL;
            more_data = 0; 
        }
        else // Yes, another employee
        {
            // set the next pointer of the current node to point to the new node
            current_ptr->next = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));

            // move the current node pointer to the new node
            current_ptr = current_ptr->next;
        }
         
    } // while
 
    return(head_ptr);
    
} // getEmpData

//*************************************************************
// Function: isEmployeeSize
//**************************************************************

int isEmployeeSize (EMPLOYEE * head_ptr)
{
    
    EMPLOYEE * current_ptr;
    int theSize;

    theSize = 0;

    // assume there is no data if the first node does
    // not have an employee name
    if (head_ptr->empName.firstName[0] != '\0')
    {
        
        // traverse through the linked list, keep a running count of nodes
        for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
        {  
            ++theSize;
        } // for
    }

    return (theSize);

} // isEmployeeSize

//**************************************************************
// Function: printHeader
//**************************************************************

void printHeader (void) 
{ 

    printf ("\n\n*** Pay Calculator ***\n");

    // print the table header
    printf("\n--------------------------------------------------------------");
    printf("-------------------");
    printf("\nName                Tax  Clock# Wage   Hours  OT   Gross ");
    printf("  State  Fed      Net");
    printf("\n                   State                           Pay   ");
    printf("  Tax    Tax      Pay");
    
    printf("\n--------------------------------------------------------------");
    printf("-------------------");

} // printHeader

//************************************************************* 
// Function: printEmp 
//**************************************************************

void printEmp (EMPLOYEE * head_ptr)
{
    
    // Used to format the employee name
    char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];

    EMPLOYEE * current_ptr;

    // traverse through the linked list to process each employee
    for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
    {
        strcpy (name, current_ptr->empName.firstName);
        strcat (name, " ");
        strcat (name, current_ptr->empName.lastName);
    
        // Print out current employee in the current linked list node
        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 
//**************************************************************

void printEmpStatistics (TOTALS * emp_totals_ptr,
                         MIN_MAX * emp_minMax_ptr,
                         int theSize)
{
    
    // print a separator line
    printf("\n--------------------------------------------------------------");
    printf("-------------------");
    
    // print the totals for all the floating point items
    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);
    
    // make sure you don't divide by zero or a negative number
    if (theSize > 0)       
    {
        // print the averages for all the floating point items
        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
    
    // print the min and max values for each item
    
    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);
           
    // print out the total employees process
    printf ("\n\nThe total employees processed was: %i\n", theSize);

} // printEmpStatistics

//*************************************************************
// Function: calcOvertimeHrs
//**************************************************************

void calcOvertimeHrs (EMPLOYEE * head_ptr)
{
    
    EMPLOYEE * current_ptr;

    // traverse through the linked list to calculate overtime hours
    for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
    {  
        current_ptr->overtimeHrs = CALC_OT_HOURS(current_ptr->hours);
        
    } // for

} // calcOvertimeHrs

//*************************************************************
// Function: calcGrossPay
//**************************************************************

void calcGrossPay (EMPLOYEE * head_ptr)
{

    float theNormalPay;
    float theOvertimePay;

    EMPLOYEE * current_ptr;

    // traverse through the linked list to calculate gross pay
    for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
    {
        // calculate normal pay and any overtime pay
        theNormalPay = CALC_NORMAL_PAY(current_ptr->wageRate,
                                       current_ptr->hours,
                                       current_ptr->overtimeHrs);
        theOvertimePay = CALC_OT_PAY(current_ptr->wageRate,
                                     current_ptr->overtimeHrs);
                                     
        // calculate gross pay for employee as normalPay + any overtime pay
        current_ptr->grossPay = theNormalPay + theOvertimePay;

    }

} // calcGrossPay

//*************************************************************
// Function: calcStateTax
//**************************************************************

void calcStateTax (EMPLOYEE * head_ptr)
{
    
    EMPLOYEE * current_ptr;

    // traverse through the linked list to calculate the state tax
    for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
    {
        // Make sure tax state is all uppercase
        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]);
        
        // calculate state tax based on where employee resides
        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
            // any other state is the default rate
            current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
                                                   DEFAULT_STATE_TAX_RATE);
        
    } // for
       
} // calcStateTax

//*************************************************************
// Function: calcFedTax
//**************************************************************

void calcFedTax (EMPLOYEE * head_ptr)
{
    
    EMPLOYEE * current_ptr;

    // traverse through the linked list to calculate the federal tax
    for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
    {
        // Fed Tax is the same for all regardless of state
        current_ptr->fedTax = CALC_FED_TAX(current_ptr->grossPay);
        
    } // for
    
} // calcFedTax

//*************************************************************
// Function: calcNetPay
//**************************************************************

void calcNetPay (EMPLOYEE * head_ptr)
{
    
    EMPLOYEE * current_ptr;

    // traverse through the linked list to calculate the net pay
    for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
    {
        // calculate the net pay
        current_ptr->netPay = CALC_NET_PAY(current_ptr->grossPay,
                                           current_ptr->stateTax,
                                           current_ptr->fedTax);
    } // for
    
} // calcNetPay

//*************************************************************
// Function: calcEmployeeTotals
//**************************************************************

void calcEmployeeTotals (EMPLOYEE * head_ptr,
                         TOTALS * emp_totals_ptr)
{
       
    EMPLOYEE * current_ptr;

    // traverse through the linked list to calculate a running 
    // sum of each employee floating point member item
    for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
    {
        // add current employee data to our running totals
        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
//**************************************************************

void calcEmployeeMinMax (EMPLOYEE * head_ptr,
                         MIN_MAX * emp_minMax_ptr)
{

    EMPLOYEE * current_ptr;
   
    // set to first employee, our initial linked list node
    current_ptr = head_ptr;
    
    // set the min to the first employee members
    emp_minMax_ptr->min_wageRate = current_ptr->wageRate; 
    emp_minMax_ptr->min_hours = current_ptr->hours;
    emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs; 
    emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
    emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
    emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
    emp_minMax_ptr->min_netPay = current_ptr->netPay;
        
    // set the max to the first employee members
    emp_minMax_ptr->max_wageRate = current_ptr->wageRate; 
    emp_minMax_ptr->max_hours = current_ptr->hours;
    emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
    emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
    emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
    emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
    emp_minMax_ptr->max_netPay = current_ptr->netPay;

    // move to the next employee
    current_ptr = current_ptr->next;
    
    // traverse the linked list
    // compare the rest of the employees to each other for min and max
    for (; current_ptr; current_ptr = current_ptr->next) 
    {
        
        // check if current Wage Rate is the new min and/or max
        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);
        
        // check if current Hours is the new min and/or max
        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);       
        
        // check if current Overtime Hours is the new min and/or max
        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);
        
        // check if current Gross Pay is the new min and/or max
        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);
        
        // check if current State Tax is the new min and/or max
        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);
        
        // check if current Federal Tax is the new min and/or max
        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);
        
        // check if current Net Pay is the new min and/or max
        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
