fork download
  1. //********************************************************
  2. //
  3. // Assignment 7 - Structures and Strings
  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. // This assignment also adds the employee name, their tax state,
  16. // and calulates state tax, federal tax, and net pay.
  17. //
  18. // Call by Value design
  19. //
  20. //********************************************************
  21.  
  22. /*Define and Includes */
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26.  
  27. /* Define Constants */
  28. #define NUM_EMPL 5
  29. #define STD_HOURS 40.0
  30. #define OT_RATE 1.5
  31. #define MA_TAX_RATE 0.05
  32. #define NH_TAX_RATE 0.0
  33. #define VT_TAX_RATE 0.06
  34. #define CA_TAX_RATE 0.07
  35. #define DEFAULT_TAX_RATE 0.08
  36.  
  37. /* Define a global structure to pass employee data between functions */
  38. /* Note that the structure type is global, but you don't want a variable */
  39. /* of that type to be global. Best to declare a variable of that type */
  40. /* in a function like main or another function and pass as needed. */
  41.  
  42. struct employee
  43. {
  44. char name[20];
  45. char taxState[2];
  46. long int clockNumber;
  47. float wageRate;
  48. float hours;
  49. float overtimeHrs;
  50. float grossPay;
  51. float stateTax;
  52. float fedTax;
  53. float netPay;
  54. };
  55.  
  56. struct totals
  57. {
  58. float total_wageRate;
  59. float total_hours;
  60. float total_overtimeHrs;
  61. float total_grossPay;
  62. float total_stateTax;
  63. float total_fedTax;
  64. float total_netPay;
  65. };
  66.  
  67. /* define prototypes here for each function except main */
  68. float getHours (long int clockNumber);
  69. float calcOvertimeHrs (float hours);
  70. float calcGrossPay (float wageRate, float hours, float overtimeHrs);
  71. void printHeader (void);
  72. void printEmp (char name [], char taxState [],
  73. long int clockNumber, float wageRate,
  74. float hours, float overtimeHrs, float grossPay,
  75. float stateTax, float fedTax, float netPay);
  76.  
  77. /* TODO: Add your other function prototypes here */
  78. float calcStateTax (float grossPay, char taxState[]);
  79. float calcFedTax (float grossPay);
  80. float calcNetPay (float grossPay, float stateTax, float fedTax);
  81.  
  82. struct totals calcEmployeeTotals (float wageRate,
  83. float hours,
  84. float overtimeHrs,
  85. float grossPay,
  86. float stateTax,
  87. float fedTax,
  88. float netPay,
  89. struct totals employeeTotals);
  90.  
  91. void printEmpStatistics (struct totals employeeTotals, int size);
  92.  
  93. int main ()
  94. {
  95.  
  96. /* Set up a local variable to store the employee information */
  97. /* Initialize the name, tax state, clock number, and wage rate */
  98. struct employee employeeData[NUM_EMPL] = {
  99. { "Connie Cobol", "MA", 98401, 10.60},
  100. { "Mary Apl", "NH", 526488, 9.75 },
  101. { "Frank Fortran", "VT", 765349, 10.50 },
  102. { "Jeff Ada", "NY", 34645, 12.25 },
  103. { "Anton Pascal","CA",127615, 8.35 }
  104. };
  105.  
  106. /* set up structure to store totals and initialize all to zero */
  107. struct totals employeeTotals = {0,0,0,0,0,0,0};
  108.  
  109. int i; /* loop and array index */
  110.  
  111. /* Call functions as needed to read and calculate information */
  112. for (i = 0; i < NUM_EMPL; ++i)
  113. {
  114.  
  115. /* Prompt for the number of hours worked by the employee */
  116. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  117.  
  118. employeeData[i].overtimeHrs = calcOvertimeHrs (employeeData[i].hours);
  119.  
  120. employeeData[i].grossPay = calcGrossPay (employeeData[i].wageRate,
  121. employeeData[i].hours,
  122. employeeData[i].overtimeHrs);
  123.  
  124. /* TODO: Add other function calls as needed to calculate */
  125. /* the state tax, the federal tax, and the net pay */
  126.  
  127. employeeData[i].stateTax = calcStateTax(employeeData[i].grossPay,
  128. employeeData[i].taxState);
  129.  
  130. employeeData[i].fedTax = calcFedTax(employeeData[i].grossPay);
  131.  
  132. employeeData[i].netPay = calcNetPay (employeeData[i].grossPay,
  133. employeeData[i].stateTax,
  134. employeeData[i].fedTax);
  135.  
  136. /* Keep a running sum of the employee totals */
  137. employeeTotals = calcEmployeeTotals (employeeData[i].wageRate,
  138. employeeData[i].hours,
  139. employeeData[i].overtimeHrs,
  140. employeeData[i].grossPay,
  141. employeeData[i].stateTax,
  142. employeeData[i].fedTax,
  143. employeeData[i].netPay,
  144. employeeTotals);
  145.  
  146. } /* for */
  147.  
  148. /* Print the column headers */
  149. printHeader();
  150.  
  151. /* print out each employee */
  152. for (i = 0; i < NUM_EMPL; ++i)
  153. {
  154. printEmp (employeeData[i].name,
  155. employeeData[i].taxState,
  156. employeeData[i].clockNumber,
  157. employeeData[i].wageRate,
  158. employeeData[i].hours,
  159. employeeData[i].overtimeHrs,
  160. employeeData[i].grossPay,
  161. employeeData[i].stateTax,
  162. employeeData[i].fedTax,
  163. employeeData[i].netPay);
  164. }
  165.  
  166. /* print the totals and averages for all float items */
  167. printEmpStatistics (employeeTotals, NUM_EMPL);
  168.  
  169. return(0); /* success */
  170.  
  171. } /* main */
  172.  
  173. //**************************************************************
  174. // Function: getHours
  175. //
  176. // Purpose: Obtains input from user, the number of hours worked
  177. // per employee and stores the result in a local variable
  178. // that is passed back to the calling function.
  179. //
  180. // Parameters: clockNumber - The unique employee ID
  181. //
  182. // Returns: hoursWorked - hours worked in a given week
  183. //
  184. //**************************************************************
  185.  
  186. float getHours (long int clockNumber)
  187. {
  188.  
  189. float hoursWorked; /* hours worked in a given week */
  190.  
  191. /* Read in hours for employee */
  192. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  193. scanf ("%f", &hoursWorked);
  194.  
  195. /* return hours back to the calling function */
  196. return (hoursWorked);
  197. }
  198.  
  199. //**************************************************************
  200. // Function: printHeader
  201. //
  202. // Purpose: Prints the initial table header information.
  203. //
  204. // Parameters: none
  205. //
  206. // Returns: void
  207. //
  208. //**************************************************************
  209.  
  210. void printHeader (void)
  211. {
  212.  
  213. printf ("\n\n*** Pay Calculator ***\n");
  214.  
  215. /* print the table header */
  216. printf("\n--------------------------------------------------------------");
  217. printf("-------------------");
  218. printf("\nName Tax Clock# Wage Hours OT Gross ");
  219. printf(" State Fed Net");
  220. printf("\n State Pay ");
  221. printf(" Tax Tax Pay");
  222.  
  223. printf("\n--------------------------------------------------------------");
  224. printf("-------------------");
  225.  
  226. }
  227.  
  228.  
  229. //*************************************************************
  230. // Function: printEmp
  231. //
  232. // Purpose: Prints out all the information for an employee
  233. // in a nice and orderly table format.
  234. //
  235. // Parameters:
  236. //
  237. // clockNumber - unique employee ID
  238. // wageRate - hourly wage rate
  239. // hours - Hours worked for the week
  240. // overtimeHrs - overtime hours worked in a week
  241. // grossPay - gross pay for the week
  242. //
  243. // Returns: void
  244. //
  245. //**************************************************************
  246.  
  247. void printEmp (char name [], char taxState [],
  248. long int clockNumber, float wageRate,
  249. float hours, float overtimeHrs, float grossPay,
  250. float stateTax, float fedTax, float netPay)
  251. {
  252.  
  253. /* Print out a single employee */
  254. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  255. name, taxState, clockNumber, wageRate, hours,
  256. overtimeHrs, grossPay, stateTax, fedTax, netPay);
  257.  
  258. }
  259.  
  260. void printEmpStatistics (struct totals employeeTotals, int size)
  261. {
  262.  
  263. /* print the totals for all the floating point fields */
  264. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  265. employeeTotals.total_wageRate,
  266. employeeTotals.total_hours,
  267. employeeTotals.total_overtimeHrs,
  268. employeeTotals.total_grossPay,
  269. employeeTotals.total_stateTax,
  270. employeeTotals.total_fedTax,
  271. employeeTotals.total_netPay);
  272.  
  273. /* make sure you don't divide by zero or a negative number */
  274. if (size > 0)
  275. {
  276. /* print the averages for all the floating point fields */
  277. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  278. employeeTotals.total_wageRate/size,
  279. employeeTotals.total_hours/size,
  280. employeeTotals.total_overtimeHrs/size,
  281. employeeTotals.total_grossPay/size,
  282. employeeTotals.total_stateTax/size,
  283. employeeTotals.total_fedTax/size,
  284. employeeTotals.total_netPay/size);
  285. }
  286.  
  287. }
  288.  
  289. //*************************************************************
  290. // Function: calcOvertimeHrs
  291. //
  292. // Purpose: Calculates the overtime hours worked by an employee
  293. // in a given week.
  294. //
  295. // Parameters:
  296. //
  297. // hours - Hours worked in a given week
  298. //
  299. // Returns: theOvertimeHrs - overtime hours worked by an employee
  300. // in a given week
  301. //
  302. //**************************************************************
  303.  
  304. float calcOvertimeHrs (float hours)
  305. {
  306. float theOvertimeHrs; /* caculated overtime hours for employee */
  307.  
  308. /* Any overtime ? */
  309. if (hours >= STD_HOURS)
  310. {
  311. theOvertimeHrs = hours - STD_HOURS;
  312. }
  313. else /* no overtime */
  314. {
  315. theOvertimeHrs = 0;
  316. }
  317.  
  318. /* return overtime hours back to the calling function */
  319. return (theOvertimeHrs);
  320.  
  321. }
  322.  
  323. //*************************************************************
  324. // Function: calcGrossPay
  325. //
  326. // Purpose: Calculates the gross pay based on the the normal pay
  327. // and any overtime pay for a given week.
  328. //
  329. // Parameters:
  330. //
  331. // wageRate - the hourly wage rate
  332. // hours - the hours worked in a given week
  333. // overtimeHrs - hours worked above normal hours
  334. //
  335. // Returns: theGrossPay - total weekly gross pay for an employee
  336. //
  337. //**************************************************************
  338.  
  339. float calcGrossPay (float wageRate, float hours, float overtimeHrs)
  340. {
  341.  
  342. float theGrossPay; /* gross pay earned in a given week */
  343. float theNormalPay; /* normal pay without any overtime hours */
  344. float theOvertimePay; /* overtime pay */
  345.  
  346. /* calcuate normal pay and any overtime pay */
  347. theNormalPay = wageRate * (hours - overtimeHrs);
  348. theOvertimePay = overtimeHrs * (OT_RATE * wageRate);
  349.  
  350. /* calculate gross pay for employee as normalPay + any overtime pay */
  351. theGrossPay = theNormalPay + theOvertimePay;
  352.  
  353. return(theGrossPay);
  354.  
  355. }
  356.  
  357. //*************************************************************
  358. // Function: calcStateTax
  359. //
  360. // Purpose: Calculates the State Tax owed based on gross pay
  361. //
  362. // Parameters:
  363. //
  364. // grossPay - the grossPay for a given week
  365. // taxState - the physical state where the employee works
  366. //
  367. // Returns: theStateTax - calculated state tax owed
  368. //
  369. //**************************************************************
  370.  
  371. float calcStateTax (float grossPay, char taxState[])
  372. {
  373. float theStateTax; /* calculated state tax */
  374.  
  375. theStateTax = grossPay; /* initialize to gross pay */
  376.  
  377. /* calculate state tax based on where employee resides */
  378. if (strcmp(taxState, "MA") == 0)
  379. theStateTax *= MA_TAX_RATE;
  380. else if (strcmp(taxState, "VT") == 0)
  381. theStateTax *= VT_TAX_RATE;
  382. else if (strcmp(taxState, "NH") == 0)
  383. theStateTax *= NH_TAX_RATE;
  384. else if (strcmp(taxState, "CA") == 0)
  385. theStateTax *= CA_TAX_RATE;
  386. else
  387. theStateTax *= DEFAULT_TAX_RATE; /* any other state */
  388.  
  389. return (theStateTax);
  390.  
  391. }
  392.  
  393. //*************************************************************
  394. // Function: calcFedTax
  395. //
  396. // Purpose: Calculates the Federal Tax owed based on the gross
  397. // pay
  398. //
  399. // Parameters:
  400. //
  401. // grossPay - the grossPay for a given week
  402. //
  403. // Returns: theFedTax - calculated federal tax owed
  404. //
  405. //**************************************************************
  406.  
  407. float calcFedTax (float grossPay)
  408. {
  409. float theFedTax; /* The calculated Federal Tax */
  410.  
  411. /* Fed Tax is the same for all regardless of state */
  412. theFedTax = grossPay * 0.25;
  413.  
  414. return (theFedTax);
  415.  
  416. }
  417.  
  418. //*************************************************************
  419. // Function: calcNetPay
  420. //
  421. // Purpose: Calculates the net pay as the gross pay minus any
  422. // state and federal taxes owed. Essentially, your
  423. // "take home" pay.
  424. //
  425. // Parameters:
  426. //
  427. // grossPay - the grossPay for a given week
  428. // stateTax - the state taxes owed
  429. // fedTax - the fed taxes owed
  430. //
  431. // Returns: theNetPay - the calculated net pay.
  432. //**************************************************************
  433.  
  434. float calcNetPay (float grossPay, float stateTax, float fedTax)
  435. {
  436. float theNetPay; /* The calculated Federal Tax */
  437. float theTotalTaxes; /* total taxes owed */
  438.  
  439. /* calculate the total taxes */
  440. theTotalTaxes = stateTax + fedTax;
  441.  
  442. /* calculate the net pay */
  443. theNetPay = grossPay - theTotalTaxes;
  444.  
  445. return (theNetPay);
  446.  
  447. }
  448.  
  449.  
  450. struct totals calcEmployeeTotals (float wageRate,
  451. float hours,
  452. float overtimeHrs,
  453. float grossPay,
  454. float stateTax,
  455. float fedTax,
  456. float netPay,
  457. struct totals employeeTotals)
  458. {
  459.  
  460. /* add current employee to our running totals */
  461. employeeTotals.total_wageRate += wageRate;
  462. employeeTotals.total_hours += hours;
  463. employeeTotals.total_overtimeHrs += overtimeHrs;
  464. employeeTotals.total_grossPay += grossPay;
  465. employeeTotals.total_stateTax += stateTax;
  466. employeeTotals.total_fedTax += fedTax;
  467. employeeTotals.total_netPay += netPay;
  468.  
  469. /* return all the updated totals to the calling function */
  470. return (employeeTotals);
  471.  
  472. }
Success #stdin #stdout 0.01s 5300KB
stdin
51.0
42.5
37.0
45.0
40.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 ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net
                   State                           Pay     Tax    Tax      Pay
---------------------------------------------------------------------------------
Connie Cobol         MA  098401 10.60  51.0  11.0  598.90  29.95  149.73   419.23
Mary Apl             NH  526488  9.75  42.5   2.5  426.56   0.00  106.64   319.92
Frank Fortran        VT  765349 10.50  37.0   0.0  388.50  23.31   97.12   268.07
Jeff Ada             NY  034645 12.25  45.0   5.0  581.88  46.55  145.47   389.86
Anton Pascal         CA  127615  8.35  40.0   0.0  334.00  23.38   83.50   227.12
Totals:                         51.45 215.5  18.5 2329.84 123.18  582.46  1624.19
Averages:                       10.29  43.1   3.7  465.97  24.64  116.49   324.84