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