fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Benjamin Lin
  6. //
  7. // Class: C Programming, Spring 2024
  8. //
  9. // Date: 4/18/2024
  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. // Call by Value design
  16. //
  17. //********************************************************
  18.  
  19. // Define and Includes
  20.  
  21. #include <stdio.h>
  22.  
  23. // Define Constants
  24. #define SIZE 5
  25. #define STD_HOURS 40.0
  26. #define OT_RATE 1.5
  27.  
  28. // Define a global structure to pass employee data between functions
  29. // Note that the structure type is global, but you don't want a variable
  30. // of that type to be global. Best to declare a variable of that type
  31. // in a function like main or another function and pass as needed.
  32.  
  33. struct employee
  34. {
  35. long int clockNumber;
  36. float wageRate;
  37. float hours;
  38. float overtimeHrs;
  39. float grossPay;
  40. };
  41.  
  42. // define prototypes here for each function except main
  43. float getHours (long int clockNumber);
  44. void printHeader (void);
  45. void printEmp (long int clockNumber, float wageRate, float hours,
  46. float overtimeHrs, float grossPay);
  47. float calcOT (float hours);
  48. float calcGross (float wageRate, float hours, float overtimeHrs);
  49.  
  50.  
  51. int main ()
  52. {
  53. // Set up a local variable to store the employee information
  54. struct employee employeeData[SIZE] = {
  55. { 98401, 10.60 },
  56. { 526488, 9.75 },
  57. { 765349, 10.50 }, // initialize clock and wage values
  58. { 34645, 12.25 },
  59. { 127615, 8.35 }
  60. };
  61.  
  62. int i; // loop and array index
  63.  
  64. // Call functions as needed to read and calculate information
  65. for (i = 0; i < SIZE; ++i)
  66. {
  67.  
  68. // Prompt for the number of hours worked by the employee
  69. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  70.  
  71. // Function call to calculate overtime hours
  72. employeeData[i].overtimeHrs = calcOT (employeeData[i].hours);
  73.  
  74. // Function call to calculate gross pay
  75. employeeData[i].grossPay = calcGross (employeeData[i].wageRate, employeeData[i].hours, employeeData[i].overtimeHrs);
  76.  
  77. } // for
  78.  
  79. // Print the column headers
  80. printHeader();
  81.  
  82. // print out each employee
  83. for (i = 0; i < SIZE; ++i)
  84. {
  85. printEmp (employeeData[i].clockNumber,
  86. employeeData[i].wageRate,
  87. employeeData[i].hours,
  88. employeeData[i].overtimeHrs,
  89. employeeData[i].grossPay);
  90. }
  91.  
  92. return(0); // success
  93.  
  94. } // main
  95.  
  96. //**************************************************************
  97. // Function: getHours
  98. //
  99. // Purpose: Obtains input from user, the number of hours worked
  100. // per employee and stores the result in a local variable
  101. // that is passed back to the calling function.
  102. //
  103. // Parameters: clockNumber - The unique employee ID
  104. //
  105. // Returns: hoursWorked - hours worked in a given week
  106. //
  107. //**************************************************************
  108.  
  109. float getHours (long int clockNumber)
  110. {
  111.  
  112. float hoursWorked; // hours worked in a given week
  113.  
  114. // Read in hours for employee
  115. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  116. scanf ("%f", &hoursWorked);
  117.  
  118. // return hours back to the calling function
  119. return (hoursWorked);
  120.  
  121. } // getHours
  122.  
  123. //**************************************************************
  124. // Function: printHeader
  125. //
  126. // Purpose: Prints the initial table header information.
  127. //
  128. // Parameters: none
  129. //
  130. // Returns: void
  131. //
  132. //**************************************************************
  133.  
  134. void printHeader (void)
  135. {
  136.  
  137. printf ("\n\n*** Pay Calculator ***\n");
  138.  
  139. // print the table header
  140. printf("\nClock# Wage Hours OT Gross\n");
  141. printf("------------------------------------------------\n");
  142.  
  143. } // printHeader
  144.  
  145.  
  146. //*************************************************************
  147. // Function: printEmp
  148. //
  149. // Purpose: Prints out all the information for an employee
  150. // in a nice and orderly table format.
  151. //
  152. // Parameters:
  153. //
  154. // clockNumber - unique employee ID
  155. // wageRate - hourly wage rate
  156. // hours - Hours worked for the week
  157. // overtimeHrs - overtime hours worked in a week
  158. // grossPay - gross pay for the week
  159. //
  160. // Returns: void
  161. //
  162. //**************************************************************
  163.  
  164. void printEmp (long int clockNumber, float wageRate, float hours,
  165. float overtimeHrs, float grossPay)
  166. {
  167.  
  168. // Print out a single employee
  169. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  170. clockNumber, wageRate, hours,
  171. overtimeHrs, grossPay);
  172.  
  173. } // printEmp
  174.  
  175. //**************************************************************
  176. // Function: calcOT
  177. //
  178. // Purpose: Calculates overtime hours.
  179. //
  180. // Parameters: hours - Hours worked for the week
  181. //
  182. // Returns: overtimeHrs - hours worked overtime.
  183. //
  184. //**************************************************************
  185. float calcOT (float hours)
  186. {
  187.  
  188. float overtimeHrs; // number of hours overtime
  189.  
  190. // find number of overtime hours and if yes calculate
  191. if (hours >= STD_HOURS)
  192. {
  193. overtimeHrs = hours - STD_HOURS;
  194. }
  195. else
  196. {
  197. overtimeHrs = 0;
  198.  
  199. } // if
  200.  
  201. return (overtimeHrs);
  202.  
  203. } // calcOT
  204.  
  205.  
  206. //**************************************************************
  207. // Function: calcGross
  208. //
  209. // Purpose: Calculates gross pay.
  210. //
  211. // Parameters:
  212. //
  213. // wageRate - hourly wage rate
  214. // hours - Hours worked for the week
  215. // overtimeHrs - overtime hours worked in a week
  216. //
  217. // Returns: grossPay - standard hours pay plus overtime pay
  218. //
  219. //**************************************************************
  220. float calcGross (float wageRate, float hours, float overtimeHrs)
  221. {
  222. float grossPay; // normal pay and overtime pay added
  223. float overtimePay; // extra pay after 40 hours
  224. float normalPay; // pay within 40 hours
  225. float overtimeRate; // 1.5x increased rate of pay when over 40 hours
  226.  
  227. // calculate gross pay based on having overtime or not
  228. if (hours >= STD_HOURS)
  229. {
  230. normalPay = wageRate * STD_HOURS;
  231. overtimeRate = wageRate * OT_RATE;
  232. overtimePay = overtimeRate * overtimeHrs;
  233.  
  234. }
  235. else // no OT
  236. {
  237. overtimePay = 0;
  238. normalPay = wageRate * hours;
  239.  
  240. } // if
  241.  
  242. // Calculate Gross Pay
  243. grossPay = normalPay + overtimePay;
  244. return(grossPay);
  245.  
  246. } // calcGross
Success #stdin #stdout 0s 5280KB
stdin
51.0
42.5
37.0
45.0
0.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 ***

Clock# Wage  Hours  OT      Gross
------------------------------------------------

 098401 10.60 51.0 11.0   598.90
 526488  9.75 42.5  2.5   426.56
 765349 10.50 37.0  0.0   388.50
 034645 12.25 45.0  5.0   581.88
 127615  8.35  0.0  0.0     0.00