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