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.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29.  
  30. // define constants
  31. #define SIZE 5
  32. #define STD_HOURS 40.0
  33. #define OT_RATE 1.5
  34. #define MA_TAX_RATE 0.05
  35. #define NH_TAX_RATE 0.0
  36. #define VT_TAX_RATE 0.06
  37. #define CA_TAX_RATE 0.07
  38. #define DEFAULT_TAX_RATE 0.08
  39. #define NAME_SIZE 20
  40. #define TAX_STATE_SIZE 3
  41. #define FED_TAX_RATE 0.25
  42. #define FIRST_NAME_SIZE 10
  43. #define LAST_NAME_SIZE 10
  44.  
  45. // Define a structure type to store an employee name
  46. struct name
  47. {
  48. char firstName[FIRST_NAME_SIZE];
  49. char lastName [LAST_NAME_SIZE];
  50. };
  51.  
  52. // Define a structure type to pass employee data between functions
  53. struct employee
  54. {
  55. struct name empName;
  56. char taxState[TAX_STATE_SIZE];
  57. long int clockNumber;
  58. float wageRate;
  59. float hours;
  60. float overtimeHrs;
  61. float grossPay;
  62. float stateTax;
  63. float fedTax;
  64. float netPay;
  65. };
  66.  
  67. // this structure type defines the totals of all floating point items
  68. struct totals
  69. {
  70. float total_wageRate;
  71. float total_hours;
  72. float total_overtimeHrs;
  73. float total_grossPay;
  74. float total_stateTax;
  75. float total_fedTax;
  76. float total_netPay;
  77. };
  78.  
  79. // this structure type defines the min and max values of all floating
  80. // point items so they can be displayed in our final report
  81. struct min_max
  82. {
  83. float min_wageRate;
  84. float min_hours;
  85. float min_overtimeHrs;
  86. float min_grossPay;
  87. float min_stateTax;
  88. float min_fedTax;
  89. float min_netPay;
  90. float max_wageRate;
  91. float max_hours;
  92. float max_overtimeHrs;
  93. float max_grossPay;
  94. float max_stateTax;
  95. float max_fedTax;
  96. float max_netPay;
  97. };
  98.  
  99. // function prototypes
  100. void getHours(struct employee *emp_ptr, int theSize);
  101. void printEmp(struct employee *emp_ptr, int theSize);
  102. void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize);
  103. void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_minMax_ptr, int theSize);
  104. void printHeader(void);
  105. void calcOvertimeHrs(struct employee *emp_ptr, int theSize);
  106. void calcGrossPay(struct employee *emp_ptr, int theSize);
  107. void calcStateTax(struct employee *emp_ptr, int theSize);
  108. void calcFedTax(struct employee *emp_ptr, int theSize);
  109. void calcNetPay(struct employee *emp_ptr, int theSize);
  110. void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr, int theSize);
  111.  
  112. int main()
  113. {
  114. // Set up a local variable to store the employee information
  115. struct employee employeeData[SIZE] = {
  116. { {"Connie", "Cobol"}, "MA", 98401, 10.60},
  117. { {"Mary", "Apl"}, "NH", 526488, 9.75 },
  118. { {"Frank", "Fortran"}, "VT", 765349, 10.50 },
  119. { {"Jeff", "Ada"}, "NY", 34645, 12.25 },
  120. { {"Anton", "Pascal"},"CA",127615, 8.35 }
  121. };
  122.  
  123. // declare a pointer to the array of employee structures
  124. struct employee *emp_ptr = employeeData;
  125.  
  126. // set up structure to store totals and initialize all to zero
  127. struct totals employeeTotals = {0, 0, 0, 0, 0, 0, 0};
  128.  
  129. // pointer to the employeeTotals structure
  130. struct totals *emp_totals_ptr = &employeeTotals;
  131.  
  132. // set up structure to store min and max values and initialize all to zero
  133. struct min_max employeeMinMax = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  134.  
  135. // pointer to the employeeMinMax structure
  136. struct min_max *emp_minMax_ptr = &employeeMinMax;
  137.  
  138. // Call functions to read and calculate information
  139. getHours(emp_ptr, SIZE);
  140. calcOvertimeHrs(emp_ptr, SIZE);
  141. calcGrossPay(emp_ptr, SIZE);
  142. calcStateTax(emp_ptr, SIZE);
  143. calcFedTax(emp_ptr, SIZE);
  144. calcNetPay(emp_ptr, SIZE);
  145.  
  146. calcEmployeeTotals(emp_ptr, emp_totals_ptr, SIZE);
  147. calcEmployeeMinMax(emp_ptr, emp_minMax_ptr, SIZE);
  148.  
  149. // Print the column headers
  150. printHeader();
  151.  
  152. // Print out final information on each employee
  153. printEmp(emp_ptr, SIZE);
  154.  
  155. // Print totals and averages for all float items
  156. printEmpStatistics(emp_totals_ptr, emp_minMax_ptr, SIZE);
  157.  
  158. return 0;
  159. }
  160.  
  161. //**************************************************************
  162. // Function: getHours
  163. // Purpose: Obtains input from user, the number of hours worked per employee
  164. void getHours(struct employee *emp_ptr, int theSize)
  165. {
  166. for (int i = 0; i < theSize; ++i)
  167. {
  168. printf("\nEnter hours worked by emp # %06li: ", emp_ptr->clockNumber);
  169. scanf("%f", &emp_ptr->hours);
  170. ++emp_ptr; // Move to the next employee
  171. }
  172. }
  173.  
  174. //**************************************************************
  175. // Function: printHeader
  176. // Purpose: Prints the initial table header information
  177. void printHeader(void)
  178. {
  179. printf("\n\n*** Pay Calculator ***\n");
  180. printf("\n---------------------------------------------------------------------------------");
  181. printf("\nName Tax Clock# Wage Hours OT Gross State Fed Net");
  182. printf("\n State Pay Tax Tax Pay");
  183. printf("\n---------------------------------------------------------------------------------");
  184. }
  185.  
  186. //*************************************************************
  187. // Function: printEmp
  188. // Purpose: Prints out all the information for each employee
  189. void printEmp(struct employee *emp_ptr, int theSize)
  190. {
  191. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  192.  
  193. for (int i = 0; i < theSize; ++i)
  194. {
  195. strcpy(name, emp_ptr->empName.firstName);
  196. strcat(name, " ");
  197. strcat(name, emp_ptr->empName.lastName);
  198.  
  199. printf("\n%--20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  200. name, emp_ptr->taxState, emp_ptr->clockNumber,
  201. emp_ptr->wageRate, emp_ptr->hours,
  202. emp_ptr->overtimeHrs, emp_ptr->grossPay,
  203. emp_ptr->stateTax, emp_ptr->fedTax,
  204. emp_ptr->netPay);
  205.  
  206. ++emp_ptr; // Move to the next employee
  207. }
  208. }
  209.  
  210. //*************************************************************
  211. // Function: printEmpStatistics
  212. // Purpose: Prints the summary totals and averages for all employees
  213. void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr, int theSize)
  214. {
  215. printf("\n---------------------------------------------------------------------------------");
  216. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  217. emp_totals_ptr->total_wageRate, emp_totals_ptr->total_hours,
  218. emp_totals_ptr->total_overtimeHrs, emp_totals_ptr->total_grossPay,
  219. emp_totals_ptr->total_stateTax, emp_totals_ptr->total_fedTax,
  220. emp_totals_ptr->total_netPay);
  221.  
  222. if (theSize > 0)
  223. {
  224. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  225. emp_totals_ptr->total_wageRate / theSize,
  226. emp_totals_ptr->total_hours / theSize,
  227. emp_totals_ptr->total_overtimeHrs / theSize,
  228. emp_totals_ptr->total_grossPay / theSize,
  229. emp_totals_ptr->total_stateTax / theSize,
  230. emp_totals_ptr->total_fedTax / theSize,
  231. emp_totals_ptr->total_netPay / theSize);
  232. }
  233.  
  234. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  235. emp_minMax_ptr->min_wageRate, emp_minMax_ptr->min_hours,
  236. emp_minMax_ptr->min_overtimeHrs, emp_minMax_ptr->min_grossPay,
  237. emp_minMax_ptr->min_stateTax, emp_minMax_ptr->min_fedTax,
  238. emp_minMax_ptr->min_netPay);
  239.  
  240. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  241. emp_minMax_ptr->max_wageRate, emp_minMax_ptr->max_hours,
  242. emp_minMax_ptr->max_overtimeHrs, emp_minMax_ptr->max_grossPay,
  243. emp_minMax_ptr->max_stateTax, emp_minMax_ptr->max_fedTax,
  244. emp_minMax_ptr->max_netPay);
  245. }
  246.  
  247. //*************************************************************
  248. // Function: calcOvertimeHrs
  249. // Purpose: Calculates the overtime hours worked by an employee
  250. void calcOvertimeHrs(struct employee *emp_ptr, int theSize)
  251. {
  252. for (int i = 0; i < theSize; ++i)
  253. {
  254. emp_ptr->overtimeHrs = (emp_ptr->hours > STD_HOURS) ? emp_ptr->hours - STD_HOURS : 0;
  255. ++emp_ptr; // Move to the next employee
  256. }
  257. }
  258.  
  259. //*************************************************************
  260. // Function: calcGrossPay
  261. // Purpose: Calculates the gross pay based on the wage rate and overtime
  262. void calcGrossPay(struct employee *emp_ptr, int theSize)
  263. {
  264. for (int i = 0; i < theSize; ++i)
  265. {
  266. float normalPay = emp_ptr->wageRate * (emp_ptr->hours - emp_ptr->overtimeHrs);
  267. float overtimePay = emp_ptr->overtimeHrs * OT_RATE * emp_ptr->wageRate;
  268. emp_ptr->grossPay = normalPay + overtimePay;
  269. ++emp_ptr; // Move to the next employee
  270. }
  271. }
  272.  
  273. //*************************************************************
  274. // Function: calcStateTax
  275. // Purpose: Calculates the state tax based on the gross pay
  276. void calcStateTax(struct employee *emp_ptr, int theSize)
  277. {
  278. for (int i = 0; i < theSize; ++i)
  279. {
  280. if (strcmp(emp_ptr->taxState, "MA") == 0)
  281. emp_ptr->stateTax = emp_ptr->grossPay * MA_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, "NH") == 0)
  285. emp_ptr->stateTax = emp_ptr->grossPay * NH_TAX_RATE;
  286. else if (strcmp(emp_ptr->taxState, "CA") == 0)
  287. emp_ptr->stateTax = emp_ptr->grossPay * CA_TAX_RATE;
  288. else
  289. emp_ptr->stateTax = emp_ptr->grossPay * DEFAULT_TAX_RATE;
  290.  
  291. ++emp_ptr; // Move to the next employee
  292. }
  293. }
  294.  
  295. //*************************************************************
  296. // Function: calcFedTax
  297. // Purpose: Calculates the federal tax based on gross pay
  298. void calcFedTax(struct employee *emp_ptr, int theSize)
  299. {
  300. for (int i = 0; i < theSize; ++i)
  301. {
  302. emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
  303. ++emp_ptr; // Move to the next employee
  304. }
  305. }
  306.  
  307. //*************************************************************
  308. // Function: calcNetPay
  309. // Purpose: Calculates the net pay (gross pay minus state and federal tax)
  310. void calcNetPay(struct employee *emp_ptr, int theSize)
  311. {
  312. for (int i = 0; i < theSize; ++i)
  313. {
  314. float totalTaxes = emp_ptr->stateTax + emp_ptr->fedTax;
  315. emp_ptr->netPay = emp_ptr->grossPay - totalTaxes;
  316. ++emp_ptr; // Move to the next employee
  317. }
  318. }
  319.  
  320. //*************************************************************
  321. // Function: calcEmployeeTotals
  322. // Purpose: Performs a running total (sum) of each employee's floating point member
  323. void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize)
  324. {
  325. for (int i = 0; i < theSize; ++i)
  326. {
  327. emp_totals_ptr->total_wageRate += emp_ptr->wageRate;
  328. emp_totals_ptr->total_hours += emp_ptr->hours;
  329. emp_totals_ptr->total_overtimeHrs += emp_ptr->overtimeHrs;
  330. emp_totals_ptr->total_grossPay += emp_ptr->grossPay;
  331. emp_totals_ptr->total_stateTax += emp_ptr->stateTax;
  332. emp_totals_ptr->total_fedTax += emp_ptr->fedTax;
  333. emp_totals_ptr->total_netPay += emp_ptr->netPay;
  334. ++emp_ptr; // Move to the next employee
  335. }
  336. }
  337.  
  338. //*************************************************************
  339. // Function: calcEmployeeMinMax
  340. // Purpose: Accepts various floating point values from an employee and adds to a running update of min and max values
  341. void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_minMax_ptr, int theSize)
  342. {
  343. int i;
  344. // Initialize min and max values to first employee
  345. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  346. emp_minMax_ptr->min_hours = emp_ptr->hours;
  347. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  348. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  349. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  350. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  351. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  352. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  353. emp_minMax_ptr->max_hours = emp_ptr->hours;
  354. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  355. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  356. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  357. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  358. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  359. ++emp_ptr;
  360.  
  361. // Compare the rest of the employees
  362. for (i = 1; i < theSize; ++i)
  363. {
  364. if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate)
  365. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  366. if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate)
  367. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  368.  
  369. if (emp_ptr->hours < emp_minMax_ptr->min_hours)
  370. emp_minMax_ptr->min_hours = emp_ptr->hours;
  371. if (emp_ptr->hours > emp_minMax_ptr->max_hours)
  372. emp_minMax_ptr->max_hours = emp_ptr->hours;
  373.  
  374. if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
  375. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  376. if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
  377. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  378.  
  379. if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  380. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  381. if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  382. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  383.  
  384. if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  385. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  386. if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  387. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  388.  
  389. if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  390. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  391. if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  392. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  393.  
  394. if (emp_ptr->netPay < emp_minMax_ptr->min_netPay)
  395. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  396. if (emp_ptr->netPay > emp_minMax_ptr->max_netPay)
  397. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  398.  
  399. ++emp_ptr; // Move to the next employee
  400. }
  401. }
  402.  
Success #stdin #stdout 0.01s 5288KB
stdin
51.0
42.5
37.0
45.0
40.0
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  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