fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. #define SIZE 5
  7. #define STD_HOURS 40.0
  8. #define OT_RATE 1.5
  9. #define MA_TAX_RATE 0.05
  10. #define NH_TAX_RATE 0.0
  11. #define VT_TAX_RATE 0.06
  12. #define CA_TAX_RATE 0.07
  13. #define DEFAULT_TAX_RATE 0.08
  14. #define TAX_STATE_SIZE 3
  15. #define FED_TAX_RATE 0.25
  16. #define FIRST_NAME_SIZE 20
  17. #define LAST_NAME_SIZE 20
  18.  
  19. struct name
  20. {
  21. char firstName[FIRST_NAME_SIZE];
  22. char lastName[LAST_NAME_SIZE];
  23. };
  24.  
  25. struct employee
  26. {
  27. struct name empName;
  28. char taxState[TAX_STATE_SIZE];
  29. long int clockNumber;
  30. float wageRate;
  31. float hours;
  32. float overtimeHrs;
  33. float grossPay;
  34. float stateTax;
  35. float fedTax;
  36. float netPay;
  37. struct employee *next;
  38. };
  39.  
  40. struct totals
  41. {
  42. float total_wageRate;
  43. float total_hours;
  44. float total_overtimeHrs;
  45. float total_grossPay;
  46. float total_stateTax;
  47. float total_fedTax;
  48. float total_netPay;
  49. };
  50.  
  51. struct min_max
  52. {
  53. float min_wageRate;
  54. float min_hours;
  55. float min_overtimeHrs;
  56. float min_grossPay;
  57. float min_stateTax;
  58. float min_fedTax;
  59. float min_netPay;
  60. float max_wageRate;
  61. float max_hours;
  62. float max_overtimeHrs;
  63. float max_grossPay;
  64. float max_stateTax;
  65. float max_fedTax;
  66. float max_netPay;
  67. };
  68.  
  69. struct employee *getEmpData(int theSize);
  70. void printEmp(struct employee *emp_ptr);
  71.  
  72. void calcOvertimeHrs(struct employee *emp_ptr);
  73. void calcGrossPay(struct employee *emp_ptr);
  74. void calcStateTax(struct employee *emp_ptr);
  75. void calcFedTax(struct employee *emp_ptr);
  76. void calcNetPay(struct employee *emp_ptr);
  77.  
  78. void calcEmployeeTotals(struct employee *emp_ptr,
  79. struct totals *emp_totals_ptr);
  80.  
  81. void calcEmployeeMinMax(struct employee *emp_ptr,
  82. struct min_max *emp_MinMax_ptr);
  83.  
  84. void printHeader(void);
  85. void printEmpStatistics(struct totals *employeeTotals_ptr,
  86. struct min_max *employeeMinMax_ptr,
  87. int theSize);
  88.  
  89. int main()
  90. {
  91. struct employee *head_ptr = NULL;
  92. struct employee *free_ptr = NULL;
  93.  
  94. struct totals employeeTotals = {0, 0, 0, 0, 0, 0, 0};
  95. struct totals *emp_totals_ptr = &employeeTotals;
  96.  
  97. struct min_max employeeMinMax = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  98. struct min_max *employeeMinMax_ptr = &employeeMinMax;
  99.  
  100. head_ptr = getEmpData(SIZE);
  101.  
  102. calcOvertimeHrs(head_ptr);
  103. calcGrossPay(head_ptr);
  104. calcStateTax(head_ptr);
  105. calcFedTax(head_ptr);
  106. calcNetPay(head_ptr);
  107.  
  108. calcEmployeeTotals(head_ptr, emp_totals_ptr);
  109. calcEmployeeMinMax(head_ptr, employeeMinMax_ptr);
  110.  
  111. printHeader();
  112. printEmp(head_ptr);
  113. printEmpStatistics(emp_totals_ptr, employeeMinMax_ptr, SIZE);
  114.  
  115. while (head_ptr != NULL)
  116. {
  117. free_ptr = head_ptr;
  118. head_ptr = head_ptr->next;
  119. free(free_ptr);
  120. }
  121.  
  122. return 0;
  123. }
  124.  
  125. struct employee *getEmpData(int theSize)
  126. {
  127. struct employee *head_ptr = NULL;
  128. struct employee *tail_ptr = NULL;
  129. struct employee *emp_ptr = NULL;
  130. char response[10];
  131. int i;
  132.  
  133. for (i = 0; i < theSize; ++i)
  134. {
  135. emp_ptr = (struct employee *)malloc(sizeof(struct employee));
  136. if (emp_ptr == NULL)
  137. {
  138. printf("Memory allocation failed.\n");
  139. return head_ptr;
  140. }
  141.  
  142. printf("Enter first name: ");
  143. scanf("%19s", emp_ptr->empName.firstName);
  144.  
  145. printf("Enter last name: ");
  146. scanf("%19s", emp_ptr->empName.lastName);
  147.  
  148. printf("Enter tax state: ");
  149. scanf("%2s", emp_ptr->taxState);
  150.  
  151. printf("Enter clock number: ");
  152. scanf("%ld", &emp_ptr->clockNumber);
  153.  
  154. printf("Enter wage rate: ");
  155. scanf("%f", &emp_ptr->wageRate);
  156.  
  157. printf("Enter hours worked: ");
  158. scanf("%f", &emp_ptr->hours);
  159.  
  160. // Read and discard trailing Y/N confirmation prompt if present in input
  161. printf("Would you like to enter another employee? (Y/N): ");
  162. scanf("%9s", response);
  163.  
  164. emp_ptr->overtimeHrs = 0;
  165. emp_ptr->grossPay = 0;
  166. emp_ptr->stateTax = 0;
  167. emp_ptr->fedTax = 0;
  168. emp_ptr->netPay = 0;
  169. emp_ptr->next = NULL;
  170.  
  171. if (head_ptr == NULL)
  172. {
  173. head_ptr = emp_ptr;
  174. tail_ptr = emp_ptr;
  175. }
  176. else
  177. {
  178. tail_ptr->next = emp_ptr;
  179. tail_ptr = emp_ptr;
  180. }
  181. }
  182.  
  183. return head_ptr;
  184. }
  185.  
  186. void printHeader(void)
  187. {
  188. printf("\n*** Pay Calculator ***\n");
  189. printf("\n---------------------------------------------------------------------------------");
  190. printf("\nName Tax Clock# Wage Hours OT Gross State Fed Net");
  191. printf("\n State Pay Tax Tax Pay");
  192. printf("\n---------------------------------------------------------------------------------");
  193. }
  194.  
  195. void printEmp(struct employee *emp_ptr)
  196. {
  197. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 2];
  198.  
  199. while (emp_ptr != NULL)
  200. {
  201. snprintf(name, sizeof(name), "%s %s", emp_ptr->empName.firstName, emp_ptr->empName.lastName);
  202.  
  203. printf("\n%-20.20s %-2s %06ld %6.2f %4.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  204. name, emp_ptr->taxState, emp_ptr->clockNumber,
  205. emp_ptr->wageRate, emp_ptr->hours, emp_ptr->overtimeHrs,
  206. emp_ptr->grossPay, emp_ptr->stateTax,
  207. emp_ptr->fedTax, emp_ptr->netPay);
  208.  
  209. emp_ptr = emp_ptr->next;
  210. }
  211. }
  212.  
  213. void printEmpStatistics(struct totals *employeeTotals_ptr,
  214. struct min_max *employeeMinMax_ptr,
  215. int theSize)
  216. {
  217. printf("\n---------------------------------------------------------------------------------");
  218.  
  219. printf("\nTotals: %6.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  220. employeeTotals_ptr->total_wageRate,
  221. employeeTotals_ptr->total_hours,
  222. employeeTotals_ptr->total_overtimeHrs,
  223. employeeTotals_ptr->total_grossPay,
  224. employeeTotals_ptr->total_stateTax,
  225. employeeTotals_ptr->total_fedTax,
  226. employeeTotals_ptr->total_netPay);
  227.  
  228. printf("\nAverages: %6.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  229. employeeTotals_ptr->total_wageRate / theSize,
  230. employeeTotals_ptr->total_hours / theSize,
  231. employeeTotals_ptr->total_overtimeHrs / theSize,
  232. employeeTotals_ptr->total_grossPay / theSize,
  233. employeeTotals_ptr->total_stateTax / theSize,
  234. employeeTotals_ptr->total_fedTax / theSize,
  235. employeeTotals_ptr->total_netPay / theSize);
  236.  
  237. printf("\nMinimum: %6.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  238. employeeMinMax_ptr->min_wageRate,
  239. employeeMinMax_ptr->min_hours,
  240. employeeMinMax_ptr->min_overtimeHrs,
  241. employeeMinMax_ptr->min_grossPay,
  242. employeeMinMax_ptr->min_stateTax,
  243. employeeMinMax_ptr->min_fedTax,
  244. employeeMinMax_ptr->min_netPay);
  245.  
  246. printf("\nMaximum: %6.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f\n",
  247. employeeMinMax_ptr->max_wageRate,
  248. employeeMinMax_ptr->max_hours,
  249. employeeMinMax_ptr->max_overtimeHrs,
  250. employeeMinMax_ptr->max_grossPay,
  251. employeeMinMax_ptr->max_stateTax,
  252. employeeMinMax_ptr->max_fedTax,
  253. employeeMinMax_ptr->max_netPay);
  254. }
  255.  
  256. void calcOvertimeHrs(struct employee *emp_ptr)
  257. {
  258. while (emp_ptr != NULL)
  259. {
  260. emp_ptr->overtimeHrs = (emp_ptr->hours > STD_HOURS) ? (emp_ptr->hours - STD_HOURS) : 0;
  261. emp_ptr = emp_ptr->next;
  262. }
  263. }
  264.  
  265. void calcGrossPay(struct employee *emp_ptr)
  266. {
  267. while (emp_ptr != NULL)
  268. {
  269. emp_ptr->grossPay = (emp_ptr->wageRate * emp_ptr->hours) + (emp_ptr->overtimeHrs * emp_ptr->wageRate * 0.5f);
  270. emp_ptr = emp_ptr->next;
  271. }
  272. }
  273.  
  274. void calcStateTax(struct employee *emp_ptr)
  275. {
  276. while (emp_ptr != NULL)
  277. {
  278. if (strcmp(emp_ptr->taxState, "MA") == 0)
  279. emp_ptr->stateTax = emp_ptr->grossPay * MA_TAX_RATE;
  280. else if (strcmp(emp_ptr->taxState, "NH") == 0)
  281. emp_ptr->stateTax = emp_ptr->grossPay * NH_TAX_RATE;
  282. else if (strcmp(emp_ptr->taxState, "VT") == 0)
  283. emp_ptr->stateTax = emp_ptr->grossPay * VT_TAX_RATE;
  284. else if (strcmp(emp_ptr->taxState, "CA") == 0)
  285. emp_ptr->stateTax = emp_ptr->grossPay * CA_TAX_RATE;
  286. else
  287. emp_ptr->stateTax = emp_ptr->grossPay * DEFAULT_TAX_RATE;
  288.  
  289. emp_ptr = emp_ptr->next;
  290. }
  291. }
  292.  
  293. void calcFedTax(struct employee *emp_ptr)
  294. {
  295. while (emp_ptr != NULL)
  296. {
  297. emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
  298. emp_ptr = emp_ptr->next;
  299. }
  300. }
  301.  
  302. void calcNetPay(struct employee *emp_ptr)
  303. {
  304. while (emp_ptr != NULL)
  305. {
  306. emp_ptr->netPay = emp_ptr->grossPay - emp_ptr->stateTax - emp_ptr->fedTax;
  307. emp_ptr = emp_ptr->next;
  308. }
  309. }
  310.  
  311. void calcEmployeeTotals(struct employee *emp_ptr,
  312. struct totals *emp_totals_ptr)
  313. {
  314. while (emp_ptr != NULL)
  315. {
  316. emp_totals_ptr->total_wageRate += emp_ptr->wageRate;
  317. emp_totals_ptr->total_hours += emp_ptr->hours;
  318. emp_totals_ptr->total_overtimeHrs += emp_ptr->overtimeHrs;
  319. emp_totals_ptr->total_grossPay += emp_ptr->grossPay;
  320. emp_totals_ptr->total_stateTax += emp_ptr->stateTax;
  321. emp_totals_ptr->total_fedTax += emp_ptr->fedTax;
  322. emp_totals_ptr->total_netPay += emp_ptr->netPay;
  323. emp_ptr = emp_ptr->next;
  324. }
  325. }
  326.  
  327. void calcEmployeeMinMax(struct employee *emp_ptr,
  328. struct min_max *emp_minMax_ptr)
  329. {
  330. if (emp_ptr == NULL) return;
  331.  
  332. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  333. emp_minMax_ptr->min_hours = emp_ptr->hours;
  334. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  335. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  336. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  337. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  338. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  339.  
  340. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  341. emp_minMax_ptr->max_hours = emp_ptr->hours;
  342. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  343. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  344. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  345. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  346. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  347.  
  348. emp_ptr = emp_ptr->next;
  349.  
  350. while (emp_ptr != NULL)
  351. {
  352. if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate) emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  353. if (emp_ptr->hours < emp_minMax_ptr->min_hours) emp_minMax_ptr->min_hours = emp_ptr->hours;
  354. if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs) emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  355. if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay) emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  356. if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax) emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  357. if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax) emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  358. if (emp_ptr->netPay < emp_minMax_ptr->min_netPay) emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  359.  
  360. if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate) emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  361. if (emp_ptr->hours > emp_minMax_ptr->max_hours) emp_minMax_ptr->max_hours = emp_ptr->hours;
  362. if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs) emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  363. if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay) emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  364. if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax) emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  365. if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax) emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  366. if (emp_ptr->netPay > emp_minMax_ptr->max_netPay) emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  367.  
  368. emp_ptr = emp_ptr->next;
  369. }
  370. }
Success #stdin #stdout 0.01s 5312KB
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 first name: Enter last name: Enter tax state: Enter clock number: Enter wage rate: Enter hours worked: Would you like to enter another employee? (Y/N): Enter first name: Enter last name: Enter tax state: Enter clock number: Enter wage rate: Enter hours worked: Would you like to enter another employee? (Y/N): Enter first name: Enter last name: Enter tax state: Enter clock number: Enter wage rate: Enter hours worked: Would you like to enter another employee? (Y/N): Enter first name: Enter last name: Enter tax state: Enter clock number: Enter wage rate: Enter hours worked: Would you like to enter another employee? (Y/N): Enter first name: Enter last name: Enter tax state: Enter clock number: Enter wage rate: Enter hours worked: Would you like to enter 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