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

The total employees processed was: 5


 *** End of Program ***