fork download
  1. //********************************************************
  2. //
  3. // Assignment 9 - Linked Lists
  4. //
  5. // Name: Anthony Principe
  6. //
  7. // Class: C Programming Fall 2025
  8. //
  9. // Date: 11/16/25
  10. //
  11. // Description: Program that figures out overtime and
  12. // gross pay for each employee and prints the results.
  13. // It also figures out state tax, federal tax, and net
  14. // pay. At the end it prints totals, averages, and the
  15. // min and max values.
  16. //
  17. // This version uses a linked list so the program can
  18. // handle any number of employees. Each employee is
  19. // stored in a node created with malloc.
  20. //
  21. //********************************************************
  22.  
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <ctype.h>
  26. #include <stdlib.h>
  27.  
  28. // set up common constants
  29. #define STD_HOURS 40.0
  30. #define OT_RATE 1.5
  31. #define MA_TAX_RATE 0.05
  32. #define NH_TAX_RATE 0.0
  33. #define VT_TAX_RATE 0.06
  34. #define CA_TAX_RATE 0.07
  35. #define DEFAULT_TAX_RATE 0.08
  36. #define FIRST_NAME_SIZE 10
  37. #define LAST_NAME_SIZE 10
  38. #define TAX_STATE_SIZE 3
  39. #define FED_TAX_RATE 0.25
  40.  
  41. // holds the first and last name
  42. struct name
  43. {
  44. char firstName[FIRST_NAME_SIZE];
  45. char lastName [LAST_NAME_SIZE];
  46. };
  47.  
  48. // holds all employee info + pointer to next node
  49. struct employee
  50. {
  51. struct name empName;
  52. char taxState[TAX_STATE_SIZE];
  53. long int clockNumber;
  54. float wageRate;
  55. float hours;
  56. float overtimeHrs;
  57. float grossPay;
  58. float stateTax;
  59. float fedTax;
  60. float netPay;
  61.  
  62. struct employee * next; // next node in the list
  63. };
  64.  
  65. // used to add totals together
  66. struct totals
  67. {
  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. };
  76.  
  77. // used to track min and max values
  78. struct min_max
  79. {
  80. float min_wageRate;
  81. float min_hours;
  82. float min_overtimeHrs;
  83. float min_grossPay;
  84. float min_stateTax;
  85. float min_fedTax;
  86. float min_netPay;
  87.  
  88. float max_wageRate;
  89. float max_hours;
  90. float max_overtimeHrs;
  91. float max_grossPay;
  92. float max_stateTax;
  93. float max_fedTax;
  94. float max_netPay;
  95. };
  96.  
  97. // function prototypes
  98. struct employee * getEmpData (void);
  99. int isEmployeeSize (struct employee * head_ptr);
  100. void calcOvertimeHrs (struct employee * head_ptr);
  101. void calcGrossPay (struct employee * head_ptr);
  102. void printHeader (void);
  103. void printEmp (struct employee * head_ptr);
  104. void calcStateTax (struct employee * head_ptr);
  105. void calcFedTax (struct employee * head_ptr);
  106. void calcNetPay (struct employee * head_ptr);
  107. void calcEmployeeTotals (struct employee * head_ptr,
  108. struct totals * emp_totals_ptr);
  109. void calcEmployeeMinMax (struct employee * head_ptr,
  110. struct min_max * emp_minMax_ptr);
  111. void printEmpStatistics (struct totals * emp_totals_ptr,
  112. struct min_max * emp_minMax_ptr,
  113. int theSize);
  114.  
  115. int main ()
  116. {
  117. struct employee * head_ptr;
  118. int theSize;
  119.  
  120. // hold all totals
  121. struct totals employeeTotals = {0,0,0,0,0,0,0};
  122. struct totals * emp_totals_ptr = &employeeTotals;
  123.  
  124. // hold all min and max values
  125. struct min_max employeeMinMax = {0,0,0,0,0,0,0,
  126. 0,0,0,0,0,0,0};
  127. struct min_max * emp_minMax_ptr = &employeeMinMax;
  128.  
  129. // get the linked list of employees
  130. head_ptr = getEmpData();
  131.  
  132. // count how many employees were entered
  133. theSize = isEmployeeSize(head_ptr);
  134.  
  135. if (theSize <= 0)
  136. {
  137. printf("\n\n**** There was no employee input to process ***\n");
  138. }
  139. else
  140. {
  141. // run all calculations
  142. calcOvertimeHrs(head_ptr);
  143. calcGrossPay(head_ptr);
  144. calcStateTax(head_ptr);
  145. calcFedTax(head_ptr);
  146. calcNetPay(head_ptr);
  147.  
  148. calcEmployeeTotals(head_ptr, &employeeTotals);
  149. calcEmployeeMinMax(head_ptr, &employeeMinMax);
  150.  
  151. // print everything
  152. printHeader();
  153. printEmp(head_ptr);
  154. printEmpStatistics(&employeeTotals, &employeeMinMax, theSize);
  155. }
  156.  
  157. printf("\n\n *** End of Program *** \n");
  158.  
  159. return 0;
  160. }
  161.  
  162. //**************************************************************
  163. // Function: getEmpData
  164. //
  165. // Purpose: Gets employee info from the user and builds the
  166. // linked list one node at a time using malloc.
  167. //**************************************************************
  168. struct employee * getEmpData (void)
  169. {
  170. char answer[80];
  171. int more_data = 1;
  172. char value;
  173.  
  174. struct employee *current_ptr, *head_ptr;
  175.  
  176. // make first node
  177. head_ptr = (struct employee *) malloc(sizeof(struct employee));
  178. current_ptr = head_ptr;
  179.  
  180. while (more_data)
  181. {
  182. printf ("\nEnter employee first name: ");
  183. scanf ("%s", current_ptr->empName.firstName);
  184.  
  185. printf ("\nEnter employee last name: ");
  186. scanf ("%s", current_ptr->empName.lastName);
  187.  
  188. printf ("\nEnter employee two character tax state: ");
  189. scanf ("%s", current_ptr->taxState);
  190.  
  191. printf("\nEnter employee clock number: ");
  192. scanf("%li", & current_ptr->clockNumber);
  193.  
  194. printf("\nEnter employee hourly wage: ");
  195. scanf("%f", & current_ptr->wageRate);
  196.  
  197. printf("\nEnter hours worked this week: ");
  198. scanf("%f", & current_ptr->hours);
  199.  
  200. printf("\nWould you like to add another employee? (y/n): ");
  201. scanf("%s", answer);
  202.  
  203. // check if user wants another employee
  204. value = toupper(answer[0]);
  205. if (value != 'Y')
  206. {
  207. current_ptr->next = NULL;
  208. more_data = 0;
  209. }
  210. else
  211. {
  212. // make next node
  213. current_ptr->next =
  214. (struct employee *) malloc(sizeof(struct employee));
  215.  
  216. current_ptr = current_ptr->next;
  217. }
  218. }
  219.  
  220. return head_ptr;
  221.  
  222. } // getEmpData
  223.  
  224. //**************************************************************
  225. // Function: isEmployeeSize
  226. //
  227. // Purpose: Counts how many employees are in the list.
  228. //**************************************************************
  229. int isEmployeeSize (struct employee * head_ptr)
  230. {
  231. struct employee * current_ptr;
  232. int theSize = 0;
  233.  
  234. if (head_ptr->empName.firstName[0] != '\0')
  235. {
  236. for (current_ptr = head_ptr;
  237. current_ptr;
  238. current_ptr = current_ptr->next)
  239. {
  240. ++theSize;
  241. }
  242. }
  243. return theSize;
  244.  
  245. } // isEmployeeSize
  246.  
  247. //**************************************************************
  248. // Function: printHeader
  249. //
  250. // Purpose: Prints all the column labels.
  251. //**************************************************************
  252. void printHeader (void)
  253. {
  254. printf ("\n\n*** Pay Calculator ***\n");
  255.  
  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. } // printHeader
  266.  
  267. //**************************************************************
  268. // Function: printEmp
  269. //
  270. // Purpose: Prints one row for each employee.
  271. //**************************************************************
  272. void printEmp (struct employee * head_ptr)
  273. {
  274. char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  275. struct employee * current_ptr;
  276.  
  277. for (current_ptr = head_ptr;
  278. current_ptr;
  279. current_ptr = current_ptr->next)
  280. {
  281. // build full name
  282. strcpy (name, current_ptr->empName.firstName);
  283. strcat (name, " ");
  284. strcat (name, current_ptr->empName.lastName);
  285.  
  286. // print one row
  287. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f "
  288. "%7.2f %6.2f %7.2f %8.2f",
  289. name, current_ptr->taxState, current_ptr->clockNumber,
  290. current_ptr->wageRate, current_ptr->hours,
  291. current_ptr->overtimeHrs, current_ptr->grossPay,
  292. current_ptr->stateTax, current_ptr->fedTax,
  293. current_ptr->netPay);
  294. }
  295.  
  296. } // printEmp
  297.  
  298. //**************************************************************
  299. // Function: calcOvertimeHrs
  300. //
  301. // Purpose: Figures out how many overtime hours were worked.
  302. //**************************************************************
  303. void calcOvertimeHrs (struct employee * head_ptr)
  304. {
  305. struct employee * current_ptr;
  306.  
  307. for (current_ptr = head_ptr;
  308. current_ptr;
  309. current_ptr = current_ptr->next)
  310. {
  311. if (current_ptr->hours > STD_HOURS)
  312. current_ptr->overtimeHrs = current_ptr->hours - STD_HOURS;
  313. else
  314. current_ptr->overtimeHrs = 0;
  315. }
  316.  
  317. } // calcOvertimeHrs
  318.  
  319. //**************************************************************
  320. // Function: calcGrossPay
  321. //
  322. // Purpose: Figures out the normal pay + overtime pay.
  323. //**************************************************************
  324. void calcGrossPay (struct employee * head_ptr)
  325. {
  326. float theNormalPay;
  327. float theOvertimePay;
  328.  
  329. struct employee * current_ptr;
  330.  
  331. for (current_ptr = head_ptr;
  332. current_ptr;
  333. current_ptr = current_ptr->next)
  334. {
  335. theNormalPay =
  336. current_ptr->wageRate *
  337. (current_ptr->hours - current_ptr->overtimeHrs);
  338.  
  339. theOvertimePay =
  340. current_ptr->overtimeHrs *
  341. (OT_RATE * current_ptr->wageRate);
  342.  
  343. current_ptr->grossPay = theNormalPay + theOvertimePay;
  344. }
  345.  
  346. } // calcGrossPay
  347.  
  348. //**************************************************************
  349. // Function: calcStateTax
  350. //
  351. // Purpose: Calculates state tax using the employee's state code.
  352. //**************************************************************
  353. void calcStateTax (struct employee * head_ptr)
  354. {
  355. struct employee * current_ptr;
  356.  
  357. for (current_ptr = head_ptr;
  358. current_ptr;
  359. current_ptr = current_ptr->next)
  360. {
  361. // convert to uppercase if needed
  362. current_ptr->taxState[0] = toupper(current_ptr->taxState[0]);
  363. current_ptr->taxState[1] = toupper(current_ptr->taxState[1]);
  364.  
  365. if (strcmp(current_ptr->taxState, "MA") == 0)
  366. current_ptr->stateTax = current_ptr->grossPay * MA_TAX_RATE;
  367. else if (strcmp(current_ptr->taxState, "NH") == 0)
  368. current_ptr->stateTax = current_ptr->grossPay * NH_TAX_RATE;
  369. else if (strcmp(current_ptr->taxState, "VT") == 0)
  370. current_ptr->stateTax = current_ptr->grossPay * VT_TAX_RATE;
  371. else if (strcmp(current_ptr->taxState, "CA") == 0)
  372. current_ptr->stateTax = current_ptr->grossPay * CA_TAX_RATE;
  373. else
  374. current_ptr->stateTax = current_ptr->grossPay * DEFAULT_TAX_RATE;
  375. }
  376.  
  377. } // calcStateTax
  378.  
  379. //**************************************************************
  380. // Function: calcFedTax
  381. //
  382. // Purpose: Federal tax is always 25% of gross pay.
  383. //**************************************************************
  384. void calcFedTax (struct employee * head_ptr)
  385. {
  386. struct employee * current_ptr;
  387.  
  388. for (current_ptr = head_ptr;
  389. current_ptr;
  390. current_ptr = current_ptr->next)
  391. {
  392. current_ptr->fedTax = current_ptr->grossPay * FED_TAX_RATE;
  393. }
  394.  
  395. } // calcFedTax
  396.  
  397. //**************************************************************
  398. // Function: calcNetPay
  399. //
  400. // Purpose: Net pay = gross pay minus taxes.
  401. //**************************************************************
  402. void calcNetPay (struct employee * head_ptr)
  403. {
  404. float theTotalTaxes;
  405. struct employee * current_ptr;
  406.  
  407. for (current_ptr = head_ptr;
  408. current_ptr;
  409. current_ptr = current_ptr->next)
  410. {
  411. theTotalTaxes = current_ptr->stateTax + current_ptr->fedTax;
  412. current_ptr->netPay = current_ptr->grossPay - theTotalTaxes;
  413. }
  414.  
  415. } // calcNetPay
  416.  
  417. //**************************************************************
  418. // Function: calcEmployeeTotals
  419. //
  420. // Purpose: Adds up all values to make a totals row.
  421. //**************************************************************
  422. void calcEmployeeTotals (struct employee * head_ptr,
  423. struct totals * emp_totals_ptr)
  424. {
  425. struct employee * current_ptr;
  426.  
  427. for (current_ptr = head_ptr;
  428. current_ptr;
  429. current_ptr = current_ptr->next)
  430. {
  431. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  432. emp_totals_ptr->total_hours += current_ptr->hours;
  433. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  434. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  435. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  436. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  437. emp_totals_ptr->total_netPay += current_ptr->netPay;
  438. }
  439.  
  440. } // calcEmployeeTotals
  441.  
  442. //**************************************************************
  443. // Function: calcEmployeeMinMax
  444. //
  445. // Purpose: Finds the lowest and highest numbers for each field.
  446. //**************************************************************
  447. void calcEmployeeMinMax (struct employee * head_ptr,
  448. struct min_max * emp_minMax_ptr)
  449. {
  450. struct employee * current_ptr;
  451.  
  452. // start with first employee
  453. current_ptr = head_ptr;
  454.  
  455. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  456. emp_minMax_ptr->min_hours = current_ptr->hours;
  457. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  458. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  459. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  460. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  461. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  462.  
  463. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  464. emp_minMax_ptr->max_hours = current_ptr->hours;
  465. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  466. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  467. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  468. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  469. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  470.  
  471. // move to the next employee
  472. current_ptr = current_ptr->next;
  473.  
  474. // compare the rest of the employees
  475. for (; current_ptr; current_ptr = current_ptr->next)
  476. {
  477. if (current_ptr->wageRate < emp_minMax_ptr->min_wageRate)
  478. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  479. if (current_ptr->wageRate > emp_minMax_ptr->max_wageRate)
  480. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  481.  
  482. if (current_ptr->hours < emp_minMax_ptr->min_hours)
  483. emp_minMax_ptr->min_hours = current_ptr->hours;
  484. if (current_ptr->hours > emp_minMax_ptr->max_hours)
  485. emp_minMax_ptr->max_hours = current_ptr->hours;
  486.  
  487. if (current_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
  488. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  489. if (current_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
  490. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  491.  
  492. if (current_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  493. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  494. if (current_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  495. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  496.  
  497. if (current_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  498. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  499. if (current_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  500. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  501.  
  502. if (current_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  503. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  504. if (current_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  505. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  506.  
  507. if (current_ptr->netPay < emp_minMax_ptr->min_netPay)
  508. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  509. if (current_ptr->netPay > emp_minMax_ptr->max_netPay)
  510. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  511. }
  512.  
  513. } // calcEmployeeMinMax
  514.  
  515. //**************************************************************
  516. // Function: printEmpStatistics
  517. //
  518. // Purpose: Prints totals, averages, and min/max rows.
  519. //
  520. //**************************************************************
  521. void printEmpStatistics (struct totals * emp_totals_ptr,
  522. struct min_max * emp_minMax_ptr,
  523. int theSize)
  524. {
  525. printf("\n--------------------------------------------------------------");
  526. printf("-------------------");
  527.  
  528. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f "
  529. "%6.2f %7.2f %8.2f",
  530. emp_totals_ptr->total_wageRate,
  531. emp_totals_ptr->total_hours,
  532. emp_totals_ptr->total_overtimeHrs,
  533. emp_totals_ptr->total_grossPay,
  534. emp_totals_ptr->total_stateTax,
  535. emp_totals_ptr->total_fedTax,
  536. emp_totals_ptr->total_netPay);
  537.  
  538. if (theSize > 0)
  539. {
  540. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f "
  541. "%6.2f %7.2f %8.2f",
  542. emp_totals_ptr->total_wageRate/theSize,
  543. emp_totals_ptr->total_hours/theSize,
  544. emp_totals_ptr->total_overtimeHrs/theSize,
  545. emp_totals_ptr->total_grossPay/theSize,
  546. emp_totals_ptr->total_stateTax/theSize,
  547. emp_totals_ptr->total_fedTax/theSize,
  548. emp_totals_ptr->total_netPay/theSize);
  549. }
  550.  
  551. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f "
  552. "%6.2f %7.2f %8.2f",
  553. emp_minMax_ptr->min_wageRate,
  554. emp_minMax_ptr->min_hours,
  555. emp_minMax_ptr->min_overtimeHrs,
  556. emp_minMax_ptr->min_grossPay,
  557. emp_minMax_ptr->min_stateTax,
  558. emp_minMax_ptr->min_fedTax,
  559. emp_minMax_ptr->min_netPay);
  560.  
  561. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f "
  562. "%6.2f %7.2f %8.2f",
  563. emp_minMax_ptr->max_wageRate,
  564. emp_minMax_ptr->max_hours,
  565. emp_minMax_ptr->max_overtimeHrs,
  566. emp_minMax_ptr->max_grossPay,
  567. emp_minMax_ptr->max_stateTax,
  568. emp_minMax_ptr->max_fedTax,
  569. emp_minMax_ptr->max_netPay);
  570.  
  571. printf ("\n\nThe total employees processed was: %i\n", theSize);
  572.  
  573. } // printEmpStatistics
  574.  
Success #stdin #stdout 0s 5320KB
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 ***