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

*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net
                   State                           Pay     Tax    Tax      Pay
---------------------------------------------------------------------------------
Connie Cobol         MA  098401 10.60  51.0  11.0  598.90  29.95  149.73   419.23
Mary Apl             NH  526488  9.75  42.5   2.5  426.56   0.00  106.64   319.92
Frank Fortran        VT  765349 10.50  37.0   0.0  388.50  23.31   97.12   268.07
Jeff Ada             NY  034645 12.25  45.0   5.0  581.88  46.55  145.47   389.86
Anton Pascal         CA  127615  8.35  40.0   0.0  334.00  23.38   83.50   227.12
---------------------------------------------------------------------------------
Totals:                         51.45 215.5  18.5 2329.84 123.18  582.46  1624.19
Averages:                       10.29  43.1   3.7  465.97  24.64  116.49   324.84
Minimum:                         8.35  37.0   0.0  334.00   0.00   83.50   227.12
Maximum:                        12.25  51.0  11.0  598.90  46.55  149.73   419.23

The total employees processed was: 5


 *** End of Program ***