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