fork download
  1. //********************************************************
  2. //
  3. // Assignment 10 - Linked Lists, Typedef, and Macros
  4. //
  5. // Name: Hans Solo
  6. //
  7. // Class: C Programming, Spring 2026
  8. //
  9. // Date: April 18, 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. // It will also take advantage of the C Preprocessor features,
  25. // in particular with using macros, and will replace all
  26. // struct type references in the code with a typedef alias
  27. // reference.
  28. //
  29. // Call by Reference design (using pointers)
  30. //
  31. //********************************************************
  32.  
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <ctype.h>
  36. #include <stdlib.h>
  37.  
  38. //---------------- CONSTANTS ----------------
  39.  
  40. #define STD_HOURS 40.0
  41. #define OT_RATE 1.5
  42.  
  43. #define MA_TAX_RATE 0.05
  44. #define NH_TAX_RATE 0.0
  45. #define VT_TAX_RATE 0.06
  46. #define CA_TAX_RATE 0.07
  47. #define DEFAULT_STATE_TAX_RATE 0.08
  48.  
  49. #define FED_TAX_RATE 0.25
  50.  
  51. #define FIRST_NAME_SIZE 10
  52. #define LAST_NAME_SIZE 10
  53. #define TAX_STATE_SIZE 3
  54.  
  55. //---------------- MACROS ----------------
  56.  
  57. #define CALC_OT_HOURS(h) ((h > STD_HOURS) ? (h - STD_HOURS) : 0)
  58. #define CALC_STATE_TAX(pay, rate) (pay * rate)
  59. #define CALC_FED_TAX(pay) (pay * FED_TAX_RATE)
  60.  
  61. #define CALC_NET_PAY(pay, st, ft) (pay - (st + ft))
  62. #define CALC_NORMAL_PAY(w, h, ot) (w * (h - ot))
  63. #define CALC_OT_PAY(w, ot) (ot * (w * OT_RATE))
  64.  
  65. #define CALC_MIN(val, min) ((val < min) ? val : min)
  66. #define CALC_MAX(val, max) ((val > max) ? val : max)
  67.  
  68. //---------------- STRUCTURES ----------------
  69.  
  70. struct name {
  71. char firstName[FIRST_NAME_SIZE];
  72. char lastName[FIRST_NAME_SIZE];
  73. };
  74.  
  75. typedef struct employee {
  76. struct name empName;
  77. char taxState[TAX_STATE_SIZE];
  78. long clockNumber;
  79. float wageRate;
  80. float hours;
  81. float overtimeHrs;
  82. float grossPay;
  83. float stateTax;
  84. float fedTax;
  85. float netPay;
  86. struct employee *next;
  87. } EMPLOYEE;
  88.  
  89. typedef struct totals {
  90. float total_wageRate;
  91. float total_hours;
  92. float total_overtimeHrs;
  93. float total_grossPay;
  94. float total_stateTax;
  95. float total_fedTax;
  96. float total_netPay;
  97. } TOTALS;
  98.  
  99. typedef struct min_max {
  100. float min_wageRate;
  101. float min_hours;
  102. float min_overtimeHrs;
  103. float min_grossPay;
  104. float min_stateTax;
  105. float min_fedTax;
  106. float min_netPay;
  107.  
  108. float max_wageRate;
  109. float max_hours;
  110. float max_overtimeHrs;
  111. float max_grossPay;
  112. float max_stateTax;
  113. float max_fedTax;
  114. float max_netPay;
  115. } MIN_MAX;
  116.  
  117. //---------------- PROTOTYPES ----------------
  118.  
  119. EMPLOYEE *getEmpData(void);
  120. int isEmployeeSize(EMPLOYEE *head_ptr);
  121.  
  122. void calcOvertimeHrs(EMPLOYEE *head_ptr);
  123. void calcGrossPay(EMPLOYEE *head_ptr);
  124. void calcStateTax(EMPLOYEE *head_ptr);
  125. void calcFedTax(EMPLOYEE *head_ptr);
  126. void calcNetPay(EMPLOYEE *head_ptr);
  127.  
  128. void calcEmployeeTotals(EMPLOYEE *head_ptr, TOTALS *totals);
  129. void calcEmployeeMinMax(EMPLOYEE *head_ptr, MIN_MAX *mm);
  130.  
  131. void printHeader(void);
  132. void printEmp(EMPLOYEE *head_ptr);
  133. void printEmpStatistics(TOTALS *totals, MIN_MAX *mm, int size);
  134.  
  135. //---------------- MAIN ----------------
  136.  
  137. int main() {
  138.  
  139. EMPLOYEE *head_ptr;
  140. int size;
  141.  
  142. TOTALS totals = {0};
  143. MIN_MAX mm = {0};
  144.  
  145. head_ptr = getEmpData();
  146. size = isEmployeeSize(head_ptr);
  147.  
  148. if (size > 0) {
  149.  
  150. calcOvertimeHrs(head_ptr);
  151. calcGrossPay(head_ptr);
  152. calcStateTax(head_ptr);
  153. calcFedTax(head_ptr);
  154. calcNetPay(head_ptr);
  155.  
  156. calcEmployeeTotals(head_ptr, &totals);
  157. calcEmployeeMinMax(head_ptr, &mm);
  158.  
  159. printHeader();
  160. printEmp(head_ptr);
  161. printEmpStatistics(&totals, &mm, size);
  162. }
  163.  
  164. printf("\n\n *** End of Program *** \n");
  165. return 0;
  166. }
  167.  
  168. //---------------- INPUT ----------------
  169.  
  170. EMPLOYEE *getEmpData(void) {
  171.  
  172. EMPLOYEE *head = NULL, *current = NULL;
  173. char ans;
  174.  
  175. while (1) {
  176.  
  177. EMPLOYEE *node = malloc(sizeof(EMPLOYEE));
  178.  
  179. scanf("%s", node->empName.firstName);
  180. scanf("%s", node->empName.lastName);
  181. scanf("%s", node->taxState);
  182. scanf("%ld", &node->clockNumber);
  183. scanf("%f", &node->wageRate);
  184. scanf("%f", &node->hours);
  185.  
  186. node->next = NULL;
  187.  
  188. if (!head)
  189. head = node;
  190. else
  191. current->next = node;
  192.  
  193. current = node;
  194.  
  195. scanf(" %c", &ans);
  196. if (toupper(ans) != 'Y')
  197. break;
  198. }
  199.  
  200. return head;
  201. }
  202.  
  203. //---------------- SIZE ----------------
  204.  
  205. int isEmployeeSize(EMPLOYEE *head_ptr) {
  206. int count = 0;
  207. while (head_ptr) {
  208. count++;
  209. head_ptr = head_ptr->next;
  210. }
  211. return count;
  212. }
  213.  
  214. //---------------- CALCULATIONS ----------------
  215.  
  216. void calcOvertimeHrs(EMPLOYEE *h) {
  217. while (h) {
  218. h->overtimeHrs = CALC_OT_HOURS(h->hours);
  219. h = h->next;
  220. }
  221. }
  222.  
  223. void calcGrossPay(EMPLOYEE *h) {
  224. while (h) {
  225. float normal = CALC_NORMAL_PAY(h->wageRate, h->hours, h->overtimeHrs);
  226. float ot = CALC_OT_PAY(h->wageRate, h->overtimeHrs);
  227. h->grossPay = normal + ot;
  228. h = h->next;
  229. }
  230. }
  231.  
  232. void calcStateTax(EMPLOYEE *h) {
  233. while (h) {
  234.  
  235. if (strcmp(h->taxState, "MA") == 0)
  236. h->stateTax = CALC_STATE_TAX(h->grossPay, MA_TAX_RATE);
  237. else if (strcmp(h->taxState, "NH") == 0)
  238. h->stateTax = CALC_STATE_TAX(h->grossPay, NH_TAX_RATE);
  239. else if (strcmp(h->taxState, "VT") == 0)
  240. h->stateTax = CALC_STATE_TAX(h->grossPay, VT_TAX_RATE);
  241. else if (strcmp(h->taxState, "CA") == 0)
  242. h->stateTax = CALC_STATE_TAX(h->grossPay, CA_TAX_RATE);
  243. else
  244. h->stateTax = CALC_STATE_TAX(h->grossPay, DEFAULT_STATE_TAX_RATE);
  245.  
  246. h = h->next;
  247. }
  248. }
  249.  
  250. void calcFedTax(EMPLOYEE *h) {
  251. while (h) {
  252. h->fedTax = CALC_FED_TAX(h->grossPay);
  253. h = h->next;
  254. }
  255. }
  256.  
  257. void calcNetPay(EMPLOYEE *h) {
  258. while (h) {
  259. h->netPay = CALC_NET_PAY(h->grossPay, h->stateTax, h->fedTax);
  260. h = h->next;
  261. }
  262. }
  263.  
  264. //---------------- TOTALS ----------------
  265.  
  266. void calcEmployeeTotals(EMPLOYEE *h, TOTALS *t) {
  267.  
  268. while (h) {
  269. t->total_wageRate += h->wageRate;
  270. t->total_hours += h->hours;
  271. t->total_overtimeHrs += h->overtimeHrs;
  272. t->total_grossPay += h->grossPay;
  273. t->total_stateTax += h->stateTax;
  274. t->total_fedTax += h->fedTax;
  275. t->total_netPay += h->netPay;
  276. h = h->next;
  277. }
  278. }
  279.  
  280. //---------------- MIN/MAX ----------------
  281.  
  282. void calcEmployeeMinMax(EMPLOYEE *h, MIN_MAX *m) {
  283.  
  284. EMPLOYEE *p = h;
  285.  
  286. m->min_wageRate = m->max_wageRate = p->wageRate;
  287. m->min_hours = m->max_hours = p->hours;
  288. m->min_overtimeHrs = m->max_overtimeHrs = p->overtimeHrs;
  289. m->min_grossPay = m->max_grossPay = p->grossPay;
  290. m->min_stateTax = m->max_stateTax = p->stateTax;
  291. m->min_fedTax = m->max_fedTax = p->fedTax;
  292. m->min_netPay = m->max_netPay = p->netPay;
  293.  
  294. p = p->next;
  295.  
  296. while (p) {
  297.  
  298. m->min_wageRate = CALC_MIN(p->wageRate, m->min_wageRate);
  299. m->max_wageRate = CALC_MAX(p->wageRate, m->max_wageRate);
  300.  
  301. m->min_hours = CALC_MIN(p->hours, m->min_hours);
  302. m->max_hours = CALC_MAX(p->hours, m->max_hours);
  303.  
  304. m->min_overtimeHrs = CALC_MIN(p->overtimeHrs, m->min_overtimeHrs);
  305. m->max_overtimeHrs = CALC_MAX(p->overtimeHrs, m->max_overtimeHrs);
  306.  
  307. m->min_grossPay = CALC_MIN(p->grossPay, m->min_grossPay);
  308. m->max_grossPay = CALC_MAX(p->grossPay, m->max_grossPay);
  309.  
  310. m->min_stateTax = CALC_MIN(p->stateTax, m->min_stateTax);
  311. m->max_stateTax = CALC_MAX(p->stateTax, m->max_stateTax);
  312.  
  313. m->min_fedTax = CALC_MIN(p->fedTax, m->min_fedTax);
  314. m->max_fedTax = CALC_MAX(p->fedTax, m->max_fedTax);
  315.  
  316. m->min_netPay = CALC_MIN(p->netPay, m->min_netPay);
  317. m->max_netPay = CALC_MAX(p->netPay, m->max_netPay);
  318.  
  319. p = p->next;
  320. }
  321. }
  322.  
  323. //---------------- OUTPUT ----------------
  324.  
  325. void printHeader(void) {
  326.  
  327. printf("\n*** Pay Calculator ***\n");
  328. printf("\n---------------------------------------------------------------------------------");
  329. printf("\nName Tax Clock# Wage Hours OT Gross State Fed Net");
  330. printf("\n State Pay Tax Tax Pay");
  331. printf("\n---------------------------------------------------------------------------------");
  332. }
  333.  
  334. void printEmp(EMPLOYEE *h) {
  335.  
  336. while (h) {
  337.  
  338. char name[25];
  339. strcpy(name, h->empName.firstName);
  340. strcat(name, " ");
  341. strcat(name, h->empName.lastName);
  342.  
  343. printf("\n%-20s %-2s %06ld %5.2f %5.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  344. name,
  345. h->taxState,
  346. h->clockNumber,
  347. h->wageRate,
  348. h->hours,
  349. h->overtimeHrs,
  350. h->grossPay,
  351. h->stateTax,
  352. h->fedTax,
  353. h->netPay);
  354.  
  355. h = h->next;
  356. }
  357. }
  358.  
  359. void printEmpStatistics(TOTALS *t, MIN_MAX *m, int size) {
  360.  
  361. printf("\n---------------------------------------------------------------------------------");
  362.  
  363. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  364. t->total_wageRate,
  365. t->total_hours,
  366. t->total_overtimeHrs,
  367. t->total_grossPay,
  368. t->total_stateTax,
  369. t->total_fedTax,
  370. t->total_netPay);
  371.  
  372. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  373. t->total_wageRate/size,
  374. t->total_hours/size,
  375. t->total_overtimeHrs/size,
  376. t->total_grossPay/size,
  377. t->total_stateTax/size,
  378. t->total_fedTax/size,
  379. t->total_netPay/size);
  380.  
  381. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  382. m->min_wageRate,
  383. m->min_hours,
  384. m->min_overtimeHrs,
  385. m->min_grossPay,
  386. m->min_stateTax,
  387. m->min_fedTax,
  388. m->min_netPay);
  389.  
  390. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  391. m->max_wageRate,
  392. m->max_hours,
  393. m->max_overtimeHrs,
  394. m->max_grossPay,
  395. m->max_stateTax,
  396. m->max_fedTax,
  397. m->max_netPay);
  398.  
  399. printf("\n\nThe total employees processed was: %d\n", size);
  400. }
Success #stdin #stdout 0s 5308KB
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
*** 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 ***