fork download
  1. //********************************************************
  2. //
  3. // Assignment 9 - Linked Lists
  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. // Call by Reference design (using pointers)
  25. //
  26. //********************************************************
  27.  
  28. // necessary header files
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <ctype.h> // for char functions
  32. #include <stdlib.h> // for malloc
  33.  
  34. // define constants
  35. #define STD_HOURS 40.0
  36. #define OT_RATE 1.5
  37. #define MA_TAX_RATE 0.05
  38. #define NH_TAX_RATE 0.0
  39. #define VT_TAX_RATE 0.06
  40. #define CA_TAX_RATE 0.07
  41. #define DEFAULT_TAX_RATE 0.08
  42. #define NAME_SIZE 20
  43. #define TAX_STATE_SIZE 3
  44. #define FED_TAX_RATE 0.25
  45. #define FIRST_NAME_SIZE 10
  46. #define LAST_NAME_SIZE 10
  47.  
  48. // Define a global structure type to store an employee name
  49. struct name
  50. {
  51. char firstName[FIRST_NAME_SIZE];
  52. char lastName [LAST_NAME_SIZE];
  53. };
  54.  
  55. // Define a global structure type to pass employee data between functions
  56. struct employee
  57. {
  58. struct name empName;
  59. char taxState [TAX_STATE_SIZE];
  60. long int clockNumber;
  61. float wageRate;
  62. float hours;
  63. float overtimeHrs;
  64. float grossPay;
  65. float stateTax;
  66. float fedTax;
  67. float netPay;
  68. struct employee * next;
  69. };
  70.  
  71. // this structure type defines the totals of all floating point items
  72. struct totals
  73. {
  74. float total_wageRate;
  75. float total_hours;
  76. float total_overtimeHrs;
  77. float total_grossPay;
  78. float total_stateTax;
  79. float total_fedTax;
  80. float total_netPay;
  81. };
  82.  
  83. // this structure type defines the min and max values of all floating
  84. struct min_max
  85. {
  86. float min_wageRate;
  87. float min_hours;
  88. float min_overtimeHrs;
  89. float min_grossPay;
  90. float min_stateTax;
  91. float min_fedTax;
  92. float min_netPay;
  93. float max_wageRate;
  94. float max_hours;
  95. float max_overtimeHrs;
  96. float max_grossPay;
  97. float max_stateTax;
  98. float max_fedTax;
  99. float max_netPay;
  100. };
  101.  
  102. // define prototypes here for each function except main
  103. struct employee * getEmpData (void);
  104. int isEmployeeSize (struct employee * head_ptr);
  105. void calcOvertimeHrs (struct employee * head_ptr);
  106. void calcGrossPay (struct employee * head_ptr);
  107. void printHeader (void);
  108. void printEmp (struct employee * head_ptr);
  109. void calcStateTax (struct employee * head_ptr);
  110. void calcFedTax (struct employee * head_ptr);
  111. void calcNetPay (struct employee * head_ptr);
  112. void calcEmployeeTotals (struct employee * head_ptr,
  113. struct totals * emp_totals_ptr);
  114.  
  115. void calcEmployeeMinMax (struct employee * head_ptr,
  116. struct min_max * emp_minMax_ptr);
  117.  
  118. void printEmpStatistics (struct totals * emp_totals_ptr,
  119. struct min_max * emp_minMax_ptr,
  120. int theSize);
  121.  
  122. int main ()
  123. {
  124.  
  125. struct employee * head_ptr; // always points to first linked list node
  126. int theSize; // number of employees processed
  127.  
  128. struct totals employeeTotals = {0,0,0,0,0,0,0};
  129. struct totals * emp_totals_ptr = &employeeTotals;
  130.  
  131. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  132. struct min_max * emp_minMax_ptr = &employeeMinMax;
  133.  
  134. head_ptr = getEmpData ();
  135.  
  136. theSize = isEmployeeSize (head_ptr);
  137.  
  138. if (theSize <= 0)
  139. {
  140. printf("\n\n**** There was no employee input to process ***\n");
  141. }
  142.  
  143. else
  144. {
  145. calcOvertimeHrs (head_ptr);
  146. calcGrossPay (head_ptr);
  147. calcStateTax (head_ptr);
  148. calcFedTax (head_ptr);
  149. calcNetPay (head_ptr);
  150. calcEmployeeTotals (head_ptr, &employeeTotals);
  151. calcEmployeeMinMax (head_ptr, &employeeMinMax);
  152. printHeader();
  153. printEmp (head_ptr);
  154. printEmpStatistics (&employeeTotals, &employeeMinMax, theSize);
  155. }
  156.  
  157. printf ("\n\n *** End of Program *** \n");
  158. return (0);
  159. }
  160.  
  161. //**************************************************************
  162. // Function: getEmpData
  163. struct employee * getEmpData (void)
  164. {
  165. char answer[80];
  166. int more_data = 1;
  167. char value;
  168.  
  169. struct employee *current_ptr, *head_ptr;
  170. head_ptr = (struct employee *) malloc (sizeof(struct employee));
  171. current_ptr = head_ptr;
  172.  
  173. while (more_data)
  174. {
  175. printf ("\nEnter employee first name: ");
  176. scanf ("%s", current_ptr->empName.firstName);
  177. printf ("\nEnter employee last name: ");
  178. scanf ("%s", current_ptr->empName.lastName);
  179.  
  180. printf ("\nEnter employee two character tax state: ");
  181. scanf ("%s", current_ptr->taxState);
  182.  
  183. printf("\nEnter employee clock number: ");
  184. scanf("%li", & current_ptr -> clockNumber);
  185.  
  186. printf("\nEnter employee hourly wage: ");
  187. scanf("%f", & current_ptr -> wageRate);
  188.  
  189. printf("\nEnter hours worked this week: ");
  190. scanf("%f", & current_ptr -> hours);
  191.  
  192. printf("\nWould you like to add another employee? (y/n): ");
  193. scanf("%s", answer);
  194.  
  195. if ((value = toupper(answer[0])) != 'Y')
  196. {
  197. current_ptr->next = (struct employee *) NULL;
  198. more_data = 0;
  199. }
  200. else
  201. {
  202. current_ptr->next = (struct employee *) malloc (sizeof(struct employee));
  203. current_ptr = current_ptr->next;
  204. }
  205. }
  206. return(head_ptr);
  207. }
  208.  
  209. //*************************************************************
  210. int isEmployeeSize (struct employee * head_ptr)
  211. {
  212. struct employee * current_ptr;
  213. int theSize;
  214. theSize = 0;
  215.  
  216. if (head_ptr->empName.firstName[0] != '\0')
  217. {
  218. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  219. {
  220. ++theSize;
  221. }
  222. }
  223. return (theSize);
  224. }
  225.  
  226. //**************************************************************
  227. void printHeader (void)
  228. {
  229. printf ("\n\n*** Pay Calculator ***\n");
  230. printf("\n--------------------------------------------------------------");
  231. printf("-------------------");
  232. printf("\nName Tax Clock# Wage Hours OT Gross ");
  233. printf(" State Fed Net");
  234. printf("\n State Pay ");
  235. printf(" Tax Tax Pay");
  236. printf("\n--------------------------------------------------------------");
  237. printf("-------------------");
  238. }
  239.  
  240. //*************************************************************
  241. void printEmp (struct employee * head_ptr)
  242. {
  243. char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  244. struct employee * current_ptr;
  245.  
  246. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  247. {
  248. strcpy (name, current_ptr->empName.firstName);
  249. strcat (name, " ");
  250. strcat (name, current_ptr->empName.lastName);
  251.  
  252. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  253. name, current_ptr->taxState, current_ptr->clockNumber,
  254. current_ptr->wageRate, current_ptr->hours,
  255. current_ptr->overtimeHrs, current_ptr->grossPay,
  256. current_ptr->stateTax, current_ptr->fedTax,
  257. current_ptr->netPay);
  258. }
  259. }
  260.  
  261. //*************************************************************
  262. void printEmpStatistics (struct totals * emp_totals_ptr,
  263. struct min_max * emp_minMax_ptr,
  264. int theSize)
  265. {
  266. printf("\n--------------------------------------------------------------");
  267. printf("-------------------");
  268.  
  269. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  270. emp_totals_ptr->total_wageRate,
  271. emp_totals_ptr->total_hours,
  272. emp_totals_ptr->total_overtimeHrs,
  273. emp_totals_ptr->total_grossPay,
  274. emp_totals_ptr->total_stateTax,
  275. emp_totals_ptr->total_fedTax,
  276. emp_totals_ptr->total_netPay);
  277.  
  278. if (theSize > 0)
  279. {
  280. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  281. emp_totals_ptr->total_wageRate/theSize,
  282. emp_totals_ptr->total_hours/theSize,
  283. emp_totals_ptr->total_overtimeHrs/theSize,
  284. emp_totals_ptr->total_grossPay/theSize,
  285. emp_totals_ptr->total_stateTax/theSize,
  286. emp_totals_ptr->total_fedTax/theSize,
  287. emp_totals_ptr->total_netPay/theSize);
  288. }
  289.  
  290. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  291. emp_minMax_ptr->min_wageRate,
  292. emp_minMax_ptr->min_hours,
  293. emp_minMax_ptr->min_overtimeHrs,
  294. emp_minMax_ptr->min_grossPay,
  295. emp_minMax_ptr->min_stateTax,
  296. emp_minMax_ptr->min_fedTax,
  297. emp_minMax_ptr->min_netPay);
  298.  
  299. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  300. emp_minMax_ptr->max_wageRate,
  301. emp_minMax_ptr->max_hours,
  302. emp_minMax_ptr->max_overtimeHrs,
  303. emp_minMax_ptr->max_grossPay,
  304. emp_minMax_ptr->max_stateTax,
  305. emp_minMax_ptr->max_fedTax,
  306. emp_minMax_ptr->max_netPay);
  307.  
  308. printf ("\n\nThe total employees processed was: %i\n", theSize);
  309. }
  310.  
  311. //*************************************************************
  312. void calcOvertimeHrs (struct employee * head_ptr)
  313. {
  314. struct employee * current_ptr;
  315. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  316. {
  317. if (current_ptr->hours >= STD_HOURS)
  318. current_ptr->overtimeHrs = current_ptr->hours - STD_HOURS;
  319. else
  320. current_ptr->overtimeHrs = 0;
  321. }
  322. }
  323.  
  324. //*************************************************************
  325. void calcGrossPay (struct employee * head_ptr)
  326. {
  327. float theNormalPay;
  328. float theOvertimePay;
  329. struct employee * current_ptr;
  330.  
  331. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  332. {
  333. theNormalPay = current_ptr->wageRate * (current_ptr->hours - current_ptr->overtimeHrs);
  334. theOvertimePay = current_ptr->overtimeHrs * (OT_RATE * current_ptr->wageRate);
  335. current_ptr->grossPay = theNormalPay + theOvertimePay;
  336. }
  337. }
  338.  
  339. //*************************************************************
  340. void calcStateTax (struct employee * head_ptr)
  341. {
  342. struct employee * current_ptr;
  343.  
  344. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  345. {
  346. if (islower(current_ptr->taxState[0]))
  347. current_ptr->taxState[0] = toupper(current_ptr->taxState[0]);
  348. if (islower(current_ptr->taxState[1]))
  349. current_ptr->taxState[1] = toupper(current_ptr->taxState[1]);
  350.  
  351. if (strcmp(current_ptr->taxState, "MA") == 0)
  352. current_ptr->stateTax = current_ptr->grossPay * MA_TAX_RATE;
  353. else if (strcmp(current_ptr->taxState, "NH") == 0)
  354. current_ptr->stateTax = current_ptr->grossPay * NH_TAX_RATE;
  355. else if (strcmp(current_ptr->taxState, "VT") == 0)
  356. current_ptr->stateTax = current_ptr->grossPay * VT_TAX_RATE;
  357. else if (strcmp(current_ptr->taxState, "CA") == 0)
  358. current_ptr->stateTax = current_ptr->grossPay * CA_TAX_RATE;
  359. else
  360. current_ptr->stateTax = current_ptr->grossPay * DEFAULT_TAX_RATE;
  361. }
  362. }
  363.  
  364. //*************************************************************
  365. void calcFedTax (struct employee * head_ptr)
  366. {
  367. struct employee * current_ptr;
  368. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  369. {
  370. current_ptr->fedTax = current_ptr->grossPay * FED_TAX_RATE;
  371. }
  372. }
  373.  
  374. //*************************************************************
  375. void calcNetPay (struct employee * head_ptr)
  376. {
  377. float theTotalTaxes;
  378. struct employee * current_ptr;
  379.  
  380. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  381. {
  382. theTotalTaxes = current_ptr->stateTax + current_ptr->fedTax;
  383. current_ptr->netPay = current_ptr->grossPay - theTotalTaxes;
  384. }
  385. }
  386.  
  387. //*************************************************************
  388. void calcEmployeeTotals (struct employee * head_ptr,
  389. struct totals * emp_totals_ptr)
  390. {
  391. struct employee * current_ptr;
  392.  
  393. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  394. {
  395. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  396. emp_totals_ptr->total_hours += current_ptr->hours;
  397. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  398. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  399. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  400. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  401. emp_totals_ptr->total_netPay += current_ptr->netPay;
  402. }
  403. }
  404.  
  405. //*************************************************************
  406. void calcEmployeeMinMax (struct employee * head_ptr,
  407. struct min_max * emp_minMax_ptr)
  408. {
  409. struct employee * current_ptr;
  410. current_ptr = head_ptr;
  411.  
  412. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  413. emp_minMax_ptr->min_hours = current_ptr->hours;
  414. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  415. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  416. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  417. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  418. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  419.  
  420. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  421. emp_minMax_ptr->max_hours = current_ptr->hours;
  422. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  423. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  424. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  425. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  426. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  427.  
  428. current_ptr = current_ptr->next;
  429.  
  430. for (; current_ptr; current_ptr = current_ptr->next)
  431. {
  432. if (current_ptr->wageRate < emp_minMax_ptr->min_wageRate)
  433. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  434. if (current_ptr->wageRate > emp_minMax_ptr->max_wageRate)
  435. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  436.  
  437. if (current_ptr->hours < emp_minMax_ptr->min_hours)
  438. emp_minMax_ptr->min_hours = current_ptr->hours;
  439. if (current_ptr->hours > emp_minMax_ptr->max_hours)
  440. emp_minMax_ptr->max_hours = current_ptr->hours;
  441.  
  442. if (current_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
  443. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  444. if (current_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
  445. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  446.  
  447. if (current_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  448. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  449. if (current_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  450. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  451.  
  452. if (current_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  453. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  454. if (current_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  455. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  456.  
  457. if (current_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  458. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  459. if (current_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  460. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  461.  
  462. if (current_ptr->netPay < emp_minMax_ptr->min_netPay)
  463. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  464. if (current_ptr->netPay > emp_minMax_ptr->max_netPay)
  465. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  466. }
  467. }
Success #stdin #stdout 0s 5316KB
stdin
Connie
Cobol
MA
98401
10.60
51.0
Y
Mary
Apl
NH
526488
9.75
42.5
Y
Frank
Fortran
VT
765349
10.50
37.0
Y
Jeff
Ada
NY
34645
12.25
45
Y
Anton
Pascal
CA
127615
8.35
40.0
N
stdout
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 

*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net
                   State                           Pay     Tax    Tax      Pay
---------------------------------------------------------------------------------
Connie Cobol         MA  098401 10.60  51.0  11.0  598.90  29.95  149.73   419.23
Mary Apl             NH  526488  9.75  42.5   2.5  426.56   0.00  106.64   319.92
Frank Fortran        VT  765349 10.50  37.0   0.0  388.50  23.31   97.12   268.07
Jeff Ada             NY  034645 12.25  45.0   5.0  581.88  46.55  145.47   389.86
Anton Pascal         CA  127615  8.35  40.0   0.0  334.00  23.38   83.50   227.12
---------------------------------------------------------------------------------
Totals:                         51.45 215.5  18.5 2329.84 123.18  582.46  1624.19
Averages:                       10.29  43.1   3.7  465.97  24.64  116.49   324.84
Minimum:                         8.35  37.0   0.0  334.00   0.00   83.50   227.12
Maximum:                        12.25  51.0  11.0  598.90  46.55  149.73   419.23

The total employees processed was: 5


 *** End of Program ***