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