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