fork download
  1. //********************************************************
  2. //
  3. // Assignment 10 - Linked Lists, Typedef, and Macros
  4. //
  5. // Name: Raissa Almeida Beckenkamp
  6. //
  7. // Class: C Programming, Fall 2025
  8. //
  9. // Date: November 23, 2025
  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> // for char functions
  37. #include <stdlib.h> // for malloc
  38.  
  39. // define constants
  40. #define STD_HOURS 40.0
  41. #define OT_RATE 1.5
  42. #define MA_TAX_RATE 0.05
  43. #define NH_TAX_RATE 0.0
  44. #define VT_TAX_RATE 0.06
  45. #define CA_TAX_RATE 0.07
  46. #define DEFAULT_STATE_TAX_RATE 0.08
  47. #define NAME_SIZE 20
  48. #define TAX_STATE_SIZE 3
  49. #define FED_TAX_RATE 0.25
  50. #define FIRST_NAME_SIZE 10
  51. #define LAST_NAME_SIZE 10
  52.  
  53. // define macros
  54. #define CALC_OT_HOURS(theHours) ((theHours > STD_HOURS) ? theHours - STD_HOURS : 0)
  55. #define CALC_STATE_TAX(thePay,theStateTaxRate) ((thePay) * (theStateTaxRate))
  56.  
  57. // TODO - Create a macro called CALC_FED_TAX. It will be very similar
  58. // to the CALC_STATE_TAX macro above. Then call your macro in the
  59. // the calcFedTax function (replacing the current code)
  60.  
  61. //created a macro function for Federal Tax
  62. #define CALC_FED_TAX(thePay,theFedTaxRate) ((thePay) * (theFedTaxRate))
  63.  
  64. #define CALC_NET_PAY(thePay,theStateTax,theFedTax) (thePay - (theStateTax + theFedTax))
  65. #define CALC_NORMAL_PAY(theWageRate,theHours,theOvertimeHrs) \
  66. (theWageRate * (theHours - theOvertimeHrs))
  67. #define CALC_OT_PAY(theWageRate,theOvertimeHrs) (theOvertimeHrs * (OT_RATE * theWageRate))
  68.  
  69. // TODO - These two macros are missing the correct logic, they are just setting
  70. // things to zero at this point. Replace the 0.0 value below with the
  71. // right logic to determine the min and max values. These macros would
  72. // work very similar to the CALC_OT_HOURS macro above using a
  73. // conditional expression operator. The calls to these macros in the
  74. // calcEmployeeMinMax function are already correct
  75. // ... so no changes needed there.
  76.  
  77. //added the right logic to determine the min and max values
  78. #define CALC_MIN(theValue, currentMin) (((theValue) <= (currentMin)) ? (theValue) : (currentMin))
  79. #define CALC_MAX(theValue, currentMax) (((theValue) >= (currentMax)) ? (theValue) : (currentMax))
  80.  
  81. // Define a global structure type to store an employee name
  82. // ... note how one could easily extend this to other parts
  83. // parts of a name: Middle, Nickname, Prefix, Suffix, etc.
  84. struct name
  85. {
  86. char firstName[FIRST_NAME_SIZE];
  87. char lastName [LAST_NAME_SIZE];
  88. };
  89.  
  90. // Define a global structure type to pass employee data between functions
  91. // Note that the structure type is global, but you don't want a variable
  92. // of that type to be global. Best to declare a variable of that type
  93. // in a function like main or another function and pass as needed.
  94.  
  95. // Note the "next" member has been added as a pointer to structure employee.
  96. // This allows us to point to another data item of this same type,
  97. // allowing us to set up and traverse through all the linked
  98. // list nodes, with each node containing the employee information below.
  99.  
  100. // Also note the use of typedef to create an alias for struct employee
  101. typedef struct employee
  102. {
  103. struct name empName;
  104. char taxState [TAX_STATE_SIZE];
  105. long int clockNumber;
  106. float wageRate;
  107. float hours;
  108. float overtimeHrs;
  109. float grossPay;
  110. float stateTax;
  111. float fedTax;
  112. float netPay;
  113. struct employee * next;
  114. } EMPLOYEE;
  115.  
  116. // This structure type defines the totals of all floating point items
  117. // so they can be totaled and used also to calculate averages
  118.  
  119. // Also note the use of typedef to create an alias for struct totals
  120. typedef struct totals
  121. {
  122. float total_wageRate;
  123. float total_hours;
  124. float total_overtimeHrs;
  125. float total_grossPay;
  126. float total_stateTax;
  127. float total_fedTax;
  128. float total_netPay;
  129. } TOTALS;
  130.  
  131. // This structure type defines the min and max values of all floating
  132. // point items so they can be display in our final report
  133.  
  134. // Also note the use of typedef to create an alias for struct min_max
  135.  
  136. // TODO - Add a typedef alias to this structure, call it: MIN_MAX
  137. // Then update all associated code (prototypes plus the main,
  138. // printEmpStatistics and calcEmployeeMinMax functions) that reference
  139. // "struct min_max". Essentially, replacing "struct min_max" with the
  140. // typedef alias MIN_MAX
  141.  
  142. //added typedef to store min and max values
  143. typedef struct min_max
  144. {
  145. float min_wageRate;
  146. float min_hours;
  147. float min_overtimeHrs;
  148. float min_grossPay;
  149. float min_stateTax;
  150. float min_fedTax;
  151. float min_netPay;
  152. float max_wageRate;
  153. float max_hours;
  154. float max_overtimeHrs;
  155. float max_grossPay;
  156. float max_stateTax;
  157. float max_fedTax;
  158. float max_netPay;
  159. } MIN_MAX;
  160.  
  161. // Define prototypes here for each function except main
  162. //
  163. // Note the use of the typedef alias values throughout
  164. // the rest of this program, starting with the fucntions
  165. // prototypes
  166. //
  167. // EMPLOYEE instead of struct employee
  168. // TOTALS instead of struct totals
  169. // MIN_MAX instead of struct min_max
  170.  
  171. EMPLOYEE * getEmpData (void);
  172. int isEmployeeSize (EMPLOYEE * head_ptr);
  173. void calcOvertimeHrs (EMPLOYEE * head_ptr);
  174. void calcGrossPay (EMPLOYEE * head_ptr);
  175. void printHeader (void);
  176. void printEmp (EMPLOYEE * head_ptr);
  177. void calcStateTax (EMPLOYEE * head_ptr);
  178. void calcFedTax (EMPLOYEE * head_ptr);
  179. void calcNetPay (EMPLOYEE * head_ptr);
  180. void calcEmployeeTotals (EMPLOYEE * head_ptr,
  181. TOTALS * emp_totals_ptr);
  182.  
  183. // TODO - Update these two prototypes with the MIN_MAX typedef alias
  184. //updated the prototypes to MIN_MAX
  185. void calcEmployeeMinMax (EMPLOYEE * head_ptr,
  186. MIN_MAX * emp_minMax_ptr);
  187.  
  188. void printEmpStatistics (TOTALS * emp_totals_ptr,
  189. MIN_MAX * emp_minMax_ptr,
  190. int size);
  191.  
  192. int main ()
  193. {
  194.  
  195. // ******************************************************************
  196. // Set up head pointer in the main function to point to the
  197. // start of the dynamically allocated linked list nodes that will be
  198. // created and stored in the Heap area.
  199. // ******************************************************************
  200. EMPLOYEE * head_ptr; // always points to first linked list node
  201.  
  202. int theSize; // number of employees processed
  203.  
  204. // set up structure to store totals and initialize all to zero
  205. TOTALS employeeTotals = {0,0,0,0,0,0,0};
  206.  
  207. // pointer to the employeeTotals structure
  208. TOTALS * emp_totals_ptr = &employeeTotals;
  209.  
  210. // TODO - Update these two variable declarations to use
  211. // the MIN_MAX typedef alias
  212.  
  213. //updates the variables to MIN_MAX typedef
  214. // set up structure to store min and max values and initialize all to zero
  215. MIN_MAX employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  216.  
  217. // pointer to the employeeMinMax structure
  218. MIN_MAX * emp_minMax_ptr = &employeeMinMax;
  219.  
  220. // ********************************************************************
  221. // Read the employee input and dynamically allocate and set up our
  222. // linked list in the Heap area. The address of the first linked
  223. // list item representing our first employee will be returned and
  224. // its value is set in our head_ptr. We can then use the head_ptr
  225. // throughout the rest of this program anytime we want to get to get
  226. // to the beginning of our linked list.
  227. // ********************************************************************
  228.  
  229. head_ptr = getEmpData ();
  230.  
  231. // ********************************************************************
  232. // With the head_ptr now pointing to the first linked list node, we
  233. // can pass it to any function who needs to get to the starting point
  234. // of the linked list in the Heap. From there, functions can traverse
  235. // through the linked list to access and/or update each employee.
  236. //
  237. // Important: Don't update the head_ptr ... otherwise, you could lose
  238. // the address in the heap of the first linked list node.
  239. //
  240. // ********************************************************************
  241.  
  242. // determine how many employees are in our linked list
  243.  
  244. theSize = isEmployeeSize (head_ptr);
  245.  
  246. // Skip all the function calls to process the data if there
  247. // was no employee information to read in the input
  248. if (theSize <= 0)
  249. {
  250. // print a user friendly message and skip the rest of the processing
  251. printf("\n\n**** There was no employee input to process ***\n");
  252. }
  253.  
  254. else // there are employees to be processed
  255. {
  256.  
  257. // *********************************************************
  258. // Perform calculations and print out information as needed
  259. // *********************************************************
  260.  
  261. // Calculate the overtime hours
  262. calcOvertimeHrs (head_ptr);
  263.  
  264. // Calculate the weekly gross pay
  265. calcGrossPay (head_ptr);
  266.  
  267. // Calculate the state tax
  268. calcStateTax (head_ptr);
  269.  
  270. // Calculate the federal tax
  271. calcFedTax (head_ptr);
  272.  
  273. // Calculate the net pay after taxes
  274. calcNetPay (head_ptr);
  275.  
  276. // *********************************************************
  277. // Keep a running sum of the employee totals
  278. //
  279. // Note the & to specify the address of the employeeTotals
  280. // structure. Needed since pointers work with addresses.
  281. // Unlike array names, C does not see structure names
  282. // as address, hence the need for using the &employeeTotals
  283. // which the complier sees as "address of" employeeTotals
  284. // *********************************************************
  285. calcEmployeeTotals (head_ptr,
  286. &employeeTotals);
  287.  
  288. // *****************************************************************
  289. // Keep a running update of the employee minimum and maximum values
  290. //
  291. // Note we are passing the address of the MinMax structure
  292. // *****************************************************************
  293. calcEmployeeMinMax (head_ptr,
  294. &employeeMinMax);
  295.  
  296. // Print the column headers
  297. printHeader();
  298.  
  299. // print out final information on each employee
  300. printEmp (head_ptr);
  301.  
  302. // **************************************************
  303. // print the totals and averages for all float items
  304. //
  305. // Note that we are passing the addresses of the
  306. // the two structures
  307. // **************************************************
  308. printEmpStatistics (&employeeTotals,
  309. &employeeMinMax,
  310. theSize);
  311. }
  312.  
  313. // indicate that the program completed all processing
  314. printf ("\n\n *** End of Program *** \n");
  315.  
  316. return (0); // success
  317.  
  318. } // main
  319.  
  320. //**************************************************************
  321. // Function: getEmpData
  322. //
  323. // Purpose: Obtains input from user: employee name (first an last),
  324. // tax state, clock number, hourly wage, and hours worked
  325. // in a given week.
  326. //
  327. // Information in stored in a dynamically created linked
  328. // list for all employees.
  329. //
  330. // Parameters: void
  331. //
  332. // Returns:
  333. //
  334. // head_ptr - a pointer to the beginning of the dynamically
  335. // created linked list that contains the initial
  336. // input for each employee.
  337. //
  338. //**************************************************************
  339.  
  340. EMPLOYEE * getEmpData (void)
  341. {
  342.  
  343. char answer[80]; // user prompt response
  344. int more_data = 1; // a flag to indicate if another employee
  345. // needs to be processed
  346. char value; // the first char of the user prompt response
  347.  
  348. EMPLOYEE *current_ptr, // pointer to current node
  349. *head_ptr; // always points to first node
  350.  
  351. // Set up storage for first node
  352. head_ptr = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
  353. current_ptr = head_ptr;
  354.  
  355. // process while there is still input
  356. while (more_data)
  357. {
  358.  
  359. // read in employee first and last name
  360. printf ("\nEnter employee first name: ");
  361. scanf ("%s", current_ptr->empName.firstName);
  362. printf ("\nEnter employee last name: ");
  363. scanf ("%s", current_ptr->empName.lastName);
  364.  
  365. // read in employee tax state
  366. printf ("\nEnter employee two character tax state: ");
  367. scanf ("%s", current_ptr->taxState);
  368.  
  369. // read in employee clock number
  370. printf("\nEnter employee clock number: ");
  371. scanf("%li", & current_ptr -> clockNumber);
  372.  
  373. // read in employee wage rate
  374. printf("\nEnter employee hourly wage: ");
  375. scanf("%f", & current_ptr -> wageRate);
  376.  
  377. // read in employee hours worked
  378. printf("\nEnter hours worked this week: ");
  379. scanf("%f", & current_ptr -> hours);
  380.  
  381. // ask user if they would like to add another employee
  382. printf("\nWould you like to add another employee? (y/n): ");
  383. scanf("%s", answer);
  384.  
  385. // check first character for a 'Y' for yes
  386. // Ask user if they want to add another employee
  387. if ((value = toupper(answer[0])) != 'Y')
  388. {
  389. // no more employees to process
  390. current_ptr->next = (EMPLOYEE *) NULL;
  391. more_data = 0;
  392. }
  393. else // Yes, another employee
  394. {
  395. // set the next pointer of the current node to point to the new node
  396. current_ptr->next = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
  397. // move the current node pointer to the new node
  398. current_ptr = current_ptr->next;
  399. }
  400.  
  401. } // while
  402.  
  403. return(head_ptr);
  404.  
  405. } // getEmpData
  406.  
  407. //*************************************************************
  408. // Function: isEmployeeSize
  409. //
  410. // Purpose: Traverses the linked list and keeps a running count
  411. // on how many employees are currently in our list.
  412. //
  413. // Parameters:
  414. //
  415. // head_ptr - pointer to the initial node in our linked list
  416. //
  417. // Returns:
  418. //
  419. // theSize - the number of employees in our linked list
  420. //
  421. //**************************************************************
  422.  
  423. int isEmployeeSize (EMPLOYEE * head_ptr)
  424. {
  425.  
  426. EMPLOYEE * current_ptr; // pointer to current node
  427. int theSize; // number of link list nodes
  428. // (i.e., employees)
  429.  
  430. theSize = 0; // initialize
  431.  
  432. // assume there is no data if the first node does
  433. // not have an employee name
  434. if (head_ptr->empName.firstName[0] != '\0')
  435. {
  436.  
  437. // traverse through the linked list, keep a running count of nodes
  438. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  439. {
  440.  
  441. ++theSize; // employee node found, increment
  442.  
  443. } // for
  444. }
  445.  
  446. return (theSize); // number of nodes (i.e., employees)
  447.  
  448.  
  449. } // isEmployeeSize
  450.  
  451. //**************************************************************
  452. // Function: printHeader
  453. //
  454. // Purpose: Prints the initial table header information.
  455. //
  456. // Parameters: none
  457. //
  458. // Returns: void
  459. //
  460. //**************************************************************
  461.  
  462. void printHeader (void)
  463. {
  464.  
  465. printf ("\n\n*** Pay Calculator ***\n");
  466.  
  467. // print the table header
  468. printf("\n--------------------------------------------------------------");
  469. printf("-------------------");
  470. printf("\nName Tax Clock# Wage Hours OT Gross ");
  471. printf(" State Fed Net");
  472. printf("\n State Pay ");
  473. printf(" Tax Tax Pay");
  474.  
  475. printf("\n--------------------------------------------------------------");
  476. printf("-------------------");
  477.  
  478. } // printHeader
  479.  
  480. //*************************************************************
  481. // Function: printEmp
  482. //
  483. // Purpose: Prints out all the information for each employee
  484. // in a nice and orderly table format.
  485. //
  486. // Parameters:
  487. //
  488. // head_ptr - pointer to the beginning of our linked list
  489. //
  490. // Returns: void
  491. //
  492. //**************************************************************
  493.  
  494. void printEmp (EMPLOYEE * head_ptr)
  495. {
  496.  
  497.  
  498. // Used to format the employee name
  499. char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  500.  
  501. EMPLOYEE * current_ptr; // pointer to current node
  502.  
  503. // traverse through the linked list to process each employee
  504. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  505. {
  506. // While you could just print the first and last name in the printf
  507. // statement that follows, you could also use various C string library
  508. // functions to format the name exactly the way you want it. Breaking
  509. // the name into first and last members additionally gives you some
  510. // flexibility in printing. This also becomes more useful if we decide
  511. // later to store other parts of a person's name. I really did this just
  512. // to show you how to work with some of the common string functions.
  513. strcpy (name, current_ptr->empName.firstName);
  514. strcat (name, " "); // add a space between first and last names
  515. strcat (name, current_ptr->empName.lastName);
  516.  
  517. // Print out current employee in the current linked list node
  518. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  519. name, current_ptr->taxState, current_ptr->clockNumber,
  520. current_ptr->wageRate, current_ptr->hours,
  521. current_ptr->overtimeHrs, current_ptr->grossPay,
  522. current_ptr->stateTax, current_ptr->fedTax,
  523. current_ptr->netPay);
  524.  
  525. } // for
  526.  
  527. } // printEmp
  528.  
  529. //*************************************************************
  530. // Function: printEmpStatistics
  531. //
  532. // Purpose: Prints out the summary totals and averages of all
  533. // floating point value items for all employees
  534. // that have been processed. It also prints
  535. // out the min and max values.
  536. //
  537. // Parameters:
  538. //
  539. // emp_totals_ptr - pointer to a structure containing a running total
  540. // of all employee floating point items
  541. //
  542. // emp_minMax_ptr - pointer to a structure containing
  543. // the minimum and maximum values of all
  544. // employee floating point items
  545. //
  546. // tjeSize - the total number of employees processed, used
  547. // to check for zero or negative divide condition.
  548. //
  549. // Returns: void
  550. //
  551. //**************************************************************
  552.  
  553. // TODO - Update the emp_MinMax_ptr parameter below to use the MIN_MAX
  554. // typedef alias
  555.  
  556. //updated the parameter to MIN_MAX typedef
  557. void printEmpStatistics (TOTALS * emp_totals_ptr,
  558. MIN_MAX * emp_minMax_ptr,
  559. int theSize)
  560. {
  561.  
  562. // print a separator line
  563. printf("\n--------------------------------------------------------------");
  564. printf("-------------------");
  565.  
  566. // print the totals for all the floating point items
  567. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  568. emp_totals_ptr->total_wageRate,
  569. emp_totals_ptr->total_hours,
  570. emp_totals_ptr->total_overtimeHrs,
  571. emp_totals_ptr->total_grossPay,
  572. emp_totals_ptr->total_stateTax,
  573. emp_totals_ptr->total_fedTax,
  574. emp_totals_ptr->total_netPay);
  575.  
  576. // make sure you don't divide by zero or a negative number
  577. if (theSize > 0)
  578. {
  579. // print the averages for all the floating point items
  580. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  581. emp_totals_ptr->total_wageRate/theSize,
  582. emp_totals_ptr->total_hours/theSize,
  583. emp_totals_ptr->total_overtimeHrs/theSize,
  584. emp_totals_ptr->total_grossPay/theSize,
  585. emp_totals_ptr->total_stateTax/theSize,
  586. emp_totals_ptr->total_fedTax/theSize,
  587. emp_totals_ptr->total_netPay/theSize);
  588.  
  589. } // if
  590.  
  591. // print the min and max values for each item
  592.  
  593. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  594. emp_minMax_ptr->min_wageRate,
  595. emp_minMax_ptr->min_hours,
  596. emp_minMax_ptr->min_overtimeHrs,
  597. emp_minMax_ptr->min_grossPay,
  598. emp_minMax_ptr->min_stateTax,
  599. emp_minMax_ptr->min_fedTax,
  600. emp_minMax_ptr->min_netPay);
  601.  
  602. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  603. emp_minMax_ptr->max_wageRate,
  604. emp_minMax_ptr->max_hours,
  605. emp_minMax_ptr->max_overtimeHrs,
  606. emp_minMax_ptr->max_grossPay,
  607. emp_minMax_ptr->max_stateTax,
  608. emp_minMax_ptr->max_fedTax,
  609. emp_minMax_ptr->max_netPay);
  610.  
  611. // print out the total employees process
  612. printf ("\n\nThe total employees processed was: %i\n", theSize);
  613.  
  614. } // printEmpStatistics
  615.  
  616. //*************************************************************
  617. // Function: calcOvertimeHrs
  618. //
  619. // Purpose: Calculates the overtime hours worked by an employee
  620. // in a given week for each employee.
  621. //
  622. // Parameters:
  623. //
  624. // head_ptr - pointer to the beginning of our linked list
  625. //
  626. // Returns: void (the overtime hours gets updated by reference)
  627. //
  628. //**************************************************************
  629.  
  630. void calcOvertimeHrs (EMPLOYEE * head_ptr)
  631. {
  632.  
  633. EMPLOYEE * current_ptr; // pointer to current node
  634.  
  635. // traverse through the linked list to calculate overtime hours
  636. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  637. {
  638. current_ptr->overtimeHrs = CALC_OT_HOURS(current_ptr->hours);
  639.  
  640. } // for
  641.  
  642.  
  643. } // calcOvertimeHrs
  644.  
  645. //*************************************************************
  646. // Function: calcGrossPay
  647. //
  648. // Purpose: Calculates the gross pay based on the the normal pay
  649. // and any overtime pay for a given week for each
  650. // employee.
  651. //
  652. // Parameters:
  653. //
  654. // head_ptr - pointer to the beginning of our linked list
  655. //
  656. // Returns: void (the gross pay gets updated by reference)
  657. //
  658. //**************************************************************
  659.  
  660. void calcGrossPay (EMPLOYEE * head_ptr)
  661. {
  662.  
  663. float theNormalPay; // normal pay without any overtime hours
  664. float theOvertimePay; // overtime pay
  665.  
  666. EMPLOYEE * current_ptr; // pointer to current node
  667.  
  668. // traverse through the linked list to calculate gross pay
  669. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  670. {
  671. // calculate normal pay and any overtime pay
  672. theNormalPay = CALC_NORMAL_PAY(current_ptr->wageRate,
  673. current_ptr->hours,
  674. current_ptr->overtimeHrs);
  675. theOvertimePay = CALC_OT_PAY(current_ptr->wageRate,
  676. current_ptr->overtimeHrs);
  677.  
  678. // calculate gross pay for employee as normalPay + any overtime pay
  679. current_ptr->grossPay = theNormalPay + theOvertimePay;
  680.  
  681. }
  682.  
  683. } // calcGrossPay
  684.  
  685. //*************************************************************
  686. // Function: calcStateTax
  687. //
  688. // Purpose: Calculates the State Tax owed based on gross pay
  689. // for each employee. State tax rate is based on the
  690. // the designated tax state based on where the
  691. // employee is actually performing the work. Each
  692. // state decides their tax rate.
  693. //
  694. // Parameters:
  695. //
  696. // head_ptr - pointer to the beginning of our linked list
  697. //
  698. // Returns: void (the state tax gets updated by reference)
  699. //
  700. //**************************************************************
  701.  
  702. void calcStateTax (EMPLOYEE * head_ptr)
  703. {
  704.  
  705. EMPLOYEE * current_ptr; // pointer to current node
  706.  
  707. // traverse through the linked list to calculate the state tax
  708. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  709. {
  710. // Make sure tax state is all uppercase
  711. if (islower(current_ptr->taxState[0]))
  712. current_ptr->taxState[0] = toupper(current_ptr->taxState[0]);
  713. if (islower(current_ptr->taxState[1]))
  714. current_ptr->taxState[1] = toupper(current_ptr->taxState[1]);
  715.  
  716. // calculate state tax based on where employee resides
  717. if (strcmp(current_ptr->taxState, "MA") == 0)
  718. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  719. MA_TAX_RATE);
  720. else if (strcmp(current_ptr->taxState, "VT") == 0)
  721. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  722. VT_TAX_RATE);
  723. else if (strcmp(current_ptr->taxState, "NH") == 0)
  724. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  725. NH_TAX_RATE);
  726. else if (strcmp(current_ptr->taxState, "CA") == 0)
  727. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  728. CA_TAX_RATE);
  729. else
  730. // any other state is the default rate
  731. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  732. DEFAULT_STATE_TAX_RATE);
  733.  
  734. } // for
  735.  
  736. } // calcStateTax
  737.  
  738. //*************************************************************
  739. // Function: calcFedTax
  740. //
  741. // Purpose: Calculates the Federal Tax owed based on the gross
  742. // pay for each employee
  743. //
  744. // Parameters:
  745. //
  746. // head_ptr - pointer to the beginning of our linked list
  747. //
  748. // Returns: void (the federal tax gets updated by reference)
  749. //
  750. //**************************************************************
  751.  
  752. void calcFedTax (EMPLOYEE * head_ptr)
  753. {
  754.  
  755. EMPLOYEE * current_ptr; // pointer to current node
  756.  
  757. // traverse through the linked list to calculate the federal tax
  758. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  759. {
  760.  
  761. // TODO - Replace the below statement after the "=" with
  762. // a call to the CALC_FED_TAX macro you created
  763.  
  764. // Fed Tax is the same for all regardless of state
  765. // used the new macro to compute federal tax
  766. current_ptr->fedTax = CALC_FED_TAX(current_ptr->grossPay, FED_TAX_RATE);
  767.  
  768. } // for
  769.  
  770. } // calcFedTax
  771.  
  772. //*************************************************************
  773. // Function: calcNetPay
  774. //
  775. // Purpose: Calculates the net pay as the gross pay minus any
  776. // state and federal taxes owed for each employee.
  777. // Essentially, their "take home" pay.
  778. //
  779. // Parameters:
  780. //
  781. // head_ptr - pointer to the beginning of our linked list
  782. //
  783. // Returns: void (the net pay gets updated by reference)
  784. //
  785. //**************************************************************
  786.  
  787. void calcNetPay (EMPLOYEE * head_ptr)
  788. {
  789.  
  790. EMPLOYEE * current_ptr; // pointer to current node
  791.  
  792. // traverse through the linked list to calculate the net pay
  793. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  794. {
  795. // calculate the net pay
  796. current_ptr->netPay = CALC_NET_PAY(current_ptr->grossPay,
  797. current_ptr->stateTax,
  798. current_ptr->fedTax);
  799. } // for
  800.  
  801. } // calcNetPay
  802.  
  803. //*************************************************************
  804. // Function: calcEmployeeTotals
  805. //
  806. // Purpose: Performs a running total (sum) of each employee
  807. // floating point member item stored in our linked list
  808. //
  809. // Parameters:
  810. //
  811. // head_ptr - pointer to the beginning of our linked list
  812. // emp_totals_ptr - pointer to a structure containing the
  813. // running totals of each floating point
  814. // member for all employees in our linked
  815. // list
  816. //
  817. // Returns:
  818. //
  819. // void (the employeeTotals structure gets updated by reference)
  820. //
  821. //**************************************************************
  822.  
  823. void calcEmployeeTotals (EMPLOYEE * head_ptr,
  824. TOTALS * emp_totals_ptr)
  825. {
  826.  
  827. EMPLOYEE * current_ptr; // pointer to current node
  828.  
  829. // traverse through the linked list to calculate a running
  830. // sum of each employee floating point member item
  831. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  832. {
  833. // add current employee data to our running totals
  834. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  835. emp_totals_ptr->total_hours += current_ptr->hours;
  836. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  837. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  838. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  839. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  840. emp_totals_ptr->total_netPay += current_ptr->netPay;
  841.  
  842. // Note: We don't need to increment emp_totals_ptr
  843.  
  844. } // for
  845.  
  846. // no need to return anything since we used pointers and have
  847. // been referencing the linked list stored in the Heap area.
  848. // Since we used a pointer as well to the totals structure,
  849. // all values in it have been updated.
  850.  
  851. } // calcEmployeeTotals
  852.  
  853. //*************************************************************
  854. // Function: calcEmployeeMinMax
  855. //
  856. // Purpose: Accepts various floating point values from an
  857. // employee and adds to a running update of min
  858. // and max values
  859. //
  860. // Parameters:
  861. //
  862. // head_ptr - pointer to the beginning of our linked list
  863. // emp_minMax_ptr - pointer to the min/max structure
  864. //
  865. // Returns:
  866. //
  867. // void (employeeMinMax structure updated by reference)
  868. //
  869. //**************************************************************
  870.  
  871. // TODO - Update the emp_minMax_ptr parameter below to use the
  872. // the MIN_MAX typedef alias
  873.  
  874. //updated the parameter to MIN_MAX typedef
  875. void calcEmployeeMinMax (EMPLOYEE * head_ptr,
  876. MIN_MAX * emp_minMax_ptr)
  877. {
  878.  
  879. EMPLOYEE * current_ptr; // pointer to current node
  880.  
  881. // *************************************************
  882. // At this point, head_ptr is pointing to the first
  883. // employee .. the first node of our linked list
  884. //
  885. // As this is the first employee, set each min
  886. // min and max value using our emp_minMax_ptr
  887. // to the associated member fields below. They
  888. // will become the initial baseline that we
  889. // can check and update if needed against the
  890. // remaining employees in our linked list.
  891. // *************************************************
  892.  
  893.  
  894. // set to first employee, our initial linked list node
  895. current_ptr = head_ptr;
  896.  
  897. // set the min to the first employee members
  898. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  899. emp_minMax_ptr->min_hours = current_ptr->hours;
  900. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  901. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  902. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  903. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  904. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  905.  
  906. // set the max to the first employee members
  907. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  908. emp_minMax_ptr->max_hours = current_ptr->hours;
  909. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  910. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  911. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  912. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  913. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  914.  
  915. // ******************************************************
  916. // move to the next employee
  917. //
  918. // if this the only employee in our linked list
  919. // current_ptr will be NULL and will drop out the
  920. // the for loop below, otherwise, the second employee
  921. // and rest of the employees (if any) will be processed
  922. // ******************************************************
  923. current_ptr = current_ptr->next;
  924.  
  925. // traverse the linked list
  926. // compare the rest of the employees to each other for min and max
  927. for (; current_ptr; current_ptr = current_ptr->next)
  928. {
  929.  
  930. // check if current Wage Rate is the new min and/or max
  931. emp_minMax_ptr->min_wageRate =
  932. CALC_MIN(current_ptr->wageRate,emp_minMax_ptr->min_wageRate);
  933. emp_minMax_ptr->max_wageRate =
  934. CALC_MAX(current_ptr->wageRate,emp_minMax_ptr->max_wageRate);
  935.  
  936. // check if current Hours is the new min and/or max
  937. emp_minMax_ptr->min_hours =
  938. CALC_MIN(current_ptr->hours,emp_minMax_ptr->min_hours);
  939. emp_minMax_ptr->max_hours =
  940. CALC_MAX(current_ptr->hours,emp_minMax_ptr->max_hours);
  941.  
  942. // check if current Overtime Hours is the new min and/or max
  943. emp_minMax_ptr->min_overtimeHrs =
  944. CALC_MIN(current_ptr->overtimeHrs,emp_minMax_ptr->min_overtimeHrs);
  945. emp_minMax_ptr->max_overtimeHrs =
  946. CALC_MAX(current_ptr->overtimeHrs,emp_minMax_ptr->max_overtimeHrs);
  947.  
  948. // check if current Gross Pay is the new min and/or max
  949. emp_minMax_ptr->min_grossPay =
  950. CALC_MIN(current_ptr->grossPay,emp_minMax_ptr->min_grossPay);
  951. emp_minMax_ptr->max_grossPay =
  952. CALC_MAX(current_ptr->grossPay,emp_minMax_ptr->max_grossPay);
  953.  
  954. // check if current State Tax is the new min and/or max
  955. emp_minMax_ptr->min_stateTax =
  956. CALC_MIN(current_ptr->stateTax,emp_minMax_ptr->min_stateTax);
  957. emp_minMax_ptr->max_stateTax =
  958. CALC_MAX(current_ptr->stateTax,emp_minMax_ptr->max_stateTax);
  959.  
  960. // check if current Federal Tax is the new min and/or max
  961. emp_minMax_ptr->min_fedTax =
  962. CALC_MIN(current_ptr->fedTax,emp_minMax_ptr->min_fedTax);
  963. emp_minMax_ptr->max_fedTax =
  964. CALC_MAX(current_ptr->fedTax,emp_minMax_ptr->max_fedTax);
  965.  
  966. // check if current Net Pay is the new min and/or max
  967. emp_minMax_ptr->min_netPay =
  968. CALC_MIN(current_ptr->netPay,emp_minMax_ptr->min_netPay);
  969. emp_minMax_ptr->max_netPay =
  970. CALC_MAX(current_ptr->netPay,emp_minMax_ptr->max_netPay);
  971.  
  972. } // for
  973.  
  974. // no need to return anything since we used pointers and have
  975. // been referencing all the nodes in our linked list where
  976. // they reside in memory (the Heap area)
  977.  
  978. } // calcEmployeeMinMax
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 ***