fork download
  1. //********************************************************
  2. //
  3. // Assignment 10 - Linked Lists, Typedef, and Macros
  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. //********************************************************
  16.  
  17. // headers
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21. #include <stdlib.h>
  22.  
  23. // constants
  24. #define STD_HOURS 40.0
  25. #define OT_RATE 1.5
  26.  
  27. #define MA_TAX_RATE 0.05
  28. #define NH_TAX_RATE 0.0
  29. #define VT_TAX_RATE 0.06
  30. #define CA_TAX_RATE 0.07
  31. #define DEFAULT_STATE_TAX_RATE 0.08
  32.  
  33. #define FED_TAX_RATE 0.25
  34.  
  35. #define NAME_SIZE 20
  36. #define TAX_STATE_SIZE 3
  37. #define FIRST_NAME_SIZE 10
  38. #define LAST_NAME_SIZE 10
  39.  
  40. // macros
  41. #define CALC_OT_HOURS(theHours) ((theHours > STD_HOURS) ? (theHours - STD_HOURS) : 0)
  42.  
  43. #define CALC_STATE_TAX(thePay,theStateTaxRate) ((thePay) * (theStateTaxRate))
  44.  
  45. #define CALC_FED_TAX(thePay) ((thePay) * FED_TAX_RATE)
  46.  
  47. #define CALC_NET_PAY(thePay,theStateTax,theFedTax) \
  48. ((thePay) - ((theStateTax) + (theFedTax)))
  49.  
  50. #define CALC_NORMAL_PAY(theWageRate,theHours,theOvertimeHrs) \
  51. ((theWageRate) * ((theHours) - (theOvertimeHrs)))
  52.  
  53. #define CALC_OT_PAY(theWageRate,theOvertimeHrs) \
  54. ((theOvertimeHrs) * (OT_RATE * (theWageRate)))
  55.  
  56. // FIXED MIN/MAX MACROS
  57. #define CALC_MIN(theValue, currentMin) \
  58. (((theValue) < (currentMin)) ? (theValue) : (currentMin))
  59.  
  60. #define CALC_MAX(theValue, currentMax) \
  61. (((theValue) > (currentMax)) ? (theValue) : (currentMax))
  62.  
  63. // name struct
  64. struct name {
  65. char firstName[FIRST_NAME_SIZE];
  66. char lastName[LAST_NAME_SIZE];
  67. };
  68.  
  69. // employee typedef
  70. typedef struct employee {
  71. struct name empName;
  72. char taxState[TAX_STATE_SIZE];
  73. long int clockNumber;
  74. float wageRate;
  75. float hours;
  76. float overtimeHrs;
  77. float grossPay;
  78. float stateTax;
  79. float fedTax;
  80. float netPay;
  81. struct employee *next;
  82. } EMPLOYEE;
  83.  
  84. // totals typedef
  85. typedef struct totals {
  86. float total_wageRate;
  87. float total_hours;
  88. float total_overtimeHrs;
  89. float total_grossPay;
  90. float total_stateTax;
  91. float total_fedTax;
  92. float total_netPay;
  93. } TOTALS;
  94.  
  95. // FIXED typedef MIN_MAX
  96. typedef struct min_max {
  97. float min_wageRate;
  98. float min_hours;
  99. float min_overtimeHrs;
  100. float min_grossPay;
  101. float min_stateTax;
  102. float min_fedTax;
  103. float min_netPay;
  104.  
  105. float max_wageRate;
  106. float max_hours;
  107. float max_overtimeHrs;
  108. float max_grossPay;
  109. float max_stateTax;
  110. float max_fedTax;
  111. float max_netPay;
  112. } MIN_MAX;
  113.  
  114. // prototypes
  115. EMPLOYEE * getEmpData(void);
  116. int isEmployeeSize(EMPLOYEE * head_ptr);
  117.  
  118. void calcOvertimeHrs(EMPLOYEE * head_ptr);
  119. void calcGrossPay(EMPLOYEE * head_ptr);
  120. void calcStateTax(EMPLOYEE * head_ptr);
  121. void calcFedTax(EMPLOYEE * head_ptr);
  122. void calcNetPay(EMPLOYEE * head_ptr);
  123.  
  124. void calcEmployeeTotals(EMPLOYEE * head_ptr, TOTALS * emp_totals_ptr);
  125.  
  126. void calcEmployeeMinMax(EMPLOYEE * head_ptr, MIN_MAX * emp_minMax_ptr);
  127.  
  128. void printEmpStatistics(TOTALS * emp_totals_ptr, MIN_MAX * emp_minMax_ptr, int size);
  129.  
  130. void printHeader(void);
  131. void printEmp(EMPLOYEE * head_ptr);
  132.  
  133. // MAIN
  134. int main()
  135. {
  136. EMPLOYEE * head_ptr;
  137. int theSize;
  138.  
  139. TOTALS employeeTotals = {0};
  140. TOTALS * emp_totals_ptr = &employeeTotals;
  141.  
  142. MIN_MAX employeeMinMax = {0};
  143. MIN_MAX * emp_minMax_ptr = &employeeMinMax;
  144.  
  145. head_ptr = getEmpData();
  146.  
  147. theSize = isEmployeeSize(head_ptr);
  148.  
  149. if (theSize <= 0)
  150. {
  151. printf("\n\n**** There was no employee input to process ***\n");
  152. }
  153. else
  154. {
  155. calcOvertimeHrs(head_ptr);
  156. calcGrossPay(head_ptr);
  157. calcStateTax(head_ptr);
  158. calcFedTax(head_ptr);
  159. calcNetPay(head_ptr);
  160.  
  161. calcEmployeeTotals(head_ptr, emp_totals_ptr);
  162. calcEmployeeMinMax(head_ptr, emp_minMax_ptr);
  163.  
  164. printHeader();
  165. printEmp(head_ptr);
  166.  
  167. printEmpStatistics(emp_totals_ptr, emp_minMax_ptr, theSize);
  168. }
  169.  
  170. printf("\n\n *** End of Program *** \n");
  171. return 0;
  172. }
  173.  
  174. // GET DATA (unchanged)
  175. EMPLOYEE * getEmpData(void)
  176. {
  177. char answer[80];
  178. int more_data = 1;
  179. char value;
  180.  
  181. EMPLOYEE *current_ptr, *head_ptr;
  182.  
  183. head_ptr = malloc(sizeof(EMPLOYEE));
  184. current_ptr = head_ptr;
  185.  
  186. while (more_data)
  187. {
  188. printf("\nEnter employee first name: ");
  189. scanf("%s", current_ptr->empName.firstName);
  190.  
  191. printf("\nEnter employee last name: ");
  192. scanf("%s", current_ptr->empName.lastName);
  193.  
  194. printf("\nEnter employee two character tax state: ");
  195. scanf("%s", current_ptr->taxState);
  196.  
  197. printf("\nEnter employee clock number: ");
  198. scanf("%li", &current_ptr->clockNumber);
  199.  
  200. printf("\nEnter employee hourly wage: ");
  201. scanf("%f", &current_ptr->wageRate);
  202.  
  203. printf("\nEnter hours worked this week: ");
  204. scanf("%f", &current_ptr->hours);
  205.  
  206. printf("\nWould you like to add another employee? (y/n): ");
  207. scanf("%s", answer);
  208.  
  209. if ((value = toupper(answer[0])) != 'Y')
  210. {
  211. current_ptr->next = NULL;
  212. more_data = 0;
  213. }
  214. else
  215. {
  216. current_ptr->next = malloc(sizeof(EMPLOYEE));
  217. current_ptr = current_ptr->next;
  218. }
  219. }
  220.  
  221. return head_ptr;
  222. }
  223.  
  224. // SIZE
  225. int isEmployeeSize(EMPLOYEE * head_ptr)
  226. {
  227. int size = 0;
  228. EMPLOYEE * current;
  229.  
  230. if (head_ptr->empName.firstName[0] != '\0')
  231. {
  232. for (current = head_ptr; current; current = current->next)
  233. size++;
  234. }
  235.  
  236. return size;
  237. }
  238.  
  239. // PRINT HEADER
  240. void printHeader(void)
  241. {
  242. printf("\n\n*** Pay Calculator ***\n");
  243. }
  244.  
  245. // PRINT EMP
  246. void printEmp(EMPLOYEE * head_ptr)
  247. {
  248. EMPLOYEE * current;
  249. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 2];
  250.  
  251. for (current = head_ptr; current; current = current->next)
  252. {
  253. strcpy(name, current->empName.firstName);
  254. strcat(name, " ");
  255. strcat(name, current->empName.lastName);
  256.  
  257. printf("\n%-20s %-2s %06li %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  258. name,
  259. current->taxState,
  260. current->clockNumber,
  261. current->wageRate,
  262. current->hours,
  263. current->overtimeHrs,
  264. current->grossPay,
  265. current->stateTax,
  266. current->fedTax,
  267. current->netPay);
  268. }
  269. }
  270.  
  271. // OVERTIME
  272. void calcOvertimeHrs(EMPLOYEE * head_ptr)
  273. {
  274. for (EMPLOYEE * c = head_ptr; c; c = c->next)
  275. c->overtimeHrs = CALC_OT_HOURS(c->hours);
  276. }
  277.  
  278. // GROSS
  279. void calcGrossPay(EMPLOYEE * head_ptr)
  280. {
  281. for (EMPLOYEE * c = head_ptr; c; c = c->next)
  282. {
  283. float normal = CALC_NORMAL_PAY(c->wageRate, c->hours, c->overtimeHrs);
  284. float ot = CALC_OT_PAY(c->wageRate, c->overtimeHrs);
  285. c->grossPay = normal + ot;
  286. }
  287. }
  288.  
  289. // STATE TAX
  290. void calcStateTax(EMPLOYEE * head_ptr)
  291. {
  292. for (EMPLOYEE * c = head_ptr; c; c = c->next)
  293. {
  294. if (strcmp(c->taxState, "MA") == 0)
  295. c->stateTax = CALC_STATE_TAX(c->grossPay, MA_TAX_RATE);
  296. else if (strcmp(c->taxState, "VT") == 0)
  297. c->stateTax = CALC_STATE_TAX(c->grossPay, VT_TAX_RATE);
  298. else if (strcmp(c->taxState, "NH") == 0)
  299. c->stateTax = CALC_STATE_TAX(c->grossPay, NH_TAX_RATE);
  300. else if (strcmp(c->taxState, "CA") == 0)
  301. c->stateTax = CALC_STATE_TAX(c->grossPay, CA_TAX_RATE);
  302. else
  303. c->stateTax = CALC_STATE_TAX(c->grossPay, DEFAULT_STATE_TAX_RATE);
  304. }
  305. }
  306.  
  307. // FED TAX FIXED
  308. void calcFedTax(EMPLOYEE * head_ptr)
  309. {
  310. for (EMPLOYEE * c = head_ptr; c; c = c->next)
  311. c->fedTax = CALC_FED_TAX(c->grossPay);
  312. }
  313.  
  314. // NET
  315. void calcNetPay(EMPLOYEE * head_ptr)
  316. {
  317. for (EMPLOYEE * c = head_ptr; c; c = c->next)
  318. c->netPay = CALC_NET_PAY(c->grossPay, c->stateTax, c->fedTax);
  319. }
  320.  
  321. // TOTALS
  322. void calcEmployeeTotals(EMPLOYEE * head_ptr, TOTALS * t)
  323. {
  324. for (EMPLOYEE * c = head_ptr; c; c = c->next)
  325. {
  326. t->total_wageRate += c->wageRate;
  327. t->total_hours += c->hours;
  328. t->total_overtimeHrs += c->overtimeHrs;
  329. t->total_grossPay += c->grossPay;
  330. t->total_stateTax += c->stateTax;
  331. t->total_fedTax += c->fedTax;
  332. t->total_netPay += c->netPay;
  333. }
  334. }
  335.  
  336. // MIN MAX FIXED
  337. void calcEmployeeMinMax(EMPLOYEE * head_ptr, MIN_MAX * m)
  338. {
  339. EMPLOYEE * c = head_ptr;
  340.  
  341. m->min_wageRate = m->max_wageRate = c->wageRate;
  342. m->min_hours = m->max_hours = c->hours;
  343. m->min_overtimeHrs = m->max_overtimeHrs = c->overtimeHrs;
  344. m->min_grossPay = m->max_grossPay = c->grossPay;
  345. m->min_stateTax = m->max_stateTax = c->stateTax;
  346. m->min_fedTax = m->max_fedTax = c->fedTax;
  347. m->min_netPay = m->max_netPay = c->netPay;
  348.  
  349. for (c = c->next; c; c = c->next)
  350. {
  351. m->min_wageRate = CALC_MIN(c->wageRate, m->min_wageRate);
  352. m->max_wageRate = CALC_MAX(c->wageRate, m->max_wageRate);
  353.  
  354. m->min_hours = CALC_MIN(c->hours, m->min_hours);
  355. m->max_hours = CALC_MAX(c->hours, m->max_hours);
  356.  
  357. m->min_overtimeHrs = CALC_MIN(c->overtimeHrs, m->min_overtimeHrs);
  358. m->max_overtimeHrs = CALC_MAX(c->overtimeHrs, m->max_overtimeHrs);
  359.  
  360. m->min_grossPay = CALC_MIN(c->grossPay, m->min_grossPay);
  361. m->max_grossPay = CALC_MAX(c->grossPay, m->max_grossPay);
  362.  
  363. m->min_stateTax = CALC_MIN(c->stateTax, m->min_stateTax);
  364. m->max_stateTax = CALC_MAX(c->stateTax, m->max_stateTax);
  365.  
  366. m->min_fedTax = CALC_MIN(c->fedTax, m->min_fedTax);
  367. m->max_fedTax = CALC_MAX(c->fedTax, m->max_fedTax);
  368.  
  369. m->min_netPay = CALC_MIN(c->netPay, m->min_netPay);
  370. m->max_netPay = CALC_MAX(c->netPay, m->max_netPay);
  371. }
  372. }
  373.  
  374. // STATS PRINT
  375. void printEmpStatistics(TOTALS * t, MIN_MAX * m, int size)
  376. {
  377. printf("\n--------------------------------------------------------------");
  378.  
  379. printf("\nTotals...");
  380. printf("\nAverages...");
  381. printf("\nMinimum...");
  382. printf("\nMaximum...");
  383.  
  384. printf("\n\nThe total employees processed was: %d\n", size);
  385. }
  386.  
Success #stdin #stdout 0s 5320KB
stdin
Connie
Cobol
MA
98401
10.60
51.0
Y
Mary
Apl
NH
526488
9.75
42.5
Y
Frank
Fortran
VT
765349
10.50
37.0
Y
Jeff
Ada
NY
34645
12.25
45
Y
Anton
Pascal
CA
127615
8.35
40.0
N
stdout
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 
Enter employee first name: 
Enter employee last name: 
Enter employee two character tax state: 
Enter employee clock number: 
Enter employee hourly wage: 
Enter hours worked this week: 
Would you like to add another employee? (y/n): 

*** Pay Calculator ***

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...
Averages...
Minimum...
Maximum...

The total employees processed was: 5


 *** End of Program ***