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. #define CALC_FED_TAX(thePay) (thePay * FED_TAX_RATE)
  57. #define CALC_NET_PAY(thePay,theStateTax,theFedTax) (thePay - (theStateTax + theFedTax))
  58. #define CALC_NORMAL_PAY(theWageRate,theHours,theOvertimeHrs) \
  59. (theWageRate * (theHours - theOvertimeHrs))
  60. #define CALC_OT_PAY(theWageRate,theOvertimeHrs) (theOvertimeHrs * (OT_RATE * theWageRate))
  61. #define CALC_MIN(theValue, currentMin) ((theValue < currentMin) ? theValue : currentMin)
  62. #define CALC_MAX(theValue, currentMax) ((theValue > currentMax) ? theValue : currentMax)
  63.  
  64. // Define a global structure type to store an employee name
  65. struct name
  66. {
  67. char firstName[FIRST_NAME_SIZE];
  68. char lastName [LAST_NAME_SIZE];
  69. };
  70.  
  71. // Define a global structure type to pass employee data between functions
  72. typedef struct employee
  73. {
  74. struct name empName;
  75. char taxState [TAX_STATE_SIZE];
  76. long int clockNumber;
  77. float wageRate;
  78. float hours;
  79. float overtimeHrs;
  80. float grossPay;
  81. float stateTax;
  82. float fedTax;
  83. float netPay;
  84. struct employee * next;
  85. } EMPLOYEE;
  86.  
  87. // This structure type defines the totals of all floating point items
  88. typedef struct totals
  89. {
  90. float total_wageRate;
  91. float total_hours;
  92. float total_overtimeHrs;
  93. float total_grossPay;
  94. float total_stateTax;
  95. float total_fedTax;
  96. float total_netPay;
  97. } TOTALS;
  98.  
  99. // This structure type defines the min and max values
  100. typedef struct min_max
  101. {
  102. float min_wageRate;
  103. float min_hours;
  104. float min_overtimeHrs;
  105. float min_grossPay;
  106. float min_stateTax;
  107. float min_fedTax;
  108. float min_netPay;
  109. float max_wageRate;
  110. float max_hours;
  111. float max_overtimeHrs;
  112. float max_grossPay;
  113. float max_stateTax;
  114. float max_fedTax;
  115. float max_netPay;
  116. } MIN_MAX;
  117.  
  118. // Define prototypes here for each function except main
  119. EMPLOYEE * getEmpData (void);
  120. int isEmployeeSize (EMPLOYEE * head_ptr);
  121. void calcOvertimeHrs (EMPLOYEE * head_ptr);
  122. void calcGrossPay (EMPLOYEE * head_ptr);
  123. void printHeader (void);
  124. void printEmp (EMPLOYEE * head_ptr);
  125. void calcStateTax (EMPLOYEE * head_ptr);
  126. void calcFedTax (EMPLOYEE * head_ptr);
  127. void calcNetPay (EMPLOYEE * head_ptr);
  128. void calcEmployeeTotals (EMPLOYEE * head_ptr,
  129. TOTALS * emp_totals_ptr);
  130. void calcEmployeeMinMax (EMPLOYEE * head_ptr,
  131. MIN_MAX * emp_minMax_ptr);
  132. void printEmpStatistics (TOTALS * emp_totals_ptr,
  133. MIN_MAX * emp_minMax_ptr,
  134. int size);
  135.  
  136. int main ()
  137. {
  138. EMPLOYEE * head_ptr; // always points to first linked list node
  139.  
  140. int theSize; // number of employees processed
  141.  
  142. // set up structure to store totals and initialize all to zero
  143. TOTALS employeeTotals = {0,0,0,0,0,0,0};
  144.  
  145. // pointer to the employeeTotals structure
  146. TOTALS * emp_totals_ptr = &employeeTotals;
  147.  
  148. // set up structure to store min and max values and initialize all to zero
  149. MIN_MAX employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  150.  
  151. // pointer to the employeeMinMax structure
  152. MIN_MAX * emp_minMax_ptr = &employeeMinMax;
  153.  
  154. head_ptr = getEmpData ();
  155.  
  156. theSize = isEmployeeSize (head_ptr);
  157.  
  158. if (theSize <= 0)
  159. {
  160. printf("\n\n**** There was no employee input to process ***\n");
  161. }
  162. else
  163. {
  164. calcOvertimeHrs (head_ptr);
  165. calcGrossPay (head_ptr);
  166. calcStateTax (head_ptr);
  167. calcFedTax (head_ptr);
  168. calcNetPay (head_ptr);
  169. calcEmployeeTotals (head_ptr, &employeeTotals);
  170. calcEmployeeMinMax (head_ptr, &employeeMinMax);
  171. printHeader();
  172. printEmp (head_ptr);
  173. printEmpStatistics (&employeeTotals, &employeeMinMax, theSize);
  174. }
  175.  
  176. printf ("\n\n *** End of Program *** \n");
  177.  
  178. return (0);
  179.  
  180. } // main
  181.  
  182. //**************************************************************
  183. // Function: getEmpData
  184. //**************************************************************
  185. EMPLOYEE * getEmpData (void)
  186. {
  187. char answer[80];
  188. int more_data = 1;
  189. char value;
  190.  
  191. EMPLOYEE *current_ptr,
  192. *head_ptr;
  193.  
  194. head_ptr = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
  195. current_ptr = head_ptr;
  196.  
  197. while (more_data)
  198. {
  199. printf ("\nEnter employee first name: ");
  200. scanf ("%s", current_ptr->empName.firstName);
  201.  
  202. printf ("\nEnter employee last name: ");
  203. scanf ("%s", current_ptr->empName.lastName);
  204.  
  205. printf ("\nEnter employee two character tax state: ");
  206. scanf ("%s", current_ptr->taxState);
  207.  
  208. printf("\nEnter employee clock number: ");
  209. scanf("%li", &current_ptr->clockNumber);
  210.  
  211. printf("\nEnter employee hourly wage: ");
  212. scanf("%f", &current_ptr->wageRate);
  213.  
  214. printf("\nEnter hours worked this week: ");
  215. scanf("%f", &current_ptr->hours);
  216.  
  217. printf("\nWould you like to add another employee? (y/n): ");
  218. scanf("%s", answer);
  219.  
  220. if ((value = toupper(answer[0])) != 'Y')
  221. {
  222. current_ptr->next = (EMPLOYEE *) NULL;
  223. more_data = 0;
  224. }
  225. else
  226. {
  227. current_ptr->next = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
  228. current_ptr = current_ptr->next;
  229. }
  230.  
  231. } // while
  232.  
  233. return(head_ptr);
  234.  
  235. } // getEmpData
  236.  
  237. //**************************************************************
  238. // Function: isEmployeeSize
  239. //**************************************************************
  240. int isEmployeeSize (EMPLOYEE * head_ptr)
  241. {
  242. EMPLOYEE * current_ptr;
  243. int theSize;
  244.  
  245. theSize = 0;
  246.  
  247. if (head_ptr->empName.firstName[0] != '\0')
  248. {
  249. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  250. {
  251. ++theSize;
  252. }
  253. }
  254.  
  255. return (theSize);
  256.  
  257. } // isEmployeeSize
  258.  
  259. //**************************************************************
  260. // Function: printHeader
  261. //**************************************************************
  262. void printHeader (void)
  263. {
  264. printf ("\n\n*** Pay Calculator ***\n");
  265.  
  266. printf("\n--------------------------------------------------------------");
  267. printf("-------------------");
  268. printf("\nName Tax Clock# Wage Hours OT Gross ");
  269. printf(" State Fed Net");
  270. printf("\n State Pay ");
  271. printf(" Tax Tax Pay");
  272. printf("\n--------------------------------------------------------------");
  273. printf("-------------------");
  274.  
  275. } // printHeader
  276.  
  277. //**************************************************************
  278. // Function: printEmp
  279. //**************************************************************
  280. void printEmp (EMPLOYEE * head_ptr)
  281. {
  282. char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  283. EMPLOYEE * current_ptr;
  284.  
  285. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  286. {
  287. strcpy (name, current_ptr->empName.firstName);
  288. strcat (name, " ");
  289. strcat (name, current_ptr->empName.lastName);
  290.  
  291. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  292. name, current_ptr->taxState, current_ptr->clockNumber,
  293. current_ptr->wageRate, current_ptr->hours,
  294. current_ptr->overtimeHrs, current_ptr->grossPay,
  295. current_ptr->stateTax, current_ptr->fedTax,
  296. current_ptr->netPay);
  297. }
  298.  
  299. } // printEmp
  300.  
  301. //**************************************************************
  302. // Function: printEmpStatistics
  303. //**************************************************************
  304. void printEmpStatistics (TOTALS * emp_totals_ptr,
  305. MIN_MAX * emp_minMax_ptr,
  306. int theSize)
  307. {
  308. printf("\n--------------------------------------------------------------");
  309. printf("-------------------");
  310.  
  311. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  312. emp_totals_ptr->total_wageRate,
  313. emp_totals_ptr->total_hours,
  314. emp_totals_ptr->total_overtimeHrs,
  315. emp_totals_ptr->total_grossPay,
  316. emp_totals_ptr->total_stateTax,
  317. emp_totals_ptr->total_fedTax,
  318. emp_totals_ptr->total_netPay);
  319.  
  320. if (theSize > 0)
  321. {
  322. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  323. emp_totals_ptr->total_wageRate/theSize,
  324. emp_totals_ptr->total_hours/theSize,
  325. emp_totals_ptr->total_overtimeHrs/theSize,
  326. emp_totals_ptr->total_grossPay/theSize,
  327. emp_totals_ptr->total_stateTax/theSize,
  328. emp_totals_ptr->total_fedTax/theSize,
  329. emp_totals_ptr->total_netPay/theSize);
  330. }
  331.  
  332. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  333. emp_minMax_ptr->min_wageRate,
  334. emp_minMax_ptr->min_hours,
  335. emp_minMax_ptr->min_overtimeHrs,
  336. emp_minMax_ptr->min_grossPay,
  337. emp_minMax_ptr->min_stateTax,
  338. emp_minMax_ptr->min_fedTax,
  339. emp_minMax_ptr->min_netPay);
  340.  
  341. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  342. emp_minMax_ptr->max_wageRate,
  343. emp_minMax_ptr->max_hours,
  344. emp_minMax_ptr->max_overtimeHrs,
  345. emp_minMax_ptr->max_grossPay,
  346. emp_minMax_ptr->max_stateTax,
  347. emp_minMax_ptr->max_fedTax,
  348. emp_minMax_ptr->max_netPay);
  349.  
  350. printf ("\n\nThe total employees processed was: %i\n", theSize);
  351.  
  352. } // printEmpStatistics
  353.  
  354. //**************************************************************
  355. // Function: calcOvertimeHrs
  356. //**************************************************************
  357. void calcOvertimeHrs (EMPLOYEE * head_ptr)
  358. {
  359. EMPLOYEE * current_ptr;
  360.  
  361. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  362. {
  363. current_ptr->overtimeHrs = CALC_OT_HOURS(current_ptr->hours);
  364. }
  365.  
  366. } // calcOvertimeHrs
  367.  
  368. //**************************************************************
  369. // Function: calcGrossPay
  370. //**************************************************************
  371. void calcGrossPay (EMPLOYEE * head_ptr)
  372. {
  373. float theNormalPay;
  374. float theOvertimePay;
  375.  
  376. EMPLOYEE * current_ptr;
  377.  
  378. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  379. {
  380. theNormalPay = CALC_NORMAL_PAY(current_ptr->wageRate,
  381. current_ptr->hours,
  382. current_ptr->overtimeHrs);
  383.  
  384. theOvertimePay = CALC_OT_PAY(current_ptr->wageRate,
  385. current_ptr->overtimeHrs);
  386.  
  387. current_ptr->grossPay = theNormalPay + theOvertimePay;
  388. }
  389.  
  390. } // calcGrossPay
  391.  
  392. //**************************************************************
  393. // Function: calcStateTax
  394. //**************************************************************
  395. void calcStateTax (EMPLOYEE * head_ptr)
  396. {
  397. EMPLOYEE * current_ptr;
  398.  
  399. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  400. {
  401. if (islower(current_ptr->taxState[0]))
  402. current_ptr->taxState[0] = toupper(current_ptr->taxState[0]);
  403. if (islower(current_ptr->taxState[1]))
  404. current_ptr->taxState[1] = toupper(current_ptr->taxState[1]);
  405.  
  406. if (strcmp(current_ptr->taxState, "MA") == 0)
  407. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  408. MA_TAX_RATE);
  409. else if (strcmp(current_ptr->taxState, "VT") == 0)
  410. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  411. VT_TAX_RATE);
  412. else if (strcmp(current_ptr->taxState, "NH") == 0)
  413. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  414. NH_TAX_RATE);
  415. else if (strcmp(current_ptr->taxState, "CA") == 0)
  416. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  417. CA_TAX_RATE);
  418. else
  419. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay,
  420. DEFAULT_STATE_TAX_RATE);
  421. }
  422.  
  423. } // calcStateTax
  424.  
  425. //**************************************************************
  426. // Function: calcFedTax
  427. //**************************************************************
  428. void calcFedTax (EMPLOYEE * head_ptr)
  429. {
  430. EMPLOYEE * current_ptr;
  431.  
  432. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  433. {
  434. current_ptr->fedTax = CALC_FED_TAX(current_ptr->grossPay);
  435. }
  436.  
  437. } // calcFedTax
  438.  
  439. //**************************************************************
  440. // Function: calcNetPay
  441. //**************************************************************
  442. void calcNetPay (EMPLOYEE * head_ptr)
  443. {
  444. EMPLOYEE * current_ptr;
  445.  
  446. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  447. {
  448. current_ptr->netPay = CALC_NET_PAY(current_ptr->grossPay,
  449. current_ptr->stateTax,
  450. current_ptr->fedTax);
  451. }
  452.  
  453. } // calcNetPay
  454.  
  455. //**************************************************************
  456. // Function: calcEmployeeTotals
  457. //**************************************************************
  458. void calcEmployeeTotals (EMPLOYEE * head_ptr,
  459. TOTALS * emp_totals_ptr)
  460. {
  461. EMPLOYEE * current_ptr;
  462.  
  463. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  464. {
  465. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  466. emp_totals_ptr->total_hours += current_ptr->hours;
  467. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  468. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  469. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  470. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  471. emp_totals_ptr->total_netPay += current_ptr->netPay;
  472. }
  473.  
  474. } // calcEmployeeTotals
  475.  
  476. //**************************************************************
  477. // Function: calcEmployeeMinMax
  478. //**************************************************************
  479. void calcEmployeeMinMax (EMPLOYEE * head_ptr,
  480. MIN_MAX * emp_minMax_ptr)
  481. {
  482. EMPLOYEE * current_ptr;
  483.  
  484. current_ptr = head_ptr;
  485.  
  486. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  487. emp_minMax_ptr->min_hours = current_ptr->hours;
  488. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  489. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  490. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  491. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  492. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  493.  
  494. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  495. emp_minMax_ptr->max_hours = current_ptr->hours;
  496. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  497. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  498. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  499. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  500. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  501.  
  502. current_ptr = current_ptr->next;
  503.  
  504. for (; current_ptr; current_ptr = current_ptr->next)
  505. {
  506. emp_minMax_ptr->min_wageRate =
  507. CALC_MIN(current_ptr->wageRate, emp_minMax_ptr->min_wageRate);
  508. emp_minMax_ptr->max_wageRate =
  509. CALC_MAX(current_ptr->wageRate, emp_minMax_ptr->max_wageRate);
  510.  
  511. emp_minMax_ptr->min_hours =
  512. CALC_MIN(current_ptr->hours, emp_minMax_ptr->min_hours);
  513. emp_minMax_ptr->max_hours =
  514. CALC_MAX(current_ptr->hours, emp_minMax_ptr->max_hours);
  515.  
  516. emp_minMax_ptr->min_overtimeHrs =
  517. CALC_MIN(current_ptr->overtimeHrs, emp_minMax_ptr->min_overtimeHrs);
  518. emp_minMax_ptr->max_overtimeHrs =
  519. CALC_MAX(current_ptr->overtimeHrs, emp_minMax_ptr->max_overtimeHrs);
  520.  
  521. emp_minMax_ptr->min_grossPay =
  522. CALC_MIN(current_ptr->grossPay, emp_minMax_ptr->min_grossPay);
  523. emp_minMax_ptr->max_grossPay =
  524. CALC_MAX(current_ptr->grossPay, emp_minMax_ptr->max_grossPay);
  525.  
  526. emp_minMax_ptr->min_stateTax =
  527. CALC_MIN(current_ptr->stateTax, emp_minMax_ptr->min_stateTax);
  528. emp_minMax_ptr->max_stateTax =
  529. CALC_MAX(current_ptr->stateTax, emp_minMax_ptr->max_stateTax);
  530.  
  531. emp_minMax_ptr->min_fedTax =
  532. CALC_MIN(current_ptr->fedTax, emp_minMax_ptr->min_fedTax);
  533. emp_minMax_ptr->max_fedTax =
  534. CALC_MAX(current_ptr->fedTax, emp_minMax_ptr->max_fedTax);
  535.  
  536. emp_minMax_ptr->min_netPay =
  537. CALC_MIN(current_ptr->netPay, emp_minMax_ptr->min_netPay);
  538. emp_minMax_ptr->max_netPay =
  539. CALC_MAX(current_ptr->netPay, emp_minMax_ptr->max_netPay);
  540. }
  541.  
  542. } // calcEmployeeMinMax
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
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): 

**** There was no employee input to process ***


 *** End of Program ***