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

The total employees processed was: 5


 *** End of Program ***