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