fork download
  1. //********************************************************
  2. //
  3. // Assignment 9 - Linked Lists
  4. //
  5. // Name: John Nguyen
  6. //
  7. // Class: C Programming, Fall 2024
  8. //
  9. // Date: November 17, 2024
  10. //
  11. // Description: Program calculates state tax, federal tax,
  12. // gross pay, net pay, and other statistics for employees
  13. // using dynamically allocated linked lists.
  14. //
  15. //********************************************************
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20. #include <stdlib.h>
  21.  
  22. #define STD_HOURS 40.0
  23. #define OT_RATE 1.5
  24. #define MA_TAX_RATE 0.05
  25. #define NH_TAX_RATE 0.0
  26. #define VT_TAX_RATE 0.06
  27. #define CA_TAX_RATE 0.07
  28. #define DEFAULT_TAX_RATE 0.08
  29. #define FED_TAX_RATE 0.25
  30. #define FIRST_NAME_SIZE 10
  31. #define LAST_NAME_SIZE 10
  32.  
  33. struct name {
  34. char firstName[FIRST_NAME_SIZE];
  35. char lastName[LAST_NAME_SIZE];
  36. };
  37.  
  38. struct employee {
  39. struct name empName;
  40. char taxState[3];
  41. long int clockNumber;
  42. float wageRate;
  43. float hours;
  44. float overtimeHrs;
  45. float grossPay;
  46. float stateTax;
  47. float fedTax;
  48. float netPay;
  49. struct employee *next;
  50. };
  51.  
  52. struct totals {
  53. float total_wageRate;
  54. float total_hours;
  55. float total_overtimeHrs;
  56. float total_grossPay;
  57. float total_stateTax;
  58. float total_fedTax;
  59. float total_netPay;
  60. };
  61.  
  62. struct min_max {
  63. float min_wageRate, max_wageRate;
  64. float min_hours, max_hours;
  65. float min_overtimeHrs, max_overtimeHrs;
  66. float min_grossPay, max_grossPay;
  67. float min_stateTax, max_stateTax;
  68. float min_fedTax, max_fedTax;
  69. float min_netPay, max_netPay;
  70. };
  71.  
  72. struct employee *getEmpData(void);
  73. void calcOvertimeHrs(struct employee *head_ptr);
  74. void calcGrossPay(struct employee *head_ptr);
  75. void calcStateTax(struct employee *head_ptr);
  76. void calcFedTax(struct employee *head_ptr);
  77. void calcNetPay(struct employee *head_ptr);
  78. void calcEmployeeTotals(struct employee *head_ptr, struct totals *emp_totals_ptr);
  79. void calcEmployeeMinMax(struct employee *head_ptr, struct min_max *emp_minMax_ptr);
  80. void printHeader(void);
  81. void printEmp(struct employee *head_ptr);
  82. void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr, int theSize);
  83.  
  84. int main() {
  85. struct employee *head_ptr = getEmpData();
  86. struct totals employeeTotals = {0, 0, 0, 0, 0, 0, 0};
  87. struct min_max employeeMinMax = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  88. int theSize = 0;
  89.  
  90. struct employee *temp_ptr = head_ptr;
  91. while (temp_ptr) {
  92. theSize++;
  93. temp_ptr = temp_ptr->next;
  94. }
  95.  
  96. if (theSize == 0) {
  97. printf("\n\n**** No employee data entered. Program exiting. ****\n");
  98. return 0;
  99. }
  100.  
  101. calcOvertimeHrs(head_ptr);
  102. calcGrossPay(head_ptr);
  103. calcStateTax(head_ptr);
  104. calcFedTax(head_ptr);
  105. calcNetPay(head_ptr);
  106.  
  107. calcEmployeeTotals(head_ptr, &employeeTotals);
  108. calcEmployeeMinMax(head_ptr, &employeeMinMax);
  109.  
  110. printHeader();
  111. printEmp(head_ptr);
  112. printEmpStatistics(&employeeTotals, &employeeMinMax, theSize);
  113.  
  114. printf("\n\n *** End of Program *** \n");
  115. return 0;
  116. }
  117.  
  118. struct employee *getEmpData(void) {
  119. struct employee *head_ptr = NULL, *current_ptr = NULL;
  120. char answer[10];
  121.  
  122. while (1) {
  123. struct employee *new_node = malloc(sizeof(struct employee));
  124. if (!new_node) {
  125. printf("Memory allocation failed!\n");
  126. exit(1);
  127. }
  128.  
  129. printf("\nEnter employee first name: ");
  130. scanf("%s", new_node->empName.firstName);
  131. printf("Enter employee last name: ");
  132. scanf("%s", new_node->empName.lastName);
  133. printf("Enter employee two-character tax state: ");
  134. scanf("%s", new_node->taxState);
  135. printf("Enter employee clock number: ");
  136. scanf("%ld", &new_node->clockNumber);
  137. printf("Enter employee hourly wage: ");
  138. scanf("%f", &new_node->wageRate);
  139. printf("Enter hours worked this week: ");
  140. scanf("%f", &new_node->hours);
  141.  
  142. new_node->next = NULL;
  143.  
  144. if (!head_ptr) {
  145. head_ptr = new_node;
  146. } else {
  147. current_ptr->next = new_node;
  148. }
  149. current_ptr = new_node;
  150.  
  151. printf("Add another employee? (y/n): ");
  152. scanf("%s", answer);
  153. if (tolower(answer[0]) != 'y') {
  154. break;
  155. }
  156. }
  157.  
  158. return head_ptr;
  159. }
  160.  
  161. void calcOvertimeHrs(struct employee *head_ptr) {
  162. for (struct employee *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  163. current_ptr->overtimeHrs = (current_ptr->hours > STD_HOURS) ? current_ptr->hours - STD_HOURS : 0;
  164. }
  165. }
  166.  
  167. void calcGrossPay(struct employee *head_ptr) {
  168. for (struct employee *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  169. float regularPay = (current_ptr->hours - current_ptr->overtimeHrs) * current_ptr->wageRate;
  170. float overtimePay = current_ptr->overtimeHrs * OT_RATE * current_ptr->wageRate;
  171. current_ptr->grossPay = regularPay + overtimePay;
  172. }
  173. }
  174.  
  175. void calcStateTax(struct employee *head_ptr) {
  176. for (struct employee *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  177. if (strcmp(current_ptr->taxState, "MA") == 0)
  178. current_ptr->stateTax = MA_TAX_RATE * current_ptr->grossPay;
  179. else if (strcmp(current_ptr->taxState, "NH") == 0)
  180. current_ptr->stateTax = NH_TAX_RATE * current_ptr->grossPay;
  181. else if (strcmp(current_ptr->taxState, "VT") == 0)
  182. current_ptr->stateTax = VT_TAX_RATE * current_ptr->grossPay;
  183. else if (strcmp(current_ptr->taxState, "CA") == 0)
  184. current_ptr->stateTax = CA_TAX_RATE * current_ptr->grossPay;
  185. else
  186. current_ptr->stateTax = DEFAULT_TAX_RATE * current_ptr->grossPay;
  187. }
  188. }
  189.  
  190. void calcFedTax(struct employee *head_ptr) {
  191. for (struct employee *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  192. current_ptr->fedTax = FED_TAX_RATE * current_ptr->grossPay;
  193. }
  194. }
  195.  
  196. void calcNetPay(struct employee *head_ptr) {
  197. for (struct employee *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  198. current_ptr->netPay = current_ptr->grossPay - (current_ptr->stateTax + current_ptr->fedTax);
  199. }
  200. }
  201.  
  202. void calcEmployeeTotals(struct employee *head_ptr, struct totals *emp_totals_ptr) {
  203. for (struct employee *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  204. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  205. emp_totals_ptr->total_hours += current_ptr->hours;
  206. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  207. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  208. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  209. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  210. emp_totals_ptr->total_netPay += current_ptr->netPay;
  211. }
  212. }
  213.  
  214. void calcEmployeeMinMax(struct employee *head_ptr, struct min_max *emp_minMax_ptr) {
  215. struct employee *current_ptr = head_ptr;
  216.  
  217. emp_minMax_ptr->min_grossPay = emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  218. emp_minMax_ptr->min_stateTax = emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  219. emp_minMax_ptr->min_fedTax = emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  220. emp_minMax_ptr->min_netPay = emp_minMax_ptr->max_netPay = current_ptr->netPay;
  221.  
  222. current_ptr = current_ptr->next;
  223.  
  224. while (current_ptr) {
  225. if (current_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  226. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  227. if (current_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  228. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  229.  
  230. if (current_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  231. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  232. if (current_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  233. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  234.  
  235. if (current_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  236. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  237. if (current_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  238. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  239.  
  240. if (current_ptr->netPay < emp_minMax_ptr->min_netPay)
  241. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  242. if (current_ptr->netPay > emp_minMax_ptr->max_netPay)
  243. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  244.  
  245. current_ptr = current_ptr->next;
  246. }
  247. }
  248.  
  249. void printHeader(void) {
  250. printf("\n\n*** Pay Calculator ***\n");
  251. printf("\n--------------------------------------------------------------");
  252. printf("-------------------");
  253. printf("\nName Tax Clock# Wage Hours OT Gross ");
  254. printf(" State Fed Net");
  255. printf("\n State Pay ");
  256. printf(" Tax Tax Pay");
  257. printf("\n--------------------------------------------------------------");
  258. printf("-------------------");
  259. }
  260.  
  261. void printEmp(struct employee *head_ptr) {
  262. for (struct employee *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  263. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  264. current_ptr->empName.firstName, current_ptr->taxState, current_ptr->clockNumber,
  265. current_ptr->wageRate, current_ptr->hours,
  266. current_ptr->overtimeHrs, current_ptr->grossPay,
  267. current_ptr->stateTax, current_ptr->fedTax,
  268. current_ptr->netPay);
  269. }
  270. }
  271.  
  272. void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr, int theSize) {
  273. printf("\n--------------------------------------------------------------");
  274. printf("-------------------");
  275. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  276. emp_totals_ptr->total_wageRate,
  277. emp_totals_ptr->total_hours,
  278. emp_totals_ptr->total_overtimeHrs,
  279. emp_totals_ptr->total_grossPay,
  280. emp_totals_ptr->total_stateTax,
  281. emp_totals_ptr->total_fedTax,
  282. emp_totals_ptr->total_netPay);
  283.  
  284. if (theSize > 0) {
  285. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  286. emp_totals_ptr->total_wageRate / theSize,
  287. emp_totals_ptr->total_hours / theSize,
  288. emp_totals_ptr->total_overtimeHrs / theSize,
  289. emp_totals_ptr->total_grossPay / theSize,
  290. emp_totals_ptr->total_stateTax / theSize,
  291. emp_totals_ptr->total_fedTax / theSize,
  292. emp_totals_ptr->total_netPay / theSize);
  293. }
  294.  
  295. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  296. emp_minMax_ptr->min_grossPay, emp_minMax_ptr->min_stateTax,
  297. emp_minMax_ptr->min_fedTax, emp_minMax_ptr->min_netPay);
  298.  
  299. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  300. emp_minMax_ptr->max_grossPay, emp_minMax_ptr->max_stateTax,
  301. emp_minMax_ptr->max_fedTax, emp_minMax_ptr->max_netPay);
  302.  
  303. printf("\n\nThe total employees processed was: %i\n", theSize);
  304. }
  305.  
Success #stdin #stdout 0.01s 5280KB
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: 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: 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: 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: 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: Add another employee? (y/n): 

*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net
                   State                           Pay     Tax    Tax      Pay
---------------------------------------------------------------------------------
Connie               MA  098401 10.60  51.0  11.0  598.90  29.95  149.73   419.23
Mary                 NH  526488  9.75  42.5   2.5  426.56   0.00  106.64   319.92
Frank                VT  765349 10.50  37.0   0.0  388.50  23.31   97.12   268.07
Jeff                 NY  034645 12.25  45.0   5.0  581.88  46.55  145.47   389.86
Anton                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:                        334.00   0.0  83.5  227.12   0.00  116.49   324.84
Maximum:                        598.90  46.5 149.7  419.23   0.00  116.49   324.84

The total employees processed was: 5


 *** End of Program ***