fork download
  1. //********************************************************
  2. //
  3. // Assignment 9 - Employee Pay Calculator with Linked Lists
  4. //
  5. // Name: Felix Henriquez
  6. //
  7. // Class: C Programming, Fall 2025
  8. //
  9. // Date: November 16, 2025
  10. //
  11. // Description: Employee pay calculator using dynamically allocated linked lists
  12. // with corrected state and federal tax calculations.
  13. // Modified for ideone.com with batch input.
  14. //
  15. //********************************************************
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20.  
  21. // Constants for array sizes and business rules
  22. #define FIRST_NAME_SIZE 21 // 20 characters + null terminator
  23. #define LAST_NAME_SIZE 31 // 30 characters + null terminator
  24. #define TAX_STATE_SIZE 3 // 2 characters + null terminator
  25. #define STANDARD_WORK_WEEK 40.0
  26. #define OVERTIME_RATE 1.5
  27. #define FED_TAX_RATE 0.25
  28.  
  29. // Structure for employee data
  30. struct employee
  31. {
  32. char firstName[FIRST_NAME_SIZE];
  33. char lastName[LAST_NAME_SIZE];
  34. char taxState[TAX_STATE_SIZE];
  35. long int clockNumber;
  36. float wageRate;
  37. float hours;
  38. float overtimeHrs;
  39. float grossPay;
  40. float stateTax;
  41. float fedTax;
  42. float netPay;
  43. struct employee *next;
  44. };
  45.  
  46. // Structure for summary statistics
  47. struct totals
  48. {
  49. float totalWage;
  50. float totalHours;
  51. float totalOvertimeHrs;
  52. float totalGrossPay;
  53. float totalStateTax;
  54. float totalFedTax;
  55. float totalNetPay;
  56. };
  57.  
  58. // Structure for min and max values
  59. struct min_max
  60. {
  61. float minWage;
  62. float minHours;
  63. float minOvertimeHrs;
  64. float minGrossPay;
  65. float minStateTax;
  66. float minFedTax;
  67. float minNetPay;
  68.  
  69. float maxWage;
  70. float maxHours;
  71. float maxOvertimeHrs;
  72. float maxGrossPay;
  73. float maxStateTax;
  74. float maxFedTax;
  75. float maxNetPay;
  76. };
  77.  
  78. // Function prototypes
  79. void addEmployee(struct employee **head, int employeeNumber);
  80. void deleteList(struct employee **head);
  81. void calcOvertime(struct employee *head);
  82. void calcGrossPay(struct employee *head);
  83. float calcStateTax(float gross, char *state);
  84. float calcFedTax(float gross);
  85. void calcNetPay(struct employee *head);
  86. void calcEmployeeTotals(struct employee *head, struct totals *empTotals);
  87. void calcEmployeeMinMax(struct employee *head, struct min_max *empMinMax);
  88. void printHeader(void);
  89. void printEmployee(struct employee *emp);
  90. void printSummary(struct totals *empTotals, struct min_max *empMinMax, int count);
  91.  
  92. /*****************************************************************************
  93.  * Function: main
  94.  *
  95.  * Purpose: Program entry point - manages employee data using linked lists
  96.  *
  97.  * Parameters: none
  98.  *
  99.  * Returns: int - program exit status
  100.  *****************************************************************************/
  101. int main()
  102. {
  103. struct employee *head = NULL; // head of linked list
  104. struct totals employeeTotals; // structure for totals
  105. struct min_max employeeMinMax; // structure for min/max values
  106. int employeeCount = 5; // we know we have exactly 5 employees
  107.  
  108. // Add all 5 employees from batch input
  109. int i;
  110. for(i = 0; i < 5; i++) {
  111. addEmployee(&head, i+1);
  112. }
  113.  
  114. // Calculate payroll values
  115. calcOvertime(head);
  116. calcGrossPay(head);
  117. calcNetPay(head);
  118.  
  119. // Calculate summary statistics
  120. calcEmployeeTotals(head, &employeeTotals);
  121. calcEmployeeMinMax(head, &employeeMinMax);
  122.  
  123. // Display report
  124. printHeader();
  125.  
  126. struct employee *current = head;
  127. while (current != NULL) {
  128. printEmployee(current);
  129. current = current->next;
  130. }
  131.  
  132. printSummary(&employeeTotals, &employeeMinMax, employeeCount);
  133.  
  134. // Clean up memory
  135. deleteList(&head);
  136.  
  137. return 0;
  138. }
  139.  
  140. /*****************************************************************************
  141.  * Function: addEmployee
  142.  *
  143.  * Purpose: Adds a new employee to the linked list from batch input
  144.  *
  145.  * Parameters: head - pointer to head of linked list
  146.  * employeeNumber - which employee we're adding (for debugging)
  147.  *
  148.  * Returns: void
  149.  *****************************************************************************/
  150. void addEmployee(struct employee **head, int employeeNumber)
  151. {
  152. struct employee *newEmployee = (struct employee *)malloc(sizeof(struct employee));
  153. struct employee *current;
  154.  
  155. if (newEmployee == NULL) {
  156. printf("Memory allocation failed!\n");
  157. exit(1);
  158. }
  159.  
  160. // Read employee data from separate lines (ideone format)
  161. // Each field is on its own line as specified in the assignment
  162. if (scanf("%20s", newEmployee->firstName) != 1) {
  163. printf("Error reading first name for employee %d\n", employeeNumber);
  164. free(newEmployee);
  165. exit(1);
  166. }
  167. if (scanf("%30s", newEmployee->lastName) != 1) {
  168. printf("Error reading last name for employee %d\n", employeeNumber);
  169. free(newEmployee);
  170. exit(1);
  171. }
  172. if (scanf("%2s", newEmployee->taxState) != 1) {
  173. printf("Error reading tax state for employee %d\n", employeeNumber);
  174. free(newEmployee);
  175. exit(1);
  176. }
  177. if (scanf("%ld", &newEmployee->clockNumber) != 1) {
  178. printf("Error reading clock number for employee %d\n", employeeNumber);
  179. free(newEmployee);
  180. exit(1);
  181. }
  182. if (scanf("%f", &newEmployee->wageRate) != 1) {
  183. printf("Error reading wage rate for employee %d\n", employeeNumber);
  184. free(newEmployee);
  185. exit(1);
  186. }
  187. if (scanf("%f", &newEmployee->hours) != 1) {
  188. printf("Error reading hours for employee %d\n", employeeNumber);
  189. free(newEmployee);
  190. exit(1);
  191. }
  192.  
  193. // Read the Y/N character but don't use it (we know we have exactly 5 employees)
  194. char choice;
  195. if (scanf(" %c", &choice) != 1) {
  196. // If we can't read a character on the last one, that's OK
  197. }
  198.  
  199. // Initialize calculated fields
  200. newEmployee->overtimeHrs = 0.0;
  201. newEmployee->grossPay = 0.0;
  202. newEmployee->stateTax = 0.0;
  203. newEmployee->fedTax = 0.0;
  204. newEmployee->netPay = 0.0;
  205. newEmployee->next = NULL;
  206.  
  207. // Add to linked list
  208. if (*head == NULL) {
  209. *head = newEmployee;
  210. } else {
  211. current = *head;
  212. while (current->next != NULL) {
  213. current = current->next;
  214. }
  215. current->next = newEmployee;
  216. }
  217. }
  218.  
  219. /*****************************************************************************
  220.  * Function: deleteList
  221.  *
  222.  * Purpose: Deletes all employees from the linked list and frees memory
  223.  *
  224.  * Parameters: head - pointer to head of linked list
  225.  *
  226.  * Returns: void
  227.  *****************************************************************************/
  228. void deleteList(struct employee **head)
  229. {
  230. struct employee *current = *head;
  231. struct employee *next;
  232.  
  233. while (current != NULL) {
  234. next = current->next;
  235. free(current);
  236. current = next;
  237. }
  238.  
  239. *head = NULL;
  240. }
  241.  
  242. /*****************************************************************************
  243.  * Function: calcOvertime
  244.  *
  245.  * Purpose: Calculates overtime hours for each employee
  246.  *
  247.  * Parameters: head - pointer to head of linked list
  248.  *
  249.  * Returns: void
  250.  *****************************************************************************/
  251. void calcOvertime(struct employee *head)
  252. {
  253. struct employee *current = head;
  254.  
  255. while (current != NULL) {
  256. if (current->hours > STANDARD_WORK_WEEK) {
  257. current->overtimeHrs = current->hours - STANDARD_WORK_WEEK;
  258. } else {
  259. current->overtimeHrs = 0.0;
  260. }
  261. current = current->next;
  262. }
  263. }
  264.  
  265. /*****************************************************************************
  266.  * Function: calcGrossPay
  267.  *
  268.  * Purpose: Calculates gross pay including overtime at time-and-a-half
  269.  *
  270.  * Parameters: head - pointer to head of linked list
  271.  *
  272.  * Returns: void
  273.  *****************************************************************************/
  274. void calcGrossPay(struct employee *head)
  275. {
  276. struct employee *current = head;
  277. float regularHours;
  278. float basePay;
  279. float overtimePay;
  280.  
  281. while (current != NULL) {
  282. regularHours = current->hours;
  283. if (regularHours > STANDARD_WORK_WEEK) {
  284. regularHours = STANDARD_WORK_WEEK;
  285. }
  286.  
  287. basePay = regularHours * current->wageRate;
  288. overtimePay = current->overtimeHrs * current->wageRate * OVERTIME_RATE;
  289. current->grossPay = basePay + overtimePay;
  290.  
  291. current = current->next;
  292. }
  293. }
  294.  
  295. /*****************************************************************************
  296.  * Function: calcStateTax
  297.  *
  298.  * Purpose: Calculates state tax based on gross pay and state code
  299.  *
  300.  * Parameters: gross - gross pay amount
  301.  * state - state code string
  302.  *
  303.  * Returns: float - state tax amount
  304.  *****************************************************************************/
  305. float calcStateTax(float gross, char *state)
  306. {
  307. if (strcmp(state, "MA") == 0) return gross * 0.05; // Massachusetts: 5%
  308. if (strcmp(state, "NH") == 0) return 0.00; // New Hampshire: 0%
  309. if (strcmp(state, "VT") == 0) return gross * 0.06; // Vermont: 6%
  310. if (strcmp(state, "CA") == 0) return gross * 0.07; // California: 7%
  311. return gross * 0.08; // All other states: 8%
  312. }
  313.  
  314. /*****************************************************************************
  315.  * Function: calcFedTax
  316.  *
  317.  * Purpose: Calculates federal tax based on gross pay
  318.  *
  319.  * Parameters: gross - gross pay amount
  320.  *
  321.  * Returns: float - federal tax amount
  322.  *****************************************************************************/
  323. float calcFedTax(float gross)
  324. {
  325. return gross * FED_TAX_RATE; // Federal tax rate of 25%
  326. }
  327.  
  328. /*****************************************************************************
  329.  * Function: calcNetPay
  330.  *
  331.  * Purpose: Calculates net pay for each employee
  332.  *
  333.  * Parameters: head - pointer to head of linked list
  334.  *
  335.  * Returns: void
  336.  *****************************************************************************/
  337. void calcNetPay(struct employee *head)
  338. {
  339. struct employee *current = head;
  340.  
  341. while (current != NULL) {
  342. // Calculate taxes using the tax functions
  343. current->stateTax = calcStateTax(current->grossPay, current->taxState);
  344. current->fedTax = calcFedTax(current->grossPay);
  345.  
  346. // Calculate net pay
  347. current->netPay = current->grossPay - current->stateTax - current->fedTax;
  348.  
  349. current = current->next;
  350. }
  351. }
  352.  
  353. /*****************************************************************************
  354.  * Function: calcEmployeeTotals
  355.  *
  356.  * Purpose: Calculates total values for all employees
  357.  *
  358.  * Parameters: head - pointer to head of linked list
  359.  * empTotals - pointer to totals structure to store results
  360.  *
  361.  * Returns: void
  362.  *****************************************************************************/
  363. void calcEmployeeTotals(struct employee *head, struct totals *empTotals)
  364. {
  365. struct employee *current = head;
  366.  
  367. // Initialize totals to zero
  368. empTotals->totalWage = 0;
  369. empTotals->totalHours = 0;
  370. empTotals->totalOvertimeHrs = 0;
  371. empTotals->totalGrossPay = 0;
  372. empTotals->totalStateTax = 0;
  373. empTotals->totalFedTax = 0;
  374. empTotals->totalNetPay = 0;
  375.  
  376. // Calculate totals
  377. while (current != NULL) {
  378. empTotals->totalWage += current->wageRate;
  379. empTotals->totalHours += current->hours;
  380. empTotals->totalOvertimeHrs += current->overtimeHrs;
  381. empTotals->totalGrossPay += current->grossPay;
  382. empTotals->totalStateTax += current->stateTax;
  383. empTotals->totalFedTax += current->fedTax;
  384. empTotals->totalNetPay += current->netPay;
  385.  
  386. current = current->next;
  387. }
  388. }
  389.  
  390. /*****************************************************************************
  391.  * Function: calcEmployeeMinMax
  392.  *
  393.  * Purpose: Calculates minimum and maximum values for all employees
  394.  *
  395.  * Parameters: head - pointer to head of linked list
  396.  * empMinMax - pointer to min_max structure to store results
  397.  *
  398.  * Returns: void
  399.  *****************************************************************************/
  400. void calcEmployeeMinMax(struct employee *head, struct min_max *empMinMax)
  401. {
  402. struct employee *current = head;
  403.  
  404. if (current == NULL) {
  405. return; // No employees
  406. }
  407.  
  408. // Initialize with first employee's values
  409. empMinMax->minWage = current->wageRate;
  410. empMinMax->minHours = current->hours;
  411. empMinMax->minOvertimeHrs = current->overtimeHrs;
  412. empMinMax->minGrossPay = current->grossPay;
  413. empMinMax->minStateTax = current->stateTax;
  414. empMinMax->minFedTax = current->fedTax;
  415. empMinMax->minNetPay = current->netPay;
  416.  
  417. empMinMax->maxWage = current->wageRate;
  418. empMinMax->maxHours = current->hours;
  419. empMinMax->maxOvertimeHrs = current->overtimeHrs;
  420. empMinMax->maxGrossPay = current->grossPay;
  421. empMinMax->maxStateTax = current->stateTax;
  422. empMinMax->maxFedTax = current->fedTax;
  423. empMinMax->maxNetPay = current->netPay;
  424.  
  425. // Process remaining employees
  426. current = current->next;
  427. while (current != NULL) {
  428. // Update minimum values
  429. if (current->wageRate < empMinMax->minWage) empMinMax->minWage = current->wageRate;
  430. if (current->hours < empMinMax->minHours) empMinMax->minHours = current->hours;
  431. if (current->overtimeHrs < empMinMax->minOvertimeHrs) empMinMax->minOvertimeHrs = current->overtimeHrs;
  432. if (current->grossPay < empMinMax->minGrossPay) empMinMax->minGrossPay = current->grossPay;
  433. if (current->stateTax < empMinMax->minStateTax) empMinMax->minStateTax = current->stateTax;
  434. if (current->fedTax < empMinMax->minFedTax) empMinMax->minFedTax = current->fedTax;
  435. if (current->netPay < empMinMax->minNetPay) empMinMax->minNetPay = current->netPay;
  436.  
  437. // Update maximum values
  438. if (current->wageRate > empMinMax->maxWage) empMinMax->maxWage = current->wageRate;
  439. if (current->hours > empMinMax->maxHours) empMinMax->maxHours = current->hours;
  440. if (current->overtimeHrs > empMinMax->maxOvertimeHrs) empMinMax->maxOvertimeHrs = current->overtimeHrs;
  441. if (current->grossPay > empMinMax->maxGrossPay) empMinMax->maxGrossPay = current->grossPay;
  442. if (current->stateTax > empMinMax->maxStateTax) empMinMax->maxStateTax = current->stateTax;
  443. if (current->fedTax > empMinMax->maxFedTax) empMinMax->maxFedTax = current->fedTax;
  444. if (current->netPay > empMinMax->maxNetPay) empMinMax->maxNetPay = current->netPay;
  445.  
  446. current = current->next;
  447. }
  448. }
  449.  
  450. /*****************************************************************************
  451.  * Function: printHeader
  452.  *
  453.  * Purpose: Prints the report header
  454.  *
  455.  * Parameters: none
  456.  *
  457.  * Returns: void
  458.  *****************************************************************************/
  459. void printHeader(void)
  460. {
  461. printf("\n*** Pay Calculator ***\n");
  462. printf("---------------------------------------------------------------------------------\n");
  463. printf("Name Tax Clock# Wage Hours OT Gross State Fed Net\n");
  464. printf(" State Pay Tax Tax Pay\n");
  465. printf("---------------------------------------------------------------------------------\n");
  466. }
  467.  
  468. /*****************************************************************************
  469.  * Function: printEmployee
  470.  *
  471.  * Purpose: Prints data for a single employee
  472.  *
  473.  * Parameters: emp - pointer to employee structure
  474.  *
  475.  * Returns: void
  476.  *****************************************************************************/
  477. void printEmployee(struct employee *emp)
  478. {
  479. char fullName[52]; // First + last name + space
  480.  
  481. sprintf(fullName, "%s %s", emp->firstName, emp->lastName);
  482.  
  483. printf("%-18s %-3s %06ld %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  484. fullName,
  485. emp->taxState,
  486. emp->clockNumber,
  487. emp->wageRate,
  488. emp->hours,
  489. emp->overtimeHrs,
  490. emp->grossPay,
  491. emp->stateTax,
  492. emp->fedTax,
  493. emp->netPay);
  494. }
  495.  
  496. /*****************************************************************************
  497.  * Function: printSummary
  498.  *
  499.  * Purpose: Prints summary statistics
  500.  *
  501.  * Parameters: empTotals - pointer to totals structure
  502.  * empMinMax - pointer to min_max structure
  503.  * count - number of employees
  504.  *
  505.  * Returns: void
  506.  *****************************************************************************/
  507. void printSummary(struct totals *empTotals, struct min_max *empMinMax, int count)
  508. {
  509. printf("---------------------------------------------------------------------------------\n");
  510. printf("Totals: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  511. empTotals->totalWage, empTotals->totalHours, empTotals->totalOvertimeHrs,
  512. empTotals->totalGrossPay, empTotals->totalStateTax, empTotals->totalFedTax, empTotals->totalNetPay);
  513.  
  514. if (count > 0) {
  515. printf("Averages: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  516. empTotals->totalWage / count, empTotals->totalHours / count, empTotals->totalOvertimeHrs / count,
  517. empTotals->totalGrossPay / count, empTotals->totalStateTax / count, empTotals->totalFedTax / count, empTotals->totalNetPay / count);
  518. }
  519.  
  520. printf("Minimum: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  521. empMinMax->minWage, empMinMax->minHours, empMinMax->minOvertimeHrs,
  522. empMinMax->minGrossPay, empMinMax->minStateTax, empMinMax->minFedTax, empMinMax->minNetPay);
  523. printf("Maximum: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  524. empMinMax->maxWage, empMinMax->maxHours, empMinMax->maxOvertimeHrs,
  525. empMinMax->maxGrossPay, empMinMax->maxStateTax, empMinMax->maxFedTax, empMinMax->maxNetPay);
  526. }
Success #stdin #stdout 0s 5308KB
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
*** 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