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

*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net
                   State                           Pay     Tax    Tax      Pay
---------------------------------------------------------------------------------
Connie Cobol         MA  098401 10.60  51.0  11.0  598.90  29.95  149.73   419.23
Mary Apl             NH  526488  9.75  42.5   2.5  426.56   0.00  106.64   319.92
Frank Fortran        VT  765349 10.50  37.0   0.0  388.50  23.31   97.12   268.07
Jeff Ada             NY  034645 12.25  45.0   5.0  581.88  46.55  145.47   389.86
Anton Pascal         CA  127615  8.35  40.0   0.0  334.00  23.38   83.50   227.12
---------------------------------------------------------------------------------
Totals:                         51.45 215.5  18.5 2329.84 123.18  582.46  1624.19
Averages:                       10.29  43.1   3.7  465.97  24.64  116.49   324.84
Minimum:                         8.35  37.0   0.0  334.00   0.00   83.50   227.12
Maximum:                        12.25  51.0  11.0  598.90  46.55  149.73   419.23

The total employees processed was: 5


 *** End of Program ***