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