fork(1) download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  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. // 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. #define STD_WORK_WEEK 40.0f
  28.  
  29. // Define a global structure to pass employee data between functions
  30. // Note that the structure type is global, but you don't want a variable
  31. // of that type to be global. Best to declare a variable of that type
  32. // in a function like main or another function and pass as needed.
  33.  
  34. struct employee
  35. {
  36. long int clockNumber;
  37. float wageRate;
  38. float hours;
  39. float overtimeHrs;
  40. float grossPay;
  41. };
  42.  
  43. // define prototypes here for each function except main
  44. float getHours (long int clockNumber);
  45. void printHeader (void);
  46. void printEmp (long int clockNumber, float wageRate, float hours,
  47. float overtimeHrs, float grossPay);
  48.  
  49. // TODO: Add your other function prototypes here
  50. //adding other function prototypes provided
  51. float calcOT (float hours);
  52. float calcGross (float wageRate, float hours, float overtimeHrs);
  53.  
  54. int main ()
  55. {
  56. // Set up a local variable to store the employee information
  57. struct employee employeeData[SIZE] = {
  58. { 98401, 10.60 },
  59. { 526488, 9.75 },
  60. { 765349, 10.50 }, // initialize clock and wage values
  61. { 34645, 12.25 },
  62. { 127615, 8.35 }
  63. };
  64.  
  65. int i; // loop and array index
  66.  
  67. // Call functions as needed to read and calculate information
  68. for (i = 0; i < SIZE; ++i)
  69. {
  70.  
  71. // Prompt for the number of hours worked by the employee
  72. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  73.  
  74. // TODO: Add other function calls as needed to calculate overtime and gross
  75. employeeData[i].overtimeHrs = calcOT(employeeData[i].hours);
  76. employeeData[i].grossPay = calcGross(employeeData[i].wageRate, employeeData[i].hours, employeeData[i].overtimeHrs);
  77.  
  78.  
  79. } // for
  80.  
  81. // Print the column headers
  82. printHeader();
  83.  
  84. // print out each employee
  85. for (i = 0; i < SIZE; ++i)
  86. {
  87. printEmp (employeeData[i].clockNumber,
  88. employeeData[i].wageRate,
  89. employeeData[i].hours,
  90. employeeData[i].overtimeHrs,
  91. employeeData[i].grossPay);
  92. }
  93.  
  94. return(0); // success
  95.  
  96. } // main
  97.  
  98. //**************************************************************
  99. // Function: getHours
  100. //
  101. // Purpose: Obtains input from user, the number of hours worked
  102. // per employee and stores the result in a local variable
  103. // that is passed back to the calling function.
  104. //
  105. // Parameters: clockNumber - The unique employee ID
  106. //
  107. // Returns: hoursWorked - hours worked in a given week
  108. //
  109. //**************************************************************
  110.  
  111. float getHours (long int clockNumber)
  112. {
  113.  
  114. float hoursWorked; // hours worked in a given week
  115.  
  116. // Read in hours for employee
  117. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  118. scanf ("%f", &hoursWorked);
  119.  
  120. // return hours back to the calling function
  121. return (hoursWorked);
  122.  
  123. } // getHours
  124.  
  125. //**************************************************************
  126. // Function: printHeader
  127. //
  128. // Purpose: Prints the initial table header information.
  129. //
  130. // Parameters: none
  131. //
  132. // Returns: void
  133. //
  134. //**************************************************************
  135.  
  136. void printHeader (void)
  137. {
  138.  
  139. printf ("\n\n*** Pay Calculator ***\n");
  140.  
  141. // print the table header
  142. printf("\nClock# Wage Hours OT Gross\n");
  143. printf("------------------------------------------------\n");
  144.  
  145. } // printHeader
  146.  
  147.  
  148. //*************************************************************
  149. // Function: printEmp
  150. //
  151. // Purpose: Prints out all the information for an employee
  152. // in a nice and orderly table format.
  153. //
  154. // Parameters:
  155. //
  156. // clockNumber - unique employee ID
  157. // wageRate - hourly wage rate
  158. // hours - Hours worked for the week
  159. // overtimeHrs - overtime hours worked in a week
  160. // grossPay - gross pay for the week
  161. //
  162. // Returns: void
  163. //
  164. //**************************************************************
  165.  
  166. void printEmp (long int clockNumber, float wageRate, float hours,
  167. float overtimeHrs, float grossPay)
  168. {
  169.  
  170. // Print out a single employee
  171. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  172. clockNumber, wageRate, hours,
  173. overtimeHrs, grossPay);
  174.  
  175. } // printEmp
  176.  
  177. // TODO: Add your functions here
  178. float calcOT(float hours)
  179. {
  180. if (hours > STD_WORK_WEEK) {
  181. return hours - STD_WORK_WEEK;//OT hours calculated
  182. }
  183. return 0.0f; //if no OT, return 0
  184. }
  185. float calcGross (float wageRate, float hours, float overtimeHrs)
  186. //calculates grosspayby adding reg + OT pay. calculations listed below
  187. {
  188. float regularPay = wageRate * (hours - overtimeHrs); //calcs regular pay
  189. float overtimePay = overtimeHrs * wageRate * OT_RATE; //calcs OT pay
  190. return regularPay + overtimePay; //total gross
  191. }
Success #stdin #stdout 0.01s 5296KB
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