//********************************************************
//
// 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 calculates the state tax, federal tax, and net pay. It
// also calculates totals, averages, minimum, and maximum values.
//
// Call by Reference design
//
//********************************************************
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#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 TAX_STATE_SIZE 3
#define FED_TAX_RATE 0.25
#define FIRST_NAME_SIZE 10
#define LAST_NAME_SIZE 10
// name structure
struct name
{
char firstName[FIRST_NAME_SIZE];
char lastName[LAST_NAME_SIZE];
};
// employee structure
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;
};
// totals structure
struct totals
{
float total_wageRate;
float total_hours;
float total_overtimeHrs;
float total_grossPay;
float total_stateTax;
float total_fedTax;
float total_netPay;
};
// min/max structure
struct min_max
{
float min_wageRate, min_hours, min_overtimeHrs;
float min_grossPay, min_stateTax, min_fedTax, min_netPay;
float max_wageRate, max_hours, max_overtimeHrs;
float max_grossPay, max_stateTax, max_fedTax, max_netPay;
};
// prototypes
void getHours(struct employee[], int);
void calcOvertimeHrs(struct employee[], int);
void calcGrossPay(struct employee[], int);
void calcStateTax(struct employee[], int);
void calcFedTax(struct employee[], int);
void calcNetPay(struct employee[], int);
struct totals calcEmployeeTotals(struct employee[], struct totals, int);
struct min_max calcEmployeeMinMax(struct employee[], struct min_max, int);
void printHeader(void);
void printEmp(struct employee[], int);
void printEmpStatistics(struct totals, struct min_max, int);
//**************************************************************
// Function: main
//**************************************************************
int main()
{
struct employee employeeData[SIZE] = {
{{"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}
};
struct totals employeeTotals = {0,0,0,0,0,0,0};
struct min_max employeeMinMax;
getHours(employeeData, SIZE);
calcOvertimeHrs(employeeData, SIZE);
calcGrossPay(employeeData, SIZE);
calcStateTax(employeeData, SIZE);
calcFedTax(employeeData, SIZE);
calcNetPay(employeeData, SIZE);
employeeTotals = calcEmployeeTotals(employeeData, employeeTotals, SIZE);
employeeMinMax = calcEmployeeMinMax(employeeData, employeeMinMax, SIZE);
printHeader();
printEmp(employeeData, SIZE);
printEmpStatistics(employeeTotals, employeeMinMax, SIZE);
return 0;
}
//**************************************************************
// getHours
//**************************************************************
void getHours(struct employee employeeData[], int size)
{
int i; // loop index
for(i=0;i<size;i++)
{
printf("Enter hours for employee %06li: ", employeeData
[i
].
clockNumber); scanf("%f",&employeeData
[i
].
hours); }
}
//**************************************************************
// calcOvertimeHrs
//**************************************************************
void calcOvertimeHrs(struct employee employeeData[], int size)
{
int i; // loop index
for(i=0;i<size;i++)
{
if(employeeData[i].hours > STD_HOURS)
employeeData[i].overtimeHrs = employeeData[i].hours - STD_HOURS;
else
employeeData[i].overtimeHrs = 0;
}
}
//**************************************************************
// calcGrossPay
//**************************************************************
void calcGrossPay(struct employee employeeData[], int size)
{
int i; // loop index
float normalPay; // normal pay
float overtimePay; // overtime pay
for(i=0;i<size;i++)
{
normalPay = employeeData[i].wageRate *
(employeeData[i].hours - employeeData[i].overtimeHrs);
overtimePay = employeeData[i].overtimeHrs *
(employeeData[i].wageRate * OT_RATE);
employeeData[i].grossPay = normalPay + overtimePay;
}
}
//**************************************************************
// calcStateTax
//**************************************************************
void calcStateTax(struct employee employeeData[], int size)
{
int i; // loop index
for(i=0;i<size;i++)
{
employeeData
[i
].
taxState[0] = toupper(employeeData
[i
].
taxState[0]); employeeData
[i
].
taxState[1] = toupper(employeeData
[i
].
taxState[1]);
if(strcmp(employeeData
[i
].
taxState,"MA")==0) employeeData[i].stateTax = employeeData[i].grossPay * MA_TAX_RATE;
else if(strcmp(employeeData
[i
].
taxState,"NH")==0) employeeData[i].stateTax = employeeData[i].grossPay * NH_TAX_RATE;
else if(strcmp(employeeData
[i
].
taxState,"VT")==0) employeeData[i].stateTax = employeeData[i].grossPay * VT_TAX_RATE;
else if(strcmp(employeeData
[i
].
taxState,"CA")==0) employeeData[i].stateTax = employeeData[i].grossPay * CA_TAX_RATE;
else
employeeData[i].stateTax = employeeData[i].grossPay * DEFAULT_TAX_RATE;
}
}
//**************************************************************
// calcFedTax
//**************************************************************
void calcFedTax(struct employee employeeData[], int size)
{
int i; // loop index
for(i=0;i<size;i++)
{
employeeData[i].fedTax = employeeData[i].grossPay * FED_TAX_RATE;
}
}
//**************************************************************
// calcNetPay
//**************************************************************
void calcNetPay(struct employee employeeData[], int size)
{
int i; // loop index
float totalTax; // total taxes
for(i=0;i<size;i++)
{
totalTax = employeeData[i].stateTax + employeeData[i].fedTax;
employeeData[i].netPay = employeeData[i].grossPay - totalTax;
}
}
//**************************************************************
// calcEmployeeTotals
//**************************************************************
struct totals calcEmployeeTotals(struct employee employeeData[],
struct totals t, int size)
{
int i; // loop index
for(i=0;i<size;i++)
{
t.total_wageRate += employeeData[i].wageRate;
t.total_hours += employeeData[i].hours;
t.total_overtimeHrs += employeeData[i].overtimeHrs;
t.total_grossPay += employeeData[i].grossPay;
t.total_stateTax += employeeData[i].stateTax;
t.total_fedTax += employeeData[i].fedTax;
t.total_netPay += employeeData[i].netPay;
}
return t;
}
//**************************************************************
// calcEmployeeMinMax
//**************************************************************
struct min_max calcEmployeeMinMax(struct employee employeeData[],
struct min_max m, int size)
{
int i; // loop index
m.min_wageRate = m.max_wageRate = employeeData[0].wageRate;
m.min_hours = m.max_hours = employeeData[0].hours;
m.min_overtimeHrs = m.max_overtimeHrs = employeeData[0].overtimeHrs;
m.min_grossPay = m.max_grossPay = employeeData[0].grossPay;
m.min_stateTax = m.max_stateTax = employeeData[0].stateTax;
m.min_fedTax = m.max_fedTax = employeeData[0].fedTax;
m.min_netPay = m.max_netPay = employeeData[0].netPay;
for(i=1;i<size;i++)
{
if(employeeData[i].hours < m.min_hours) m.min_hours = employeeData[i].hours;
if(employeeData[i].hours > m.max_hours) m.max_hours = employeeData[i].hours;
if(employeeData[i].grossPay < m.min_grossPay) m.min_grossPay = employeeData[i].grossPay;
if(employeeData[i].grossPay > m.max_grossPay) m.max_grossPay = employeeData[i].grossPay;
if(employeeData[i].netPay < m.min_netPay) m.min_netPay = employeeData[i].netPay;
if(employeeData[i].netPay > m.max_netPay) m.max_netPay = employeeData[i].netPay;
}
return m;
}
//**************************************************************
// printHeader
//**************************************************************
void printHeader(void)
{
printf("\n*** Pay Calculator ***\n"); }
//**************************************************************
// printEmp
//**************************************************************
void printEmp(struct employee employeeData[], int size)
{
int i; // loop index
for(i=0;i<size;i++)
{
printf("\n%s %s %.2f %.2f %.2f %.2f", employeeData[i].empName.firstName,
employeeData[i].empName.lastName,
employeeData[i].grossPay,
employeeData[i].stateTax,
employeeData[i].fedTax,
employeeData[i].netPay);
}
}
//**************************************************************
// printEmpStatistics
//**************************************************************
void printEmpStatistics(struct totals t, struct min_max m, int size)
{
printf("\nTotals Gross: %.2f", t.
total_grossPay); printf("\nAverage Gross: %.2f", t.
total_grossPay/size
); printf("\nMin Gross: %.2f", m.
min_grossPay); printf("\nMax Gross: %.2f", m.
max_grossPay); }