fork download
  1.  
  2.  
  3. //********************************************************
  4. //
  5. // Assignment 9 - Dynamically Allocated Linked Lists
  6. //
  7. // Name: Andrea Huskey
  8. //
  9. // Class: C Programming, Summer 2026
  10. //
  11. // Date: July 24, 2026
  12. //
  13. // Description: Program which determines overtime and
  14. // gross pay for a set of employees with outputs sent
  15. // to standard output (the screen).
  16. //
  17. // This assignment also adds the employee name, their tax state,
  18. // and calculates the state tax, federal tax, and net pay. It
  19. // also calculates totals, averages, minimum, and maximum values.
  20. //
  21. // Dynamically allocated linked list version
  22. // Call by Reference design (using pointers)
  23. //
  24. //********************************************************
  25.  
  26. //necessary header files
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include <stdlib.h>
  31.  
  32. //define constants
  33. #define SIZE 5
  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 TAX_STATE_SIZE 3
  42. #define FED_TAX_RATE 0.25
  43. #define FIRST_NAME_SIZE 20
  44. #define LAST_NAME_SIZE 20
  45.  
  46. // Define a structure type to store an employee name
  47. // ... note how one could easily extend this to other parts
  48. // parts of a name: Middle, Nickname, Prefix, Suffix, etc.
  49. struct name
  50. {
  51. char firstName[FIRST_NAME_SIZE];
  52. char lastName[LAST_NAME_SIZE];
  53. };
  54.  
  55. // Define a structure type to pass employee data between functions
  56. // Note that the structure type is global, but you don't want a variable
  57. // of that type to be global. Best to declare a variable of that type
  58. // in a function like main or another function and pass as needed.
  59. struct employee
  60. {
  61. struct name empName;
  62. char taxState[TAX_STATE_SIZE];
  63. long int clockNumber;
  64. float wageRate;
  65. float hours;
  66. float overtimeHrs;
  67. float grossPay;
  68. float stateTax;
  69. float fedTax;
  70. float netPay;
  71. struct employee *next;
  72. };
  73. // this structure type defines the totals of all floating point items
  74. // so they can be totaled and used also to calculate averages
  75. struct totals
  76. {
  77. float total_wageRate;
  78. float total_hours;
  79. float total_overtimeHrs;
  80. float total_grossPay;
  81. float total_stateTax;
  82. float total_fedTax;
  83. float total_netPay;
  84. };
  85. // this structure type defines the min and max values of all floating
  86. // point items so they can be display in our final report
  87. struct min_max
  88. {
  89. float min_wageRate;
  90. float min_hours;
  91. float min_overtimeHrs;
  92. float min_grossPay;
  93. float min_stateTax;
  94. float min_fedTax;
  95. float min_netPay;
  96. float max_wageRate;
  97. float max_hours;
  98. float max_overtimeHrs;
  99. float max_grossPay;
  100. float max_stateTax;
  101. float max_fedTax;
  102. float max_netPay;
  103. };
  104.  
  105. // define prototypes here for each function except main
  106. // These prototypes have already been transitioned to pointers
  107. struct employee *getEmpData(int theSize);
  108. void printEmp(struct employee *emp_ptr);
  109.  
  110. void calcOvertimeHrs(struct employee *emp_ptr);
  111. void calcGrossPay(struct employee *emp_ptr);
  112. void calcStateTax(struct employee *emp_ptr);
  113. void calcFedTax(struct employee *emp_ptr);
  114. void calcNetPay(struct employee *emp_ptr);
  115.  
  116. void calcEmployeeTotals(struct employee *emp_ptr,
  117. struct totals *emp_totals_ptr);
  118.  
  119. void calcEmployeeMinMax(struct employee *emp_ptr,
  120. struct min_max *emp_MinMax_ptr);
  121.  
  122. void printHeader(void);
  123. void printEmpStatistics(struct totals *employeeTotals_ptr,
  124. struct min_max *employeeMinMax_ptr,
  125. int theSize);
  126.  
  127. //start main code
  128. int main()
  129. {
  130. //Hold node for linked list and loop to release node memory
  131. struct employee *head_ptr = NULL;
  132. struct employee *free_ptr = NULL;
  133.  
  134. //Allocate employeeTotal stack and create pointer
  135. struct totals employeeTotals = {0, 0, 0, 0, 0, 0, 0};
  136. struct totals *emp_totals_ptr = &employeeTotals;
  137.  
  138. struct min_max employeeMinMax = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  139. struct min_max *employeeMinMax_ptr = &employeeMinMax;
  140.  
  141. head_ptr = getEmpData(SIZE);
  142.  
  143. //Calculate payroll values for each employee
  144. calcOvertimeHrs(head_ptr);
  145. calcGrossPay(head_ptr);
  146. calcStateTax(head_ptr);
  147. calcFedTax(head_ptr);
  148. calcNetPay(head_ptr);
  149.  
  150. //Calculate totals for each employee
  151. calcEmployeeTotals(head_ptr, emp_totals_ptr);
  152. calcEmployeeMinMax(head_ptr, employeeMinMax_ptr);
  153.  
  154. //Print the formatted report
  155. printHeader();
  156. printEmp(head_ptr);
  157. printEmpStatistics(emp_totals_ptr, employeeMinMax_ptr, SIZE);
  158.  
  159. //Dynamic allocation clean up - before exit
  160. while (head_ptr != NULL)
  161. {
  162. free_ptr = head_ptr;
  163. head_ptr = head_ptr->next;
  164. free(free_ptr);
  165. }
  166.  
  167. return 0; //success
  168. } //main
  169.  
  170.  
  171. //**************************************************************
  172. // Function: getEmpData
  173. //
  174. // Purpose: Set up function designed to construct a dynamic
  175. // linked list for structures
  176. //
  177. // Parameters:
  178. //
  179. // int theSize total number of employee records to collect or allocate
  180. //
  181. // Returns: a pointer to the memory address of the first node.
  182. //
  183. //**************************************************************
  184. struct employee *getEmpData(int theSize)
  185. {
  186. struct employee *head_ptr = NULL;
  187. struct employee *tail_ptr = NULL;
  188. struct employee *emp_ptr = NULL;
  189. char response[10];
  190. int i;
  191.  
  192. for (i = 0; i < theSize; ++i)
  193. {
  194. emp_ptr = (struct employee *)malloc(sizeof(struct employee));
  195. if (emp_ptr == NULL)
  196. {
  197. printf("Memory allocation failed.\n");
  198. return head_ptr;
  199. }
  200.  
  201. // collect first and last name, state, clock number, wage rate, and hours worked
  202. printf("Enter first name: ");
  203. scanf("%19s", emp_ptr->empName.firstName);
  204.  
  205. printf("Enter last name: ");
  206. scanf("%19s", emp_ptr->empName.lastName);
  207.  
  208. printf("Enter tax state: ");
  209. scanf("%2s", emp_ptr->taxState);
  210.  
  211. printf("Enter clock number: ");
  212. scanf("%ld", &emp_ptr->clockNumber);
  213.  
  214. printf("Enter wage rate: ");
  215. scanf("%f", &emp_ptr->wageRate);
  216.  
  217. printf("Enter hours worked: ");
  218. scanf("%f", &emp_ptr->hours);
  219.  
  220. // Read and discard trailing Y/N confirmation prompt if present in input
  221. printf("Would you like to enter another employee? (Y/N): ");
  222. scanf("%9s", response);
  223.  
  224. emp_ptr->overtimeHrs = 0;
  225. emp_ptr->grossPay = 0;
  226. emp_ptr->stateTax = 0;
  227. emp_ptr->fedTax = 0;
  228. emp_ptr->netPay = 0;
  229. emp_ptr->next = NULL;
  230.  
  231. if (head_ptr == NULL)
  232. {
  233. head_ptr = emp_ptr;
  234. tail_ptr = emp_ptr;
  235. }
  236. else
  237. {
  238. tail_ptr->next = emp_ptr;
  239. tail_ptr = emp_ptr;
  240. }
  241. }
  242.  
  243. return head_ptr;
  244. }
  245. // payroll calculator - header
  246. void printHeader(void)
  247. {
  248. printf("\n*** Pay Calculator ***\n");
  249. printf("\n---------------------------------------------------------------------------------");
  250. printf("\nName Tax Clock# Wage Hours OT Gross State Fed Net");
  251. printf("\n State Pay Tax Tax Pay");
  252. printf("\n---------------------------------------------------------------------------------");
  253. }
  254.  
  255.  
  256. //**************************************************************
  257. // Function: printEmp
  258. //
  259. // Purpose: Head pointer, Serves as starting point of linked list for
  260. // employee records
  261. //
  262. // Parameters:
  263. // employee records read and following nodes
  264. //
  265. //
  266. // Returns: Standard print output
  267. //
  268. //**************************************************************
  269.  
  270. void printEmp(struct employee *emp_ptr)
  271. {
  272. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 2];
  273.  
  274. while (emp_ptr != NULL)
  275. {
  276. snprintf(name, sizeof(name), "%s %s", emp_ptr->empName.firstName, emp_ptr->empName.lastName);
  277.  
  278. printf("\n%-20.20s %-2s %06ld %6.2f %4.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  279. name, emp_ptr->taxState, emp_ptr->clockNumber,
  280. emp_ptr->wageRate, emp_ptr->hours, emp_ptr->overtimeHrs,
  281. emp_ptr->grossPay, emp_ptr->stateTax,
  282. emp_ptr->fedTax, emp_ptr->netPay);
  283.  
  284. emp_ptr = emp_ptr->next;
  285. }
  286. }
  287. //**************************************************************
  288. // Function: printEmpStatistics
  289. //
  290. // Purpose: Summarize statistical data
  291. //
  292. // Parameters:
  293. // employee records , states, taxes, hours, grosspay, fed/stat tax,
  294. // wagerate, and netpay
  295. //
  296. //
  297. // Returns: Standard print output
  298. //
  299. //**************************************************************
  300. void printEmpStatistics(struct totals *employeeTotals_ptr,
  301. struct min_max *employeeMinMax_ptr,
  302. int theSize)
  303. {
  304. printf("\n---------------------------------------------------------------------------------");
  305.  
  306. printf("\nTotals: %6.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  307. employeeTotals_ptr->total_wageRate,
  308. employeeTotals_ptr->total_hours,
  309. employeeTotals_ptr->total_overtimeHrs,
  310. employeeTotals_ptr->total_grossPay,
  311. employeeTotals_ptr->total_stateTax,
  312. employeeTotals_ptr->total_fedTax,
  313. employeeTotals_ptr->total_netPay);
  314.  
  315. printf("\nAverages: %6.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  316. employeeTotals_ptr->total_wageRate / theSize,
  317. employeeTotals_ptr->total_hours / theSize,
  318. employeeTotals_ptr->total_overtimeHrs / theSize,
  319. employeeTotals_ptr->total_grossPay / theSize,
  320. employeeTotals_ptr->total_stateTax / theSize,
  321. employeeTotals_ptr->total_fedTax / theSize,
  322. employeeTotals_ptr->total_netPay / theSize);
  323.  
  324. printf("\nMinimum: %6.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  325. employeeMinMax_ptr->min_wageRate,
  326. employeeMinMax_ptr->min_hours,
  327. employeeMinMax_ptr->min_overtimeHrs,
  328. employeeMinMax_ptr->min_grossPay,
  329. employeeMinMax_ptr->min_stateTax,
  330. employeeMinMax_ptr->min_fedTax,
  331. employeeMinMax_ptr->min_netPay);
  332.  
  333. printf("\nMaximum: %6.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f\n",
  334. employeeMinMax_ptr->max_wageRate,
  335. employeeMinMax_ptr->max_hours,
  336. employeeMinMax_ptr->max_overtimeHrs,
  337. employeeMinMax_ptr->max_grossPay,
  338. employeeMinMax_ptr->max_stateTax,
  339. employeeMinMax_ptr->max_fedTax,
  340. employeeMinMax_ptr->max_netPay);
  341. }
  342. //calc overTimeHrs
  343. void calcOvertimeHrs(struct employee *emp_ptr)
  344. {
  345. while (emp_ptr != NULL)
  346. {
  347. emp_ptr->overtimeHrs = (emp_ptr->hours > STD_HOURS) ? (emp_ptr->hours - STD_HOURS) : 0;
  348. emp_ptr = emp_ptr->next;
  349. }
  350. }
  351. //calc grossPay
  352. void calcGrossPay(struct employee *emp_ptr)
  353. {
  354. while (emp_ptr != NULL)
  355. {
  356. emp_ptr->grossPay = (emp_ptr->wageRate * emp_ptr->hours) + (emp_ptr->overtimeHrs * emp_ptr->wageRate * 0.5f);
  357. emp_ptr = emp_ptr->next;
  358. }
  359. }
  360. //calc StateTax
  361. void calcStateTax(struct employee *emp_ptr)
  362. {
  363. while (emp_ptr != NULL)
  364. {
  365. if (strcmp(emp_ptr->taxState, "MA") == 0)
  366. emp_ptr->stateTax = emp_ptr->grossPay * MA_TAX_RATE;
  367. else if (strcmp(emp_ptr->taxState, "NH") == 0)
  368. emp_ptr->stateTax = emp_ptr->grossPay * NH_TAX_RATE;
  369. else if (strcmp(emp_ptr->taxState, "VT") == 0)
  370. emp_ptr->stateTax = emp_ptr->grossPay * VT_TAX_RATE;
  371. else if (strcmp(emp_ptr->taxState, "CA") == 0)
  372. emp_ptr->stateTax = emp_ptr->grossPay * CA_TAX_RATE;
  373. else
  374. emp_ptr->stateTax = emp_ptr->grossPay * DEFAULT_TAX_RATE;
  375.  
  376. emp_ptr = emp_ptr->next;
  377. }
  378. }
  379. //calc FedTax
  380. void calcFedTax(struct employee *emp_ptr)
  381. {
  382. while (emp_ptr != NULL)
  383. {
  384. emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
  385. emp_ptr = emp_ptr->next;
  386. }
  387. }
  388. //calc NetPay
  389. void calcNetPay(struct employee *emp_ptr)
  390. {
  391. while (emp_ptr != NULL)
  392. {
  393. emp_ptr->netPay = emp_ptr->grossPay - emp_ptr->stateTax - emp_ptr->fedTax;
  394. emp_ptr = emp_ptr->next;
  395. }
  396. }
  397. //calc EmployeeTotals
  398. void calcEmployeeTotals(struct employee *emp_ptr,
  399. struct totals *emp_totals_ptr)
  400. {
  401. while (emp_ptr != NULL)
  402. {
  403. emp_totals_ptr->total_wageRate += emp_ptr->wageRate;
  404. emp_totals_ptr->total_hours += emp_ptr->hours;
  405. emp_totals_ptr->total_overtimeHrs += emp_ptr->overtimeHrs;
  406. emp_totals_ptr->total_grossPay += emp_ptr->grossPay;
  407. emp_totals_ptr->total_stateTax += emp_ptr->stateTax;
  408. emp_totals_ptr->total_fedTax += emp_ptr->fedTax;
  409. emp_totals_ptr->total_netPay += emp_ptr->netPay;
  410. emp_ptr = emp_ptr->next;
  411. }
  412. }
  413. //calc EmployeeMinMax
  414. void calcEmployeeMinMax(struct employee *emp_ptr,
  415. struct min_max *emp_minMax_ptr)
  416. {
  417. if (emp_ptr == NULL) return;
  418.  
  419. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  420. emp_minMax_ptr->min_hours = emp_ptr->hours;
  421. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  422. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  423. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  424. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  425. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  426.  
  427. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  428. emp_minMax_ptr->max_hours = emp_ptr->hours;
  429. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  430. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  431. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  432. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  433. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  434.  
  435. emp_ptr = emp_ptr->next;
  436.  
  437. while (emp_ptr != NULL)
  438. {
  439. if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate) emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  440. if (emp_ptr->hours < emp_minMax_ptr->min_hours) emp_minMax_ptr->min_hours = emp_ptr->hours;
  441. if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs) emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  442. if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay) emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  443. if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax) emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  444. if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax) emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  445. if (emp_ptr->netPay < emp_minMax_ptr->min_netPay) emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  446.  
  447. if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate) emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  448. if (emp_ptr->hours > emp_minMax_ptr->max_hours) emp_minMax_ptr->max_hours = emp_ptr->hours;
  449. if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs) emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  450. if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay) emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  451. if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax) emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  452. if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax) emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  453. if (emp_ptr->netPay > emp_minMax_ptr->max_netPay) emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  454.  
  455. emp_ptr = emp_ptr->next;
  456. }
  457. }
  458.  
Success #stdin #stdout 0s 5316KB
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 first name: Enter last name: Enter tax state: Enter clock number: Enter wage rate: Enter hours worked: Would you like to enter another employee? (Y/N): Enter first name: Enter last name: Enter tax state: Enter clock number: Enter wage rate: Enter hours worked: Would you like to enter another employee? (Y/N): Enter first name: Enter last name: Enter tax state: Enter clock number: Enter wage rate: Enter hours worked: Would you like to enter another employee? (Y/N): Enter first name: Enter last name: Enter tax state: Enter clock number: Enter wage rate: Enter hours worked: Would you like to enter another employee? (Y/N): Enter first name: Enter last name: Enter tax state: Enter clock number: Enter wage rate: Enter hours worked: Would you like to enter 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