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

The total employees processed was: 5


 *** End of Program ***