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