fork(1) download
  1. //********************************************************
  2. //
  3. // Assignment 10 - Linked Lists, Typedef, and Macros
  4. //
  5. // Name: Anthony Principe
  6. //
  7. // Class: C Programming, Fall 2025
  8. //
  9. // Date: 11/23/25
  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 also takes advantage of C preprocessor features, using
  25. // macros, and replaces struct type references with typedef
  26. // aliases where appropriate.
  27. //
  28. // Call by Reference design (using pointers)
  29. //
  30. //********************************************************
  31.  
  32. // necessary header files
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <ctype.h> // for char functions
  36. #include <stdlib.h> // for malloc
  37.  
  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.0
  43. #define VT_TAX_RATE 0.06
  44. #define CA_TAX_RATE 0.07
  45. #define DEFAULT_STATE_TAX_RATE 0.08
  46. #define NAME_SIZE 20
  47. #define TAX_STATE_SIZE 3
  48. #define FED_TAX_RATE 0.25
  49. #define FIRST_NAME_SIZE 10
  50. #define LAST_NAME_SIZE 10
  51.  
  52. // define macros
  53.  
  54. // Calculates overtime hours (anything over STD_HOURS)
  55. #define CALC_OT_HOURS(theHours) \
  56.   (((theHours) > STD_HOURS) ? (theHours) - STD_HOURS : 0.0)
  57.  
  58. // Calculates state tax given pay and a state tax rate
  59. #define CALC_STATE_TAX(thePay,theStateTaxRate) \
  60.   ((thePay) * (theStateTaxRate))
  61.  
  62. // Calculates federal tax based on gross pay and FED_TAX_RATE
  63. #define CALC_FED_TAX(thePay) \
  64.   ((thePay) * FED_TAX_RATE)
  65.  
  66. // Calculates net pay: gross pay minus state and federal tax
  67. #define CALC_NET_PAY(thePay,theStateTax,theFedTax) \
  68.   ((thePay) - ((theStateTax) + (theFedTax)))
  69.  
  70. // Calculates the normal (non-overtime) pay
  71. #define CALC_NORMAL_PAY(theWageRate,theHours,theOvertimeHrs) \
  72.   ((theWageRate) * ((theHours) - (theOvertimeHrs)))
  73.  
  74. // Calculates the overtime pay portion
  75. #define CALC_OT_PAY(theWageRate,theOvertimeHrs) \
  76.   ((theOvertimeHrs) * (OT_RATE * (theWageRate)))
  77.  
  78. // Calculates a new minimum value using a conditional expression
  79. #define CALC_MIN(theValue, currentMin) \
  80.   (((theValue) < (currentMin)) ? (theValue) : (currentMin))
  81.  
  82. // Calculates a new maximum value using a conditional expression
  83. #define CALC_MAX(theValue, currentMax) \
  84.   (((theValue) > (currentMax)) ? (theValue) : (currentMax))
  85.  
  86. // Define a global structure type to store an employee name
  87. // ... note how one could easily extend this to other parts
  88. // of a name: Middle, Nickname, Prefix, Suffix, etc.
  89. struct name
  90. {
  91. char firstName[FIRST_NAME_SIZE];
  92. char lastName [LAST_NAME_SIZE];
  93. };
  94.  
  95. // Define a global structure type to pass employee data between functions
  96. // Note that the structure type is global, but you don't want a variable
  97. // of that type to be global. Best to declare a variable of that type
  98. // in a function like main or another function and pass as needed.
  99. //
  100. // Note the "next" member has been added as a pointer to EMPLOYEE.
  101. // This allows us to point to another data item of this same type,
  102. // allowing us to set up and traverse through all the linked list nodes,
  103. // with each node containing the employee information below.
  104.  
  105. // Also note the use of typedef to create an alias for struct employee
  106. typedef struct employee
  107. {
  108. struct name empName;
  109. char taxState [TAX_STATE_SIZE];
  110. long int clockNumber;
  111. float wageRate;
  112. float hours;
  113. float overtimeHrs;
  114. float grossPay;
  115. float stateTax;
  116. float fedTax;
  117. float netPay;
  118. struct employee * next;
  119. } EMPLOYEE;
  120.  
  121. // This structure type defines the totals of all floating point items
  122. // so they can be totaled and used also to calculate averages.
  123. //
  124. // Also note the use of typedef to create an alias for struct totals
  125. typedef struct totals
  126. {
  127. float total_wageRate;
  128. float total_hours;
  129. float total_overtimeHrs;
  130. float total_grossPay;
  131. float total_stateTax;
  132. float total_fedTax;
  133. float total_netPay;
  134. } TOTALS;
  135.  
  136. // This structure type defines the min and max values of all floating
  137. // point items so they can be displayed in our final report.
  138. //
  139. // Use typedef alias MIN_MAX instead of struct min_max directly.
  140. typedef struct min_max
  141. {
  142. float min_wageRate;
  143. float min_hours;
  144. float min_overtimeHrs;
  145. float min_grossPay;
  146. float min_stateTax;
  147. float min_fedTax;
  148. float min_netPay;
  149. float max_wageRate;
  150. float max_hours;
  151. float max_overtimeHrs;
  152. float max_grossPay;
  153. float max_stateTax;
  154. float max_fedTax;
  155. float max_netPay;
  156. } MIN_MAX;
  157.  
  158. // Define prototypes here for each function except main
  159. //
  160. // Note the use of the typedef alias values throughout
  161. // the rest of this program, starting with the function
  162. // prototypes:
  163. //
  164. // EMPLOYEE instead of struct employee
  165. // TOTALS instead of struct totals
  166. // MIN_MAX instead of struct min_max
  167.  
  168. EMPLOYEE * getEmpData (void);
  169. int isEmployeeSize (EMPLOYEE * head_ptr);
  170. void calcOvertimeHrs (EMPLOYEE * head_ptr);
  171. void calcGrossPay (EMPLOYEE * head_ptr);
  172. void printHeader (void);
  173. void printEmp (EMPLOYEE * head_ptr);
  174. void calcStateTax (EMPLOYEE * head_ptr);
  175. void calcFedTax (EMPLOYEE * head_ptr);
  176. void calcNetPay (EMPLOYEE * head_ptr);
  177. void calcEmployeeTotals (EMPLOYEE * head_ptr,
  178. TOTALS * emp_totals_ptr);
  179. void calcEmployeeMinMax (EMPLOYEE * head_ptr,
  180. MIN_MAX * emp_minMax_ptr);
  181. void printEmpStatistics (TOTALS * emp_totals_ptr,
  182. MIN_MAX * emp_minMax_ptr,
  183. int theSize);
  184.  
  185. //**************************************************************
  186. // Function: main
  187. //
  188. // Purpose: Controls overall program flow. Builds the linked list,
  189. // calls all calculation functions, and prints the final
  190. // report showing employee pay and summary statistics.
  191. //
  192. // Parameters: none
  193. //
  194. // Returns:
  195. // 0 - success
  196. //
  197. //**************************************************************
  198. int main (void)
  199. {
  200.  
  201. // Pointer to the first node in the linked list
  202. EMPLOYEE * head_ptr;
  203.  
  204. // Number of employees processed
  205. int theSize;
  206.  
  207. // Structure to store totals for all employees (initialized to zero)
  208. TOTALS employeeTotals = {0,0,0,0,0,0,0};
  209.  
  210. // Pointer to the totals structure
  211. TOTALS * emp_totals_ptr = &employeeTotals;
  212.  
  213. // Structure to store min and max values for all employees (initialized to zero)
  214. MIN_MAX employeeMinMax = {0,0,0,0,0,0,0,
  215. 0,0,0,0,0,0,0};
  216.  
  217. // Pointer to the min/max structure
  218. MIN_MAX * emp_minMax_ptr = &employeeMinMax;
  219.  
  220. // Read the employee input and dynamically allocate and set up our
  221. // linked list in the Heap area. The address of the first linked
  222. // list item representing our first employee is returned and stored
  223. // in head_ptr.
  224. head_ptr = getEmpData ();
  225.  
  226. // Determine how many employees are in our linked list
  227. theSize = isEmployeeSize (head_ptr);
  228.  
  229. // Skip all processing if there was no employee information
  230. if (theSize <= 0)
  231. {
  232. // Print a user friendly message and skip the rest of the processing
  233. printf("\n\n**** There was no employee input to process ***\n");
  234. }
  235. else // there are employees to be processed
  236. {
  237. // Calculate the overtime hours
  238. calcOvertimeHrs (head_ptr);
  239.  
  240. // Calculate the weekly gross pay
  241. calcGrossPay (head_ptr);
  242.  
  243. // Calculate the state tax
  244. calcStateTax (head_ptr);
  245.  
  246. // Calculate the federal tax
  247. calcFedTax (head_ptr);
  248.  
  249. // Calculate the net pay after taxes
  250. calcNetPay (head_ptr);
  251.  
  252. // Keep a running sum of the employee totals
  253. calcEmployeeTotals (head_ptr, emp_totals_ptr);
  254.  
  255. // Keep a running update of the employee minimum and maximum values
  256. calcEmployeeMinMax (head_ptr, emp_minMax_ptr);
  257.  
  258. // Print the column headers
  259. printHeader();
  260.  
  261. // Print out final information on each employee
  262. printEmp (head_ptr);
  263.  
  264. // Print the totals, averages, and min/max values
  265. printEmpStatistics (emp_totals_ptr,
  266. emp_minMax_ptr,
  267. theSize);
  268. }
  269.  
  270. // Indicate that the program completed all processing
  271. printf ("\n\n *** End of Program *** \n");
  272.  
  273. return 0; // success
  274.  
  275. } // main
  276.  
  277. //**************************************************************
  278. // Function: getEmpData
  279. //
  280. // Purpose: Obtains input from user: employee name (first and last),
  281. // tax state, clock number, hourly wage, and hours worked
  282. // in a given week. Each employee is stored in a node
  283. // in a dynamically allocated linked list.
  284. //
  285. // Parameters: void
  286. //
  287. // Returns:
  288. // head_ptr - a pointer to the beginning of the dynamically
  289. // created linked list that contains the initial
  290. // input for each employee.
  291. //
  292. //**************************************************************
  293. EMPLOYEE * getEmpData (void)
  294. {
  295. char answer[80]; // user prompt response
  296. int more_data = 1; // flag to indicate if another employee is needed
  297. char value; // first char of the user response
  298.  
  299. EMPLOYEE *current_ptr; // pointer to current node
  300. EMPLOYEE *head_ptr; // always points to first node
  301.  
  302. // Set up storage for first node
  303. head_ptr = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
  304. current_ptr = head_ptr;
  305.  
  306. // Process while there is still input
  307. while (more_data)
  308. {
  309. // Read in employee first and last name
  310. printf ("\nEnter employee first name: ");
  311. scanf ("%s", current_ptr->empName.firstName);
  312.  
  313. printf ("\nEnter employee last name: ");
  314. scanf ("%s", current_ptr->empName.lastName);
  315.  
  316. // Read in employee tax state
  317. printf ("\nEnter employee two character tax state: ");
  318. scanf ("%s", current_ptr->taxState);
  319.  
  320. // Read in employee clock number
  321. printf("\nEnter employee clock number: ");
  322. scanf("%li", &current_ptr->clockNumber);
  323.  
  324. // Read in employee wage rate
  325. printf("\nEnter employee hourly wage: ");
  326. scanf("%f", &current_ptr->wageRate);
  327.  
  328. // Read in employee hours worked
  329. printf("\nEnter hours worked this week: ");
  330. scanf("%f", &current_ptr->hours);
  331.  
  332. // Ask user if they would like to add another employee
  333. printf("\nWould you like to add another employee? (y/n): ");
  334. scanf("%s", answer);
  335.  
  336. // Check first character for a 'Y' for yes
  337. value = toupper(answer[0]);
  338. if (value != 'Y')
  339. {
  340. // No more employees to process, end the list
  341. current_ptr->next = (EMPLOYEE *) NULL;
  342. more_data = 0;
  343. }
  344. else
  345. {
  346. // Make the next node and move pointer forward
  347. current_ptr->next = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
  348. current_ptr = current_ptr->next;
  349. }
  350.  
  351. } // while
  352.  
  353. return head_ptr;
  354.  
  355. } // getEmpData
  356.  
  357. //*************************************************************
  358. // Function: isEmployeeSize
  359. //
  360. // Purpose: Traverses the linked list and keeps a running count
  361. // of how many employees are currently in our list.
  362. //
  363. // Parameters:
  364. // head_ptr - pointer to the initial node in our linked list
  365. //
  366. // Returns:
  367. // theSize - the number of employees in our linked list
  368. //
  369. //**************************************************************
  370. int isEmployeeSize (EMPLOYEE * head_ptr)
  371. {
  372. EMPLOYEE * current_ptr; // pointer to current node
  373. int theSize; // number of linked list nodes (employees)
  374.  
  375. theSize = 0; // initialize count
  376.  
  377. // Assume there is no data if the first node does
  378. // not have an employee name
  379. if (head_ptr->empName.firstName[0] != '\0')
  380. {
  381. // Traverse through the linked list, keep a running count of nodes
  382. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  383. {
  384. ++theSize; // employee node found, increment
  385. }
  386. }
  387.  
  388. return theSize; // number of nodes (employees)
  389.  
  390. } // isEmployeeSize
  391.  
  392. //**************************************************************
  393. // Function: printHeader
  394. //
  395. // Purpose: Prints the initial table header information.
  396. //
  397. // Parameters: none
  398. //
  399. // Returns: void
  400. //
  401. //**************************************************************
  402. void printHeader (void)
  403. {
  404. printf ("\n\n*** Pay Calculator ***\n");
  405.  
  406. // Print the table header
  407. printf("\n--------------------------------------------------------------");
  408. printf("-------------------");
  409. printf("\nName Tax Clock# Wage Hours OT Gross ");
  410. printf(" State Fed Net");
  411. printf("\n State Pay ");
  412. printf(" Tax Tax Pay");
  413. printf("\n--------------------------------------------------------------");
  414. printf("-------------------");
  415.  
  416. } // printHeader
  417.  
  418. //*************************************************************
  419. // Function: printEmp
  420. //
  421. // Purpose: Prints out all the information for each employee
  422. // in a formatted table row.
  423. //
  424. // Parameters:
  425. // head_ptr - pointer to the beginning of our linked list
  426. //
  427. // Returns: void
  428. //
  429. //**************************************************************
  430. void printEmp (EMPLOYEE * head_ptr)
  431. {
  432. // Used to format the employee full name
  433. char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  434.  
  435. EMPLOYEE * current_ptr; // pointer to current node
  436.  
  437. // Traverse through the linked list to process each employee
  438. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  439. {
  440. // Build full name into a single string: "First Last"
  441. strcpy (name, current_ptr->empName.firstName);
  442. strcat (name, " "); // add a space between first and last names
  443. strcat (name, current_ptr->empName.lastName);
  444.  
  445. // Print out current employee in the current linked list node
  446. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  447. name, current_ptr->taxState, current_ptr->clockNumber,
  448. current_ptr->wageRate, current_ptr->hours,
  449. current_ptr->overtimeHrs, current_ptr->grossPay,
  450. current_ptr->stateTax, current_ptr->fedTax,
  451. current_ptr->netPay);
  452. }
  453.  
  454. } // printEmp
  455.  
  456. //*************************************************************
  457. // Function: printEmpStatistics
  458. //
  459. // Purpose: Prints out the summary totals and averages of all
  460. // floating point value items for all employees
  461. // that have been processed. It also prints
  462. // out the min and max values.
  463. //
  464. // Parameters:
  465. // emp_totals_ptr - pointer to a structure containing a running total
  466. // of all employee floating point items
  467. // emp_minMax_ptr - pointer to a structure containing
  468. // the minimum and maximum values of all
  469. // employee floating point items
  470. // theSize - the total number of employees processed
  471. // (used to calculate averages).
  472. //
  473. // Returns: void
  474. //
  475. //**************************************************************
  476. void printEmpStatistics (TOTALS * emp_totals_ptr,
  477. MIN_MAX * emp_minMax_ptr,
  478. int theSize)
  479. {
  480. // Print a separator line
  481. printf("\n--------------------------------------------------------------");
  482. printf("-------------------");
  483.  
  484. // Print the totals for all the floating point items
  485. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  486. emp_totals_ptr->total_wageRate,
  487. emp_totals_ptr->total_hours,
  488. emp_totals_ptr->total_overtimeHrs,
  489. emp_totals_ptr->total_grossPay,
  490. emp_totals_ptr->total_stateTax,
  491. emp_totals_ptr->total_fedTax,
  492. emp_totals_ptr->total_netPay);
  493.  
  494. // Make sure you don't divide by zero or a negative number
  495. if (theSize > 0)
  496. {
  497. // Print the averages for all the floating point items
  498. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  499. emp_totals_ptr->total_wageRate/theSize,
  500. emp_totals_ptr->total_hours/theSize,
  501. emp_totals_ptr->total_overtimeHrs/theSize,
  502. emp_totals_ptr->total_grossPay/theSize,
  503. emp_totals_ptr->total_stateTax/theSize,
  504. emp_totals_ptr->total_fedTax/theSize,
  505. emp_totals_ptr->total_netPay/theSize);
  506. }
  507.  
  508. // Print the min and max values for each item
  509. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  510. emp_minMax_ptr->min_wageRate,
  511. emp_minMax_ptr->min_hours,
  512. emp_minMax_ptr->min_overtimeHrs,
  513. emp_minMax_ptr->min_grossPay,
  514. emp_minMax_ptr->min_stateTax,
  515. emp_minMax_ptr->min_fedTax,
  516. emp_minMax_ptr->min_netPay);
  517.  
  518. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  519. emp_minMax_ptr->max_wageRate,
  520. emp_minMax_ptr->max_hours,
  521. emp_minMax_ptr->max_overtimeHrs,
  522. emp_minMax_ptr->max_grossPay,
  523. emp_minMax_ptr->max_stateTax,
  524. emp_minMax_ptr->max_fedTax,
  525. emp_minMax_ptr->max_netPay);
  526.  
  527. // Print out the total employees processed
  528. printf ("\n\nThe total employees processed was: %i\n", theSize);
  529.  
  530. } // printEmpStatistics
  531.  
  532. //*************************************************************
  533. // Function: calcOvertimeHrs
  534. //
  535. // Purpose: Calculates the overtime hours worked by an employee
  536. // in a given week for each employee.
  537. //
  538. // Parameters:
  539. // head_ptr - pointer to the beginning of our linked list
  540. //
  541. // Returns: void (the overtime hours get updated by reference)
  542. //
  543. //**************************************************************
  544. void calcOvertimeHrs (EMPLOYEE * head_ptr)
  545. {
  546. EMPLOYEE * current_ptr; // pointer to current node
  547.  
  548. // Traverse through the linked list to calculate overtime hours
  549. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  550. {
  551. // Use macro to calculate overtime hours for this employee
  552. current_ptr->overtimeHrs = CALC_OT_HOURS(current_ptr->hours);
  553. }
  554.  
  555. } // calcOvertimeHrs
  556.  
  557. //*************************************************************
  558. // Function: calcGrossPay
  559. //
  560. // Purpose: Calculates the gross pay based on the normal pay
  561. // and any overtime pay for a given week for each
  562. // employee.
  563. //
  564. // Parameters:
  565. // head_ptr - pointer to the beginning of our linked list
  566. //
  567. // Returns: void (the gross pay gets updated by reference)
  568. //
  569. //**************************************************************
  570. void calcGrossPay (EMPLOYEE * head_ptr)
  571. {
  572. float theNormalPay; // normal pay without any overtime hours
  573. float theOvertimePay; // overtime pay
  574.  
  575. EMPLOYEE * current_ptr; // pointer to current node
  576.  
  577. // Traverse through the linked list to calculate gross pay
  578. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  579. {
  580. // Calculate normal pay and any overtime pay
  581. theNormalPay = CALC_NORMAL_PAY(current_ptr->wageRate,
  582. current_ptr->hours,
  583. current_ptr->overtimeHrs);
  584.  
  585. theOvertimePay = CALC_OT_PAY(current_ptr->wageRate,
  586. current_ptr->overtimeHrs);
  587.  
  588. // Gross pay is normal pay plus overtime pay
  589. current_ptr->grossPay = theNormalPay + theOvertimePay;
  590. }
  591.  
  592. } // calcGrossPay
  593.  
  594. //*************************************************************
  595. // Function: calcStateTax
  596. //
  597. // Purpose: Calculates the state tax owed based on gross pay
  598. // for each employee. State tax rate is based on the
  599. // tax state code where the employee is working.
  600. //
  601. // Parameters:
  602. // head_ptr - pointer to the beginning of our linked list
  603. //
  604. // Returns: void (the state tax gets updated by reference)
  605. //
  606. //**************************************************************
  607. void calcStateTax (EMPLOYEE * head_ptr)
  608. {
  609. EMPLOYEE * current_ptr; // pointer to current node
  610.  
  611. // Traverse through the linked list to calculate the state tax
  612. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  613. {
  614. // Make sure tax state is all uppercase
  615. if (islower(current_ptr->taxState[0]))
  616. current_ptr->taxState[0] = toupper(current_ptr->taxState[0]);
  617. if (islower(current_ptr->taxState[1]))
  618. current_ptr->taxState[1] = toupper(current_ptr->taxState[1]);
  619.  
  620. // Calculate state tax based on employee tax state
  621. if (strcmp(current_ptr->taxState, "MA") == 0)
  622. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  623. MA_TAX_RATE);
  624. else if (strcmp(current_ptr->taxState, "VT") == 0)
  625. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  626. VT_TAX_RATE);
  627. else if (strcmp(current_ptr->taxState, "NH") == 0)
  628. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  629. NH_TAX_RATE);
  630. else if (strcmp(current_ptr->taxState, "CA") == 0)
  631. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  632. CA_TAX_RATE);
  633. else
  634. // Any other state uses the default rate
  635. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  636. DEFAULT_STATE_TAX_RATE);
  637. }
  638.  
  639. } // calcStateTax
  640.  
  641. //*************************************************************
  642. // Function: calcFedTax
  643. //
  644. // Purpose: Calculates the federal tax owed based on the
  645. // gross pay for each employee.
  646. //
  647. // Parameters:
  648. // head_ptr - pointer to the beginning of our linked list
  649. //
  650. // Returns: void (the federal tax gets updated by reference)
  651. //
  652. //**************************************************************
  653. void calcFedTax (EMPLOYEE * head_ptr)
  654. {
  655. EMPLOYEE * current_ptr; // pointer to current node
  656.  
  657. // Traverse through the linked list to calculate the federal tax
  658. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  659. {
  660. // Fed tax is the same percentage for all employees
  661. current_ptr->fedTax = CALC_FED_TAX(current_ptr->grossPay);
  662. }
  663.  
  664. } // calcFedTax
  665.  
  666. //*************************************************************
  667. // Function: calcNetPay
  668. //
  669. // Purpose: Calculates the net pay as the gross pay minus any
  670. // state and federal taxes owed for each employee.
  671. // This is their "take home" pay.
  672. //
  673. // Parameters:
  674. // head_ptr - pointer to the beginning of our linked list
  675. //
  676. // Returns: void (the net pay gets updated by reference)
  677. //
  678. //**************************************************************
  679. void calcNetPay (EMPLOYEE * head_ptr)
  680. {
  681. EMPLOYEE * current_ptr; // pointer to current node
  682.  
  683. // Traverse through the linked list to calculate the net pay
  684. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  685. {
  686. // Calculate the net pay using the macro
  687. current_ptr->netPay = CALC_NET_PAY(current_ptr->grossPay,
  688. current_ptr->stateTax,
  689. current_ptr->fedTax);
  690. }
  691.  
  692. } // calcNetPay
  693.  
  694. //*************************************************************
  695. // Function: calcEmployeeTotals
  696. //
  697. // Purpose: Performs a running total (sum) of each employee
  698. // floating point member item stored in our linked list.
  699. //
  700. // Parameters:
  701. // head_ptr - pointer to the beginning of our linked list
  702. // emp_totals_ptr - pointer to a structure containing the
  703. // running totals of each floating point
  704. // member for all employees
  705. //
  706. // Returns:
  707. // void (the employeeTotals structure gets updated by reference)
  708. //
  709. //**************************************************************
  710. void calcEmployeeTotals (EMPLOYEE * head_ptr,
  711. TOTALS * emp_totals_ptr)
  712. {
  713. EMPLOYEE * current_ptr; // pointer to current node
  714.  
  715. // Traverse through the linked list to calculate a running
  716. // sum of each employee floating point member item
  717. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  718. {
  719. // Add current employee data to our running totals
  720. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  721. emp_totals_ptr->total_hours += current_ptr->hours;
  722. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  723. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  724. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  725. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  726. emp_totals_ptr->total_netPay += current_ptr->netPay;
  727. }
  728.  
  729. // No need to return anything since we used pointers and have
  730. // been updating the totals structure by reference.
  731.  
  732. } // calcEmployeeTotals
  733.  
  734. //*************************************************************
  735. // Function: calcEmployeeMinMax
  736. //
  737. // Purpose: Accepts various floating point values from each
  738. // employee and keeps a running update of min and
  739. // max values for each field.
  740. //
  741. // Parameters:
  742. // head_ptr - pointer to the beginning of our linked list
  743. // emp_minMax_ptr - pointer to the min/max structure
  744. //
  745. // Returns:
  746. // void (employeeMinMax structure updated by reference)
  747. //
  748. //**************************************************************
  749. void calcEmployeeMinMax (EMPLOYEE * head_ptr,
  750. MIN_MAX * emp_minMax_ptr)
  751. {
  752. EMPLOYEE * current_ptr; // pointer to current node
  753.  
  754. // At this point, head_ptr is pointing to the first employee
  755. // in the linked list. Use that employee as the initial
  756. // baseline for min and max values.
  757.  
  758. // Set to first employee (initial linked list node)
  759. current_ptr = head_ptr;
  760.  
  761. // Set the min to the first employee members
  762. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  763. emp_minMax_ptr->min_hours = current_ptr->hours;
  764. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  765. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  766. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  767. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  768. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  769.  
  770. // Set the max to the first employee members
  771. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  772. emp_minMax_ptr->max_hours = current_ptr->hours;
  773. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  774. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  775. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  776. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  777. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  778.  
  779. // Move to the next employee
  780. current_ptr = current_ptr->next;
  781.  
  782. // Traverse the rest of the linked list and update min and max
  783. for (; current_ptr; current_ptr = current_ptr->next)
  784. {
  785. // Wage rate min and max
  786. emp_minMax_ptr->min_wageRate =
  787. CALC_MIN(current_ptr->wageRate, emp_minMax_ptr->min_wageRate);
  788. emp_minMax_ptr->max_wageRate =
  789. CALC_MAX(current_ptr->wageRate, emp_minMax_ptr->max_wageRate);
  790.  
  791. // Hours min and max
  792. emp_minMax_ptr->min_hours =
  793. CALC_MIN(current_ptr->hours, emp_minMax_ptr->min_hours);
  794. emp_minMax_ptr->max_hours =
  795. CALC_MAX(current_ptr->hours, emp_minMax_ptr->max_hours);
  796.  
  797. // Overtime hours min and max
  798. emp_minMax_ptr->min_overtimeHrs =
  799. CALC_MIN(current_ptr->overtimeHrs, emp_minMax_ptr->min_overtimeHrs);
  800. emp_minMax_ptr->max_overtimeHrs =
  801. CALC_MAX(current_ptr->overtimeHrs, emp_minMax_ptr->max_overtimeHrs);
  802.  
  803. // Gross pay min and max
  804. emp_minMax_ptr->min_grossPay =
  805. CALC_MIN(current_ptr->grossPay, emp_minMax_ptr->min_grossPay);
  806. emp_minMax_ptr->max_grossPay =
  807. CALC_MAX(current_ptr->grossPay, emp_minMax_ptr->max_grossPay);
  808.  
  809. // State tax min and max
  810. emp_minMax_ptr->min_stateTax =
  811. CALC_MIN(current_ptr->stateTax, emp_minMax_ptr->min_stateTax);
  812. emp_minMax_ptr->max_stateTax =
  813. CALC_MAX(current_ptr->stateTax, emp_minMax_ptr->max_stateTax);
  814.  
  815. // Federal tax min and max
  816. emp_minMax_ptr->min_fedTax =
  817. CALC_MIN(current_ptr->fedTax, emp_minMax_ptr->min_fedTax);
  818. emp_minMax_ptr->max_fedTax =
  819. CALC_MAX(current_ptr->fedTax, emp_minMax_ptr->max_fedTax);
  820.  
  821. // Net pay min and max
  822. emp_minMax_ptr->min_netPay =
  823. CALC_MIN(current_ptr->netPay, emp_minMax_ptr->min_netPay);
  824. emp_minMax_ptr->max_netPay =
  825. CALC_MAX(current_ptr->netPay, emp_minMax_ptr->max_netPay);
  826. }
  827.  
  828. // No need to return anything since we used pointers and have
  829. // been referencing all the nodes in our linked list where
  830. // they reside in memory (the Heap area).
  831.  
  832. } // calcEmployeeMinMax
  833.  
Success #stdin #stdout 0s 5324KB
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 ***