fork(1) 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 which determines overtime and
  12. // gross pay for a set of employees with outputs sent
  13. // to standard output (the screen).
  14. //
  15. // This assignment also adds the employee name, their tax state,
  16. // and calculates the state tax, federal tax, and net pay. It
  17. // also calculates totals, averages, minimum, and maximum values.
  18. //
  19. // Array and Structure references have all been replaced with
  20. // pointer references to speed up the processing of this code.
  21. // A linked list has been created and deployed to dynamically
  22. // allocate and process employees as needed.
  23. //
  24. // Call by Reference design (using pointers)
  25. //
  26. //********************************************************
  27.  
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <stdlib.h>
  32.  
  33. // define constants
  34. #define STD_HOURS 40.0
  35. #define OT_RATE 1.5
  36. #define MA_TAX_RATE 0.05
  37. #define NH_TAX_RATE 0.0
  38. #define VT_TAX_RATE 0.06
  39. #define CA_TAX_RATE 0.07
  40. #define DEFAULT_TAX_RATE 0.08
  41. #define NAME_SIZE 20
  42. #define TAX_STATE_SIZE 3
  43. #define FED_TAX_RATE 0.25
  44. #define FIRST_NAME_SIZE 10
  45. #define LAST_NAME_SIZE 10
  46.  
  47. // structure for employee name
  48. struct name
  49. {
  50. char firstName[FIRST_NAME_SIZE];
  51. char lastName [LAST_NAME_SIZE];
  52. };
  53.  
  54. // structure for employee data
  55. struct employee
  56. {
  57. struct name empName;
  58. char taxState[TAX_STATE_SIZE];
  59. long int clockNumber;
  60. float wageRate;
  61. float hours;
  62. float overtimeHrs;
  63. float grossPay;
  64. float stateTax;
  65. float fedTax;
  66. float netPay;
  67.  
  68. struct employee * next; // pointer to next node
  69. };
  70.  
  71. // structure for totals
  72. struct totals
  73. {
  74. float total_wageRate;
  75. float total_hours;
  76. float total_overtimeHrs;
  77. float total_grossPay;
  78. float total_stateTax;
  79. float total_fedTax;
  80. float total_netPay;
  81. };
  82.  
  83. // structure for min/max values
  84. struct min_max
  85. {
  86. float min_wageRate;
  87. float min_hours;
  88. float min_overtimeHrs;
  89. float min_grossPay;
  90. float min_stateTax;
  91. float min_fedTax;
  92. float min_netPay;
  93.  
  94. float max_wageRate;
  95. float max_hours;
  96. float max_overtimeHrs;
  97. float max_grossPay;
  98. float max_stateTax;
  99. float max_fedTax;
  100. float max_netPay;
  101. };
  102.  
  103. // function prototypes
  104. struct employee * getEmpData (void);
  105. int isEmployeeSize (struct employee * head_ptr);
  106. void calcOvertimeHrs (struct employee * head_ptr);
  107. void calcGrossPay (struct employee * head_ptr);
  108. void printHeader (void);
  109. void printEmp (struct employee * head_ptr);
  110. void calcStateTax (struct employee * head_ptr);
  111. void calcFedTax (struct employee * head_ptr);
  112. void calcNetPay (struct employee * head_ptr);
  113. void calcEmployeeTotals (struct employee * head_ptr,
  114. struct totals * emp_totals_ptr);
  115. void calcEmployeeMinMax (struct employee * head_ptr,
  116. struct min_max * emp_minMax_ptr);
  117. void printEmpStatistics (struct totals * emp_totals_ptr,
  118. struct min_max * emp_minMax_ptr,
  119. int theSize);
  120.  
  121. int main ()
  122. {
  123. struct employee * head_ptr;
  124. int theSize;
  125.  
  126. struct totals employeeTotals = {0,0,0,0,0,0,0};
  127. struct totals * emp_totals_ptr = &employeeTotals;
  128.  
  129. struct min_max employeeMinMax = {0,0,0,0,0,0,0,
  130. 0,0,0,0,0,0,0};
  131. struct min_max * emp_minMax_ptr = &employeeMinMax;
  132.  
  133. head_ptr = getEmpData();
  134.  
  135. theSize = isEmployeeSize(head_ptr);
  136.  
  137. if (theSize <= 0)
  138. {
  139. printf("\n\n**** There was no employee input to process ***\n");
  140. }
  141. else
  142. {
  143. calcOvertimeHrs(head_ptr);
  144. calcGrossPay(head_ptr);
  145. calcStateTax(head_ptr);
  146. calcFedTax(head_ptr);
  147. calcNetPay(head_ptr);
  148.  
  149. calcEmployeeTotals(head_ptr, &employeeTotals);
  150. calcEmployeeMinMax(head_ptr, &employeeMinMax);
  151.  
  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: Reads employee input and builds a dynamically
  166. // allocated linked list stored in Heap memory.
  167. //
  168. //**************************************************************
  169. struct employee * getEmpData (void)
  170. {
  171. char answer[80];
  172. int more_data = 1;
  173. char value;
  174.  
  175. struct employee *current_ptr, *head_ptr;
  176.  
  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. if ((value = toupper(answer[0])) != 'Y')
  204. {
  205. current_ptr->next = (struct employee *) NULL;
  206. more_data = 0;
  207. }
  208. else
  209. {
  210. current_ptr->next =
  211. (struct employee *) malloc(sizeof(struct employee));
  212. current_ptr = current_ptr->next;
  213. }
  214. }
  215.  
  216. return(head_ptr);
  217.  
  218. } // getEmpData
  219.  
  220. //**************************************************************
  221. // Function: isEmployeeSize
  222. //
  223. // Purpose: Counts the number of nodes in the linked list.
  224. //
  225. //**************************************************************
  226. int isEmployeeSize (struct employee * head_ptr)
  227. {
  228. struct employee * current_ptr;
  229. int theSize = 0;
  230.  
  231. if (head_ptr->empName.firstName[0] != '\0')
  232. {
  233. for (current_ptr = head_ptr; current_ptr;
  234. current_ptr = current_ptr->next)
  235. {
  236. ++theSize;
  237. }
  238. }
  239. return theSize;
  240.  
  241. } // isEmployeeSize
  242.  
  243. //**************************************************************
  244. // Function: printHeader
  245. //
  246. // Purpose: Prints formatted column headers for the output table.
  247. //
  248. //**************************************************************
  249. void printHeader (void)
  250. {
  251. printf ("\n\n*** Pay Calculator ***\n");
  252.  
  253. printf("\n--------------------------------------------------------------");
  254. printf("-------------------");
  255. printf("\nName Tax Clock# Wage Hours OT Gross ");
  256. printf(" State Fed Net");
  257. printf("\n State Pay ");
  258. printf(" Tax Tax Pay");
  259. printf("\n--------------------------------------------------------------");
  260. printf("-------------------");
  261.  
  262. } // printHeader
  263.  
  264. //**************************************************************
  265. // Function: printEmp
  266. //
  267. // Purpose: Prints each employee's information in a formatted row.
  268. //
  269. //**************************************************************
  270. void printEmp (struct employee * head_ptr)
  271. {
  272. char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  273. struct employee * current_ptr;
  274.  
  275. for (current_ptr = head_ptr;
  276. current_ptr;
  277. current_ptr = current_ptr->next)
  278. {
  279. strcpy (name, current_ptr->empName.firstName);
  280. strcat (name, " ");
  281. strcat (name, current_ptr->empName.lastName);
  282.  
  283. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f "
  284. "%7.2f %6.2f %7.2f %8.2f",
  285. name, current_ptr->taxState, current_ptr->clockNumber,
  286. current_ptr->wageRate, current_ptr->hours,
  287. current_ptr->overtimeHrs, current_ptr->grossPay,
  288. current_ptr->stateTax, current_ptr->fedTax,
  289. current_ptr->netPay);
  290. }
  291.  
  292. } // printEmp
  293.  
  294. //**************************************************************
  295. // Function: calcOvertimeHrs
  296. //
  297. // Purpose: Determines overtime hours for each employee.
  298. //
  299. //**************************************************************
  300. void calcOvertimeHrs (struct employee * head_ptr)
  301. {
  302. struct employee * current_ptr;
  303.  
  304. for (current_ptr = head_ptr; current_ptr;
  305. current_ptr = current_ptr->next)
  306. {
  307. if (current_ptr->hours >= STD_HOURS)
  308. current_ptr->overtimeHrs = current_ptr->hours - STD_HOURS;
  309. else
  310. current_ptr->overtimeHrs = 0;
  311. }
  312.  
  313. } // calcOvertimeHrs
  314.  
  315. //**************************************************************
  316. // Function: calcGrossPay
  317. //
  318. // Purpose: Computes gross pay including overtime.
  319. //
  320. //**************************************************************
  321. void calcGrossPay (struct employee * head_ptr)
  322. {
  323. float theNormalPay;
  324. float theOvertimePay;
  325.  
  326. struct employee * current_ptr;
  327.  
  328. for (current_ptr = head_ptr;
  329. current_ptr;
  330. current_ptr = current_ptr->next)
  331. {
  332. theNormalPay =
  333. current_ptr->wageRate *
  334. (current_ptr->hours - current_ptr->overtimeHrs);
  335.  
  336. theOvertimePay =
  337. current_ptr->overtimeHrs *
  338. (OT_RATE * current_ptr->wageRate);
  339.  
  340. current_ptr->grossPay = theNormalPay + theOvertimePay;
  341. }
  342.  
  343. } // calcGrossPay
  344.  
  345. //**************************************************************
  346. // Function: calcStateTax
  347. //
  348. // Purpose: Computes state tax based on gross pay and state code.
  349. //
  350. //**************************************************************
  351. void calcStateTax (struct employee * head_ptr)
  352. {
  353. struct employee * current_ptr;
  354.  
  355. for (current_ptr = head_ptr;
  356. current_ptr;
  357. current_ptr = current_ptr->next)
  358. {
  359. if (islower(current_ptr->taxState[0]))
  360. current_ptr->taxState[0] = toupper(current_ptr->taxState[0]);
  361. if (islower(current_ptr->taxState[1]))
  362. current_ptr->taxState[1] = toupper(current_ptr->taxState[1]);
  363.  
  364. if (strcmp(current_ptr->taxState, "MA") == 0)
  365. current_ptr->stateTax = current_ptr->grossPay * MA_TAX_RATE;
  366. else if (strcmp(current_ptr->taxState, "NH") == 0)
  367. current_ptr->stateTax = current_ptr->grossPay * NH_TAX_RATE;
  368. else if (strcmp(current_ptr->taxState, "VT") == 0)
  369. current_ptr->stateTax = current_ptr->grossPay * VT_TAX_RATE;
  370. else if (strcmp(current_ptr->taxState, "CA") == 0)
  371. current_ptr->stateTax = current_ptr->grossPay * CA_TAX_RATE;
  372. else
  373. current_ptr->stateTax = current_ptr->grossPay * DEFAULT_TAX_RATE;
  374. }
  375.  
  376. } // calcStateTax
  377.  
  378. //**************************************************************
  379. // Function: calcFedTax
  380. //
  381. // Purpose: Calculates federal tax (flat rate 25%).
  382. //
  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: Computes take-home pay after taxes.
  401. //
  402. //**************************************************************
  403. void calcNetPay (struct employee * head_ptr)
  404. {
  405. float theTotalTaxes;
  406. struct employee * current_ptr;
  407.  
  408. for (current_ptr = head_ptr;
  409. current_ptr;
  410. current_ptr = current_ptr->next)
  411. {
  412. theTotalTaxes = current_ptr->stateTax + current_ptr->fedTax;
  413. current_ptr->netPay = current_ptr->grossPay - theTotalTaxes;
  414. }
  415.  
  416. } // calcNetPay
  417.  
  418. //**************************************************************
  419. // Function: calcEmployeeTotals
  420. //
  421. // Purpose: Computes running totals for all floating point fields.
  422. //
  423. //**************************************************************
  424. void calcEmployeeTotals (struct employee * head_ptr,
  425. struct totals * emp_totals_ptr)
  426. {
  427. struct employee * current_ptr;
  428.  
  429. for (current_ptr = head_ptr;
  430. current_ptr;
  431. current_ptr = current_ptr->next)
  432. {
  433. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  434. emp_totals_ptr->total_hours += current_ptr->hours;
  435. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  436. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  437.  
  438. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  439. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  440.  
  441. emp_totals_ptr->total_netPay += current_ptr->netPay;
  442. }
  443.  
  444. } // calcEmployeeTotals
  445.  
  446. //**************************************************************
  447. // Function: calcEmployeeMinMax
  448. //
  449. // Purpose: Tracks min and max values for all floating point items.
  450. //
  451. //**************************************************************
  452. void calcEmployeeMinMax (struct employee * head_ptr,
  453. struct min_max * emp_minMax_ptr)
  454. {
  455. struct employee * current_ptr;
  456.  
  457. current_ptr = head_ptr;
  458.  
  459. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  460. emp_minMax_ptr->min_hours = current_ptr->hours;
  461. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  462. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  463. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  464. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  465. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  466.  
  467. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  468. emp_minMax_ptr->max_hours = current_ptr->hours;
  469. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  470. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  471. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  472. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  473. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  474.  
  475. current_ptr = current_ptr->next;
  476.  
  477. for (; current_ptr; current_ptr = current_ptr->next)
  478. {
  479. if (current_ptr->wageRate < emp_minMax_ptr->min_wageRate)
  480. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  481. if (current_ptr->wageRate > emp_minMax_ptr->max_wageRate)
  482. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  483.  
  484. if (current_ptr->hours < emp_minMax_ptr->min_hours)
  485. emp_minMax_ptr->min_hours = current_ptr->hours;
  486. if (current_ptr->hours > emp_minMax_ptr->max_hours)
  487. emp_minMax_ptr->max_hours = current_ptr->hours;
  488.  
  489. if (current_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
  490. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  491. if (current_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
  492. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  493.  
  494. if (current_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  495. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  496. if (current_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  497. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  498.  
  499. if (current_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  500. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  501. if (current_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  502. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  503.  
  504. if (current_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  505. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  506. if (current_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  507. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  508.  
  509. if (current_ptr->netPay < emp_minMax_ptr->min_netPay)
  510. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  511. if (current_ptr->netPay > emp_minMax_ptr->max_netPay)
  512. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  513. }
  514.  
  515. } // calcEmployeeMinMax
  516.  
  517. //**************************************************************
  518. // Function: printEmpStatistics
  519. //
  520. // Purpose: Prints totals, averages, and min/max values.
  521. //
  522. //**************************************************************
  523. void printEmpStatistics (struct totals * emp_totals_ptr,
  524. struct min_max * emp_minMax_ptr,
  525. int theSize)
  526. {
  527. printf("\n--------------------------------------------------------------");
  528. printf("-------------------");
  529.  
  530. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f "
  531. "%6.2f %7.2f %8.2f",
  532. emp_totals_ptr->total_wageRate,
  533. emp_totals_ptr->total_hours,
  534. emp_totals_ptr->total_overtimeHrs,
  535. emp_totals_ptr->total_grossPay,
  536. emp_totals_ptr->total_stateTax,
  537. emp_totals_ptr->total_fedTax,
  538. emp_totals_ptr->total_netPay);
  539.  
  540. if (theSize > 0)
  541. {
  542. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f "
  543. "%6.2f %7.2f %8.2f",
  544. emp_totals_ptr->total_wageRate/theSize,
  545. emp_totals_ptr->total_hours/theSize,
  546. emp_totals_ptr->total_overtimeHrs/theSize,
  547. emp_totals_ptr->total_grossPay/theSize,
  548. emp_totals_ptr->total_stateTax/theSize,
  549. emp_totals_ptr->total_fedTax/theSize,
  550. emp_totals_ptr->total_netPay/theSize);
  551. }
  552.  
  553. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f "
  554. "%6.2f %7.2f %8.2f",
  555. emp_minMax_ptr->min_wageRate,
  556. emp_minMax_ptr->min_hours,
  557. emp_minMax_ptr->min_overtimeHrs,
  558. emp_minMax_ptr->min_grossPay,
  559. emp_minMax_ptr->min_stateTax,
  560. emp_minMax_ptr->min_fedTax,
  561. emp_minMax_ptr->min_netPay);
  562.  
  563. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f "
  564. "%6.2f %7.2f %8.2f",
  565. emp_minMax_ptr->max_wageRate,
  566. emp_minMax_ptr->max_hours,
  567. emp_minMax_ptr->max_overtimeHrs,
  568. emp_minMax_ptr->max_grossPay,
  569. emp_minMax_ptr->max_stateTax,
  570. emp_minMax_ptr->max_fedTax,
  571. emp_minMax_ptr->max_netPay);
  572.  
  573. printf ("\n\nThe total employees processed was: %i\n", theSize);
  574.  
  575. } // printEmpStatistics
  576.  
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: 
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 ***