fork download
  1. //********************************************************
  2. //
  3. // Assignment 10 - Linked Lists, Typedef, and Macros
  4. //
  5. // Name: John Semenuk
  6. //
  7. // Class: C Programming, <replace with Semester and Year>
  8. //
  9. // Date: April 17, 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. // necessary header files
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <ctype.h>
  37. #include <stdlib.h>
  38. // define constants
  39. #define STD_HOURS 40.0
  40. #define OT_RATE 1.5
  41. #define MA_TAX_RATE 0.05
  42. #define NH_TAX_RATE 0.00
  43. #define VT_TAX_RATE 0.06
  44. #define CA_TAX_RATE 0.07
  45. #define DEFAULT_STATE_TAX_RATE 0.08
  46. #define FED_TAX_RATE 0.25
  47. #define FIRST_NAME_SIZE 10
  48. #define LAST_NAME_SIZE 10
  49. #define TAX_STATE_SIZE 3
  50.  
  51. // define macros
  52.  
  53. #define CALC_OT_HOURS(h) ((h > STD_HOURS) ? (h - STD_HOURS) : 0)
  54.  
  55. #define CALC_STATE_TAX(pay,rate) (pay * rate)
  56.  
  57. #define CALC_FED_TAX(pay) (pay * FED_TAX_RATE)
  58.  
  59. #define CALC_NET_PAY(pay,state,fed) (pay - (state + fed))
  60.  
  61. #define CALC_NORMAL_PAY(rate,hours,ot) (rate * (hours - ot))
  62.  
  63. #define CALC_OT_PAY(rate,ot) (ot * (OT_RATE * rate))
  64.  
  65. #define CALC_MIN(val,min) ((val < min) ? val : min)
  66.  
  67. #define CALC_MAX(val,max) ((val > max) ? val : max)
  68.  
  69. // Define a global structure type to store an employee name
  70. // ... note how one could easily extend this to other parts
  71. // parts of a name: Middle, Nickname, Prefix, Suffix, etc.
  72.  
  73. struct name {
  74. char firstName[FIRST_NAME_SIZE];
  75. char lastName[LAST_NAME_SIZE];
  76. };
  77. // Define a global structure type to pass employee data between functions
  78. // Note that the structure type is global, but you don't want a variable
  79. // of that type to be global. Best to declare a variable of that type
  80. // in a function like main or another function and pass as needed.
  81.  
  82. // Note the "next" member has been added as a pointer to structure employee.
  83. // This allows us to point to another data item of this same type,
  84. // allowing us to set up and traverse through all the linked
  85. // list nodes, with each node containing the employee information below.
  86.  
  87. // Also note the use of typedef to create an alias for struct employee
  88. typedef struct employee {
  89. struct name empName;
  90. char taxState[TAX_STATE_SIZE];
  91. long clockNumber;
  92. float wageRate;
  93. float hours;
  94. float overtimeHrs;
  95. float grossPay;
  96. float stateTax;
  97. float fedTax;
  98. float netPay;
  99. struct employee *next;
  100. } EMPLOYEE;
  101.  
  102. // This structure type defines the min and max values of all floating
  103. // point items so they can be display in our final report
  104.  
  105. // Also note the use of typedef to create an alias for struct min_max
  106.  
  107.  
  108. typedef struct totals {
  109. float total_wageRate;
  110. float total_hours;
  111. float total_overtimeHrs;
  112. float total_grossPay;
  113. float total_stateTax;
  114. float total_fedTax;
  115. float total_netPay;
  116. } TOTALS;
  117.  
  118. typedef struct min_max {
  119. float min_wageRate, min_hours, min_overtimeHrs, min_grossPay, min_stateTax, min_fedTax, min_netPay;
  120. float max_wageRate, max_hours, max_overtimeHrs, max_grossPay, max_stateTax, max_fedTax, max_netPay;
  121. } MIN_MAX;
  122.  
  123. // Define prototypes here for each function except main
  124. //
  125. // Note the use of the typedef alias values throughout
  126. // the rest of this program, starting with the fucntions
  127. // prototypes
  128. //
  129. // EMPLOYEE instead of struct employee
  130. // TOTALS instead of struct totals
  131. // MIN_MAX instead of struct min_max
  132.  
  133. EMPLOYEE *getEmpData(void);
  134. int isEmployeeSize(EMPLOYEE *head);
  135. void calcOvertimeHrs(EMPLOYEE *head);
  136. void calcGrossPay(EMPLOYEE *head);
  137. void calcStateTax(EMPLOYEE *head);
  138. void calcFedTax(EMPLOYEE *head);
  139. void calcNetPay(EMPLOYEE *head);
  140. void calcEmployeeTotals(EMPLOYEE *head, TOTALS *t);
  141. void calcEmployeeMinMax(EMPLOYEE *head, MIN_MAX *m);
  142. void printHeader(void);
  143. void printEmp(EMPLOYEE *head);
  144. void printEmpStatistics(TOTALS *t, MIN_MAX *m, int size);
  145. // ******************************************************************
  146. // Set up head pointer in the main function to point to the
  147. // start of the dynamically allocated linked list nodes that will be
  148. // created and stored in the Heap area.
  149. // ******************************************************************
  150.  
  151. int main()
  152. {
  153. EMPLOYEE *head_ptr; // always points to first linked list node
  154. int size; // number of employees processed
  155. // set up structure to store totals and initialize all to zero
  156. TOTALS totals = {0};
  157. MIN_MAX minmax = {0};
  158.  
  159. head_ptr = getEmpData();
  160. size = isEmployeeSize(head_ptr);
  161.  
  162. if (size <= 0)
  163. {
  164. printf("\nNo employees found.\n");
  165. return 0;
  166. }
  167.  
  168. calcOvertimeHrs(head_ptr);
  169. calcGrossPay(head_ptr);
  170. calcStateTax(head_ptr);
  171. calcFedTax(head_ptr);
  172. calcNetPay(head_ptr);
  173.  
  174. calcEmployeeTotals(head_ptr, &totals);
  175. calcEmployeeMinMax(head_ptr, &minmax);
  176.  
  177. printHeader();
  178. printEmp(head_ptr);
  179. printEmpStatistics(&totals, &minmax, size);
  180.  
  181. printf("\n\n*** End of Program ***\n");
  182. return 0;
  183. }
  184.  
  185. // ********************************************************************
  186. // Read the employee input and dynamically allocate and set up our
  187. // linked list in the Heap area. The address of the first linked
  188. // list item representing our first employee will be returned and
  189. // its value is set in our head_ptr. We can then use the head_ptr
  190. // throughout the rest of this program anytime we want to get to get
  191. // to the beginning of our linked list.
  192. // ********************************************************************
  193.  
  194. EMPLOYEE *getEmpData(void)
  195. {
  196. EMPLOYEE *head, *curr;
  197. char ans[5];
  198.  
  199. head = malloc(sizeof(EMPLOYEE));
  200. curr = head;
  201.  
  202. while (1)
  203. {
  204. printf("\nFirst name: ");
  205. scanf("%s", curr->empName.firstName);
  206.  
  207. printf("Last name: ");
  208. scanf("%s", curr->empName.lastName);
  209.  
  210. printf("State: ");
  211. scanf("%s", curr->taxState);
  212.  
  213. printf("Clock #: ");
  214. scanf("%li", &curr->clockNumber);
  215.  
  216. printf("Wage: ");
  217. scanf("%f", &curr->wageRate);
  218.  
  219. printf("Hours: ");
  220. scanf("%f", &curr->hours);
  221.  
  222. printf("Another (y/n): ");
  223. scanf("%s", ans);
  224.  
  225. if (toupper(ans[0]) != 'Y')
  226. {
  227. curr->next = NULL;
  228. break;
  229. }
  230.  
  231. curr->next = malloc(sizeof(EMPLOYEE));
  232. curr = curr->next;
  233. }
  234.  
  235. return head;
  236. }
  237.  
  238. // ********************************************************************
  239. // With the head_ptr now pointing to the first linked list node, we
  240. // can pass it to any function who needs to get to the starting point
  241. // of the linked list in the Heap. From there, functions can traverse
  242. // through the linked list to access and/or update each employee.
  243. //
  244. // Important: Don't update the head_ptr ... otherwise, you could lose
  245. // the address in the heap of the first linked list node.
  246. //
  247. // ********************************************************************
  248.  
  249. // determine how many employees are in our linked list
  250. int isEmployeeSize(EMPLOYEE *head)
  251. {
  252. int count = 0;
  253. while (head)
  254. {
  255. count++;
  256. head = head->next;
  257. }
  258. return count;
  259. }
  260.  
  261. // *********************************************************
  262. // Perform calculations and print out information as needed
  263. // *********************************************************
  264.  
  265. void calcOvertimeHrs(EMPLOYEE *h) // Calculate the overtime hours
  266. {
  267. for (; h; h = h->next)
  268. h->overtimeHrs = CALC_OT_HOURS(h->hours);
  269. }
  270.  
  271. void calcGrossPay(EMPLOYEE *h) // Calculate the weekly gross pay
  272. {
  273. for (; h; h = h->next)
  274. h->grossPay =
  275. CALC_NORMAL_PAY(h->wageRate, h->hours, h->overtimeHrs)
  276. + CALC_OT_PAY(h->wageRate, h->overtimeHrs);
  277. }
  278.  
  279. void calcStateTax(EMPLOYEE *h) // Calculate the state tax
  280. {
  281. for (; h; h = h->next)
  282. {
  283. if (strcmp(h->taxState, "MA") == 0)
  284. h->stateTax = CALC_STATE_TAX(h->grossPay, MA_TAX_RATE);
  285. else if (strcmp(h->taxState, "NH") == 0)
  286. h->stateTax = CALC_STATE_TAX(h->grossPay, NH_TAX_RATE);
  287. else if (strcmp(h->taxState, "VT") == 0)
  288. h->stateTax = CALC_STATE_TAX(h->grossPay, VT_TAX_RATE);
  289. else if (strcmp(h->taxState, "CA") == 0)
  290. h->stateTax = CALC_STATE_TAX(h->grossPay, CA_TAX_RATE);
  291. else
  292. h->stateTax = CALC_STATE_TAX(h->grossPay, DEFAULT_STATE_TAX_RATE);
  293. }
  294. }
  295.  
  296. void calcFedTax(EMPLOYEE *h) // Calculate the federal tax
  297. {
  298. for (; h; h = h->next)
  299. h->fedTax = CALC_FED_TAX(h->grossPay);
  300. }
  301.  
  302. void calcNetPay(EMPLOYEE *h)
  303. {
  304. for (; h; h = h->next)
  305. h->netPay =
  306. CALC_NET_PAY(h->grossPay, h->stateTax, h->fedTax);
  307. }
  308.  
  309. //*************************************************************
  310. // Function: calcEmployeeMinMax
  311. //
  312. // Purpose: Accepts various floating point values from an
  313. // employee and adds to a running update of min
  314. // and max values
  315. //
  316. // Parameters:
  317. //
  318. // head_ptr - pointer to the beginning of our linked list
  319. // emp_minMax_ptr - pointer to the min/max structure
  320. //
  321. // Returns:
  322. //
  323. // void (employeeMinMax structure updated by reference)
  324. //
  325. //**************************************************************
  326. void calcEmployeeTotals(EMPLOYEE *h, TOTALS *t)
  327. {
  328. for (; h; h = h->next)
  329. {
  330. t->total_wageRate += h->wageRate;
  331. t->total_hours += h->hours;
  332. t->total_overtimeHrs += h->overtimeHrs;
  333. t->total_grossPay += h->grossPay;
  334. t->total_stateTax += h->stateTax;
  335. t->total_fedTax += h->fedTax;
  336. t->total_netPay += h->netPay;
  337. }
  338. }
  339.  
  340. //================ MIN / MAX =================
  341.  
  342. void calcEmployeeMinMax(EMPLOYEE *h, MIN_MAX *m)
  343. {
  344. EMPLOYEE *p = h;
  345.  
  346. m->min_wageRate = m->max_wageRate = p->wageRate;
  347. m->min_hours = m->max_hours = p->hours;
  348. m->min_overtimeHrs = m->max_overtimeHrs = p->overtimeHrs;
  349. m->min_grossPay = m->max_grossPay = p->grossPay;
  350. m->min_stateTax = m->max_stateTax = p->stateTax;
  351. m->min_fedTax = m->max_fedTax = p->fedTax;
  352. m->min_netPay = m->max_netPay = p->netPay;
  353.  
  354. p = p->next;
  355.  
  356. for (; p; p = p->next)
  357. {
  358. m->min_wageRate = CALC_MIN(p->wageRate, m->min_wageRate);
  359. m->max_wageRate = CALC_MAX(p->wageRate, m->max_wageRate);
  360.  
  361. m->min_hours = CALC_MIN(p->hours, m->min_hours);
  362. m->max_hours = CALC_MAX(p->hours, m->max_hours);
  363.  
  364. m->min_overtimeHrs = CALC_MIN(p->overtimeHrs, m->min_overtimeHrs);
  365. m->max_overtimeHrs = CALC_MAX(p->overtimeHrs, m->max_overtimeHrs);
  366.  
  367. m->min_grossPay = CALC_MIN(p->grossPay, m->min_grossPay);
  368. m->max_grossPay = CALC_MAX(p->grossPay, m->max_grossPay);
  369.  
  370. m->min_stateTax = CALC_MIN(p->stateTax, m->min_stateTax);
  371. m->max_stateTax = CALC_MAX(p->stateTax, m->max_stateTax);
  372.  
  373. m->min_fedTax = CALC_MIN(p->fedTax, m->min_fedTax);
  374. m->max_fedTax = CALC_MAX(p->fedTax, m->max_fedTax);
  375.  
  376. m->min_netPay = CALC_MIN(p->netPay, m->min_netPay);
  377. m->max_netPay = CALC_MAX(p->netPay, m->max_netPay);
  378. }
  379. }// printHeader
  380.  
  381. //*************************************************************
  382. // Function: printEmp
  383. //
  384. // Purpose: Prints out all the information for each employee
  385. // in a nice and orderly table format.
  386. //
  387. // Parameters:
  388. //
  389. // head_ptr - pointer to the beginning of our linked list
  390. //
  391. // Returns: void
  392. //
  393. //**************************************************************
  394.  
  395. void printHeader(void)
  396. {
  397. printf("\n---------------------------------------------------------------------------------");
  398. printf("\nName Tax Clock# Wage Hours OT Gross State Fed Net");
  399. printf("\n State Pay Tax Tax Pay");
  400. printf("\n---------------------------------------------------------------------------------");
  401. }
  402.  
  403. void printEmp(EMPLOYEE *h)
  404. {
  405. char name[25];
  406.  
  407. for (; h; h = h->next)
  408. {
  409. strcpy(name, h->empName.firstName);
  410. strcat(name, " ");
  411. strcat(name, h->empName.lastName);
  412.  
  413. printf("\n%-20.20s %-2s %06li %6.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  414. name,
  415. h->taxState,
  416. h->clockNumber,
  417. h->wageRate,
  418. h->hours,
  419. h->overtimeHrs,
  420. h->grossPay,
  421. h->stateTax,
  422. h->fedTax,
  423. h->netPay);
  424. }
  425. }// printEmp
  426.  
  427. //*************************************************************
  428. // Function: printEmpStatistics
  429. //
  430. // Purpose: Prints out the summary totals and averages of all
  431. // floating point value items for all employees
  432. // that have been processed. It also prints
  433. // out the min and max values.
  434. //
  435. // Parameters:
  436. //
  437. // emp_totals_ptr - pointer to a structure containing a running total
  438. // of all employee floating point items
  439. //
  440. // emp_minMax_ptr - pointer to a structure containing
  441. // the minimum and maximum values of all
  442. // employee floating point items
  443. //
  444. // tjeSize - the total number of employees processed, used
  445. // to check for zero or negative divide condition.
  446. //
  447. // Returns: void
  448. //
  449. //**************************************************************
  450.  
  451. void printEmpStatistics(TOTALS *t, MIN_MAX *m, int size)
  452. {
  453. printf("\n---------------------------------------------------------------------------------");
  454.  
  455. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  456. t->total_wageRate,
  457. t->total_hours,
  458. t->total_overtimeHrs,
  459. t->total_grossPay,
  460. t->total_stateTax,
  461. t->total_fedTax,
  462. t->total_netPay);
  463.  
  464. if (size > 0)
  465. {
  466. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  467. t->total_wageRate / size,
  468. t->total_hours / size,
  469. t->total_overtimeHrs / size,
  470. t->total_grossPay / size,
  471. t->total_stateTax / size,
  472. t->total_fedTax / size,
  473. t->total_netPay / size);
  474. }
  475.  
  476. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  477. m->min_wageRate,
  478. m->min_hours,
  479. m->min_overtimeHrs,
  480. m->min_grossPay,
  481. m->min_stateTax,
  482. m->min_fedTax,
  483. m->min_netPay);
  484.  
  485. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  486. m->max_wageRate,
  487. m->max_hours,
  488. m->max_overtimeHrs,
  489. m->max_grossPay,
  490. m->max_stateTax,
  491. m->max_fedTax,
  492. m->max_netPay);
  493.  
  494. printf("\n\nThe total employees processed was: %d\n", size);
  495. }
  496.  
Success #stdin #stdout 0s 5284KB
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
First name: Last name: State: Clock #: Wage: Hours: Another (y/n): 
First name: Last name: State: Clock #: Wage: Hours: Another (y/n): 
First name: Last name: State: Clock #: Wage: Hours: Another (y/n): 
First name: Last name: State: Clock #: Wage: Hours: Another (y/n): 
First name: Last name: State: Clock #: Wage: Hours: Another (y/n): 
---------------------------------------------------------------------------------
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 ***