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

*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net
                   State                           Pay     Tax    Tax      Pay
---------------------------------------------------------------------------------
Connie Cobol         MA  098401 10.60  51.0  11.0  598.90  35.93   89.84   473.13
Mary Apl             NH  526488  9.75  42.5   2.5  426.56  25.59   42.66   358.31
Frank Fortran        VT  765349 10.50  37.0   0.0  388.50  23.31   38.85   326.34
Jeff Ada             NY  034645 12.25  45.0   5.0  581.88  40.73   87.28   453.86
Anton Pascal         CA  127615  8.35  40.0   0.0  334.00  26.72   33.40   273.88
---------------------------------------------------------------------------------
Totals:                         51.45 215.5  18.5 2329.84 152.29  292.02  1885.53
Averages:                       10.29  43.1   3.7  465.97  30.46   58.40   377.11
Minimum:                         8.35  37.0   0.0  334.00  23.31   33.40   273.88
Maximum:                        12.25  51.0  11.0  598.90  40.73   89.84   473.13

The total employees processed was: 5


 *** End of Program ***