fork 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.  
  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. float calcOvertimeHrs (float hours);
  45. float calcGrossPay (float wageRate, float hours, float overtimeHrs);
  46. void printHeader (void);
  47. void printEmp (long int clockNumber, float wageRate, float hours,
  48. float overtimeHrs, float grossPay);
  49.  
  50. int main ()
  51. {
  52. // Set up a local variable to store the employee information
  53. struct employee employeeData[SIZE] = {
  54. { 98401, 10.60 },
  55. { 526488, 9.75 },
  56. { 765349, 10.50 }, // initialize clock and wage values
  57. { 34645, 12.25 },
  58. { 127615, 8.35 }
  59. };
  60.  
  61. int i; // loop and array index
  62.  
  63. // Call functions as needed to read and calculate information
  64. for (i = 0; i < SIZE; ++i)
  65. {
  66. // Prompt for the number of hours worked by the employee
  67. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  68.  
  69. // Calculate overtime hours
  70. employeeData[i].overtimeHrs = calcOvertimeHrs(employeeData[i].hours);
  71.  
  72. // Calculate gross pay
  73. employeeData[i].grossPay = calcGrossPay(employeeData[i].wageRate, employeeData[i].hours, employeeData[i].overtimeHrs);
  74. }
  75.  
  76. // Print the header for the output table
  77. printHeader();
  78.  
  79. // Print the employee data
  80. for (i = 0; i < SIZE; ++i)
  81. {
  82. printEmp(employeeData[i].clockNumber, employeeData[i].wageRate, employeeData[i].hours,
  83. employeeData[i].overtimeHrs, employeeData[i].grossPay);
  84. }
  85.  
  86. return 0;
  87. }
  88.  
  89. // Function Definitions
  90.  
  91. /*******************************************************
  92.  * getHours: Prompts user to input hours worked
  93.  * Input: clockNumber of employee
  94.  * Output: returns hours worked
  95.  *******************************************************/
  96. float getHours (long int clockNumber)
  97. {
  98. float hours;
  99. printf("Enter hours worked for clock# %06li: ", clockNumber);
  100. scanf("%f", &hours);
  101. return hours;
  102. }
  103.  
  104. /*******************************************************
  105.  * calcOvertimeHrs: Calculates overtime hours
  106.  * Input: total hours worked
  107.  * Output: returns overtime hours
  108.  *******************************************************/
  109. float calcOvertimeHrs (float hours)
  110. {
  111. if (hours > STD_HOURS)
  112. {
  113. return hours - STD_HOURS;
  114. }
  115. else
  116. {
  117. return 0.0;
  118. }
  119. }
  120.  
  121. /*******************************************************
  122.  * calcGrossPay: Calculates gross pay including overtime
  123.  * Input: wageRate, hours, overtimeHrs
  124.  * Output: returns gross pay
  125.  *******************************************************/
  126. float calcGrossPay (float wageRate, float hours, float overtimeHrs)
  127. {
  128. float regularPay;
  129. float overtimePay;
  130.  
  131. if (overtimeHrs > 0)
  132. {
  133. regularPay = STD_HOURS * wageRate;
  134. overtimePay = overtimeHrs * (wageRate * OT_RATE);
  135. return regularPay + overtimePay;
  136. }
  137. else
  138. {
  139. return hours * wageRate;
  140. }
  141. }
  142.  
  143. /*******************************************************
  144.  * printHeader: Prints the formatted table header
  145.  * Input: None
  146.  * Output: Prints header to standard output
  147.  *******************************************************/
  148. void printHeader (void)
  149. {
  150. printf("\n*** Pay Calculator ***\n");
  151. printf("--------------------------------------------------\n");
  152. printf("Clock# Wage Hours OT Hours Gross Pay\n");
  153. printf("--------------------------------------------------\n");
  154. }
  155.  
  156. /*******************************************************
  157.  * printEmp: Prints one line of employee data
  158.  * Input: clockNumber, wageRate, hours, overtimeHrs, grossPay
  159.  * Output: Prints formatted employee data to standard output
  160.  *******************************************************/
  161. void printEmp (long int clockNumber, float wageRate, float hours,
  162. float overtimeHrs, float grossPay)
  163. {
  164. printf("%06li $%6.2f %5.1f %8.1f $%8.2f\n",
  165. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  166. }
Success #stdin #stdout 0s 5284KB
stdin
51.0
42.5
37.0
45.0
0.0
stdout
Enter hours worked for clock# 098401: Enter hours worked for clock# 526488: Enter hours worked for clock# 765349: Enter hours worked for clock# 034645: Enter hours worked for clock# 127615: 
*** Pay Calculator ***
--------------------------------------------------
Clock#    Wage     Hours   OT Hours   Gross Pay
--------------------------------------------------
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