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

*** Pay Calculator ***

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

The total employees processed was: 5


 *** End of Program ***