fork download
  1. //********************************************************
  2. //
  3. // Assignment 9 - Linked Lists
  4. //
  5. // Name: Seth Hin
  6. //
  7. // Class: C Programming, Spring 2026
  8. //
  9. // Date: April 11,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>
  32. #include <stdlib.h>
  33.  
  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. //================ STRUCT DEFINITIONS =================
  48.  
  49. // Structure to store employee name
  50. struct name {
  51. char firstName[FIRST_NAME_SIZE];
  52. char lastName[LAST_NAME_SIZE];
  53. };
  54.  
  55. // Structure to store employee data (linked list node)
  56. struct employee {
  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. struct employee *next;
  68. };
  69.  
  70. // Structure to store running totals
  71. struct totals {
  72. float total_wageRate;
  73. float total_hours;
  74. float total_overtimeHrs;
  75. float total_grossPay;
  76. float total_stateTax;
  77. float total_fedTax;
  78. float total_netPay;
  79. };
  80.  
  81. // Structure to store minimum and maximum values
  82. struct min_max {
  83. float min_wageRate, min_hours, min_overtimeHrs, min_grossPay, min_stateTax, min_fedTax, min_netPay;
  84. float max_wageRate, max_hours, max_overtimeHrs, max_grossPay, max_stateTax, max_fedTax, max_netPay;
  85. };
  86.  
  87. //================ FUNCTION PROTOTYPES =================
  88. struct employee * getEmpData (void);
  89. int isEmployeeSize (struct employee * head_ptr);
  90. void calcOvertimeHrs (struct employee * head_ptr);
  91. void calcGrossPay (struct employee * head_ptr);
  92. void calcStateTax (struct employee * head_ptr);
  93. void calcFedTax (struct employee * head_ptr);
  94. void calcNetPay (struct employee * head_ptr);
  95. void calcEmployeeTotals (struct employee * head_ptr, struct totals * emp_totals_ptr);
  96. void calcEmployeeMinMax (struct employee * head_ptr, struct min_max * emp_minMax_ptr);
  97. void printHeader (void);
  98. void printEmp (struct employee * head_ptr);
  99. void printEmpStatistics (struct totals * emp_totals_ptr, struct min_max * emp_minMax_ptr, int theSize);
  100.  
  101. //================ MAIN FUNCTION =================
  102. int main() {
  103.  
  104. struct employee *head_ptr; // pointer to first node in linked list
  105. int theSize; // number of employees
  106.  
  107. // initialize totals and min/max structures
  108. struct totals employeeTotals = {0,0,0,0,0,0,0};
  109. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  110.  
  111. // get employee data and build linked list
  112. head_ptr = getEmpData();
  113.  
  114. // count number of employees
  115. theSize = isEmployeeSize(head_ptr);
  116.  
  117. // if no employees entered
  118. if (theSize <= 0) {
  119. printf("\n\n**** There was no employee input to process ***\n");
  120. }
  121. else {
  122.  
  123. // calculate overtime hours for each employee
  124. calcOvertimeHrs(head_ptr);
  125.  
  126. // calculate gross pay
  127. calcGrossPay(head_ptr);
  128.  
  129. // calculate state tax
  130. calcStateTax(head_ptr);
  131.  
  132. // calculate federal tax
  133. calcFedTax(head_ptr);
  134.  
  135. // calculate net pay
  136. calcNetPay(head_ptr);
  137.  
  138. // compute totals across all employees
  139. calcEmployeeTotals(head_ptr, &employeeTotals);
  140.  
  141. // compute min and max values
  142. calcEmployeeMinMax(head_ptr, &employeeMinMax);
  143.  
  144. // print report header
  145. printHeader();
  146.  
  147. // print employee details
  148. printEmp(head_ptr);
  149.  
  150. // print summary statistics
  151. printEmpStatistics(&employeeTotals, &employeeMinMax, theSize);
  152. }
  153.  
  154. printf("\n\n *** End of Program *** \n");
  155. return 0;
  156. }
  157.  
  158. //================ INPUT FUNCTION =================
  159. struct employee * getEmpData (void)
  160. {
  161. char answer[10];
  162.  
  163. struct employee *head = NULL; // first node
  164. struct employee *current = NULL; // last node
  165.  
  166. do {
  167. // allocate new employee node
  168. struct employee *newNode =
  169. (struct employee*)malloc(sizeof(struct employee));
  170.  
  171. // input employee data
  172. printf("\nEnter employee first name: ");
  173. scanf("%s", newNode->empName.firstName);
  174.  
  175. printf("\nEnter employee last name: ");
  176. scanf("%s", newNode->empName.lastName);
  177.  
  178. printf("\nEnter employee two character tax state: ");
  179. scanf("%s", newNode->taxState);
  180.  
  181. printf("\nEnter employee clock number: ");
  182. scanf("%li", &newNode->clockNumber);
  183.  
  184. printf("\nEnter employee hourly wage: ");
  185. scanf("%f", &newNode->wageRate);
  186.  
  187. printf("\nEnter hours worked this week: ");
  188. scanf("%f", &newNode->hours);
  189.  
  190. // initialize pointer
  191. newNode->next = NULL;
  192.  
  193. // insert into linked list
  194. if (head == NULL)
  195. head = newNode;
  196. else
  197. current->next = newNode;
  198.  
  199. current = newNode;
  200.  
  201. // ask for more employees
  202. printf("\nWould you like to add another employee? (y/n): ");
  203. scanf("%s", answer);
  204.  
  205. } while (toupper(answer[0]) == 'Y');
  206.  
  207. return head;
  208. }
  209.  
  210. //================ COUNT FUNCTION =================
  211. int isEmployeeSize(struct employee *head)
  212. {
  213. int count = 0;
  214.  
  215. // traverse list and count nodes
  216. while (head) {
  217. count++;
  218. head = head->next;
  219. }
  220.  
  221. return count;
  222. }
  223.  
  224. //================ OVERTIME CALCULATION =================
  225. void calcOvertimeHrs(struct employee *head)
  226. {
  227. // compute overtime hours for each employee
  228. for (; head; head = head->next)
  229. head->overtimeHrs =
  230. (head->hours > STD_HOURS) ? head->hours - STD_HOURS : 0;
  231. }
  232.  
  233. //================ GROSS PAY CALCULATION =================
  234. void calcGrossPay(struct employee *head)
  235. {
  236. for (; head; head = head->next) {
  237.  
  238. float normalPay;
  239. float overtimePay;
  240.  
  241. // calculate normal pay
  242. normalPay = head->wageRate * (head->hours - head->overtimeHrs);
  243.  
  244. // calculate overtime pay
  245. overtimePay = head->overtimeHrs * (head->wageRate * OT_RATE);
  246.  
  247. // total gross pay
  248. head->grossPay = normalPay + overtimePay;
  249. }
  250. }
  251.  
  252. //================ STATE TAX CALCULATION =================
  253. void calcStateTax(struct employee *head)
  254. {
  255. for (; head; head = head->next) {
  256.  
  257. // determine tax rate based on state code
  258. if (strcmp(head->taxState, "MA") == 0)
  259. head->stateTax = head->grossPay * MA_TAX_RATE;
  260.  
  261. else if (strcmp(head->taxState, "NH") == 0)
  262. head->stateTax = head->grossPay * NH_TAX_RATE;
  263.  
  264. else if (strcmp(head->taxState, "VT") == 0)
  265. head->stateTax = head->grossPay * VT_TAX_RATE;
  266.  
  267. else if (strcmp(head->taxState, "CA") == 0)
  268. head->stateTax = head->grossPay * CA_TAX_RATE;
  269.  
  270. else
  271. head->stateTax = head->grossPay * DEFAULT_TAX_RATE;
  272. }
  273. }
  274.  
  275. //================ FEDERAL TAX CALCULATION =================
  276. void calcFedTax(struct employee *head)
  277. {
  278. for (; head; head = head->next) {
  279.  
  280. // federal tax is flat rate
  281. head->fedTax = head->grossPay * FED_TAX_RATE;
  282. }
  283. }
  284.  
  285. //================ NET PAY CALCULATION =================
  286. void calcNetPay(struct employee *head)
  287. {
  288. for (; head; head = head->next) {
  289.  
  290. // net pay = gross pay - taxes
  291. head->netPay = head->grossPay - (head->stateTax + head->fedTax);
  292. }
  293. }
  294.  
  295. //================ TOTALS CALCULATION =================
  296. void calcEmployeeTotals(struct employee *head, struct totals *t)
  297. {
  298. for (; head; head = head->next) {
  299.  
  300. // accumulate totals for each field
  301. t->total_wageRate += head->wageRate;
  302. t->total_hours += head->hours;
  303. t->total_overtimeHrs += head->overtimeHrs;
  304. t->total_grossPay += head->grossPay;
  305. t->total_stateTax += head->stateTax;
  306. t->total_fedTax += head->fedTax;
  307. t->total_netPay += head->netPay;
  308. }
  309. }
  310.  
  311. //================ MIN / MAX CALCULATION =================
  312. void calcEmployeeMinMax(struct employee *head, struct min_max *m)
  313. {
  314. // initialize min/max using first employee
  315. m->min_wageRate = m->max_wageRate = head->wageRate;
  316. m->min_hours = m->max_hours = head->hours;
  317. m->min_overtimeHrs = m->max_overtimeHrs = head->overtimeHrs;
  318. m->min_grossPay = m->max_grossPay = head->grossPay;
  319. m->min_stateTax = m->max_stateTax = head->stateTax;
  320. m->min_fedTax = m->max_fedTax = head->fedTax;
  321. m->min_netPay = m->max_netPay = head->netPay;
  322.  
  323. head = head->next;
  324.  
  325. // traverse remaining nodes
  326. for (; head; head = head->next) {
  327.  
  328. // compare wage rate
  329. if (head->wageRate < m->min_wageRate) m->min_wageRate = head->wageRate;
  330. if (head->wageRate > m->max_wageRate) m->max_wageRate = head->wageRate;
  331.  
  332. // compare hours
  333. if (head->hours < m->min_hours) m->min_hours = head->hours;
  334. if (head->hours > m->max_hours) m->max_hours = head->hours;
  335.  
  336. // compare overtime
  337. if (head->overtimeHrs < m->min_overtimeHrs) m->min_overtimeHrs = head->overtimeHrs;
  338. if (head->overtimeHrs > m->max_overtimeHrs) m->max_overtimeHrs = head->overtimeHrs;
  339.  
  340. // compare gross pay
  341. if (head->grossPay < m->min_grossPay) m->min_grossPay = head->grossPay;
  342. if (head->grossPay > m->max_grossPay) m->max_grossPay = head->grossPay;
  343.  
  344. // compare state tax
  345. if (head->stateTax < m->min_stateTax) m->min_stateTax = head->stateTax;
  346. if (head->stateTax > m->max_stateTax) m->max_stateTax = head->stateTax;
  347.  
  348. // compare federal tax
  349. if (head->fedTax < m->min_fedTax) m->min_fedTax = head->fedTax;
  350. if (head->fedTax > m->max_fedTax) m->max_fedTax = head->fedTax;
  351.  
  352. // compare net pay
  353. if (head->netPay < m->min_netPay) m->min_netPay = head->netPay;
  354. if (head->netPay > m->max_netPay) m->max_netPay = head->netPay;
  355. }
  356. }
  357.  
  358. //================ PRINT HEADER =================
  359. void printHeader(void)
  360. {
  361. printf("\n\n*** Pay Calculator ***\n");
  362.  
  363. printf("\n---------------------------------------------------------------------------------");
  364. printf("\nName Tax Clock# Wage Hours OT Gross State Fed Net");
  365. printf("\n State Pay Tax Tax Pay");
  366. printf("\n---------------------------------------------------------------------------------");
  367. }
  368.  
  369. //================ PRINT EMPLOYEES =================
  370. void printEmp(struct employee *head)
  371. {
  372. char name[25];
  373.  
  374. for (; head; head = head->next) {
  375.  
  376. // combine first and last name
  377. sprintf(name, "%s %s", head->empName.firstName, head->empName.lastName);
  378.  
  379. printf("\n%-20s %-2s %06li %6.2f %6.1f %5.1f %7.2f %7.2f %7.2f %9.2f",
  380. name, head->taxState, head->clockNumber,
  381. head->wageRate, head->hours, head->overtimeHrs,
  382. head->grossPay, head->stateTax, head->fedTax, head->netPay);
  383. }
  384. }
  385.  
  386. //================ PRINT STATISTICS =================
  387. void printEmpStatistics(struct totals *t, struct min_max *m, int n)
  388. {
  389. printf("\n---------------------------------------------------------------------------------");
  390.  
  391. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %7.2f %7.2f %9.2f",
  392. t->total_wageRate, t->total_hours, t->total_overtimeHrs,
  393. t->total_grossPay, t->total_stateTax, t->total_fedTax, t->total_netPay);
  394.  
  395. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %7.2f %7.2f %9.2f",
  396. t->total_wageRate/n, t->total_hours/n, t->total_overtimeHrs/n,
  397. t->total_grossPay/n, t->total_stateTax/n, t->total_fedTax/n, t->total_netPay/n);
  398.  
  399. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %7.2f %7.2f %9.2f",
  400. m->min_wageRate, m->min_hours, m->min_overtimeHrs,
  401. m->min_grossPay, m->min_stateTax, m->min_fedTax, m->min_netPay);
  402.  
  403. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %7.2f %7.2f %9.2f",
  404. m->max_wageRate, m->max_hours, m->max_overtimeHrs,
  405. m->max_grossPay, m->max_stateTax, m->max_fedTax, m->max_netPay);
  406.  
  407. printf("\n\nThe total employees processed was: %d\n", n);
  408. }
Success #stdin #stdout 0s 5328KB
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 ***