fork download
  1. //********************************************************
  2. //
  3. // Assignment 8 - Structures and Strings and Pointers
  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 are to be replaced with
  20. // pointer references to speed up the processing of this code.
  21. //
  22. // Call by Reference design (using pointers)
  23. //
  24. //********************************************************
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <ctype.h>
  28.  
  29. // Define constants
  30. #define SIZE 5
  31. #define STD_HOURS 40.0
  32. #define OT_RATE 1.5
  33. #define MA_TAX_RATE 0.05
  34. #define NH_TAX_RATE 0.0
  35. #define VT_TAX_RATE 0.06
  36. #define CA_TAX_RATE 0.07
  37. #define DEFAULT_TAX_RATE 0.08
  38. #define FED_TAX_RATE 0.25
  39. #define FIRST_NAME_SIZE 10
  40. #define LAST_NAME_SIZE 10
  41. #define TAX_STATE_SIZE 3
  42.  
  43. // Define structures
  44. struct name {
  45. char firstName[FIRST_NAME_SIZE];
  46. char lastName[LAST_NAME_SIZE];
  47. };
  48.  
  49. struct employee {
  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. struct totals {
  63. float total_wageRate;
  64. float total_hours;
  65. float total_overtimeHrs;
  66. float total_grossPay;
  67. float total_stateTax;
  68. float total_fedTax;
  69. float total_netPay;
  70. };
  71.  
  72. struct min_max {
  73. float min_wageRate;
  74. float min_hours;
  75. float min_overtimeHrs;
  76. float min_grossPay;
  77. float min_stateTax;
  78. float min_fedTax;
  79. float min_netPay;
  80. float max_wageRate;
  81. float max_hours;
  82. float max_overtimeHrs;
  83. float max_grossPay;
  84. float max_stateTax;
  85. float max_fedTax;
  86. float max_netPay;
  87. };
  88.  
  89. // Define function prototypes
  90. void getHours(struct employee *emp_ptr, int theSize);
  91. void printEmp(struct employee *emp_ptr, int theSize);
  92. void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize);
  93. void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_minMax_ptr, int theSize);
  94. void printHeader(void);
  95. void calcOvertimeHrs(struct employee *emp_ptr, int theSize);
  96. void calcGrossPay(struct employee *emp_ptr, int theSize);
  97. void calcStateTax(struct employee *emp_ptr, int theSize);
  98. void calcFedTax(struct employee *emp_ptr, int theSize);
  99. void calcNetPay(struct employee *emp_ptr, int theSize);
  100. void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr, int theSize);
  101.  
  102. int main() {
  103. struct employee employeeData[SIZE] = {
  104. {{"Connie", "Cobol"}, "MA", 98401, 10.60, 51.0},
  105. {{"Mary", "Apl"}, "NH", 526488, 9.75, 42.5},
  106. {{"Frank", "Fortran"}, "VT", 765349, 10.50, 37.0},
  107. {{"Jeff", "Ada"}, "NY", 34645, 12.25, 45.0},
  108. {{"Anton", "Pascal"}, "CA", 127615, 8.35, 40.0}
  109. };
  110.  
  111. struct totals employeeTotals = {0, 0, 0, 0, 0, 0, 0};
  112. struct min_max employeeMinMax = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  113.  
  114. struct employee *emp_ptr = employeeData;
  115. struct totals *emp_totals_ptr = &employeeTotals;
  116. struct min_max *emp_minMax_ptr = &employeeMinMax;
  117.  
  118. getHours(emp_ptr, SIZE);
  119. calcOvertimeHrs(emp_ptr, SIZE);
  120. calcGrossPay(emp_ptr, SIZE);
  121. calcStateTax(emp_ptr, SIZE);
  122. calcFedTax(emp_ptr, SIZE);
  123. calcNetPay(emp_ptr, SIZE);
  124. calcEmployeeTotals(emp_ptr, emp_totals_ptr, SIZE);
  125. calcEmployeeMinMax(emp_ptr, emp_minMax_ptr, SIZE);
  126.  
  127. printHeader();
  128. printEmp(emp_ptr, SIZE);
  129. printEmpStatistics(emp_totals_ptr, emp_minMax_ptr, SIZE);
  130.  
  131. return 0;
  132. }
  133.  
  134. // Function Definitions
  135.  
  136. void getHours(struct employee *emp_ptr, int theSize) {
  137. for (int i = 0; i < theSize; ++i, ++emp_ptr) {
  138. // The hours are already initialized in the structure, so no user input is needed
  139. }
  140. }
  141.  
  142. void printHeader(void) {
  143. printf("\n\n*** Pay Calculator ***\n");
  144. printf("---------------------------------------------------------------------------------\n");
  145. printf("Name Tax Clock# Wage Hours OT Gross State Fed Net\n");
  146. printf(" State Pay Tax Tax Pay\n");
  147. printf("---------------------------------------------------------------------------------\n");
  148. }
  149.  
  150. void printEmp(struct employee *emp_ptr, int theSize) {
  151. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  152. for (int i = 0; i < theSize; ++i) {
  153. snprintf(name, sizeof(name), "%s %s", emp_ptr->empName.firstName, emp_ptr->empName.lastName);
  154.  
  155. printf("%-20.20s %-2.2s %06li %5.2f %5.1f %5.1f %8.2f %7.2f %7.2f %8.2f\n",
  156. name, emp_ptr->taxState, emp_ptr->clockNumber,
  157. emp_ptr->wageRate, emp_ptr->hours,
  158. emp_ptr->overtimeHrs, emp_ptr->grossPay,
  159. emp_ptr->stateTax, emp_ptr->fedTax,
  160. emp_ptr->netPay);
  161.  
  162. ++emp_ptr;
  163. }
  164. printf("---------------------------------------------------------------------------------\n");
  165. }
  166.  
  167. void calcOvertimeHrs(struct employee *emp_ptr, int theSize) {
  168. for (int i = 0; i < theSize; ++i, ++emp_ptr) {
  169. emp_ptr->overtimeHrs = (emp_ptr->hours > STD_HOURS) ? emp_ptr->hours - STD_HOURS : 0;
  170. }
  171. }
  172.  
  173. void calcGrossPay(struct employee *emp_ptr, int theSize) {
  174. for (int i = 0; i < theSize; ++i, ++emp_ptr) {
  175. float normalPay = emp_ptr->wageRate * (emp_ptr->hours - emp_ptr->overtimeHrs);
  176. float overtimePay = emp_ptr->overtimeHrs * OT_RATE * emp_ptr->wageRate;
  177. emp_ptr->grossPay = normalPay + overtimePay;
  178. }
  179. }
  180.  
  181. void calcStateTax(struct employee *emp_ptr, int theSize) {
  182. for (int i = 0; i < theSize; ++i, ++emp_ptr) {
  183. if (strcmp(emp_ptr->taxState, "MA") == 0)
  184. emp_ptr->stateTax = emp_ptr->grossPay * MA_TAX_RATE;
  185. else if (strcmp(emp_ptr->taxState, "VT") == 0)
  186. emp_ptr->stateTax = emp_ptr->grossPay * VT_TAX_RATE;
  187. else if (strcmp(emp_ptr->taxState, "NH") == 0)
  188. emp_ptr->stateTax = emp_ptr->grossPay * NH_TAX_RATE;
  189. else if (strcmp(emp_ptr->taxState, "CA") == 0)
  190. emp_ptr->stateTax = emp_ptr->grossPay * CA_TAX_RATE;
  191. else
  192. emp_ptr->stateTax = emp_ptr->grossPay * DEFAULT_TAX_RATE;
  193. }
  194. }
  195.  
  196. void calcFedTax(struct employee *emp_ptr, int theSize) {
  197. for (int i = 0; i < theSize; ++i, ++emp_ptr) {
  198. emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
  199. }
  200. }
  201.  
  202. void calcNetPay(struct employee *emp_ptr, int theSize) {
  203. for (int i = 0; i < theSize; ++i, ++emp_ptr) {
  204. emp_ptr->netPay = emp_ptr->grossPay - (emp_ptr->stateTax + emp_ptr->fedTax);
  205. }
  206. }
  207.  
  208. void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize) {
  209. for (int i = 0; i < theSize; ++i, ++emp_ptr) {
  210. emp_totals_ptr->total_wageRate += emp_ptr->wageRate;
  211. emp_totals_ptr->total_hours += emp_ptr->hours;
  212. emp_totals_ptr->total_overtimeHrs += emp_ptr->overtimeHrs;
  213. emp_totals_ptr->total_grossPay += emp_ptr->grossPay;
  214. emp_totals_ptr->total_stateTax += emp_ptr->stateTax;
  215. emp_totals_ptr->total_fedTax += emp_ptr->fedTax;
  216. emp_totals_ptr->total_netPay += emp_ptr->netPay;
  217. }
  218. }
  219.  
  220. void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_minMax_ptr, int theSize) {
  221. emp_minMax_ptr->min_wageRate = emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  222. emp_minMax_ptr->min_hours = emp_minMax_ptr->max_hours = emp_ptr->hours;
  223. emp_minMax_ptr->min_overtimeHrs = emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  224. emp_minMax_ptr->min_grossPay = emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  225. emp_minMax_ptr->min_stateTax = emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  226. emp_minMax_ptr->min_fedTax = emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  227. emp_minMax_ptr->min_netPay = emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  228.  
  229. for (int i = 1; i < theSize; ++i, ++emp_ptr) {
  230. if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate) emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  231. if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate) emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  232. if (emp_ptr->hours < emp_minMax_ptr->min_hours) emp_minMax_ptr->min_hours = emp_ptr->hours;
  233. if (emp_ptr->hours > emp_minMax_ptr->max_hours) emp_minMax_ptr->max_hours = emp_ptr->hours;
  234. if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs) emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  235. if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs) emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  236. if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay) emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  237. if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay) emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  238. if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax) emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  239. if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax) emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  240. if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax) emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  241. if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax) emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  242. if (emp_ptr->netPay < emp_minMax_ptr->min_netPay) emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  243. if (emp_ptr->netPay > emp_minMax_ptr->max_netPay) emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  244. }
  245. }
  246.  
  247. void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr, int theSize) {
  248. printf("Totals: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n",
  249. emp_totals_ptr->total_wageRate, emp_totals_ptr->total_hours,
  250. emp_totals_ptr->total_overtimeHrs, emp_totals_ptr->total_grossPay,
  251. emp_totals_ptr->total_stateTax, emp_totals_ptr->total_fedTax,
  252. emp_totals_ptr->total_netPay);
  253.  
  254. if (theSize > 0) {
  255. printf("Averages: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n",
  256. emp_totals_ptr->total_wageRate / theSize,
  257. emp_totals_ptr->total_hours / theSize,
  258. emp_totals_ptr->total_overtimeHrs / theSize,
  259. emp_totals_ptr->total_grossPay / theSize,
  260. emp_totals_ptr->total_stateTax / theSize,
  261. emp_totals_ptr->total_fedTax / theSize,
  262. emp_totals_ptr->total_netPay / theSize);
  263. }
  264.  
  265. printf("Minimum: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n",
  266. emp_minMax_ptr->min_wageRate, emp_minMax_ptr->min_hours,
  267. emp_minMax_ptr->min_overtimeHrs, emp_minMax_ptr->min_grossPay,
  268. emp_minMax_ptr->min_stateTax, emp_minMax_ptr->min_fedTax,
  269. emp_minMax_ptr->min_netPay);
  270.  
  271. printf("Maximum: %.2f %.1f %.1f %.2f %.2f %.2f %.2f\n",
  272. emp_minMax_ptr->max_wageRate, emp_minMax_ptr->max_hours,
  273. emp_minMax_ptr->max_overtimeHrs, emp_minMax_ptr->max_grossPay,
  274. emp_minMax_ptr->max_stateTax, emp_minMax_ptr->max_fedTax,
  275. emp_minMax_ptr->max_netPay);
  276. }
  277.  
Success #stdin #stdout 0s 5288KB
stdin
51.0
42.5
37.0
45.0
40.0
stdout

*** 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:                         9.75 37.0 0.0 388.50 0.00 97.12 268.07
Maximum:                         12.25 51.0 11.0 598.90 46.55 149.73 419.23