fork download
  1. //********************************************************
  2. //
  3. // Assignment 10 - Linked Lists, Typedef, and Macros
  4. //
  5. // Name: John Nguyen
  6. //
  7. // Class: C Programming, Fall 2024
  8. //
  9. // Date: 11/24/2024
  10. //
  11. // Description: Program determines overtime and gross pay
  12. // for employees and calculates state, federal tax, net pay.
  13. // It also calculates totals, averages, minimum, and maximum
  14. // values for all floating point items.
  15. //
  16. //********************************************************
  17.  
  18. // necessary header files
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <stdlib.h>
  23.  
  24. // define constants
  25. #define STD_HOURS 40.0
  26. #define OT_RATE 1.5
  27. #define MA_TAX_RATE 0.05
  28. #define NH_TAX_RATE 0.0
  29. #define VT_TAX_RATE 0.06
  30. #define CA_TAX_RATE 0.07
  31. #define DEFAULT_STATE_TAX_RATE 0.08
  32. #define NAME_SIZE 20
  33. #define TAX_STATE_SIZE 3
  34. #define FED_TAX_RATE 0.25
  35. #define FIRST_NAME_SIZE 10
  36. #define LAST_NAME_SIZE 10
  37.  
  38. // define macros
  39. #define CALC_OT_HOURS(theHours) ((theHours > STD_HOURS) ? theHours - STD_HOURS : 0)
  40. #define CALC_STATE_TAX(thePay,theStateTaxRate) (thePay * theStateTaxRate)
  41. #define CALC_FED_TAX(thePay) (thePay * FED_TAX_RATE)
  42. #define CALC_NET_PAY(thePay,theStateTax,theFedTax) (thePay - (theStateTax + theFedTax))
  43. #define CALC_NORMAL_PAY(theWageRate,theHours,theOvertimeHrs) \
  44.   (theWageRate * (theHours - theOvertimeHrs))
  45. #define CALC_OT_PAY(theWageRate,theOvertimeHrs) (theOvertimeHrs * (OT_RATE * theWageRate))
  46. #define CALC_MIN(theValue, currentMin) ((theValue < currentMin) ? theValue : currentMin)
  47. #define CALC_MAX(theValue, currentMax) ((theValue > currentMax) ? theValue : currentMax)
  48.  
  49. // typedef for structures
  50. typedef struct employee {
  51. struct {
  52. char firstName[FIRST_NAME_SIZE];
  53. char lastName[LAST_NAME_SIZE];
  54. } empName;
  55. char taxState[TAX_STATE_SIZE];
  56. long int clockNumber;
  57. float wageRate;
  58. float hours;
  59. float overtimeHrs;
  60. float grossPay;
  61. float stateTax;
  62. float fedTax;
  63. float netPay;
  64. struct employee *next;
  65. } EMPLOYEE;
  66.  
  67. typedef struct totals {
  68. float total_wageRate;
  69. float total_hours;
  70. float total_overtimeHrs;
  71. float total_grossPay;
  72. float total_stateTax;
  73. float total_fedTax;
  74. float total_netPay;
  75. } TOTALS;
  76.  
  77. typedef struct min_max {
  78. float min_wageRate, min_hours, min_overtimeHrs, min_grossPay;
  79. float min_stateTax, min_fedTax, min_netPay;
  80. float max_wageRate, max_hours, max_overtimeHrs, max_grossPay;
  81. float max_stateTax, max_fedTax, max_netPay;
  82. } MIN_MAX;
  83.  
  84. // Function prototypes
  85. EMPLOYEE *getEmpData(void);
  86. int isEmployeeSize(EMPLOYEE *head_ptr);
  87. void calcOvertimeHrs(EMPLOYEE *head_ptr);
  88. void calcGrossPay(EMPLOYEE *head_ptr);
  89. void calcStateTax(EMPLOYEE *head_ptr);
  90. void calcFedTax(EMPLOYEE *head_ptr);
  91. void calcNetPay(EMPLOYEE *head_ptr);
  92. void calcEmployeeTotals(EMPLOYEE *head_ptr, TOTALS *emp_totals_ptr);
  93. void calcEmployeeMinMax(EMPLOYEE *head_ptr, MIN_MAX *emp_minMax_ptr);
  94. void printHeader(void);
  95. void printEmp(EMPLOYEE *head_ptr);
  96. void printEmpStatistics(TOTALS *emp_totals_ptr, MIN_MAX *emp_minMax_ptr, int size);
  97.  
  98. int main() {
  99. EMPLOYEE *head_ptr;
  100. int theSize;
  101.  
  102. TOTALS employeeTotals = {0, 0, 0, 0, 0, 0, 0};
  103. TOTALS *emp_totals_ptr = &employeeTotals;
  104.  
  105. MIN_MAX employeeMinMax = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  106. MIN_MAX *emp_minMax_ptr = &employeeMinMax;
  107.  
  108. head_ptr = getEmpData();
  109. theSize = isEmployeeSize(head_ptr);
  110.  
  111. if (theSize <= 0) {
  112. printf("\n\n**** There was no employee input to process ***\n");
  113. } else {
  114. calcOvertimeHrs(head_ptr);
  115. calcGrossPay(head_ptr);
  116. calcStateTax(head_ptr);
  117. calcFedTax(head_ptr);
  118. calcNetPay(head_ptr);
  119. calcEmployeeTotals(head_ptr, emp_totals_ptr);
  120. calcEmployeeMinMax(head_ptr, emp_minMax_ptr);
  121. printHeader();
  122. printEmp(head_ptr);
  123. printEmpStatistics(emp_totals_ptr, emp_minMax_ptr, theSize);
  124. }
  125.  
  126. printf("\n\n *** End of Program *** \n");
  127. return 0;
  128. }
  129.  
  130. EMPLOYEE *getEmpData(void) {
  131. EMPLOYEE *current_ptr, *head_ptr;
  132. char answer[80];
  133. int more_data = 1;
  134.  
  135. head_ptr = (EMPLOYEE *)malloc(sizeof(EMPLOYEE));
  136. current_ptr = head_ptr;
  137.  
  138. while (more_data) {
  139. printf("\nEnter employee first name: ");
  140. scanf("%s", current_ptr->empName.firstName);
  141. printf("\nEnter employee last name: ");
  142. scanf("%s", current_ptr->empName.lastName);
  143. printf("\nEnter employee two character tax state: ");
  144. scanf("%s", current_ptr->taxState);
  145. printf("\nEnter employee clock number: ");
  146. scanf("%li", &current_ptr->clockNumber);
  147. printf("\nEnter employee hourly wage: ");
  148. scanf("%f", &current_ptr->wageRate);
  149. printf("\nEnter hours worked this week: ");
  150. scanf("%f", &current_ptr->hours);
  151.  
  152. printf("\nWould you like to add another employee? (y/n): ");
  153. scanf("%s", answer);
  154. if (toupper(answer[0]) != 'Y') {
  155. current_ptr->next = NULL;
  156. more_data = 0;
  157. } else {
  158. current_ptr->next = (EMPLOYEE *)malloc(sizeof(EMPLOYEE));
  159. current_ptr = current_ptr->next;
  160. }
  161. }
  162. return head_ptr;
  163. }
  164.  
  165. int isEmployeeSize(EMPLOYEE *head_ptr) {
  166. int theSize = 0;
  167. for (EMPLOYEE *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  168. theSize++;
  169. }
  170. return theSize;
  171. }
  172.  
  173. void calcOvertimeHrs(EMPLOYEE *head_ptr) {
  174. for (EMPLOYEE *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  175. current_ptr->overtimeHrs = CALC_OT_HOURS(current_ptr->hours);
  176. }
  177. }
  178.  
  179. void calcGrossPay(EMPLOYEE *head_ptr) {
  180. for (EMPLOYEE *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  181. current_ptr->grossPay = CALC_NORMAL_PAY(current_ptr->wageRate, current_ptr->hours, current_ptr->overtimeHrs) +
  182. CALC_OT_PAY(current_ptr->wageRate, current_ptr->overtimeHrs);
  183. }
  184. }
  185.  
  186. void calcStateTax(EMPLOYEE *head_ptr) {
  187. for (EMPLOYEE *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  188. if (strcmp(current_ptr->taxState, "MA") == 0)
  189. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, MA_TAX_RATE);
  190. else if (strcmp(current_ptr->taxState, "VT") == 0)
  191. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, VT_TAX_RATE);
  192. else if (strcmp(current_ptr->taxState, "NH") == 0)
  193. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, NH_TAX_RATE);
  194. else if (strcmp(current_ptr->taxState, "CA") == 0)
  195. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, CA_TAX_RATE);
  196. else
  197. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, DEFAULT_STATE_TAX_RATE);
  198. }
  199. }
  200.  
  201. void calcFedTax(EMPLOYEE *head_ptr) {
  202. for (EMPLOYEE *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  203. current_ptr->fedTax = CALC_FED_TAX(current_ptr->grossPay);
  204. }
  205. }
  206.  
  207. void calcNetPay(EMPLOYEE *head_ptr) {
  208. for (EMPLOYEE *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  209. current_ptr->netPay = CALC_NET_PAY(current_ptr->grossPay, current_ptr->stateTax, current_ptr->fedTax);
  210. }
  211. }
  212.  
  213. void calcEmployeeTotals(EMPLOYEE *head_ptr, TOTALS *emp_totals_ptr) {
  214. for (EMPLOYEE *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  215. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  216. emp_totals_ptr->total_hours += current_ptr->hours;
  217. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  218. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  219. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  220. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  221. emp_totals_ptr->total_netPay += current_ptr->netPay;
  222. }
  223. }
  224.  
  225. void calcEmployeeMinMax(EMPLOYEE *head_ptr, MIN_MAX *emp_minMax_ptr) {
  226. EMPLOYEE *current_ptr = head_ptr;
  227.  
  228. emp_minMax_ptr->min_wageRate = emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  229. emp_minMax_ptr->min_hours = emp_minMax_ptr->max_hours = current_ptr->hours;
  230. emp_minMax_ptr->min_overtimeHrs = emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  231. emp_minMax_ptr->min_grossPay = emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  232. emp_minMax_ptr->min_stateTax = emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  233. emp_minMax_ptr->min_fedTax = emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  234. emp_minMax_ptr->min_netPay = emp_minMax_ptr->max_netPay = current_ptr->netPay;
  235.  
  236. for (current_ptr = current_ptr->next; current_ptr; current_ptr = current_ptr->next) {
  237. emp_minMax_ptr->min_wageRate = CALC_MIN(current_ptr->wageRate, emp_minMax_ptr->min_wageRate);
  238. emp_minMax_ptr->max_wageRate = CALC_MAX(current_ptr->wageRate, emp_minMax_ptr->max_wageRate);
  239. emp_minMax_ptr->min_hours = CALC_MIN(current_ptr->hours, emp_minMax_ptr->min_hours);
  240. emp_minMax_ptr->max_hours = CALC_MAX(current_ptr->hours, emp_minMax_ptr->max_hours);
  241. emp_minMax_ptr->min_overtimeHrs = CALC_MIN(current_ptr->overtimeHrs, emp_minMax_ptr->min_overtimeHrs);
  242. emp_minMax_ptr->max_overtimeHrs = CALC_MAX(current_ptr->overtimeHrs, emp_minMax_ptr->max_overtimeHrs);
  243. emp_minMax_ptr->min_grossPay = CALC_MIN(current_ptr->grossPay, emp_minMax_ptr->min_grossPay);
  244. emp_minMax_ptr->max_grossPay = CALC_MAX(current_ptr->grossPay, emp_minMax_ptr->max_grossPay);
  245. emp_minMax_ptr->min_stateTax = CALC_MIN(current_ptr->stateTax, emp_minMax_ptr->min_stateTax);
  246. emp_minMax_ptr->max_stateTax = CALC_MAX(current_ptr->stateTax, emp_minMax_ptr->max_stateTax);
  247. emp_minMax_ptr->min_fedTax = CALC_MIN(current_ptr->fedTax, emp_minMax_ptr->min_fedTax);
  248. emp_minMax_ptr->max_fedTax = CALC_MAX(current_ptr->fedTax, emp_minMax_ptr->max_fedTax);
  249. emp_minMax_ptr->min_netPay = CALC_MIN(current_ptr->netPay, emp_minMax_ptr->min_netPay);
  250. emp_minMax_ptr->max_netPay = CALC_MAX(current_ptr->netPay, emp_minMax_ptr->max_netPay);
  251. }
  252. }
  253.  
  254. void printHeader(void) {
  255. printf("\n\n*** Pay Calculator ***\n");
  256. printf("\n--------------------------------------------------------------");
  257. printf("-------------------");
  258. printf("\nName Tax Clock# Wage Hours OT Gross ");
  259. printf(" State Fed Net");
  260. printf("\n State Pay ");
  261. printf(" Tax Tax Pay");
  262. printf("\n--------------------------------------------------------------");
  263. printf("-------------------");
  264. }
  265.  
  266. void printEmp(EMPLOYEE *head_ptr) {
  267. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  268. for (EMPLOYEE *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  269. strcpy(name, current_ptr->empName.firstName);
  270. strcat(name, " ");
  271. strcat(name, current_ptr->empName.lastName);
  272. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  273. name, current_ptr->taxState, current_ptr->clockNumber,
  274. current_ptr->wageRate, current_ptr->hours,
  275. current_ptr->overtimeHrs, current_ptr->grossPay,
  276. current_ptr->stateTax, current_ptr->fedTax,
  277. current_ptr->netPay);
  278. }
  279. }
  280.  
  281. void printEmpStatistics(TOTALS *emp_totals_ptr, MIN_MAX *emp_minMax_ptr, int size) {
  282. printf("\n--------------------------------------------------------------");
  283. printf("-------------------");
  284. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  285. emp_totals_ptr->total_wageRate, emp_totals_ptr->total_hours,
  286. emp_totals_ptr->total_overtimeHrs, emp_totals_ptr->total_grossPay,
  287. emp_totals_ptr->total_stateTax, emp_totals_ptr->total_fedTax,
  288. emp_totals_ptr->total_netPay);
  289.  
  290. if (size > 0) {
  291. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  292. emp_totals_ptr->total_wageRate / size,
  293. emp_totals_ptr->total_hours / size,
  294. emp_totals_ptr->total_overtimeHrs / size,
  295. emp_totals_ptr->total_grossPay / size,
  296. emp_totals_ptr->total_stateTax / size,
  297. emp_totals_ptr->total_fedTax / size,
  298. emp_totals_ptr->total_netPay / size);
  299. }
  300.  
  301. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  302. emp_minMax_ptr->min_wageRate, emp_minMax_ptr->min_hours,
  303. emp_minMax_ptr->min_overtimeHrs, emp_minMax_ptr->min_grossPay,
  304. emp_minMax_ptr->min_stateTax, emp_minMax_ptr->min_fedTax,
  305. emp_minMax_ptr->min_netPay);
  306.  
  307. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  308. emp_minMax_ptr->max_wageRate, emp_minMax_ptr->max_hours,
  309. emp_minMax_ptr->max_overtimeHrs, emp_minMax_ptr->max_grossPay,
  310. emp_minMax_ptr->max_stateTax, emp_minMax_ptr->max_fedTax,
  311. emp_minMax_ptr->max_netPay);
  312.  
  313. printf("\n\nThe total employees processed was: %i\n", size);
  314. }
  315.  
