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

The total employees processed was: 5


 *** End of Program ***