fork download
  1. //********************************************************
  2. //
  3. // Assignment 8 - Structures and Strings and Pointers
  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 calculates the state tax, federal tax, and net pay. It
  17. // also calculates totals, averages, minimum, and maximum values.
  18. //
  19. // Array and Structure references are to be replaced with
  20. // pointer references to speed up the processing of this code.
  21. //
  22. // Call by Reference design (using pointers)
  23. //
  24. //********************************************************
  25.  
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29.  
  30. // Define constants
  31. #define SIZE 5
  32. #define STD_HOURS 40.0
  33. #define OT_RATE 1.5
  34. #define MA_TAX_RATE 0.05
  35. #define NH_TAX_RATE 0.0
  36. #define VT_TAX_RATE 0.06
  37. #define CA_TAX_RATE 0.07
  38. #define DEFAULT_TAX_RATE 0.08
  39. #define NAME_SIZE 20
  40. #define TAX_STATE_SIZE 3
  41. #define FED_TAX_RATE 0.25
  42. #define FIRST_NAME_SIZE 10
  43. #define LAST_NAME_SIZE 10
  44.  
  45. // Structure to store employee name
  46. struct name
  47. {
  48. char firstName[FIRST_NAME_SIZE];
  49. char lastName[LAST_NAME_SIZE];
  50. };
  51.  
  52. // Structure to store employee information
  53. struct employee
  54. {
  55. struct name empName;
  56. char taxState[TAX_STATE_SIZE];
  57. long int clockNumber;
  58. float wageRate;
  59. float hours;
  60. float overtimeHrs;
  61. float grossPay;
  62. float stateTax;
  63. float fedTax;
  64. float netPay;
  65. };
  66.  
  67. // Structure to store employee totals
  68. struct totals
  69. {
  70. float total_wageRate;
  71. float total_hours;
  72. float total_overtimeHrs;
  73. float total_grossPay;
  74. float total_stateTax;
  75. float total_fedTax;
  76. float total_netPay;
  77. };
  78.  
  79. // Structure to store minimum and maximum values
  80. struct min_max
  81. {
  82. float min_wageRate;
  83. float min_hours;
  84. float min_overtimeHrs;
  85. float min_grossPay;
  86. float min_stateTax;
  87. float min_fedTax;
  88. float min_netPay;
  89.  
  90. float max_wageRate;
  91. float max_hours;
  92. float max_overtimeHrs;
  93. float max_grossPay;
  94. float max_stateTax;
  95. float max_fedTax;
  96. float max_netPay;
  97. };
  98.  
  99. // Function prototypes
  100.  
  101. void getHours(struct employee *emp_ptr, int theSize);
  102. void printEmp(struct employee *emp_ptr, int theSize);
  103.  
  104. void calcEmployeeTotals(struct employee *emp_ptr,
  105. struct totals *emp_totals_ptr,
  106. int theSize);
  107.  
  108. void calcEmployeeMinMax(struct employee *emp_ptr,
  109. struct min_max *emp_minMax_ptr,
  110. int theSize);
  111.  
  112. void printHeader(void);
  113.  
  114. void calcOvertimeHrs(struct employee *emp_ptr, int theSize);
  115. void calcGrossPay(struct employee *emp_ptr, int theSize);
  116. void calcStateTax(struct employee *emp_ptr, int theSize);
  117. void calcFedTax(struct employee *emp_ptr, int theSize);
  118. void calcNetPay(struct employee *emp_ptr, int theSize);
  119.  
  120. void printEmpStatistics(struct totals *emp_totals_ptr,
  121. struct min_max *emp_minMax_ptr,
  122. int theSize);
  123.  
  124. //*************************************************************
  125. // Function: main
  126. //*************************************************************
  127.  
  128. int main(void)
  129. {
  130. // Initialize employee information
  131. struct employee employeeData[SIZE] =
  132. {
  133. { {"Connie", "Cobol"}, "MA", 98401, 10.60 },
  134. { {"Mary", "Apl"}, "NH", 526488, 9.75 },
  135. { {"Frank", "Fortran"}, "VT", 765349, 10.50 },
  136. { {"Jeff", "Ada"}, "NY", 34645, 12.25 },
  137. { {"Anton", "Pascal"}, "CA", 127615, 8.35 }
  138. };
  139.  
  140. // Pointer to employee array
  141. struct employee *emp_ptr;
  142.  
  143. // Point to the first employee
  144. emp_ptr = employeeData;
  145.  
  146. // Initialize employee totals
  147. struct totals employeeTotals = {0};
  148.  
  149. // Pointer to employee totals
  150. struct totals *emp_totals_ptr = &employeeTotals;
  151.  
  152. // Initialize minimum and maximum values
  153. struct min_max employeeMinMax = {0};
  154.  
  155. // Pointer to minimum and maximum structure
  156. struct min_max *emp_minMax_ptr = &employeeMinMax;
  157.  
  158. // Get employee hours
  159. getHours(emp_ptr, SIZE);
  160.  
  161. // Calculate overtime hours
  162. calcOvertimeHrs(emp_ptr, SIZE);
  163.  
  164. // Calculate gross pay
  165. calcGrossPay(emp_ptr, SIZE);
  166.  
  167. // Calculate state taxes
  168. calcStateTax(emp_ptr, SIZE);
  169.  
  170. // Calculate federal taxes
  171. calcFedTax(emp_ptr, SIZE);
  172.  
  173. // Calculate net pay
  174. calcNetPay(emp_ptr, SIZE);
  175.  
  176. // Calculate employee totals
  177. calcEmployeeTotals(emp_ptr, emp_totals_ptr, SIZE);
  178.  
  179. // Calculate employee minimum and maximum values
  180. calcEmployeeMinMax(emp_ptr, emp_minMax_ptr, SIZE);
  181.  
  182. // Print report header
  183. printHeader();
  184.  
  185. // Print employee information
  186. printEmp(emp_ptr, SIZE);
  187.  
  188. // Print totals, averages, minimums, and maximums
  189. printEmpStatistics(emp_totals_ptr, emp_minMax_ptr, SIZE);
  190.  
  191. return 0;
  192. }
  193.  
  194. //*************************************************************
  195. // Function: getHours
  196. // Purpose: Obtains hours worked for each employee.
  197. //*************************************************************
  198.  
  199. void getHours(struct employee *emp_ptr, int theSize)
  200. {
  201. int i;
  202.  
  203. for (i = 0; i < theSize; ++i)
  204. {
  205. printf("\nEnter hours worked by emp # %06li: ",
  206. emp_ptr->clockNumber);
  207.  
  208. scanf("%f", &emp_ptr->hours);
  209.  
  210. // Advance to the next employee
  211. ++emp_ptr;
  212. }
  213. }
  214.  
  215. //*************************************************************
  216. // Function: printHeader
  217. // Purpose: Prints report headings.
  218. //*************************************************************
  219.  
  220. void printHeader(void)
  221. {
  222. printf("\n\n*** Pay Calculator ***\n");
  223.  
  224. printf("\n--------------------------------------------------------------");
  225. printf("-------------------");
  226.  
  227. printf("\nName Tax Clock# Wage Hours OT Gross ");
  228. printf(" State Fed Net");
  229.  
  230. printf("\n State Pay ");
  231. printf(" Tax Tax Pay");
  232.  
  233. printf("\n--------------------------------------------------------------");
  234. printf("-------------------");
  235. }
  236.  
  237. //*************************************************************
  238. // Function: printEmp
  239. // Purpose: Prints employee information.
  240. //*************************************************************
  241.  
  242. void printEmp(struct employee *emp_ptr, int theSize)
  243. {
  244. int i;
  245.  
  246. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  247.  
  248. for (i = 0; i < theSize; ++i)
  249. {
  250. // Combine first and last names
  251. strcpy(name, emp_ptr->empName.firstName);
  252. strcat(name, " ");
  253. strcat(name, emp_ptr->empName.lastName);
  254.  
  255. // Print employee information
  256. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  257. name,
  258. emp_ptr->taxState,
  259. emp_ptr->clockNumber,
  260. emp_ptr->wageRate,
  261. emp_ptr->hours,
  262. emp_ptr->overtimeHrs,
  263. emp_ptr->grossPay,
  264. emp_ptr->stateTax,
  265. emp_ptr->fedTax,
  266. emp_ptr->netPay);
  267.  
  268. // Advance to the next employee
  269. ++emp_ptr;
  270. }
  271. }
  272.  
  273. //*************************************************************
  274. // Function: printEmpStatistics
  275. // Purpose: Prints totals, averages, minimums, and maximums.
  276. //*************************************************************
  277.  
  278. void printEmpStatistics(struct totals *emp_totals_ptr,
  279. struct min_max *emp_minMax_ptr,
  280. int theSize)
  281. {
  282. printf("\n--------------------------------------------------------------");
  283. printf("-------------------");
  284.  
  285. // Print totals
  286. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  287. emp_totals_ptr->total_wageRate,
  288. emp_totals_ptr->total_hours,
  289. emp_totals_ptr->total_overtimeHrs,
  290. emp_totals_ptr->total_grossPay,
  291. emp_totals_ptr->total_stateTax,
  292. emp_totals_ptr->total_fedTax,
  293. emp_totals_ptr->total_netPay);
  294.  
  295. if (theSize > 0)
  296. {
  297. // Print averages
  298. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  299. emp_totals_ptr->total_wageRate / theSize,
  300. emp_totals_ptr->total_hours / theSize,
  301. emp_totals_ptr->total_overtimeHrs / theSize,
  302. emp_totals_ptr->total_grossPay / theSize,
  303. emp_totals_ptr->total_stateTax / theSize,
  304. emp_totals_ptr->total_fedTax / theSize,
  305. emp_totals_ptr->total_netPay / theSize);
  306.  
  307. // Print minimum values
  308. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  309. emp_minMax_ptr->min_wageRate,
  310. emp_minMax_ptr->min_hours,
  311. emp_minMax_ptr->min_overtimeHrs,
  312. emp_minMax_ptr->min_grossPay,
  313. emp_minMax_ptr->min_stateTax,
  314. emp_minMax_ptr->min_fedTax,
  315. emp_minMax_ptr->min_netPay);
  316.  
  317. // Print maximum values
  318. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  319. emp_minMax_ptr->max_wageRate,
  320. emp_minMax_ptr->max_hours,
  321. emp_minMax_ptr->max_overtimeHrs,
  322. emp_minMax_ptr->max_grossPay,
  323. emp_minMax_ptr->max_stateTax,
  324. emp_minMax_ptr->max_fedTax,
  325. emp_minMax_ptr->max_netPay);
  326. }
  327. }
  328.  
  329. //*************************************************************
  330. // Function: calcOvertimeHrs
  331. // Purpose: Calculates employee overtime hours.
  332. //*************************************************************
  333.  
  334. void calcOvertimeHrs(struct employee *emp_ptr, int theSize)
  335. {
  336. int i;
  337.  
  338. for (i = 0; i < theSize; ++i)
  339. {
  340. if (emp_ptr->hours > STD_HOURS)
  341. {
  342. emp_ptr->overtimeHrs = emp_ptr->hours - STD_HOURS;
  343. }
  344. else
  345. {
  346. emp_ptr->overtimeHrs = 0.0f;
  347. }
  348.  
  349. // Advance pointer
  350. ++emp_ptr;
  351. }
  352. }
  353.  
  354. //*************************************************************
  355. // Function: calcGrossPay
  356. // Purpose: Calculates employee gross pay.
  357. //*************************************************************
  358.  
  359. void calcGrossPay(struct employee *emp_ptr, int theSize)
  360. {
  361. int i;
  362. float theNormalPay;
  363. float theOvertimePay;
  364.  
  365. for (i = 0; i < theSize; ++i)
  366. {
  367. theNormalPay = emp_ptr->wageRate *
  368. (emp_ptr->hours - emp_ptr->overtimeHrs);
  369.  
  370. theOvertimePay = emp_ptr->overtimeHrs *
  371. (OT_RATE * emp_ptr->wageRate);
  372.  
  373. emp_ptr->grossPay = theNormalPay + theOvertimePay;
  374.  
  375. // Advance pointer
  376. ++emp_ptr;
  377. }
  378. }
  379.  
  380. //*************************************************************
  381. // Function: calcStateTax
  382. // Purpose: Calculates employee state tax.
  383. //*************************************************************
  384.  
  385. void calcStateTax(struct employee *emp_ptr, int theSize)
  386. {
  387. int i;
  388.  
  389. for (i = 0; i < theSize; ++i)
  390. {
  391. // Convert state abbreviation to uppercase
  392. if (islower((unsigned char)emp_ptr->taxState[0]))
  393. {
  394. emp_ptr->taxState[0] =
  395. (char)toupper((unsigned char)emp_ptr->taxState[0]);
  396. }
  397.  
  398. if (islower((unsigned char)emp_ptr->taxState[1]))
  399. {
  400. emp_ptr->taxState[1] =
  401. (char)toupper((unsigned char)emp_ptr->taxState[1]);
  402. }
  403.  
  404. if (strcmp(emp_ptr->taxState, "MA") == 0)
  405. {
  406. emp_ptr->stateTax = emp_ptr->grossPay * MA_TAX_RATE;
  407. }
  408. else if (strcmp(emp_ptr->taxState, "VT") == 0)
  409. {
  410. emp_ptr->stateTax = emp_ptr->grossPay * VT_TAX_RATE;
  411. }
  412. else if (strcmp(emp_ptr->taxState, "NH") == 0)
  413. {
  414. emp_ptr->stateTax = emp_ptr->grossPay * NH_TAX_RATE;
  415. }
  416. else if (strcmp(emp_ptr->taxState, "CA") == 0)
  417. {
  418. emp_ptr->stateTax = emp_ptr->grossPay * CA_TAX_RATE;
  419. }
  420. else
  421. {
  422. emp_ptr->stateTax = emp_ptr->grossPay * DEFAULT_TAX_RATE;
  423. }
  424.  
  425. // Advance pointer
  426. ++emp_ptr;
  427. }
  428. }
  429.  
  430. //*************************************************************
  431. // Function: calcFedTax
  432. // Purpose: Calculates employee federal tax.
  433. //*************************************************************
  434.  
  435. void calcFedTax(struct employee *emp_ptr, int theSize)
  436. {
  437. int i;
  438.  
  439. for (i = 0; i < theSize; ++i)
  440. {
  441. emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
  442.  
  443. // Advance pointer
  444. ++emp_ptr;
  445. }
  446. }
  447.  
  448. //*************************************************************
  449. // Function: calcNetPay
  450. // Purpose: Calculates employee net pay.
  451. //*************************************************************
  452.  
  453. void calcNetPay(struct employee *emp_ptr, int theSize)
  454. {
  455. int i;
  456. float theTotalTaxes;
  457.  
  458. for (i = 0; i < theSize; ++i)
  459. {
  460. theTotalTaxes = emp_ptr->stateTax + emp_ptr->fedTax;
  461.  
  462. emp_ptr->netPay = emp_ptr->grossPay - theTotalTaxes;
  463.  
  464. // Advance pointer
  465. ++emp_ptr;
  466. }
  467. }
  468.  
  469. //*************************************************************
  470. // Function: calcEmployeeTotals
  471. // Purpose: Calculates totals for all employee records.
  472. //*************************************************************
  473.  
  474. void calcEmployeeTotals(struct employee *emp_ptr,
  475. struct totals *emp_totals_ptr,
  476. int theSize)
  477. {
  478. int i;
  479.  
  480. for (i = 0; i < theSize; ++i)
  481. {
  482. emp_totals_ptr->total_wageRate += emp_ptr->wageRate;
  483. emp_totals_ptr->total_hours += emp_ptr->hours;
  484. emp_totals_ptr->total_overtimeHrs += emp_ptr->overtimeHrs;
  485. emp_totals_ptr->total_grossPay += emp_ptr->grossPay;
  486. emp_totals_ptr->total_stateTax += emp_ptr->stateTax;
  487. emp_totals_ptr->total_fedTax += emp_ptr->fedTax;
  488. emp_totals_ptr->total_netPay += emp_ptr->netPay;
  489.  
  490. // Advance to next employee
  491. ++emp_ptr;
  492. }
  493. }
  494.  
  495. //*************************************************************
  496. // Function: calcEmployeeMinMax
  497. // Purpose: Calculates minimum and maximum employee values.
  498. //*************************************************************
  499.  
  500. void calcEmployeeMinMax(struct employee *emp_ptr,
  501. struct min_max *emp_minMax_ptr,
  502. int theSize)
  503. {
  504. int i;
  505.  
  506. // Initialize minimum values using the first employee
  507. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  508. emp_minMax_ptr->min_hours = emp_ptr->hours;
  509. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  510. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  511. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  512. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  513. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  514.  
  515. // Initialize maximum values using the first employee
  516. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  517. emp_minMax_ptr->max_hours = emp_ptr->hours;
  518. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  519. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  520. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  521. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  522. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  523.  
  524. // Compare remaining employees
  525. for (i = 1; i < theSize; ++i)
  526. {
  527. // Advance pointer to next employee
  528. ++emp_ptr;
  529.  
  530. if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate)
  531. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  532.  
  533. if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate)
  534. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  535.  
  536. if (emp_ptr->hours < emp_minMax_ptr->min_hours)
  537. emp_minMax_ptr->min_hours = emp_ptr->hours;
  538.  
  539. if (emp_ptr->hours > emp_minMax_ptr->max_hours)
  540. emp_minMax_ptr->max_hours = emp_ptr->hours;
  541.  
  542. if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
  543. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  544.  
  545. if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
  546. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  547.  
  548. if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  549. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  550.  
  551. if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  552. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  553.  
  554. if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  555. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  556.  
  557. if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  558. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  559.  
  560. if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  561. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  562.  
  563. if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  564. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  565.  
  566. if (emp_ptr->netPay < emp_minMax_ptr->min_netPay)
  567. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  568.  
  569. if (emp_ptr->netPay > emp_minMax_ptr->max_netPay)
  570. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  571. }
  572. }
Success #stdin #stdout 0s 5320KB
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
Minimum:                         8.35  37.0   0.0  334.00   0.00   83.50   227.12
Maximum:                        12.25  51.0  11.0  598.90  46.55  149.73   419.23