fork(1) 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.  
  61. #define CALC_NET_PAY(thePay,theStateTax,theFedTax) (thePay - (theStateTax + theFedTax))
  62. #define CALC_NORMAL_PAY(theWageRate,theHours,theOvertimeHrs) \
  63. (theWageRate * (theHours - theOvertimeHrs))
  64. #define CALC_OT_PAY(theWageRate,theOvertimeHrs) (theOvertimeHrs * (OT_RATE * theWageRate))
  65.  
  66. // TODO - These two macros are missing the correct logic, they are just setting
  67. // things to zero at this point. Replace the 0.0 value below with the
  68. // right logic to determine the min and max values. These macros would
  69. // work very similar to the CALC_OT_HOURS macro above using a
  70. // conditional expression operator. The calls to these macros in the
  71. // calcEmployeeMinMax function are already correct
  72. // ... so no changes needed there.
  73.  
  74. #define CALC_MIN(theValue, currentMin) (0.0)
  75. #define CALC_MAX(theValue, currentMax) (0.0)
  76.  
  77. // Define a global structure type to store an employee name
  78. // ... note how one could easily extend this to other parts
  79. // parts of a name: Middle, Nickname, Prefix, Suffix, etc.
  80. struct name
  81. {
  82. char firstName[FIRST_NAME_SIZE];
  83. char lastName [LAST_NAME_SIZE];
  84. };
  85.  
  86. // Define a global structure type to pass employee data between functions
  87. // Note that the structure type is global, but you don't want a variable
  88. // of that type to be global. Best to declare a variable of that type
  89. // in a function like main or another function and pass as needed.
  90.  
  91. // Note the "next" member has been added as a pointer to structure employee.
  92. // This allows us to point to another data item of this same type,
  93. // allowing us to set up and traverse through all the linked
  94. // list nodes, with each node containing the employee information below.
  95.  
  96. // Also note the use of typedef to create an alias for struct employee
  97. typedef struct employee
  98. {
  99. struct name empName;
  100. char taxState [TAX_STATE_SIZE];
  101. long int clockNumber;
  102. float wageRate;
  103. float hours;
  104. float overtimeHrs;
  105. float grossPay;
  106. float stateTax;
  107. float fedTax;
  108. float netPay;
  109. struct employee * next;
  110. } EMPLOYEE;
  111.  
  112. // This structure type defines the totals of all floating point items
  113. // so they can be totaled and used also to calculate averages
  114.  
  115. // Also note the use of typedef to create an alias for struct totals
  116. typedef struct totals
  117. {
  118. float total_wageRate;
  119. float total_hours;
  120. float total_overtimeHrs;
  121. float total_grossPay;
  122. float total_stateTax;
  123. float total_fedTax;
  124. float total_netPay;
  125. } TOTALS;
  126.  
  127. // This structure type defines the min and max values of all floating
  128. // point items so they can be display in our final report
  129.  
  130. // Also note the use of typedef to create an alias for struct min_max
  131.  
  132. // TODO - Add a typedef alias to this structure, call it: MIN_MAX
  133. // Then update all associated code (prototypes plus the main,
  134. // printEmpStatistics and calcEmployeeMinMax functions) that reference
  135. // "struct min_max". Essentially, replacing "struct min_max" with the
  136. // typedef alias MIN_MAX
  137.  
  138. struct min_max
  139. {
  140. float min_wageRate;
  141. float min_hours;
  142. float min_overtimeHrs;
  143. float min_grossPay;
  144. float min_stateTax;
  145. float min_fedTax;
  146. float min_netPay;
  147. float max_wageRate;
  148. float max_hours;
  149. float max_overtimeHrs;
  150. float max_grossPay;
  151. float max_stateTax;
  152. float max_fedTax;
  153. float max_netPay;
  154. };
  155.  
  156. // Define prototypes here for each function except main
  157. //
  158. // Note the use of the typedef alias values throughout
  159. // the rest of this program, starting with the fucntions
  160. // prototypes
  161. //
  162. // EMPLOYEE instead of struct employee
  163. // TOTALS instead of struct totals
  164. // MIN_MAX instead of struct min_max
  165.  
  166. EMPLOYEE * getEmpData (void);
  167. int isEmployeeSize (EMPLOYEE * head_ptr);
  168. void calcOvertimeHrs (EMPLOYEE * head_ptr);
  169. void calcGrossPay (EMPLOYEE * head_ptr);
  170. void printHeader (void);
  171. void printEmp (EMPLOYEE * head_ptr);
  172. void calcStateTax (EMPLOYEE * head_ptr);
  173. void calcFedTax (EMPLOYEE * head_ptr);
  174. void calcNetPay (EMPLOYEE * head_ptr);
  175. void calcEmployeeTotals (EMPLOYEE * head_ptr,
  176. TOTALS * emp_totals_ptr);
  177.  
  178. // TODO - Update these two prototypes with the MIN_MAX typedef alias
  179. void calcEmployeeMinMax (EMPLOYEE * head_ptr,
  180. struct min_max * emp_minMax_ptr);
  181.  
  182. void printEmpStatistics (TOTALS * emp_totals_ptr,
  183. struct min_max * emp_minMax_ptr,
  184. int size);
  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. struct 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. struct 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. struct min_max * emp_minMax_ptr,
  551. int theSize)
  552. {
  553.  
  554. // print a separator line
  555. printf("\n--------------------------------------------------------------");
  556. printf("-------------------");
  557.  
  558. // print the totals for all the floating point items
  559. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  560. emp_totals_ptr->total_wageRate,
  561. emp_totals_ptr->total_hours,
  562. emp_totals_ptr->total_overtimeHrs,
  563. emp_totals_ptr->total_grossPay,
  564. emp_totals_ptr->total_stateTax,
  565. emp_totals_ptr->total_fedTax,
  566. emp_totals_ptr->total_netPay);
  567.  
  568. // make sure you don't divide by zero or a negative number
  569. if (theSize > 0)
  570. {
  571. // print the averages for all the floating point items
  572. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  573. emp_totals_ptr->total_wageRate/theSize,
  574. emp_totals_ptr->total_hours/theSize,
  575. emp_totals_ptr->total_overtimeHrs/theSize,
  576. emp_totals_ptr->total_grossPay/theSize,
  577. emp_totals_ptr->total_stateTax/theSize,
  578. emp_totals_ptr->total_fedTax/theSize,
  579. emp_totals_ptr->total_netPay/theSize);
  580.  
  581. } // if
  582.  
  583. // print the min and max values for each item
  584.  
  585. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  586. emp_minMax_ptr->min_wageRate,
  587. emp_minMax_ptr->min_hours,
  588. emp_minMax_ptr->min_overtimeHrs,
  589. emp_minMax_ptr->min_grossPay,
  590. emp_minMax_ptr->min_stateTax,
  591. emp_minMax_ptr->min_fedTax,
  592. emp_minMax_ptr->min_netPay);
  593.  
  594. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  595. emp_minMax_ptr->max_wageRate,
  596. emp_minMax_ptr->max_hours,
  597. emp_minMax_ptr->max_overtimeHrs,
  598. emp_minMax_ptr->max_grossPay,
  599. emp_minMax_ptr->max_stateTax,
  600. emp_minMax_ptr->max_fedTax,
  601. emp_minMax_ptr->max_netPay);
  602.  
  603. // print out the total employees process
  604. printf ("\n\nThe total employees processed was: %i\n", theSize);
  605.  
  606. } // printEmpStatistics
  607.  
  608. //*************************************************************
  609. // Function: calcOvertimeHrs
  610. //
  611. // Purpose: Calculates the overtime hours worked by an employee
  612. // in a given week for each employee.
  613. //
  614. // Parameters:
  615. //
  616. // head_ptr - pointer to the beginning of our linked list
  617. //
  618. // Returns: void (the overtime hours gets updated by reference)
  619. //
  620. //**************************************************************
  621.  
  622. void calcOvertimeHrs (EMPLOYEE * head_ptr)
  623. {
  624.  
  625. EMPLOYEE * current_ptr; // pointer to current node
  626.  
  627. // traverse through the linked list to calculate overtime hours
  628. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  629. {
  630. current_ptr->overtimeHrs = CALC_OT_HOURS(current_ptr->hours);
  631.  
  632. } // for
  633.  
  634.  
  635. } // calcOvertimeHrs
  636.  
  637. //*************************************************************
  638. // Function: calcGrossPay
  639. //
  640. // Purpose: Calculates the gross pay based on the the normal pay
  641. // and any overtime pay for a given week for each
  642. // employee.
  643. //
  644. // Parameters:
  645. //
  646. // head_ptr - pointer to the beginning of our linked list
  647. //
  648. // Returns: void (the gross pay gets updated by reference)
  649. //
  650. //**************************************************************
  651.  
  652. void calcGrossPay (EMPLOYEE * head_ptr)
  653. {
  654.  
  655. float theNormalPay; // normal pay without any overtime hours
  656. float theOvertimePay; // overtime pay
  657.  
  658. EMPLOYEE * current_ptr; // pointer to current node
  659.  
  660. // traverse through the linked list to calculate gross pay
  661. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  662. {
  663. // calculate normal pay and any overtime pay
  664. theNormalPay = CALC_NORMAL_PAY(current_ptr->wageRate,
  665. current_ptr->hours,
  666. current_ptr->overtimeHrs);
  667. theOvertimePay = CALC_OT_PAY(current_ptr->wageRate,
  668. current_ptr->overtimeHrs);
  669.  
  670. // calculate gross pay for employee as normalPay + any overtime pay
  671. current_ptr->grossPay = theNormalPay + theOvertimePay;
  672.  
  673. }
  674.  
  675. } // calcGrossPay
  676.  
  677. //*************************************************************
  678. // Function: calcStateTax
  679. //
  680. // Purpose: Calculates the State Tax owed based on gross pay
  681. // for each employee. State tax rate is based on the
  682. // the designated tax state based on where the
  683. // employee is actually performing the work. Each
  684. // state decides their tax rate.
  685. //
  686. // Parameters:
  687. //
  688. // head_ptr - pointer to the beginning of our linked list
  689. //
  690. // Returns: void (the state tax gets updated by reference)
  691. //
  692. //**************************************************************
  693.  
  694. void calcStateTax (EMPLOYEE * head_ptr)
  695. {
  696.  
  697. EMPLOYEE * current_ptr; // pointer to current node
  698.  
  699. // traverse through the linked list to calculate the state tax
  700. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  701. {
  702. // Make sure tax state is all uppercase
  703. if (islower(current_ptr->taxState[0]))
  704. current_ptr->taxState[0] = toupper(current_ptr->taxState[0]);
  705. if (islower(current_ptr->taxState[1]))
  706. current_ptr->taxState[1] = toupper(current_ptr->taxState[1]);
  707.  
  708. // calculate state tax based on where employee resides
  709. if (strcmp(current_ptr->taxState, "MA") == 0)
  710. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  711. MA_TAX_RATE);
  712. else if (strcmp(current_ptr->taxState, "VT") == 0)
  713. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  714. VT_TAX_RATE);
  715. else if (strcmp(current_ptr->taxState, "NH") == 0)
  716. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  717. NH_TAX_RATE);
  718. else if (strcmp(current_ptr->taxState, "CA") == 0)
  719. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  720. CA_TAX_RATE);
  721. else
  722. // any other state is the default rate
  723. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  724. DEFAULT_STATE_TAX_RATE);
  725.  
  726. } // for
  727.  
  728. } // calcStateTax
  729.  
  730. //*************************************************************
  731. // Function: calcFedTax
  732. //
  733. // Purpose: Calculates the Federal Tax owed based on the gross
  734. // pay for each employee
  735. //
  736. // Parameters:
  737. //
  738. // head_ptr - pointer to the beginning of our linked list
  739. //
  740. // Returns: void (the federal tax gets updated by reference)
  741. //
  742. //**************************************************************
  743.  
  744. void calcFedTax (EMPLOYEE * head_ptr)
  745. {
  746.  
  747. EMPLOYEE * current_ptr; // pointer to current node
  748.  
  749. // traverse through the linked list to calculate the federal tax
  750. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  751. {
  752.  
  753. // TODO - Replace the below statement after the "=" with
  754. // a call to the CALC_FED_TAX macro you created
  755.  
  756. // Fed Tax is the same for all regardless of state
  757. current_ptr->fedTax = current_ptr->grossPay * FED_TAX_RATE;
  758.  
  759. } // for
  760.  
  761. } // calcFedTax
  762.  
  763. //*************************************************************
  764. // Function: calcNetPay
  765. //
  766. // Purpose: Calculates the net pay as the gross pay minus any
  767. // state and federal taxes owed for each employee.
  768. // Essentially, their "take home" pay.
  769. //
  770. // Parameters:
  771. //
  772. // head_ptr - pointer to the beginning of our linked list
  773. //
  774. // Returns: void (the net pay gets updated by reference)
  775. //
  776. //**************************************************************
  777.  
  778. void calcNetPay (EMPLOYEE * head_ptr)
  779. {
  780.  
  781. EMPLOYEE * current_ptr; // pointer to current node
  782.  
  783. // traverse through the linked list to calculate the net pay
  784. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  785. {
  786. // calculate the net pay
  787. current_ptr->netPay = CALC_NET_PAY(current_ptr->grossPay,
  788. current_ptr->stateTax,
  789. current_ptr->fedTax);
  790. } // for
  791.  
  792. } // calcNetPay
  793.  
  794. //*************************************************************
  795. // Function: calcEmployeeTotals
  796. //
  797. // Purpose: Performs a running total (sum) of each employee
  798. // floating point member item stored in our linked list
  799. //
  800. // Parameters:
  801. //
  802. // head_ptr - pointer to the beginning of our linked list
  803. // emp_totals_ptr - pointer to a structure containing the
  804. // running totals of each floating point
  805. // member for all employees in our linked
  806. // list
  807. //
  808. // Returns:
  809. //
  810. // void (the employeeTotals structure gets updated by reference)
  811. //
  812. //**************************************************************
  813.  
  814. void calcEmployeeTotals (EMPLOYEE * head_ptr,
  815. TOTALS * emp_totals_ptr)
  816. {
  817.  
  818. EMPLOYEE * current_ptr; // pointer to current node
  819.  
  820. // traverse through the linked list to calculate a running
  821. // sum of each employee floating point member item
  822. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  823. {
  824. // add current employee data to our running totals
  825. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  826. emp_totals_ptr->total_hours += current_ptr->hours;
  827. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  828. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  829. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  830. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  831. emp_totals_ptr->total_netPay += current_ptr->netPay;
  832.  
  833. // Note: We don't need to increment emp_totals_ptr
  834.  
  835. } // for
  836.  
  837. // no need to return anything since we used pointers and have
  838. // been referencing the linked list stored in the Heap area.
  839. // Since we used a pointer as well to the totals structure,
  840. // all values in it have been updated.
  841.  
  842. } // calcEmployeeTotals
  843.  
  844. //*************************************************************
  845. // Function: calcEmployeeMinMax
  846. //
  847. // Purpose: Accepts various floating point values from an
  848. // employee and adds to a running update of min
  849. // and max values
  850. //
  851. // Parameters:
  852. //
  853. // head_ptr - pointer to the beginning of our linked list
  854. // emp_minMax_ptr - pointer to the min/max structure
  855. //
  856. // Returns:
  857. //
  858. // void (employeeMinMax structure updated by reference)
  859. //
  860. //**************************************************************
  861.  
  862. // TODO - Update the emp_minMax_ptr parameter below to use the
  863. // the MIN_MAX typedef alias
  864.  
  865. void calcEmployeeMinMax (EMPLOYEE * head_ptr,
  866. struct min_max * emp_minMax_ptr)
  867. {
  868.  
  869. EMPLOYEE * current_ptr; // pointer to current node
  870.  
  871. // *************************************************
  872. // At this point, head_ptr is pointing to the first
  873. // employee .. the first node of our linked list
  874. //
  875. // As this is the first employee, set each min
  876. // min and max value using our emp_minMax_ptr
  877. // to the associated member fields below. They
  878. // will become the initial baseline that we
  879. // can check and update if needed against the
  880. // remaining employees in our linked list.
  881. // *************************************************
  882.  
  883.  
  884. // set to first employee, our initial linked list node
  885. current_ptr = head_ptr;
  886.  
  887. // set the min to the first employee members
  888. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  889. emp_minMax_ptr->min_hours = current_ptr->hours;
  890. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  891. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  892. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  893. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  894. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  895.  
  896. // set the max to the first employee members
  897. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  898. emp_minMax_ptr->max_hours = current_ptr->hours;
  899. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  900. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  901. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  902. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  903. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  904.  
  905. // ******************************************************
  906. // move to the next employee
  907. //
  908. // if this the only employee in our linked list
  909. // current_ptr will be NULL and will drop out the
  910. // the for loop below, otherwise, the second employee
  911. // and rest of the employees (if any) will be processed
  912. // ******************************************************
  913. current_ptr = current_ptr->next;
  914.  
  915. // traverse the linked list
  916. // compare the rest of the employees to each other for min and max
  917. for (; current_ptr; current_ptr = current_ptr->next)
  918. {
  919.  
  920. // check if current Wage Rate is the new min and/or max
  921. emp_minMax_ptr->min_wageRate =
  922. CALC_MIN(current_ptr->wageRate,emp_minMax_ptr->min_wageRate);
  923. emp_minMax_ptr->max_wageRate =
  924. CALC_MAX(current_ptr->wageRate,emp_minMax_ptr->max_wageRate);
  925.  
  926. // check if current Hours is the new min and/or max
  927. emp_minMax_ptr->min_hours =
  928. CALC_MIN(current_ptr->hours,emp_minMax_ptr->min_hours);
  929. emp_minMax_ptr->max_hours =
  930. CALC_MAX(current_ptr->hours,emp_minMax_ptr->max_hours);
  931.  
  932. // check if current Overtime Hours is the new min and/or max
  933. emp_minMax_ptr->min_overtimeHrs =
  934. CALC_MIN(current_ptr->overtimeHrs,emp_minMax_ptr->min_overtimeHrs);
  935. emp_minMax_ptr->max_overtimeHrs =
  936. CALC_MAX(current_ptr->overtimeHrs,emp_minMax_ptr->max_overtimeHrs);
  937.  
  938. // check if current Gross Pay is the new min and/or max
  939. emp_minMax_ptr->min_grossPay =
  940. CALC_MIN(current_ptr->grossPay,emp_minMax_ptr->min_grossPay);
  941. emp_minMax_ptr->max_grossPay =
  942. CALC_MAX(current_ptr->grossPay,emp_minMax_ptr->max_grossPay);
  943.  
  944. // check if current State Tax is the new min and/or max
  945. emp_minMax_ptr->min_stateTax =
  946. CALC_MIN(current_ptr->stateTax,emp_minMax_ptr->min_stateTax);
  947. emp_minMax_ptr->max_stateTax =
  948. CALC_MAX(current_ptr->stateTax,emp_minMax_ptr->max_stateTax);
  949.  
  950. // check if current Federal Tax is the new min and/or max
  951. emp_minMax_ptr->min_fedTax =
  952. CALC_MIN(current_ptr->fedTax,emp_minMax_ptr->min_fedTax);
  953. emp_minMax_ptr->max_fedTax =
  954. CALC_MAX(current_ptr->fedTax,emp_minMax_ptr->max_fedTax);
  955.  
  956. // check if current Net Pay is the new min and/or max
  957. emp_minMax_ptr->min_netPay =
  958. CALC_MIN(current_ptr->netPay,emp_minMax_ptr->min_netPay);
  959. emp_minMax_ptr->max_netPay =
  960. CALC_MAX(current_ptr->netPay,emp_minMax_ptr->max_netPay);
  961.  
  962. } // for
  963.  
  964. // no need to return anything since we used pointers and have
  965. // been referencing all the nodes in our linked list where
  966. // they reside in memory (the Heap area)
  967.  
  968. } // 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:                         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 ***