Success #stdin #stdout 0.01s 5272KB
stdin
Connie
Cobol
MA
98401
10.60
51.0
Y
Mary
Apl
NH
526488
9.75
42.5
Y
Frank
Fortran
VT
765349
10.50
37.0
Y
Jeff
Ada
NY
34645
12.25
45
Y
Anton
Pascal
CA
127615
8.35
40.0
N
stdout
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 

*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net
                   State                           Pay     Tax    Tax      Pay
---------------------------------------------------------------------------------
Connie Cobol         MA  098401 10.60  51.0  11.0  598.90  29.95  149.73   419.23
Mary Apl             NH  526488  9.75  42.5   2.5  426.56   0.00  106.64   319.92
Frank Fortran        VT  765349 10.50  37.0   0.0  388.50  23.31   97.12   268.07
Jeff Ada             NY  034645 12.25  45.0   5.0  581.88  46.55  145.47   389.86
Anton Pascal         CA  127615  8.35  40.0   0.0  334.00  23.38   83.50   227.12
---------------------------------------------------------------------------------
Totals:                         51.45 215.5  18.5 2329.84 123.18  582.46  1624.19
Averages:                       10.29  43.1   3.7  465.97  24.64  116.49   324.84
Minimum:                         8.35  37.0   0.0  334.00   0.00   83.50   227.12
Maximum:                        12.25  51.0  11.0  598.90  46.55  149.73   419.23

The total employees processed was: 5


 *** End of Program ***