fork download
  1. //********************************************************
  2. //
  3. // Assignment 9 - Linked Lists
  4. //
  5. // Name: Seth Hin
  6. //
  7. // Class: C Programming, Spring 2026
  8. //
  9. // Date: <replace with the current date>
  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 TAX_STATE_SIZE 3
  42. #define FED_TAX_RATE 0.25
  43. #define FIRST_NAME_SIZE 10
  44. #define LAST_NAME_SIZE 10
  45.  
  46. struct name {
  47. char firstName[FIRST_NAME_SIZE];
  48. char lastName[LAST_NAME_SIZE];
  49. };
  50.  
  51. struct employee {
  52. struct name empName;
  53. char taxState[TAX_STATE_SIZE];
  54. long int clockNumber;
  55. float wageRate;
  56. float hours;
  57. float overtimeHrs;
  58. float grossPay;
  59. float stateTax;
  60. float fedTax;
  61. float netPay;
  62. struct employee *next;
  63. };
  64.  
  65. struct totals {
  66. float total_wageRate;
  67. float total_hours;
  68. float total_overtimeHrs;
  69. float total_grossPay;
  70. float total_stateTax;
  71. float total_fedTax;
  72. float total_netPay;
  73. };
  74.  
  75. struct min_max {
  76. float min_wageRate, min_hours, min_overtimeHrs, min_grossPay, min_stateTax, min_fedTax, min_netPay;
  77. float max_wageRate, max_hours, max_overtimeHrs, max_grossPay, max_stateTax, max_fedTax, max_netPay;
  78. };
  79.  
  80. // prototypes
  81. struct employee * getEmpData (void);
  82. int isEmployeeSize (struct employee * head_ptr);
  83. void calcOvertimeHrs (struct employee * head_ptr);
  84. void calcGrossPay (struct employee * head_ptr);
  85. void calcStateTax (struct employee * head_ptr);
  86. void calcFedTax (struct employee * head_ptr);
  87. void calcNetPay (struct employee * head_ptr);
  88. void calcEmployeeTotals (struct employee * head_ptr, struct totals * emp_totals_ptr);
  89. void calcEmployeeMinMax (struct employee * head_ptr, struct min_max * emp_minMax_ptr);
  90. void printHeader (void);
  91. void printEmp (struct employee * head_ptr);
  92. void printEmpStatistics (struct totals * emp_totals_ptr, struct min_max * emp_minMax_ptr, int theSize);
  93.  
  94. int main() {
  95. struct employee *head_ptr;
  96. int theSize;
  97.  
  98. struct totals employeeTotals = {0,0,0,0,0,0,0};
  99. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  100.  
  101. head_ptr = getEmpData();
  102. theSize = isEmployeeSize(head_ptr);
  103.  
  104. if (theSize <= 0) {
  105. printf("\n\n**** There was no employee input to process ***\n");
  106. } else {
  107. calcOvertimeHrs(head_ptr);
  108. calcGrossPay(head_ptr);
  109. calcStateTax(head_ptr);
  110. calcFedTax(head_ptr);
  111. calcNetPay(head_ptr);
  112.  
  113. calcEmployeeTotals(head_ptr, &employeeTotals);
  114. calcEmployeeMinMax(head_ptr, &employeeMinMax);
  115.  
  116. printHeader();
  117. printEmp(head_ptr);
  118. printEmpStatistics(&employeeTotals, &employeeMinMax, theSize);
  119. }
  120.  
  121. printf("\n\n *** End of Program *** \n");
  122. return 0;
  123. }
  124.  
  125. //================ INPUT =================
  126. struct employee * getEmpData (void)
  127. {
  128. char answer[10];
  129. struct employee *head = NULL, *current = NULL;
  130.  
  131. do {
  132. struct employee *newNode = (struct employee*)malloc(sizeof(struct employee));
  133.  
  134. printf("\nEnter employee first name: ");
  135. scanf("%s", newNode->empName.firstName);
  136. printf("\nEnter employee last name: ");
  137. scanf("%s", newNode->empName.lastName);
  138. printf("\nEnter employee two character tax state: ");
  139. scanf("%s", newNode->taxState);
  140. printf("\nEnter employee clock number: ");
  141. scanf("%li", &newNode->clockNumber);
  142. printf("\nEnter employee hourly wage: ");
  143. scanf("%f", &newNode->wageRate);
  144. printf("\nEnter hours worked this week: ");
  145. scanf("%f", &newNode->hours);
  146.  
  147. newNode->next = NULL;
  148.  
  149. if (head == NULL)
  150. head = newNode;
  151. else
  152. current->next = newNode;
  153.  
  154. current = newNode;
  155.  
  156. printf("\nWould you like to add another employee? (y/n): ");
  157. scanf("%s", answer);
  158.  
  159. } while (toupper(answer[0]) == 'Y');
  160.  
  161. return head;
  162. }
  163.  
  164. //================ BASIC CALCULATIONS =================
  165. int isEmployeeSize(struct employee *head) {
  166. int count = 0;
  167. while (head) { count++; head = head->next; }
  168. return count;
  169. }
  170.  
  171. void calcOvertimeHrs(struct employee *head) {
  172. for (; head; head = head->next)
  173. head->overtimeHrs = (head->hours > STD_HOURS) ? head->hours - STD_HOURS : 0;
  174. }
  175.  
  176. void calcGrossPay(struct employee *head) {
  177. for (; head; head = head->next) {
  178. float normal = head->wageRate * (head->hours - head->overtimeHrs);
  179. float ot = head->overtimeHrs * (head->wageRate * OT_RATE);
  180. head->grossPay = normal + ot;
  181. }
  182. }
  183.  
  184. //================ REQUIRED FIXES =================
  185. void calcStateTax(struct employee *head) {
  186. for (; head; head = head->next) {
  187. if (strcmp(head->taxState, "MA") == 0) head->stateTax = head->grossPay * MA_TAX_RATE;
  188. else if (strcmp(head->taxState, "NH") == 0) head->stateTax = head->grossPay * NH_TAX_RATE;
  189. else if (strcmp(head->taxState, "VT") == 0) head->stateTax = head->grossPay * VT_TAX_RATE;
  190. else if (strcmp(head->taxState, "CA") == 0) head->stateTax = head->grossPay * CA_TAX_RATE;
  191. else head->stateTax = head->grossPay * DEFAULT_TAX_RATE;
  192. }
  193. }
  194.  
  195. void calcFedTax(struct employee *head) {
  196. for (; head; head = head->next)
  197. head->fedTax = head->grossPay * FED_TAX_RATE;
  198. }
  199.  
  200. void calcNetPay(struct employee *head) {
  201. for (; head; head = head->next)
  202. head->netPay = head->grossPay - (head->stateTax + head->fedTax);
  203. }
  204.  
  205. //================ TOTALS =================
  206. void calcEmployeeTotals(struct employee *head, struct totals *t) {
  207. for (; head; head = head->next) {
  208. t->total_wageRate += head->wageRate;
  209. t->total_hours += head->hours;
  210. t->total_overtimeHrs += head->overtimeHrs;
  211. t->total_grossPay += head->grossPay;
  212. t->total_stateTax += head->stateTax;
  213. t->total_fedTax += head->fedTax;
  214. t->total_netPay += head->netPay;
  215. }
  216. }
  217.  
  218. //================ MIN MAX (FULL FIXED) =================
  219. void calcEmployeeMinMax(struct employee *head, struct min_max *m) {
  220.  
  221. m->min_wageRate = m->max_wageRate = head->wageRate;
  222. m->min_hours = m->max_hours = head->hours;
  223. m->min_overtimeHrs = m->max_overtimeHrs = head->overtimeHrs;
  224. m->min_grossPay = m->max_grossPay = head->grossPay;
  225. m->min_stateTax = m->max_stateTax = head->stateTax;
  226. m->min_fedTax = m->max_fedTax = head->fedTax;
  227. m->min_netPay = m->max_netPay = head->netPay;
  228.  
  229. head = head->next;
  230.  
  231. for (; head; head = head->next) {
  232. if (head->wageRate < m->min_wageRate) m->min_wageRate = head->wageRate;
  233. if (head->wageRate > m->max_wageRate) m->max_wageRate = head->wageRate;
  234.  
  235. if (head->hours < m->min_hours) m->min_hours = head->hours;
  236. if (head->hours > m->max_hours) m->max_hours = head->hours;
  237.  
  238. if (head->overtimeHrs < m->min_overtimeHrs) m->min_overtimeHrs = head->overtimeHrs;
  239. if (head->overtimeHrs > m->max_overtimeHrs) m->max_overtimeHrs = head->overtimeHrs;
  240.  
  241. if (head->grossPay < m->min_grossPay) m->min_grossPay = head->grossPay;
  242. if (head->grossPay > m->max_grossPay) m->max_grossPay = head->grossPay;
  243.  
  244. if (head->stateTax < m->min_stateTax) m->min_stateTax = head->stateTax;
  245. if (head->stateTax > m->max_stateTax) m->max_stateTax = head->stateTax;
  246.  
  247. if (head->fedTax < m->min_fedTax) m->min_fedTax = head->fedTax;
  248. if (head->fedTax > m->max_fedTax) m->max_fedTax = head->fedTax;
  249.  
  250. if (head->netPay < m->min_netPay) m->min_netPay = head->netPay;
  251. if (head->netPay > m->max_netPay) m->max_netPay = head->netPay;
  252. }
  253. }
  254.  
  255. //================ OUTPUT =================
  256. void printHeader(void) {
  257. printf("\n\n*** Pay Calculator ***\n");
  258. printf("\n---------------------------------------------------------------------------------");
  259. printf("\nName Tax Clock# Wage Hours OT Gross State Fed Net");
  260. printf("\n State Pay Tax Tax Pay");
  261. printf("\n---------------------------------------------------------------------------------");
  262. }
  263.  
  264. void printEmp(struct employee *head) {
  265. char name[25];
  266.  
  267. for (; head; head = head->next) {
  268. sprintf(name, "%s %s", head->empName.firstName, head->empName.lastName);
  269.  
  270. printf("\n%-20s %-2s %06li %6.2f %6.1f %5.1f %7.2f %7.2f %7.2f %9.2f",
  271. name, head->taxState, head->clockNumber,
  272. head->wageRate, head->hours, head->overtimeHrs,
  273. head->grossPay, head->stateTax, head->fedTax, head->netPay);
  274. }
  275. }
  276.  
  277. void printEmpStatistics(struct totals *t, struct min_max *m, int n) {
  278.  
  279. printf("\n---------------------------------------------------------------------------------");
  280.  
  281. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %7.2f %7.2f %9.2f",
  282. t->total_wageRate, t->total_hours, t->total_overtimeHrs,
  283. t->total_grossPay, t->total_stateTax, t->total_fedTax, t->total_netPay);
  284.  
  285. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %7.2f %7.2f %9.2f",
  286. t->total_wageRate/n, t->total_hours/n, t->total_overtimeHrs/n,
  287. t->total_grossPay/n, t->total_stateTax/n, t->total_fedTax/n, t->total_netPay/n);
  288.  
  289. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %7.2f %7.2f %9.2f",
  290. m->min_wageRate, m->min_hours, m->min_overtimeHrs,
  291. m->min_grossPay, m->min_stateTax, m->min_fedTax, m->min_netPay);
  292.  
  293. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %7.2f %7.2f %9.2f",
  294. m->max_wageRate, m->max_hours, m->max_overtimeHrs,
  295. m->max_grossPay, m->max_stateTax, m->max_fedTax, m->max_netPay);
  296.  
  297. printf("\n\nThe total employees processed was: %d\n", n);
  298. }
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 ***