fork download
  1. #include <stdio.h> // Standard input-output header
  2.  
  3. // Define Constants
  4. #define SIZE 5
  5. #define STD_HOURS 40.0
  6. #define OT_RATE 1.5
  7.  
  8. // Define a structure to hold employee data
  9. struct employee {
  10. long int clockNumber;
  11. float wageRate;
  12. float hours;
  13. float overtimeHrs;
  14. float grossPay;
  15. };
  16.  
  17. // Function Prototypes
  18. float getHours(long int clockNumber);
  19. void printHeader(void);
  20. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay);
  21. void calculateOvertime(struct employee *emp);
  22. void calculateGrossPay(struct employee *emp);
  23.  
  24. int main() {
  25. // Initialize employee data with clock number and wage rate
  26. struct employee employeeData[SIZE] = {
  27. {98401, 10.60, 0, 0, 0},
  28. {526488, 9.75, 0, 0, 0},
  29. {765349, 10.50, 0, 0, 0},
  30. {34645, 12.25, 0, 0, 0},
  31. {127615, 8.35, 0, 0, 0}
  32. };
  33.  
  34. int i; // Loop index
  35.  
  36. // Loop through employees to get hours, calculate overtime & gross pay
  37. for (i = 0; i < SIZE; ++i) {
  38. employeeData[i].hours = getHours(employeeData[i].clockNumber);
  39. calculateOvertime(&employeeData[i]);
  40. calculateGrossPay(&employeeData[i]);
  41. }
  42.  
  43. // Print the column headers
  44. printHeader();
  45.  
  46. // Print employee details
  47. for (i = 0; i < SIZE; ++i) {
  48. printEmp(employeeData[i].clockNumber, employeeData[i].wageRate, employeeData[i].hours, employeeData[i].overtimeHrs, employeeData[i].grossPay);
  49. }
  50.  
  51. return 0; // Program executed successfully
  52. }
  53.  
  54. //**************************************************************
  55. // Function: getHours
  56. // Purpose: Gets user input for hours worked per employee
  57. // Parameters: clockNumber - Unique employee ID
  58. // Returns: hoursWorked - Hours worked in a given week
  59. //**************************************************************
  60. float getHours(long int clockNumber) {
  61. float hoursWorked;
  62. printf("Enter hours worked by emp #%06li: ", clockNumber);
  63. scanf("%f", &hoursWorked);
  64. return hoursWorked;
  65. }
  66.  
  67. //**************************************************************
  68. // Function: calculateOvertime
  69. // Purpose: Calculates overtime hours worked
  70. // Parameters: emp - Pointer to an employee structure
  71. // Returns: void (modifies structure directly)
  72. //**************************************************************
  73. void calculateOvertime(struct employee *emp) {
  74. if (emp->hours > STD_HOURS) {
  75. emp->overtimeHrs = emp->hours - STD_HOURS;
  76. } else {
  77. emp->overtimeHrs = 0.0;
  78. }
  79. }
  80.  
  81. //**************************************************************
  82. // Function: calculateGrossPay
  83. // Purpose: Calculates gross pay for an employee
  84. // Parameters: emp - Pointer to an employee structure
  85. // Returns: void (modifies structure directly)
  86. //**************************************************************
  87. void calculateGrossPay(struct employee *emp) {
  88. float regularPay, overtimePay;
  89.  
  90. if (emp->hours > STD_HOURS) {
  91. regularPay = STD_HOURS * emp->wageRate;
  92. overtimePay = emp->overtimeHrs * emp->wageRate * OT_RATE;
  93. } else {
  94. regularPay = emp->hours * emp->wageRate;
  95. overtimePay = 0.0;
  96. }
  97.  
  98. emp->grossPay = regularPay + overtimePay;
  99. }
  100.  
  101. //**************************************************************
  102. // Function: printHeader
  103. // Purpose: Prints the table header
  104. // Parameters: none
  105. // Returns: void
  106. //**************************************************************
  107. void printHeader(void) {
  108. printf("\n*** Pay Calculator ***\n\n");
  109. printf("Clock# Wage Hours OT Gross\n");
  110. printf("----------------------------------\n");
  111. }
  112.  
  113. //**************************************************************
  114. // Function: printEmp
  115. // Purpose: Prints employee details in a tabular format
  116. // Parameters:
  117. // - clockNumber: Unique employee ID
  118. // - wageRate: Hourly wage rate
  119. // - hours: Hours worked in the week
  120. // - overtimeHrs: Overtime hours worked
  121. // - grossPay: Gross pay for the week
  122. // Returns: void
  123. //**************************************************************
  124. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay) {
  125. printf("%06li %5.2f %5.1f %4.1f %8.2f\n", clockNumber, wageRate, hours, overtimeHrs, grossPay);
  126. }
  127.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
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     0.0    0.0       0.00
526488    9.75     0.0    0.0       0.00
765349   10.50     0.0    0.0       0.00
034645   12.25     0.0    0.0       0.00
127615    8.35     0.0    0.0       0.00