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

Connie Cobol 598.90 29.95 149.73 419.23
Mary Apl 426.56 0.00 106.64 319.92
Frank Fortran 388.50 23.31 97.12 268.07
Jeff Ada 581.88 46.55 145.47 389.86
Anton Pascal 334.00 23.38 83.50 227.12
Totals Gross: 2329.84
Average Gross: 465.97
Min Gross: 334.00
Max Gross: 598.90