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