//********************************************************
//
// Assignment 7 - Structures and Strings
//
// 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 calulates state tax, federal tax, and net pay.
//
// Call by Value design
//
//********************************************************
/*Define and Includes */
#include <stdio.h>
#include <string.h>
/* Define Constants */
#define NUM_EMPL 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 a global structure 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. */
struct employee
{
char name[20];
char taxState[2];
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;
};
/* define prototypes here for each function except main */
float getHours (long int clockNumber);
float calcOvertimeHrs (float hours);
float calcGrossPay (float wageRate, float hours, float overtimeHrs);
void printHeader (void);
void printEmp (char name [], char taxState [],
long int clockNumber, float wageRate,
float hours, float overtimeHrs, float grossPay,
float stateTax, float fedTax, float netPay);
/* TODO: Add your other function prototypes here */
float calcStateTax (float grossPay, char taxState[]);
float calcFedTax (float grossPay);
float calcNetPay (float grossPay, float stateTax, float fedTax);
struct totals calcEmployeeTotals (float wageRate,
float hours,
float overtimeHrs,
float grossPay,
float stateTax,
float fedTax,
float netPay,
struct totals employeeTotals);
void printEmpStatistics (struct totals employeeTotals, int size);
int main ()
{
/* Set up a local variable to store the employee information */
/* Initialize the name, tax state, clock number, and wage rate */
struct employee employeeData[NUM_EMPL] = {
{ "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 }
};
/* set up structure to store totals and initialize all to zero */
struct totals employeeTotals = {0,0,0,0,0,0,0};
int i; /* loop and array index */
/* Call functions as needed to read and calculate information */
for (i = 0; i < NUM_EMPL; ++i)
{
/* Prompt for the number of hours worked by the employee */
employeeData[i].hours = getHours (employeeData[i].clockNumber);
employeeData[i].overtimeHrs = calcOvertimeHrs (employeeData[i].hours);
employeeData[i].grossPay = calcGrossPay (employeeData[i].wageRate,
employeeData[i].hours,
employeeData[i].overtimeHrs);
/* TODO: Add other function calls as needed to calculate */
/* the state tax, the federal tax, and the net pay */
employeeData[i].stateTax = calcStateTax(employeeData[i].grossPay,
employeeData[i].taxState);
employeeData[i].fedTax = calcFedTax(employeeData[i].grossPay);
employeeData[i].netPay = calcNetPay (employeeData[i].grossPay,
employeeData[i].stateTax,
employeeData[i].fedTax);
/* Keep a running sum of the employee totals */
employeeTotals = calcEmployeeTotals (employeeData[i].wageRate,
employeeData[i].hours,
employeeData[i].overtimeHrs,
employeeData[i].grossPay,
employeeData[i].stateTax,
employeeData[i].fedTax,
employeeData[i].netPay,
employeeTotals);
} /* for */
/* Print the column headers */
printHeader();
/* print out each employee */
for (i = 0; i < NUM_EMPL; ++i)
{
printEmp (employeeData[i].name,
employeeData[i].taxState,
employeeData[i].clockNumber,
employeeData[i].wageRate,
employeeData[i].hours,
employeeData[i].overtimeHrs,
employeeData[i].grossPay,
employeeData[i].stateTax,
employeeData[i].fedTax,
employeeData[i].netPay);
}
/* print the totals and averages for all float items */
printEmpStatistics (employeeTotals, NUM_EMPL);
return(0); /* success */
} /* main */
//**************************************************************
// Function: getHours
//
// Purpose: Obtains input from user, the number of hours worked
// per employee and stores the result in a local variable
// that is passed back to the calling function.
//
// Parameters: clockNumber - The unique employee ID
//
// Returns: hoursWorked - hours worked in a given week
//
//**************************************************************
float getHours (long int clockNumber)
{
float hoursWorked; /* hours worked in a given week */
/* Read in hours for employee */
printf("\nEnter hours worked by emp # %06li: ", clockNumber
); scanf ("%f", &hoursWorked
);
/* return hours back to the calling function */
return (hoursWorked);
}
//**************************************************************
// Function: printHeader
//
// Purpose: Prints the initial table header information.
//
// Parameters: none
//
// Returns: void
//
//**************************************************************
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("\n--------------------------------------------------------------"); printf("-------------------");
}
//*************************************************************
// Function: printEmp
//
// Purpose: Prints out all the information for an employee
// in a nice and orderly table format.
//
// Parameters:
//
// clockNumber - unique employee ID
// wageRate - hourly wage rate
// hours - Hours worked for the week
// overtimeHrs - overtime hours worked in a week
// grossPay - gross pay for the week
//
// Returns: void
//
//**************************************************************
void printEmp (char name [], char taxState [],
long int clockNumber, float wageRate,
float hours, float overtimeHrs, float grossPay,
float stateTax, float fedTax, float netPay)
{
/* Print out a single employee */
printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f", name, taxState, clockNumber, wageRate, hours,
overtimeHrs, grossPay, stateTax, fedTax, netPay);
}
void printEmpStatistics (struct totals employeeTotals, int size)
{
/* print the totals for all the floating point fields */
printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", employeeTotals.total_wageRate,
employeeTotals.total_hours,
employeeTotals.total_overtimeHrs,
employeeTotals.total_grossPay,
employeeTotals.total_stateTax,
employeeTotals.total_fedTax,
employeeTotals.total_netPay);
/* make sure you don't divide by zero or a negative number */
if (size > 0)
{
/* print the averages for all the floating point fields */
printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", employeeTotals.total_wageRate/size,
employeeTotals.total_hours/size,
employeeTotals.total_overtimeHrs/size,
employeeTotals.total_grossPay/size,
employeeTotals.total_stateTax/size,
employeeTotals.total_fedTax/size,
employeeTotals.total_netPay/size);
}
}
//*************************************************************
// Function: calcOvertimeHrs
//
// Purpose: Calculates the overtime hours worked by an employee
// in a given week.
//
// Parameters:
//
// hours - Hours worked in a given week
//
// Returns: theOvertimeHrs - overtime hours worked by an employee
// in a given week
//
//**************************************************************
float calcOvertimeHrs (float hours)
{
float theOvertimeHrs; /* caculated overtime hours for employee */
/* Any overtime ? */
if (hours >= STD_HOURS)
{
theOvertimeHrs = hours - STD_HOURS;
}
else /* no overtime */
{
theOvertimeHrs = 0;
}
/* return overtime hours back to the calling function */
return (theOvertimeHrs);
}
//*************************************************************
// Function: calcGrossPay
//
// Purpose: Calculates the gross pay based on the the normal pay
// and any overtime pay for a given week.
//
// Parameters:
//
// wageRate - the hourly wage rate
// hours - the hours worked in a given week
// overtimeHrs - hours worked above normal hours
//
// Returns: theGrossPay - total weekly gross pay for an employee
//
//**************************************************************
float calcGrossPay (float wageRate, float hours, float overtimeHrs)
{
float theGrossPay; /* gross pay earned in a given week */
float theNormalPay; /* normal pay without any overtime hours */
float theOvertimePay; /* overtime pay */
/* calcuate normal pay and any overtime pay */
theNormalPay = wageRate * (hours - overtimeHrs);
theOvertimePay = overtimeHrs * (OT_RATE * wageRate);
/* calculate gross pay for employee as normalPay + any overtime pay */
theGrossPay = theNormalPay + theOvertimePay;
return(theGrossPay);
}
//*************************************************************
// Function: calcStateTax
//
// Purpose: Calculates the State Tax owed based on gross pay
//
// Parameters:
//
// grossPay - the grossPay for a given week
// taxState - the physical state where the employee works
//
// Returns: theStateTax - calculated state tax owed
//
//**************************************************************
float calcStateTax (float grossPay, char taxState[])
{
float theStateTax; /* calculated state tax */
theStateTax = grossPay; /* initialize to gross pay */
/* calculate state tax based on where employee resides */
if (strcmp(taxState
, "MA") == 0) theStateTax *= MA_TAX_RATE;
else if (strcmp(taxState
, "VT") == 0) theStateTax *= VT_TAX_RATE;
else if (strcmp(taxState
, "NH") == 0) theStateTax *= NH_TAX_RATE;
else if (strcmp(taxState
, "CA") == 0) theStateTax *= CA_TAX_RATE;
else
theStateTax *= DEFAULT_TAX_RATE; /* any other state */
return (theStateTax);
}
//*************************************************************
// Function: calcFedTax
//
// Purpose: Calculates the Federal Tax owed based on the gross
// pay
//
// Parameters:
//
// grossPay - the grossPay for a given week
//
// Returns: theFedTax - calculated federal tax owed
//
//**************************************************************
float calcFedTax (float grossPay)
{
float theFedTax; /* The calculated Federal Tax */
/* Fed Tax is the same for all regardless of state */
theFedTax = grossPay * 0.25;
return (theFedTax);
}
//*************************************************************
// Function: calcNetPay
//
// Purpose: Calculates the net pay as the gross pay minus any
// state and federal taxes owed. Essentially, your
// "take home" pay.
//
// Parameters:
//
// grossPay - the grossPay for a given week
// stateTax - the state taxes owed
// fedTax - the fed taxes owed
//
// Returns: theNetPay - the calculated net pay.
//**************************************************************
float calcNetPay (float grossPay, float stateTax, float fedTax)
{
float theNetPay; /* The calculated Federal Tax */
float theTotalTaxes; /* total taxes owed */
/* calculate the total taxes */
theTotalTaxes = stateTax + fedTax;
/* calculate the net pay */
theNetPay = grossPay - theTotalTaxes;
return (theNetPay);
}
struct totals calcEmployeeTotals (float wageRate,
float hours,
float overtimeHrs,
float grossPay,
float stateTax,
float fedTax,
float netPay,
struct totals employeeTotals)
{
/* add current employee to our running totals */
employeeTotals.total_wageRate += wageRate;
employeeTotals.total_hours += hours;
employeeTotals.total_overtimeHrs += overtimeHrs;
employeeTotals.total_grossPay += grossPay;
employeeTotals.total_stateTax += stateTax;
employeeTotals.total_fedTax += fedTax;
employeeTotals.total_netPay += netPay;
/* return all the updated totals to the calling function */
return (employeeTotals);
}