fork download
  1. //********************************************************
  2. //
  3. // Assignment 9 - Linked Lists
  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 calculates overtime, gross pay,
  12. // state/federal taxes, and net pay for employees using
  13. // a dynamically allocated linked list.
  14. //
  15. //********************************************************
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21.  
  22. #define STD_HOURS 40.0
  23. #define OT_RATE 1.5
  24. #define MA_TAX_RATE 0.05
  25. #define NH_TAX_RATE 0.0
  26. #define VT_TAX_RATE 0.06
  27. #define CA_TAX_RATE 0.07
  28. #define DEFAULT_TAX_RATE 0.08
  29. #define NAME_SIZE 20
  30. #define TAX_STATE_SIZE 3
  31. #define FED_TAX_RATE 0.25
  32. #define FIRST_NAME_SIZE 10
  33. #define LAST_NAME_SIZE 10
  34.  
  35. struct name {
  36. char firstName[FIRST_NAME_SIZE];
  37. char lastName [LAST_NAME_SIZE];
  38. };
  39.  
  40. struct employee {
  41. struct name empName;
  42. char taxState[TAX_STATE_SIZE];
  43. long int clockNumber;
  44. float wageRate;
  45. float hours;
  46. float overtimeHrs;
  47. float grossPay;
  48. float stateTax;
  49. float fedTax;
  50. float netPay;
  51. struct employee *next;
  52. };
  53.  
  54. struct totals {
  55. float total_wageRate;
  56. float total_hours;
  57. float total_overtimeHrs;
  58. float total_grossPay;
  59. float total_stateTax;
  60. float total_fedTax;
  61. float total_netPay;
  62. };
  63.  
  64. struct min_max {
  65. float min_wageRate;
  66. float min_hours;
  67. float min_overtimeHrs;
  68. float min_grossPay;
  69. float min_stateTax;
  70. float min_fedTax;
  71. float min_netPay;
  72. float max_wageRate;
  73. float max_hours;
  74. float max_overtimeHrs;
  75. float max_grossPay;
  76. float max_stateTax;
  77. float max_fedTax;
  78. float max_netPay;
  79. };
  80.  
  81. // Prototypes
  82. struct employee* getEmpData(void);
  83. int isEmployeeSize(struct employee* head_ptr);
  84. void calcOvertimeHrs(struct employee* head_ptr);
  85. void calcGrossPay(struct employee* head_ptr);
  86. void calcStateTax(struct employee* head_ptr);
  87. void calcFedTax(struct employee* head_ptr);
  88. void calcNetPay(struct employee* head_ptr);
  89. void calcEmployeeTotals(struct employee* head_ptr, struct totals* emp_totals_ptr);
  90. void calcEmployeeMinMax(struct employee* head_ptr, struct min_max* emp_minMax_ptr);
  91. void printHeader(void);
  92. void printEmp(struct employee* head_ptr);
  93. void printEmpStatistics(struct totals* emp_totals_ptr, struct min_max* emp_minMax_ptr, int theSize);
  94.  
  95. int main() {
  96. struct employee* head_ptr;
  97. int theSize;
  98.  
  99. struct totals employeeTotals = {0,0,0,0,0,0,0};
  100. struct totals* emp_totals_ptr = &employeeTotals;
  101.  
  102. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  103. struct min_max* emp_minMax_ptr = &employeeMinMax;
  104.  
  105. head_ptr = getEmpData();
  106. theSize = isEmployeeSize(head_ptr);
  107.  
  108. if (theSize <= 0) {
  109. printf("\n\n**** There was no employee input to process ***\n");
  110. } else {
  111. calcOvertimeHrs(head_ptr);
  112. calcGrossPay(head_ptr);
  113. calcStateTax(head_ptr);
  114. calcFedTax(head_ptr);
  115. calcNetPay(head_ptr);
  116. calcEmployeeTotals(head_ptr, emp_totals_ptr);
  117. calcEmployeeMinMax(head_ptr, emp_minMax_ptr);
  118. printHeader();
  119. printEmp(head_ptr);
  120. printEmpStatistics(emp_totals_ptr, emp_minMax_ptr, theSize);
  121. }
  122.  
  123. printf("\n\n *** End of Program *** \n");
  124. return 0;
  125. }
  126.  
  127. // ======================= FUNCTIONS ========================
  128.  
  129. struct employee* getEmpData(void) {
  130. char answer[80], value;
  131. int more_data = 1;
  132.  
  133. struct employee *head_ptr, *current_ptr;
  134.  
  135. head_ptr = (struct employee*) malloc(sizeof(struct employee));
  136. current_ptr = head_ptr;
  137.  
  138. while (more_data) {
  139. printf("\nEnter employee first name: ");
  140. scanf("%s", current_ptr->empName.firstName);
  141. printf("\nEnter employee last name: ");
  142. scanf("%s", current_ptr->empName.lastName);
  143. printf("\nEnter employee two character tax state: ");
  144. scanf("%s", current_ptr->taxState);
  145. printf("\nEnter employee clock number: ");
  146. scanf("%li", &current_ptr->clockNumber);
  147. printf("\nEnter employee hourly wage: ");
  148. scanf("%f", &current_ptr->wageRate);
  149. printf("\nEnter hours worked this week: ");
  150. scanf("%f", &current_ptr->hours);
  151.  
  152. printf("\nWould you like to add another employee? (y/n): ");
  153. scanf("%s", answer);
  154. value = toupper(answer[0]);
  155.  
  156. if (value != 'Y') {
  157. current_ptr->next = NULL;
  158. more_data = 0;
  159. } else {
  160. current_ptr->next = (struct employee*) malloc(sizeof(struct employee));
  161. current_ptr = current_ptr->next;
  162. }
  163. }
  164.  
  165. return head_ptr;
  166. }
  167.  
  168. int isEmployeeSize(struct employee* head_ptr) {
  169. struct employee* current_ptr;
  170. int theSize = 0;
  171.  
  172. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  173. if (current_ptr->empName.firstName[0] != '\0') ++theSize;
  174.  
  175. return theSize;
  176. }
  177.  
  178. void calcOvertimeHrs(struct employee* head_ptr) {
  179. struct employee* current_ptr;
  180. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  181. current_ptr->overtimeHrs = (current_ptr->hours > STD_HOURS) ? current_ptr->hours - STD_HOURS : 0;
  182. }
  183. }
  184.  
  185. void calcGrossPay(struct employee* head_ptr) {
  186. struct employee* current_ptr;
  187. float normalPay, otPay;
  188. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  189. normalPay = current_ptr->wageRate * (current_ptr->hours - current_ptr->overtimeHrs);
  190. otPay = current_ptr->overtimeHrs * current_ptr->wageRate * OT_RATE;
  191. current_ptr->grossPay = normalPay + otPay;
  192. }
  193. }
  194.  
  195. void calcStateTax(struct employee* head_ptr) {
  196. struct employee* current_ptr;
  197. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  198. for (int i = 0; i < 2; i++)
  199. if (islower(current_ptr->taxState[i])) current_ptr->taxState[i] = toupper(current_ptr->taxState[i]);
  200.  
  201. if (strcmp(current_ptr->taxState, "MA") == 0) current_ptr->stateTax = current_ptr->grossPay * MA_TAX_RATE;
  202. else if (strcmp(current_ptr->taxState, "NH") == 0) current_ptr->stateTax = current_ptr->grossPay * NH_TAX_RATE;
  203. else if (strcmp(current_ptr->taxState, "VT") == 0) current_ptr->stateTax = current_ptr->grossPay * VT_TAX_RATE;
  204. else if (strcmp(current_ptr->taxState, "CA") == 0) current_ptr->stateTax = current_ptr->grossPay * CA_TAX_RATE;
  205. else current_ptr->stateTax = current_ptr->grossPay * DEFAULT_TAX_RATE;
  206. }
  207. }
  208.  
  209. void calcFedTax(struct employee* head_ptr) {
  210. struct employee* current_ptr;
  211. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  212. current_ptr->fedTax = current_ptr->grossPay * FED_TAX_RATE;
  213. }
  214. }
  215.  
  216. void calcNetPay(struct employee* head_ptr) {
  217. struct employee* current_ptr;
  218. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  219. current_ptr->netPay = current_ptr->grossPay - (current_ptr->stateTax + current_ptr->fedTax);
  220. }
  221.  
  222. void calcEmployeeTotals(struct employee* head_ptr, struct totals* emp_totals_ptr) {
  223. struct employee* current_ptr;
  224. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  225. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  226. emp_totals_ptr->total_hours += current_ptr->hours;
  227. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  228. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  229. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  230. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  231. emp_totals_ptr->total_netPay += current_ptr->netPay;
  232. }
  233. }
  234.  
  235. void calcEmployeeMinMax(struct employee* head_ptr, struct min_max* emp_minMax_ptr) {
  236. struct employee* current_ptr = head_ptr;
  237.  
  238. // initialize min and max with first employee
  239. emp_minMax_ptr->min_wageRate = emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  240. emp_minMax_ptr->min_hours = emp_minMax_ptr->max_hours = current_ptr->hours;
  241. emp_minMax_ptr->min_overtimeHrs = emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  242. emp_minMax_ptr->min_grossPay = emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  243. emp_minMax_ptr->min_stateTax = emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  244. emp_minMax_ptr->min_fedTax = emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  245. emp_minMax_ptr->min_netPay = emp_minMax_ptr->max_netPay = current_ptr->netPay;
  246.  
  247. current_ptr = current_ptr->next;
  248. for (; current_ptr; current_ptr = current_ptr->next) {
  249. if (current_ptr->wageRate < emp_minMax_ptr->min_wageRate) emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  250. if (current_ptr->wageRate > emp_minMax_ptr->max_wageRate) emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  251. if (current_ptr->hours < emp_minMax_ptr->min_hours) emp_minMax_ptr->min_hours = current_ptr->hours;
  252. if (current_ptr->hours > emp_minMax_ptr->max_hours) emp_minMax_ptr->max_hours = current_ptr->hours;
  253. if (current_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs) emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  254. if (current_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs) emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  255. if (current_ptr->grossPay < emp_minMax_ptr->min_grossPay) emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  256. if (current_ptr->grossPay > emp_minMax_ptr->max_grossPay) emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  257. if (current_ptr->stateTax < emp_minMax_ptr->min_stateTax) emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  258. if (current_ptr->stateTax > emp_minMax_ptr->max_stateTax) emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  259. if (current_ptr->fedTax < emp_minMax_ptr->min_fedTax) emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  260. if (current_ptr->fedTax > emp_minMax_ptr->max_fedTax) emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  261. if (current_ptr->netPay < emp_minMax_ptr->min_netPay) emp_minMax_ptr->min_netPay = current_ptr->netPay;
  262. if (current_ptr->netPay > emp_minMax_ptr->max_netPay) emp_minMax_ptr->max_netPay = current_ptr->netPay;
  263. }
  264. }
  265.  
  266. void printHeader(void) {
  267. printf("\n\n*** Pay Calculator ***\n");
  268. printf("\n--------------------------------------------------------------");
  269. printf("-------------------");
  270. printf("\nName Tax Clock# Wage Hours OT Gross State Fed Net");
  271. printf("\n State Pay Tax Tax Pay");
  272. printf("\n--------------------------------------------------------------");
  273. printf("-------------------");
  274. }
  275.  
  276. void printEmp(struct employee* head_ptr) {
  277. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 2];
  278. struct employee* current_ptr;
  279.  
  280. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
  281. strcpy(name, current_ptr->empName.firstName);
  282. strcat(name, " ");
  283. strcat(name, current_ptr->empName.lastName);
  284. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  285. name, current_ptr->taxState, current_ptr->clockNumber,
  286. current_ptr->wageRate, current_ptr->hours, current_ptr->overtimeHrs,
  287. current_ptr->grossPay, current_ptr->stateTax, current_ptr->fedTax,
  288. current_ptr->netPay);
  289. }
  290. }
  291.  
  292. void printEmpStatistics(struct totals* emp_totals_ptr, struct min_max* emp_minMax_ptr, int theSize) {
  293. printf("\n--------------------------------------------------------------");
  294. printf("-------------------");
  295.  
  296. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  297. emp_totals_ptr->total_wageRate, emp_totals_ptr->total_hours,
  298. emp_totals_ptr->total_overtimeHrs, emp_totals_ptr->total_grossPay,
  299. emp_totals_ptr->total_stateTax, emp_totals_ptr->total_fedTax,
  300. emp_totals_ptr->total_netPay);
  301.  
  302. if (theSize > 0)
  303. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  304. emp_totals_ptr->total_wageRate/theSize, emp_totals_ptr->total_hours/theSize,
  305. emp_totals_ptr->total_overtimeHrs/theSize, emp_totals_ptr->total_grossPay/theSize,
  306. emp_totals_ptr->total_stateTax/theSize, emp_totals_ptr->total_fedTax/theSize,
  307. emp_totals_ptr->total_netPay/theSize);
  308.  
  309. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  310. emp_minMax_ptr->min_wageRate, emp_minMax_ptr->min_hours,
  311. emp_minMax_ptr->min_overtimeHrs, emp_minMax_ptr->min_grossPay,
  312. emp_minMax_ptr->min_stateTax, emp_minMax_ptr->min_fedTax,
  313. emp_minMax_ptr->min_netPay);
  314.  
  315. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  316. emp_minMax_ptr->max_wageRate, emp_minMax_ptr->max_hours,
  317. emp_minMax_ptr->max_overtimeHrs, emp_minMax_ptr->max_grossPay,
  318. emp_minMax_ptr->max_stateTax, emp_minMax_ptr->max_fedTax,
  319. emp_minMax_ptr->max_netPay);
  320.  
  321. printf("\n\nThe total employees processed was: %i\n", theSize);
  322. }
Success #stdin #stdout 0s 5324KB
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 ***

---------------------------------------------------------------------------------
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

The total employees processed was: 5


 *** End of Program ***