fork download
  1. //********************************************************
  2. //
  3. // Assignment 9 - Linked Lists
  4. //
  5. // Name: <Amir Gharouadi>
  6. //
  7. // Class: C Programming, <Spring 2026>
  8. //
  9. // Date: <4/10/2026>
  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. // necessary header files
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <ctype.h> // for char functions
  32. #include <stdlib.h> // for malloc
  33.  
  34. // define constants
  35. #define STD_HOURS 40.0
  36. #define OT_RATE 1.5
  37. #define MA_TAX_RATE 0.05
  38. #define NH_TAX_RATE 0.0
  39. #define VT_TAX_RATE 0.06
  40. #define CA_TAX_RATE 0.07
  41. #define DEFAULT_TAX_RATE 0.08
  42. #define NAME_SIZE 20
  43. #define TAX_STATE_SIZE 3
  44. #define FED_TAX_RATE 0.25
  45. #define FIRST_NAME_SIZE 10
  46. #define LAST_NAME_SIZE 10
  47.  
  48. // Define a global structure type to store an employee name
  49. struct name
  50. {
  51. char firstName[FIRST_NAME_SIZE];
  52. char lastName [LAST_NAME_SIZE];
  53. };
  54.  
  55. // Define a global structure type to pass employee data between functions
  56. struct employee
  57. {
  58. struct name empName;
  59. char taxState [TAX_STATE_SIZE];
  60. long int clockNumber;
  61. float wageRate;
  62. float hours;
  63. float overtimeHrs;
  64. float grossPay;
  65. float stateTax;
  66. float fedTax;
  67. float netPay;
  68. struct employee * next;
  69. };
  70.  
  71. // this structure type defines the totals of all floating point items
  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. // this structure type defines the min and max values of all floating
  84. // point items so they can be display in our final report
  85. struct min_max
  86. {
  87. float min_wageRate;
  88. float min_hours;
  89. float min_overtimeHrs;
  90. float min_grossPay;
  91. float min_stateTax;
  92. float min_fedTax;
  93. float min_netPay;
  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. // define prototypes here for each function except main
  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.  
  116. void calcEmployeeMinMax (struct employee * head_ptr,
  117. struct min_max * emp_minMax_ptr);
  118.  
  119. void printEmpStatistics (struct totals * emp_totals_ptr,
  120. struct min_max * emp_minMax_ptr,
  121. int theSize);
  122.  
  123. int main ()
  124. {
  125.  
  126. struct employee * head_ptr; // always points to first linked list node
  127.  
  128. int theSize; // number of employees processed
  129.  
  130. // set up structure to store totals and initialize all to zero
  131. struct totals employeeTotals = {0,0,0,0,0,0,0};
  132.  
  133. // pointer to the employeeTotals structure
  134. struct totals * emp_totals_ptr = &employeeTotals;
  135.  
  136. // set up structure to store min and max values and initialize all to zero
  137. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  138.  
  139. // pointer to the employeeMinMax structure
  140. struct min_max * emp_minMax_ptr = &employeeMinMax;
  141.  
  142. head_ptr = getEmpData ();
  143.  
  144. theSize = isEmployeeSize (head_ptr);
  145.  
  146. if (theSize <= 0)
  147. {
  148. printf("\n\n**** There was no employee input to process ***\n");
  149. }
  150. else
  151. {
  152. calcOvertimeHrs (head_ptr);
  153. calcGrossPay (head_ptr);
  154. calcStateTax (head_ptr);
  155. calcFedTax (head_ptr);
  156. calcNetPay (head_ptr);
  157.  
  158. calcEmployeeTotals (head_ptr,
  159. &employeeTotals);
  160.  
  161. calcEmployeeMinMax (head_ptr,
  162. &employeeMinMax);
  163.  
  164. printHeader();
  165. printEmp (head_ptr);
  166.  
  167. printEmpStatistics (&employeeTotals,
  168. &employeeMinMax,
  169. theSize);
  170. }
  171.  
  172. printf ("\n\n *** End of Program *** \n");
  173.  
  174. return (0);
  175.  
  176. } // main
  177.  
  178. //**************************************************************
  179. // Function: getEmpData
  180. //**************************************************************
  181. struct employee * getEmpData (void)
  182. {
  183.  
  184. char answer[80];
  185. int more_data = 1;
  186. char value;
  187.  
  188. struct employee *current_ptr,
  189. *head_ptr;
  190.  
  191. head_ptr = (struct employee *) malloc (sizeof(struct employee));
  192. current_ptr = head_ptr;
  193.  
  194. while (more_data)
  195. {
  196. printf ("\nEnter employee first name: ");
  197. scanf ("%s", current_ptr->empName.firstName);
  198.  
  199. printf ("\nEnter employee last name: ");
  200. scanf ("%s", current_ptr->empName.lastName);
  201.  
  202. printf ("\nEnter employee two character tax state: ");
  203. scanf ("%s", current_ptr->taxState);
  204.  
  205. printf("\nEnter employee clock number: ");
  206. scanf("%li", & current_ptr -> clockNumber);
  207.  
  208. printf("\nEnter employee hourly wage: ");
  209. scanf("%f", & current_ptr -> wageRate);
  210.  
  211. printf("\nEnter hours worked this week: ");
  212. scanf("%f", & current_ptr -> hours);
  213.  
  214. printf("\nWould you like to add another employee? (y/n): ");
  215. scanf("%s", answer);
  216.  
  217. if ((value = toupper(answer[0])) != 'Y')
  218. {
  219. current_ptr->next = (struct employee *) NULL;
  220. more_data = 0;
  221. }
  222. else
  223. {
  224. current_ptr->next = (struct employee *) malloc (sizeof(struct employee));
  225. current_ptr = current_ptr->next;
  226. }
  227.  
  228. } // while
  229.  
  230. return(head_ptr);
  231. }
  232.  
  233. //*************************************************************
  234. // Function: isEmployeeSize
  235. //**************************************************************
  236. int isEmployeeSize (struct employee * head_ptr)
  237. {
  238.  
  239. struct employee * current_ptr;
  240. int theSize;
  241.  
  242. theSize = 0;
  243.  
  244. if (head_ptr->empName.firstName[0] != '\0')
  245. {
  246. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  247. {
  248. ++theSize;
  249. }
  250. }
  251.  
  252. return (theSize);
  253.  
  254. } // isEmployeeSize
  255.  
  256. //**************************************************************
  257. // Function: printHeader
  258. //**************************************************************
  259. void printHeader (void)
  260. {
  261. printf ("\n\n*** Pay Calculator ***\n");
  262.  
  263. printf("\n--------------------------------------------------------------");
  264. printf("-------------------");
  265. printf("\nName Tax Clock# Wage Hours OT Gross ");
  266. printf(" State Fed Net");
  267. printf("\n State Pay ");
  268. printf(" Tax Tax Pay");
  269.  
  270. printf("\n--------------------------------------------------------------");
  271. printf("-------------------");
  272.  
  273. } // printHeader
  274.  
  275. //*************************************************************
  276. // Function: printEmp
  277. //**************************************************************
  278. void printEmp (struct employee * head_ptr)
  279. {
  280. char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  281.  
  282. struct employee * current_ptr;
  283.  
  284. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  285. {
  286. strcpy (name, current_ptr->empName.firstName);
  287. strcat (name, " ");
  288. strcat (name, current_ptr->empName.lastName);
  289.  
  290. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  291. name, current_ptr->taxState, current_ptr->clockNumber,
  292. current_ptr->wageRate, current_ptr->hours,
  293. current_ptr->overtimeHrs, current_ptr->grossPay,
  294. current_ptr->stateTax, current_ptr->fedTax,
  295. current_ptr->netPay);
  296.  
  297. } // for
  298.  
  299. } // printEmp
  300.  
  301. //*************************************************************
  302. // Function: printEmpStatistics
  303. //**************************************************************
  304. void printEmpStatistics (struct totals * emp_totals_ptr,
  305. struct min_max * emp_minMax_ptr,
  306. int theSize)
  307. {
  308. printf("\n--------------------------------------------------------------");
  309. printf("-------------------");
  310.  
  311. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  312. emp_totals_ptr->total_wageRate,
  313. emp_totals_ptr->total_hours,
  314. emp_totals_ptr->total_overtimeHrs,
  315. emp_totals_ptr->total_grossPay,
  316. emp_totals_ptr->total_stateTax,
  317. emp_totals_ptr->total_fedTax,
  318. emp_totals_ptr->total_netPay);
  319.  
  320. if (theSize > 0)
  321. {
  322. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  323. emp_totals_ptr->total_wageRate/theSize,
  324. emp_totals_ptr->total_hours/theSize,
  325. emp_totals_ptr->total_overtimeHrs/theSize,
  326. emp_totals_ptr->total_grossPay/theSize,
  327. emp_totals_ptr->total_stateTax/theSize,
  328. emp_totals_ptr->total_fedTax/theSize,
  329. emp_totals_ptr->total_netPay/theSize);
  330. }
  331.  
  332. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  333. emp_minMax_ptr->min_wageRate,
  334. emp_minMax_ptr->min_hours,
  335. emp_minMax_ptr->min_overtimeHrs,
  336. emp_minMax_ptr->min_grossPay,
  337. emp_minMax_ptr->min_stateTax,
  338. emp_minMax_ptr->min_fedTax,
  339. emp_minMax_ptr->min_netPay);
  340.  
  341. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  342. emp_minMax_ptr->max_wageRate,
  343. emp_minMax_ptr->max_hours,
  344. emp_minMax_ptr->max_overtimeHrs,
  345. emp_minMax_ptr->max_grossPay,
  346. emp_minMax_ptr->max_stateTax,
  347. emp_minMax_ptr->max_fedTax,
  348. emp_minMax_ptr->max_netPay);
  349.  
  350. printf ("\n\nThe total employees processed was: %i\n", theSize);
  351.  
  352. } // printEmpStatistics
  353.  
  354. //*************************************************************
  355. // Function: calcOvertimeHrs
  356. //**************************************************************
  357. void calcOvertimeHrs (struct employee * head_ptr)
  358. {
  359.  
  360. struct employee * current_ptr;
  361.  
  362. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  363. {
  364. if (current_ptr->hours >= STD_HOURS)
  365. {
  366. current_ptr->overtimeHrs = current_ptr->hours - STD_HOURS;
  367. }
  368. else
  369. {
  370. current_ptr->overtimeHrs = 0;
  371. }
  372.  
  373. } // for
  374.  
  375. } // calcOvertimeHrs
  376.  
  377. //*************************************************************
  378. // Function: calcGrossPay
  379. //**************************************************************
  380. void calcGrossPay (struct employee * head_ptr)
  381. {
  382. float theNormalPay;
  383. float theOvertimePay;
  384.  
  385. struct employee * current_ptr;
  386.  
  387. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  388. {
  389. theNormalPay = current_ptr->wageRate *
  390. (current_ptr->hours - current_ptr->overtimeHrs);
  391.  
  392. theOvertimePay = current_ptr->overtimeHrs *
  393. (OT_RATE * current_ptr->wageRate);
  394.  
  395. current_ptr->grossPay = theNormalPay + theOvertimePay;
  396. }
  397.  
  398. } // calcGrossPay
  399.  
  400. //*************************************************************
  401. // Function: calcStateTax
  402. //**************************************************************
  403. void calcStateTax (struct employee * head_ptr)
  404. {
  405.  
  406. struct employee * current_ptr;
  407.  
  408. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  409. {
  410. if (islower(current_ptr->taxState[0]))
  411. current_ptr->taxState[0] = toupper(current_ptr->taxState[0]);
  412. if (islower(current_ptr->taxState[1]))
  413. current_ptr->taxState[1] = toupper(current_ptr->taxState[1]);
  414.  
  415. if (strcmp(current_ptr->taxState, "MA") == 0)
  416. {
  417. current_ptr->stateTax = current_ptr->grossPay * MA_TAX_RATE;
  418. }
  419. else if (strcmp(current_ptr->taxState, "NH") == 0)
  420. {
  421. current_ptr->stateTax = current_ptr->grossPay * NH_TAX_RATE;
  422. }
  423. else if (strcmp(current_ptr->taxState, "VT") == 0)
  424. {
  425. current_ptr->stateTax = current_ptr->grossPay * VT_TAX_RATE;
  426. }
  427. else if (strcmp(current_ptr->taxState, "CA") == 0)
  428. {
  429. current_ptr->stateTax = current_ptr->grossPay * CA_TAX_RATE;
  430. }
  431. else
  432. {
  433. current_ptr->stateTax = current_ptr->grossPay * DEFAULT_TAX_RATE;
  434. }
  435.  
  436. } // for
  437.  
  438. } // calcStateTax
  439.  
  440. //*************************************************************
  441. // Function: calcFedTax
  442. //**************************************************************
  443. void calcFedTax (struct employee * head_ptr)
  444. {
  445.  
  446. struct employee * current_ptr;
  447.  
  448. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  449. {
  450. current_ptr->fedTax = current_ptr->grossPay * FED_TAX_RATE;
  451. }
  452.  
  453. } // calcFedTax
  454.  
  455. //*************************************************************
  456. // Function: calcNetPay
  457. //**************************************************************
  458. void calcNetPay (struct employee * head_ptr)
  459. {
  460. float theTotalTaxes;
  461.  
  462. struct employee * current_ptr;
  463.  
  464. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  465. {
  466. theTotalTaxes = current_ptr->stateTax + current_ptr->fedTax;
  467. current_ptr->netPay = current_ptr->grossPay - theTotalTaxes;
  468.  
  469. } // for
  470.  
  471. } // calcNetPay
  472.  
  473. //*************************************************************
  474. // Function: calcEmployeeTotals
  475. //**************************************************************
  476. void calcEmployeeTotals (struct employee * head_ptr,
  477. struct totals * emp_totals_ptr)
  478. {
  479.  
  480. struct employee * current_ptr;
  481.  
  482. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  483. {
  484. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  485. emp_totals_ptr->total_hours += current_ptr->hours;
  486. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  487. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  488. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  489. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  490. emp_totals_ptr->total_netPay += current_ptr->netPay;
  491.  
  492. } // for
  493.  
  494. } // calcEmployeeTotals
  495.  
  496. //*************************************************************
  497. // Function: calcEmployeeMinMax
  498. //**************************************************************
  499. void calcEmployeeMinMax (struct employee * head_ptr,
  500. struct min_max * emp_minMax_ptr)
  501. {
  502. struct employee * current_ptr;
  503.  
  504. current_ptr = head_ptr;
  505.  
  506. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  507. emp_minMax_ptr->min_hours = current_ptr->hours;
  508. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  509. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  510. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  511. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  512. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  513.  
  514. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  515. emp_minMax_ptr->max_hours = current_ptr->hours;
  516. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  517. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  518. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  519. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  520. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  521.  
  522. current_ptr = current_ptr->next;
  523.  
  524. for (; current_ptr; current_ptr = current_ptr->next)
  525. {
  526. if (current_ptr->wageRate < emp_minMax_ptr->min_wageRate)
  527. {
  528. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  529. }
  530.  
  531. if (current_ptr->wageRate > emp_minMax_ptr->max_wageRate)
  532. {
  533. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  534. }
  535.  
  536. if (current_ptr->hours < emp_minMax_ptr->min_hours)
  537. {
  538. emp_minMax_ptr->min_hours = current_ptr->hours;
  539. }
  540.  
  541. if (current_ptr->hours > emp_minMax_ptr->max_hours)
  542. {
  543. emp_minMax_ptr->max_hours = current_ptr->hours;
  544. }
  545.  
  546. if (current_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
  547. {
  548. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  549. }
  550.  
  551. if (current_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
  552. {
  553. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  554. }
  555.  
  556. if (current_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  557. {
  558. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  559. }
  560.  
  561. if (current_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  562. {
  563. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  564. }
  565.  
  566. if (current_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  567. {
  568. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  569. }
  570.  
  571. if (current_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  572. {
  573. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  574. }
  575.  
  576. if (current_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  577. {
  578. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  579. }
  580.  
  581. if (current_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  582. {
  583. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  584. }
  585.  
  586. if (current_ptr->netPay < emp_minMax_ptr->min_netPay)
  587. {
  588. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  589. }
  590.  
  591. if (current_ptr->netPay > emp_minMax_ptr->max_netPay)
  592. {
  593. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  594. }
  595.  
  596. } // for
  597.  
  598. } // calcEmployeeMinMax
Success #stdin #stdout 0s 5288KB
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 ***