fork download
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Homework: 7
  4. //
  5. // Name: Keith Rowe
  6. //
  7. // Class: C Programming, Fall 2016
  8. //
  9. // Date: 10/18/2016
  10. //
  11. // Description: Program that uses structures to calculate employee pay. The only
  12. // user input is hours worked for that particular week. All other data is stored
  13. // in structures and calculated in subsequent functions. After the user enters
  14. // hours worked the program will use pointers to calculate the following:
  15. // overtime hours, gross pay, weekly totals,and weekly averages. Final output
  16. // is a table that is easy for the user to read.
  17. //
  18. ////////////////////////////////////////////////////////////////////////////////
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <ctype.h>
  22.  
  23. // constants
  24. #define NUM_EMPL 5
  25. #define OT_FACTOR 1.5f
  26. #define STD_HOURS 40.0f
  27.  
  28. // global structures
  29.  
  30. struct employee // basic employee data
  31. {
  32. char empName[20];
  33. long int clockNumber;
  34. float wageRate;
  35. float hours;
  36. float gross;
  37. float overtime;
  38. };
  39.  
  40. struct report // reports for management - a way to get a snapshot
  41. {
  42. //totals
  43. float totalWage;
  44. float totalHours;
  45. float totalGross;
  46. float totalOvertime;
  47. //averages
  48. float avgWage;
  49. float avgHours;
  50. float avgGross;
  51. float avgOvertime;
  52. //minimums
  53. float minWage;
  54. float minHours;
  55. float minGross;
  56. float minOvertime;
  57. //maximums
  58. float maxWage;
  59. float maxHours;
  60. float maxGross;
  61. float maxOvertime;
  62. };
  63. // function prototypes
  64. void getHours (struct employee * empPtr, int size);
  65. void calcOvertime (struct employee *empPtr, int size);
  66. void calcGross (struct employee * empPtr, int size);
  67. void printData (struct employee * empPtr, struct report weeklyReport[], int size);
  68. void empTotals (struct employee * empPtr, struct report weeklyReport[], int size);
  69. void empAverages (struct report * reportPtr, int size);
  70. void empMinimums (struct employee *empPtr, struct report weeklyReport[], int size);
  71. void empMaximums (struct employee * empPtr, struct report weeklyReport[], int size);
  72.  
  73. int main()
  74. {
  75. /* Variable Declarations */
  76. struct employee employeeData[NUM_EMPL]=
  77. { // initialize clock and wage values
  78. {"Connie Coblol", 98401, 10.60},
  79. {"Mary Apl", 526488, 9.75 },
  80. {"Frank Fortran", 765349, 10.50 },
  81. {"Jeff Ada", 34645, 12.25 },
  82. {"Anton Pascal", 127615, 10.00 }
  83. }; // end employeeData
  84. struct report weeklyReport[]={0}; //end weeklyReport
  85.  
  86.  
  87. struct employee *empPtr; //pointer used to reference employee data structure
  88. struct report *reportPtr; //pointer to reference weekly report sructure
  89.  
  90. empPtr = employeeData; //set pointer to first element of employeeData
  91. reportPtr = weeklyReport; //set point to first element of weeklyReport
  92.  
  93. // Function call to get hours worked for each employee
  94. getHours (employeeData, NUM_EMPL);
  95. // Function call to calculate overtime
  96. calcOvertime (employeeData, NUM_EMPL);
  97. // Function call to calculate gross pay
  98. calcGross (employeeData, NUM_EMPL);
  99. // Function call to calculate totals
  100. empTotals(employeeData, weeklyReport, NUM_EMPL);
  101. // Function to calculate averages
  102. empAverages (weeklyReport, NUM_EMPL);
  103. // Function to calculate minimums
  104. empMinimums(employeeData, weeklyReport, NUM_EMPL);
  105. // Function to calculate maximums
  106. empMaximums(employeeData, weeklyReport, NUM_EMPL);
  107. // Function call to output results to the screen
  108. printData (employeeData, weeklyReport, NUM_EMPL);
  109. return (0);
  110. }// end main
  111.  
  112. ////////////////////////////////////////////////////////////////////////////////
  113. // Function: getHours
  114. //
  115. // Purpose: Obtains input from user, the number of hours worked per employee
  116. // and stores the results in an array that is passed back to the calling
  117. // function by reference.
  118. //
  119. // Parameters
  120. // employeeData - structure that contains employee data such as wage, clock
  121. // number, and stores hours worked, overtime, and gross for the week.
  122. // size - Number of employees to process
  123. //
  124. // Returns: Nothing
  125. //
  126. ////////////////////////////////////////////////////////////////////////////////
  127.  
  128. void getHours (struct employee *empPtr, int size)
  129. {
  130. int idx; // loop counter
  131.  
  132. //Get Hours for each employee
  133. for (idx = 0; idx < size; ++idx)
  134. {
  135. printf("\nEnter hours worked by emp # %06li: ", empPtr -> clockNumber);
  136. scanf ("%f", &empPtr -> hours);
  137. while (empPtr -> hours < 0)
  138. {
  139. printf("\n!!! Hours must be positive numbers !!!\n");
  140. printf ("Enter the number of hours worked by employee # %06li: ", empPtr -> clockNumber);
  141. scanf("%f", &empPtr -> hours);
  142. } //end while
  143.  
  144. ++ empPtr; // increment employee pointer
  145. } // end for
  146. printf("\n\n");
  147. }// end getHours
  148.  
  149. ////////////////////////////////////////////////////////////////////////////////
  150. // Function: calcOvertime
  151. //
  152. // Purpose: Calculates overtime hours for an employee based on input from other
  153. // functions. calcOvertime calculates overtime by pulling data from the array
  154. // hours. It then populates the array overtime with overtime hours.
  155. // Data is passed back to the calling function by reference.
  156. //
  157. // Parameters
  158. // employeeData - structure that contains employee data such as wage, clock
  159. // number, and stores hours worked, overtime, and gross for the week.
  160. // size: number of employees to process.
  161. //
  162. // Return: nothing
  163. //
  164. ////////////////////////////////////////////////////////////////////////////////
  165.  
  166. void calcOvertime (struct employee * empPtr, int size)
  167. {
  168. int idx; // index to count loop
  169.  
  170. for (idx = 0; idx < size; ++idx)
  171. {
  172. if (empPtr -> hours > STD_HOURS)
  173. {
  174. empPtr -> overtime = (empPtr -> hours - STD_HOURS); //calculate OT
  175. } //end if
  176. else
  177. {
  178. empPtr -> overtime = 0;
  179. } // end else
  180.  
  181. ++empPtr; //increment employee pointer
  182. } // end for
  183. }// end calcOvertime
  184.  
  185. ////////////////////////////////////////////////////////////////////////////////
  186. // Function: calcGross
  187. //
  188. // Purpose: Calculates gross pay for an employee based on input from other
  189. // functions. Calculates overtime by pulling data from the arrays
  190. // wageRate, overtime, and hours. It then populates the array gross with gross
  191. // pay. Data is passed back to the calling function by reference.
  192. //
  193. // Parameters
  194. // employeeData - structure that contains employee data such as wage, clock
  195. // number, and stores hours worked, overtime, and gross for the week.
  196. // size: number of employees to process.
  197. //
  198. // Returns: nothing
  199. //
  200. ////////////////////////////////////////////////////////////////////////////////
  201.  
  202. void calcGross (struct employee *empPtr, int size)
  203. {
  204. int idx; // index to count loop
  205.  
  206. for (idx = 0; idx < size; ++idx)
  207. {
  208. empPtr -> gross = (empPtr ->wageRate * (empPtr -> hours - empPtr -> overtime)) + (empPtr -> wageRate * empPtr -> overtime * OT_FACTOR); //calculate weekly wage
  209.  
  210. ++ empPtr; //increment employee pointer
  211. }// end for
  212. }// end calcGross
  213.  
  214. ////////////////////////////////////////////////////////////////////////////////
  215. // Function: printData
  216. //
  217. // Purpose: Function to print out various arrays that contain employee data. It
  218. // prints out a header with titles, employee data in rows and trailing data.
  219. //
  220. // Parameters
  221. // employeeData - structure that contains employee data such as wage, clock
  222. // number, and stores hours worked, overtime, and gross for the week.
  223. // weeklyReport - structure that contains weekly totals and averages for wage
  224. // hours worked, overtime hours, and gross pay
  225. // size: number of employees to process.
  226. //
  227. // Returns: nothing
  228. //
  229. ////////////////////////////////////////////////////////////////////////////////
  230.  
  231. void printData (struct employee *empPtr, struct report weeklyReport[], int size)
  232. {
  233. int idx; //loop counter
  234.  
  235. printf ("\t----------------------------------------------------------\n");
  236. printf ("\tName Clock# Wage Hours OT Gross\n");
  237. printf ("\t----------------------------------------------------------\n");
  238. for (idx = 0; idx<size; ++idx)
  239. {
  240. printf ("\t%-18.18s %06li %7.2f %6.1f %7.1f %9.2f\n", empPtr -> empName, empPtr -> clockNumber, empPtr -> wageRate, empPtr -> hours, empPtr -> overtime, empPtr ->gross);
  241.  
  242. ++empPtr;// increment employee pointer
  243. }// end for
  244. printf ("\t----------------------------------------------------------\n");
  245. printf ("\tTotal\t\t\t\t\t %7.2f %6.1f %7.1f %9.2f\n", weeklyReport->totalWage, weeklyReport->totalHours, weeklyReport->totalOvertime, weeklyReport->totalGross);
  246. printf ("\tAverage\t\t\t\t\t %5.2f %6.1f %7.1f %9.2f\n", weeklyReport->avgWage, weeklyReport->avgHours, weeklyReport->avgOvertime, weeklyReport->avgGross);
  247. printf ("\tMinimum\t\t\t\t\t %5.2f %6.1f %7.1f %9.2f\n", weeklyReport->minWage, weeklyReport->minHours, weeklyReport->minOvertime, weeklyReport->minGross);
  248. printf ("\tMaximum\t\t\t\t\t %5.2f %6.1f %7.1f %9.2f\n", weeklyReport->maxWage, weeklyReport->maxHours, weeklyReport->maxOvertime, weeklyReport->maxGross);
  249. printf("\n\n");
  250. printf("\n\n");
  251. }// end printData
  252.  
  253. ////////////////////////////////////////////////////////////////////////////////
  254. // Function: empTotals
  255. //
  256. // Purpose: Function to calculate totals for the. It will find the sum of
  257. // various arrays then populate the array empTotals.
  258. //
  259. // Parameters
  260. // employeeData - structure that contains employee data such as wage, clock
  261. // number, and stores hours worked, overtime, and gross for the week.
  262. // weeklyReport - structure that contains weekly totals and averages for wage
  263. // hours worked, overtime hours, and gross pay
  264. // size: number of employees to process.
  265. //
  266. // Return: nothing
  267. ////////////////////////////////////////////////////////////////////////////////
  268.  
  269. void empTotals (struct employee *empPtr, struct report weeklyReport[], int size)
  270. {
  271. int idx; //loop counter
  272.  
  273. for (idx = 1; idx< size; ++idx)
  274. {
  275. weeklyReport->totalWage += empPtr -> wageRate;
  276. weeklyReport->totalHours += empPtr -> hours;
  277. weeklyReport->totalOvertime += empPtr -> overtime;
  278. weeklyReport->totalGross += empPtr -> gross;
  279.  
  280. ++empPtr; // increment employee pointer
  281. }// end for
  282. }// end empTotals
  283.  
  284. ////////////////////////////////////////////////////////////////////////////////
  285. // Function: empAverages
  286. //
  287. // Purpose: Calculate weekly averages for employees based on data stored in the
  288. // array totals. Function requres no input from the user.
  289. //
  290. // Parameters:
  291. // weeklyReport - structure that contains weekly totals and averages for wage
  292. // hours worked, overtime hours, and gross pay
  293. // size: number of employees to process.
  294. //
  295. // Return: nothing
  296. ////////////////////////////////////////////////////////////////////////////////
  297.  
  298. void empAverages (struct report *reportPtr, int size)
  299. {
  300. reportPtr -> avgWage = reportPtr -> totalWage / size;
  301. reportPtr -> avgHours = reportPtr -> totalHours / size;
  302. reportPtr -> avgOvertime = reportPtr -> totalOvertime / size;
  303. reportPtr -> avgGross = reportPtr -> totalGross / size;
  304.  
  305. }// end empAverages
  306.  
  307. ////////////////////////////////////////////////////////////////////////////////
  308. // Function: empMinimums
  309. //
  310. // Purpose: Calculate the weekly minimum value for each element of the weekly
  311. // report ie wage, hours, OT hours, and gross
  312. //
  313. // Parameters:
  314. // employeeData - structure that contains employee data such as wage, clock
  315. // number, and stores hours worked, overtime, and gross for the week.
  316. // weeklyReport - structure that contains weekly totals and averages for wage
  317. // hours worked, overtime hours, and gross pay
  318. // size: number of employees to process.
  319. //
  320. // Return: nothing populates minimums array in weeklyReports
  321. ////////////////////////////////////////////////////////////////////////////////
  322.  
  323. void empMinimums (struct employee *empPtr, struct report weeklyReport[], int size)
  324. {
  325. int idx; //loop counter
  326. weeklyReport->minWage = empPtr -> wageRate;
  327. weeklyReport->minHours = empPtr -> hours;
  328. weeklyReport->minOvertime = empPtr -> overtime;
  329. weeklyReport->minGross = empPtr -> gross;
  330.  
  331. for (idx = 0; idx < size; ++idx)
  332. {
  333. if (weeklyReport->minWage > empPtr -> wageRate)
  334. {
  335. weeklyReport->minWage = empPtr -> wageRate;
  336. }// end if
  337. if (weeklyReport->minHours > empPtr -> hours)
  338. {
  339. weeklyReport->minHours = empPtr -> hours;
  340. }// end if
  341. if (weeklyReport->minOvertime > empPtr -> overtime)
  342. {
  343. weeklyReport->minOvertime = empPtr -> overtime;
  344. }// end if
  345. if (weeklyReport->minGross > empPtr -> gross)
  346. {
  347. weeklyReport->minGross = empPtr -> gross;
  348. }// end if
  349. ++empPtr; //increment employee pointer
  350. }// end for
  351.  
  352. }// end empMinimums
  353.  
  354. ////////////////////////////////////////////////////////////////////////////////
  355. // Function: empMaximums
  356. //
  357. // Purpose: Calculate the weekly maximum value for each element of the weekly
  358. // report ie wage, hours, OT hours, and gross
  359. //
  360. // Parameters:
  361. // employeeData - structure that contains employee data such as wage, clock
  362. // number, and stores hours worked, overtime, and gross for the week.
  363. // weeklyReport - structure that contains weekly totals and averages for wage
  364. // hours worked, overtime hours, and gross pay
  365. // size: number of employees to process.
  366. //
  367. // Return: nothing populates maximum array in weeklyReports
  368. ////////////////////////////////////////////////////////////////////////////////
  369.  
  370. void empMaximums (struct employee *empPtr, struct report weeklyReport[], int size)
  371. {
  372. int idx; //loop counter
  373. weeklyReport->maxWage = empPtr -> wageRate;
  374. weeklyReport->maxHours = empPtr -> hours;
  375. weeklyReport->maxOvertime = empPtr -> overtime;
  376. weeklyReport->maxGross = empPtr -> gross;
  377.  
  378. for (idx = 0; idx < size; ++idx)
  379. {
  380. if (weeklyReport->maxWage < empPtr -> wageRate)
  381. {
  382. weeklyReport->maxWage = empPtr -> wageRate;
  383. }// end if
  384. if (weeklyReport->maxHours < empPtr -> hours)
  385. {
  386. weeklyReport->maxHours = empPtr -> hours;
  387. }// end if
  388. if (weeklyReport->maxOvertime < empPtr -> overtime)
  389. {
  390. weeklyReport->maxOvertime = empPtr -> overtime;
  391. }// end if
  392. if (weeklyReport->maxGross < empPtr -> gross)
  393. {
  394. weeklyReport->maxGross = empPtr -> gross;
  395. }// end if
  396. ++empPtr; //increment employee pointer
  397. }// end for
  398.  
  399. }// end empMaximums
  400.  
  401.  
  402.  
Success #stdin #stdout 0s 3476KB
stdin
51
42.5
37
45
40
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: 

	----------------------------------------------------------
	Name               Clock#   Wage   Hours     OT     Gross
	----------------------------------------------------------
	Connie Coblol      098401   10.60   51.0    11.0    598.90
	Mary Apl           526488    9.75   42.5     2.5    426.56
	Frank Fortran      765349   10.50   37.0     0.0    388.50
	Jeff Ada           034645   12.25   45.0     5.0    581.88
	Anton Pascal       127615   10.00   40.0     0.0    400.00
	----------------------------------------------------------
	Total					    43.10  175.5    18.5   1995.84
	Average					     8.62   35.1     3.7    399.17
	Minimum					     9.75   37.0     0.0    388.50
	Maximum					    12.25   51.0    11.0    598.90