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