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

**** There was no employee input to process ***


 *** End of Program ***