fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. #define SIZE 5
  6. #define STD_HOURS 40.0
  7. #define OT_RATE 1.5
  8. #define MA_TAX_RATE 0.05
  9. #define NH_TAX_RATE 0.0
  10. #define VT_TAX_RATE 0.06
  11. #define CA_TAX_RATE 0.07
  12. #define DEFAULT_TAX_RATE 0.08
  13. #define NAME_SIZE 20
  14. #define TAX_STATE_SIZE 3
  15. #define FED_TAX_RATE 0.25
  16. #define FIRST_NAME_SIZE 10
  17. #define LAST_NAME_SIZE 10
  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. };
  38.  
  39. struct totals
  40. {
  41. float total_wageRate;
  42. float total_hours;
  43. float total_overtimeHrs;
  44. float total_grossPay;
  45. float total_stateTax;
  46. float total_fedTax;
  47. float total_netPay;
  48. };
  49.  
  50. struct min_max
  51. {
  52. float min_wageRate;
  53. float min_hours;
  54. float min_overtimeHrs;
  55. float min_grossPay;
  56. float min_stateTax;
  57. float min_fedTax;
  58. float min_netPay;
  59. float max_wageRate;
  60. float max_hours;
  61. float max_overtimeHrs;
  62. float max_grossPay;
  63. float max_stateTax;
  64. float max_fedTax;
  65. float max_netPay;
  66. };
  67.  
  68. void getHours(struct employee *emp_ptr, int theSize);
  69. void printEmp(struct employee *emp_ptr, int theSize);
  70. void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize);
  71. void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_MinMax_ptr, int theSize);
  72. void printHeader(void);
  73. void calcOvertimeHrs(struct employee *emp_ptr, int theSize);
  74. void calcGrossPay(struct employee *emp_ptr, int theSize);
  75. void calcStateTax(struct employee *emp_ptr, int theSize);
  76. void calcFedTax(struct employee *emp_ptr, int theSize);
  77. void calcNetPay(struct employee *emp_ptr, int theSize);
  78. void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_MinMax_ptr, int theSize);
  79.  
  80. int main()
  81. {
  82. struct employee employeeData[SIZE] = {
  83. {{"Connie", "Cobol"}, "MA", 98401, 10.60},
  84. {{"Mary", "Apl"}, "NH", 526488, 9.75},
  85. {{"Frank", "Fortran"}, "VT", 765349, 10.50},
  86. {{"Jeff", "Ada"}, "NY", 34645, 12.25},
  87. {{"Anton", "Pascal"}, "CA", 127615, 8.35}
  88. };
  89.  
  90. struct employee *emp_ptr = employeeData;
  91. struct totals employeeTotals = {0,0,0,0,0,0,0};
  92. struct totals *emp_totals_ptr = &employeeTotals;
  93. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  94. struct min_max *emp_minMax_ptr = &employeeMinMax;
  95.  
  96. getHours(employeeData, SIZE);
  97. calcOvertimeHrs(employeeData, SIZE);
  98. calcGrossPay(employeeData, SIZE);
  99. calcStateTax(employeeData, SIZE);
  100. calcFedTax(employeeData, SIZE);
  101. calcNetPay(employeeData, SIZE);
  102. calcEmployeeTotals(employeeData, &employeeTotals, SIZE);
  103. calcEmployeeMinMax(employeeData, &employeeMinMax, SIZE);
  104.  
  105. printHeader();
  106. printEmp(employeeData, SIZE);
  107. printEmpStatistics(&employeeTotals, &employeeMinMax, SIZE);
  108.  
  109. return 0;
  110. }
  111.  
  112. void getHours(struct employee *emp_ptr, int theSize)
  113. {
  114. for (int i = 0; i < theSize; ++i)
  115. {
  116. printf("\nEnter hours worked by emp # %06li: ", emp_ptr->clockNumber);
  117. scanf("%f", &emp_ptr->hours);
  118. ++emp_ptr;
  119. }
  120. }
  121.  
  122. void printHeader(void)
  123. {
  124. printf("\n\n*** Pay Calculator ***\n");
  125. printf("\n--------------------------------------------------------------");
  126. printf("-------------------");
  127. printf("\nName Tax Clock# Wage Hours OT Gross ");
  128. printf(" State Fed Net");
  129. printf("\n State Pay ");
  130. printf(" Tax Tax Pay");
  131. printf("\n--------------------------------------------------------------");
  132. printf("-------------------");
  133. }
  134.  
  135. void printEmp(struct employee *emp_ptr, int theSize)
  136. {
  137. for (int i = 0; i < theSize; ++i)
  138. {
  139. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  140. strcpy(name, emp_ptr->empName.firstName);
  141. strcat(name, " ");
  142. strcat(name, emp_ptr->empName.lastName);
  143.  
  144. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  145. name, emp_ptr->taxState, emp_ptr->clockNumber,
  146. emp_ptr->wageRate, emp_ptr->hours,
  147. emp_ptr->overtimeHrs, emp_ptr->grossPay,
  148. emp_ptr->stateTax, emp_ptr->fedTax,
  149. emp_ptr->netPay);
  150.  
  151. ++emp_ptr;
  152. }
  153. }
  154.  
  155. void calcOvertimeHrs(struct employee *emp_ptr, int theSize)
  156. {
  157. for (int i = 0; i < theSize; ++i)
  158. {
  159. if (emp_ptr->hours > STD_HOURS)
  160. emp_ptr->overtimeHrs = emp_ptr->hours - STD_HOURS;
  161. else
  162. emp_ptr->overtimeHrs = 0;
  163. ++emp_ptr;
  164. }
  165. }
  166.  
  167. void calcGrossPay(struct employee *emp_ptr, int theSize)
  168. {
  169. for (int i = 0; i < theSize; ++i)
  170. {
  171. float normalPay = emp_ptr->wageRate * (emp_ptr->hours - emp_ptr->overtimeHrs);
  172. float overtimePay = emp_ptr->overtimeHrs * (OT_RATE * emp_ptr->wageRate);
  173. emp_ptr->grossPay = normalPay + overtimePay;
  174. ++emp_ptr;
  175. }
  176. }
  177.  
  178. void calcStateTax(struct employee *emp_ptr, int theSize)
  179. {
  180. for (int i = 0; i < theSize; ++i)
  181. {
  182. emp_ptr->taxState[0] = toupper(emp_ptr->taxState[0]);
  183. emp_ptr->taxState[1] = toupper(emp_ptr->taxState[1]);
  184.  
  185. if (strcmp(emp_ptr->taxState, "MA") == 0)
  186. emp_ptr->stateTax = emp_ptr->grossPay * MA_TAX_RATE;
  187. else if (strcmp(emp_ptr->taxState, "VT") == 0)
  188. emp_ptr->stateTax = emp_ptr->grossPay * VT_TAX_RATE;
  189. else if (strcmp(emp_ptr->taxState, "NH") == 0)
  190. emp_ptr->stateTax = emp_ptr->grossPay * NH_TAX_RATE;
  191. else if (strcmp(emp_ptr->taxState, "CA") == 0)
  192. emp_ptr->stateTax = emp_ptr->grossPay * CA_TAX_RATE;
  193. else
  194. emp_ptr->stateTax = emp_ptr->grossPay * DEFAULT_TAX_RATE;
  195. ++emp_ptr;
  196. }
  197. }
  198.  
  199. void calcFedTax(struct employee *emp_ptr, int theSize)
  200. {
  201. for (int i = 0; i < theSize; ++i)
  202. {
  203. emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
  204. ++emp_ptr;
  205. }
  206. }
  207.  
  208. void calcNetPay(struct employee *emp_ptr, int theSize)
  209. {
  210. for (int i = 0; i < theSize; ++i)
  211. {
  212. emp_ptr->netPay = emp_ptr->grossPay - (emp_ptr->stateTax + emp_ptr->fedTax);
  213. ++emp_ptr;
  214. }
  215. }
  216.  
  217. void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize)
  218. {
  219. for (int i = 0; i < theSize; ++i)
  220. {
  221. emp_totals_ptr->total_wageRate += emp_ptr->wageRate;
  222. emp_totals_ptr->total_hours += emp_ptr->hours;
  223. emp_totals_ptr->total_overtimeHrs += emp_ptr->overtimeHrs;
  224. emp_totals_ptr->total_grossPay += emp_ptr->grossPay;
  225. emp_totals_ptr->total_stateTax += emp_ptr->stateTax;
  226. emp_totals_ptr->total_fedTax += emp_ptr->fedTax;
  227. emp_totals_ptr->total_netPay += emp_ptr->netPay;
  228. ++emp_ptr;
  229. }
  230. }
  231.  
  232. void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_minMax_ptr, int theSize)
  233. {
  234. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  235. emp_minMax_ptr->min_hours = emp_ptr->hours;
  236. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  237. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  238. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  239. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  240. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  241.  
  242. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  243. emp_minMax_ptr->max_hours = emp_ptr->hours;
  244. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  245. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  246. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  247. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  248. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  249.  
  250. ++emp_ptr;
  251.  
  252. for (int i = 1; i < theSize; ++i)
  253. {
  254. if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate)
  255. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  256. if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate)
  257. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  258.  
  259. if (emp_ptr->hours < emp_minMax_ptr->min_hours)
  260. emp_minMax_ptr->min_hours = emp_ptr->hours;
  261. if (emp_ptr->hours > emp_minMax_ptr->max_hours)
  262. emp_minMax_ptr->max_hours = emp_ptr->hours;
  263.  
  264. if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
  265. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  266. if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
  267. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  268.  
  269. if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  270. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  271. if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  272. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  273.  
  274. if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  275. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  276. if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  277. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  278.  
  279. if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  280. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  281. if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  282. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  283.  
  284. if (emp_ptr->netPay < emp_minMax_ptr->min_netPay)
  285. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  286. if (emp_ptr->netPay > emp_minMax_ptr->max_netPay)
  287. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  288.  
  289. ++emp_ptr;
  290. }
  291. }
  292.  
  293. void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr, int theSize)
  294. {
  295. printf("\n--------------------------------------------------------------");
  296. printf("-------------------");
  297.  
  298. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  299. emp_totals_ptr->total_wageRate,
  300. emp_totals_ptr->total_hours,
  301. emp_totals_ptr->total_overtimeHrs,
  302. emp_totals_ptr->total_grossPay,
  303. emp_totals_ptr->total_stateTax,
  304. emp_totals_ptr->total_fedTax,
  305. emp_totals_ptr->total_netPay);
  306.  
  307. if (theSize > 0)
  308. {
  309. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  310. emp_totals_ptr->total_wageRate/theSize,
  311. emp_totals_ptr->total_hours/theSize,
  312. emp_totals_ptr->total_overtimeHrs/theSize,
  313. emp_totals_ptr->total_grossPay/theSize,
  314. emp_totals_ptr->total_stateTax/theSize,
  315. emp_totals_ptr->total_fedTax/theSize,
  316. emp_totals_ptr->total_netPay/theSize);
  317. }
  318.  
  319. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  320. emp_minMax_ptr->min_wageRate,
  321. emp_minMax_ptr->min_hours,
  322. emp_minMax_ptr->min_overtimeHrs,
  323. emp_minMax_ptr->min_grossPay,
  324. emp_minMax_ptr->min_stateTax,
  325. emp_minMax_ptr->min_fedTax,
  326. emp_minMax_ptr->min_netPay);
  327.  
  328. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  329. emp_minMax_ptr->max_wageRate,
  330. emp_minMax_ptr->max_hours,
  331. emp_minMax_ptr->max_overtimeHrs,
  332. emp_minMax_ptr->max_grossPay,
  333. emp_minMax_ptr->max_stateTax,
  334. emp_minMax_ptr->max_fedTax,
  335. emp_minMax_ptr->max_netPay);
  336. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Enter hours worked by emp # 098401: 
Enter hours worked by emp # 526488: 
Enter hours worked by emp # 765349: 
Enter hours worked by emp # 034645: 
Enter hours worked by emp # 127615: 

*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net
                   State                           Pay     Tax    Tax      Pay
---------------------------------------------------------------------------------
Connie Cobol         MA  098401 10.60   0.0   0.0    0.00   0.00    0.00     0.00
Mary Apl             NH  526488  9.75   0.0   0.0    0.00   0.00    0.00     0.00
Frank Fortran        VT  765349 10.50   0.0   0.0    0.00   0.00    0.00     0.00
Jeff Ada             NY  034645 12.25   0.0   0.0    0.00   0.00    0.00     0.00
Anton Pascal         CA  127615  8.35   0.0   0.0    0.00   0.00    0.00     0.00
---------------------------------------------------------------------------------
Totals:                         51.45   0.0   0.0    0.00   0.00    0.00     0.00
Averages:                       10.29   0.0   0.0    0.00   0.00    0.00     0.00
Minimum:                         8.35   0.0   0.0    0.00   0.00    0.00     0.00
Maximum:                        12.25   0.0   0.0    0.00   0.00    0.00     0.00