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

*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net
                   State                           Pay     Tax    Tax      Pay
---------------------------------------------------------------------------------
Connie Cobol         MA  098401 10.60  51.0  11.0  598.90   0.00    0.00   598.90
Mary Apl             NH  526488  9.75  42.5   2.5  426.56   0.00    0.00   426.56
Frank Fortran        VT  765349 10.50  37.0   0.0  388.50   0.00    0.00   388.50
Jeff Ada             NY  034645 12.25  45.0   5.0  581.88   0.00    0.00   581.88
Anton Pascal         CA  127615  8.35  40.0   0.0  334.00   0.00    0.00   334.00
---------------------------------------------------------------------------------
Totals:                         51.45 215.5  18.5 2329.84   0.00    0.00  2329.84
Averages:                       10.29  43.1   3.7  465.97   0.00    0.00   465.97
Minimum:                         8.35  37.0   0.0  334.00   0.00    0.00   334.00
Maximum:                        12.25  51.0  11.0  598.90   0.00    0.00   598.90

The total employees processed was: 5


 *** End of Program ***