fork(1) download
  1. // Linked Lists, Typedef, and Macros
  2. // Description: Program which determines overtime and
  3. // gross pay for a set of employees with outputs sent
  4. // to standard output (the screen).
  5. //
  6. // This assignment also adds the employee name, their tax state,
  7. // and calculates the state tax, federal tax, and net pay. It
  8. // also calculates totals, averages, minimum, and maximum values.
  9. //
  10. // Array and Structure references have all been replaced with
  11. // pointer references to speed up the processing of this code.
  12. // A linked list has been created and deployed to dynamically
  13. // allocate and process employees as needed.
  14. //
  15. // It will also take advantage of the C Preprocessor features,
  16. // in particular with using macros, and will replace all
  17. // struct type references in the code with a typedef alias
  18. // reference.
  19. //
  20. // Call by Reference design (using pointers)
  21. //
  22. //********************************************************
  23.  
  24. // necessary header files
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <ctype.h> // for char functions
  28. #include <stdlib.h> // for malloc
  29.  
  30. // define constants
  31. #define STD_HOURS 40.0
  32. #define OT_RATE 1.5
  33. #define MA_TAX_RATE 0.05
  34. #define NH_TAX_RATE 0.0
  35. #define VT_TAX_RATE 0.06
  36. #define CA_TAX_RATE 0.07
  37. #define DEFAULT_STATE_TAX_RATE 0.08
  38. #define NAME_SIZE 20
  39. #define TAX_STATE_SIZE 3
  40. #define FED_TAX_RATE 0.25
  41. #define FIRST_NAME_SIZE 10
  42. #define LAST_NAME_SIZE 10
  43.  
  44. // define macros
  45. #define CALC_OT_HOURS(theHours) ((theHours > STD_HOURS) ? theHours - STD_HOURS : 0)
  46. #define CALC_STATE_TAX(thePay,theStateTaxRate) (thePay * theStateTaxRate)
  47. #define CALC_FED_TAX(thePay) (thePay * FED_TAX_RATE)
  48. #define CALC_NET_PAY(thePay,theStateTax,theFedTax) (thePay - (theStateTax + theFedTax))
  49. #define CALC_NORMAL_PAY(theWageRate,theHours,theOvertimeHrs) \
  50. (theWageRate * (theHours - theOvertimeHrs))
  51. #define CALC_OT_PAY(theWageRate,theOvertimeHrs) (theOvertimeHrs * (OT_RATE * theWageRate))
  52. #define CALC_MIN(theValue, currentMin) ((theValue < currentMin) ? theValue : currentMin)
  53. #define CALC_MAX(theValue, currentMax) ((theValue > currentMax) ? theValue : currentMax)
  54.  
  55. // Define a global structure type to store an employee name
  56. struct name
  57. {
  58. char firstName[FIRST_NAME_SIZE];
  59. char lastName [LAST_NAME_SIZE];
  60. };
  61.  
  62. // Define a global structure type to pass employee data between functions
  63. typedef struct employee
  64. {
  65. struct name empName;
  66. char taxState [TAX_STATE_SIZE];
  67. long int clockNumber;
  68. float wageRate;
  69. float hours;
  70. float overtimeHrs;
  71. float grossPay;
  72. float stateTax;
  73. float fedTax;
  74. float netPay;
  75. struct employee * next;
  76. } EMPLOYEE;
  77.  
  78. // This structure type defines the totals of all floating point items
  79. typedef struct totals
  80. {
  81. float total_wageRate;
  82. float total_hours;
  83. float total_overtimeHrs;
  84. float total_grossPay;
  85. float total_stateTax;
  86. float total_fedTax;
  87. float total_netPay;
  88. } TOTALS;
  89.  
  90. // This structure type defines the min and max values
  91. typedef struct min_max
  92. {
  93. float min_wageRate;
  94. float min_hours;
  95. float min_overtimeHrs;
  96. float min_grossPay;
  97. float min_stateTax;
  98. float min_fedTax;
  99. float min_netPay;
  100. float max_wageRate;
  101. float max_hours;
  102. float max_overtimeHrs;
  103. float max_grossPay;
  104. float max_stateTax;
  105. float max_fedTax;
  106. float max_netPay;
  107. } MIN_MAX;
  108.  
  109. // Function prototypes
  110. EMPLOYEE * getEmpData (void);
  111. int isEmployeeSize (EMPLOYEE * head_ptr);
  112. void calcOvertimeHrs (EMPLOYEE * head_ptr);
  113. void calcGrossPay (EMPLOYEE * head_ptr);
  114. void printHeader (void);
  115. void printEmp (EMPLOYEE * head_ptr);
  116. void calcStateTax (EMPLOYEE * head_ptr);
  117. void calcFedTax (EMPLOYEE * head_ptr);
  118. void calcNetPay (EMPLOYEE * head_ptr);
  119. void calcEmployeeTotals (EMPLOYEE * head_ptr, TOTALS * emp_totals_ptr);
  120. void calcEmployeeMinMax (EMPLOYEE * head_ptr, MIN_MAX * emp_minMax_ptr);
  121. void printEmpStatistics (TOTALS * emp_totals_ptr, MIN_MAX * emp_minMax_ptr, int size);
  122.  
  123. int main ()
  124. {
  125. // Set up head pointer to point to the start of the linked list
  126. EMPLOYEE * head_ptr; // always points to first linked list node
  127. int theSize; // number of employees processed
  128.  
  129. // set up structure to store totals and initialize all to zero
  130. TOTALS employeeTotals = {0,0,0,0,0,0,0};
  131. TOTALS * emp_totals_ptr = &employeeTotals;
  132.  
  133. // set up structure to store min and max values and initialize all to zero
  134. MIN_MAX employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  135. MIN_MAX * emp_minMax_ptr = &employeeMinMax;
  136.  
  137. // Read the employee input and dynamically allocate linked list
  138. head_ptr = getEmpData ();
  139.  
  140. // determine how many employees are in our linked list
  141. theSize = isEmployeeSize (head_ptr);
  142.  
  143. if (theSize <= 0)
  144. {
  145. printf("\n\n**** There was no employee input to process ***\n");
  146. }
  147. else
  148. {
  149. // Perform calculations
  150. calcOvertimeHrs (head_ptr);
  151. calcGrossPay (head_ptr);
  152. calcStateTax (head_ptr);
  153. calcFedTax (head_ptr);
  154. calcNetPay (head_ptr);
  155.  
  156. // Calculate totals and min/max values
  157. calcEmployeeTotals (head_ptr, &employeeTotals);
  158. calcEmployeeMinMax (head_ptr, &employeeMinMax);
  159.  
  160. // Print results
  161. printHeader();
  162. printEmp (head_ptr);
  163. printEmpStatistics (&employeeTotals, &employeeMinMax, theSize);
  164. }
  165.  
  166. printf ("\n\n *** End of Program *** \n");
  167. return (0);
  168. }
  169.  
  170. // Function implementations...
  171.  
  172. EMPLOYEE * getEmpData (void)
  173. {
  174. char answer[80];
  175. int more_data = 1;
  176. char value;
  177.  
  178. EMPLOYEE *current_ptr, *head_ptr;
  179.  
  180. head_ptr = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
  181. current_ptr = head_ptr;
  182.  
  183. while (more_data)
  184. {
  185. printf ("\nEnter employee first name: ");
  186. scanf ("%s", current_ptr->empName.firstName);
  187. printf ("\nEnter employee last name: ");
  188. scanf ("%s", current_ptr->empName.lastName);
  189. printf ("\nEnter employee two character tax state: ");
  190. scanf ("%s", current_ptr->taxState);
  191. printf("\nEnter employee clock number: ");
  192. scanf("%li", & current_ptr -> clockNumber);
  193. printf("\nEnter employee hourly wage: ");
  194. scanf("%f", & current_ptr -> wageRate);
  195. printf("\nEnter hours worked this week: ");
  196. scanf("%f", & current_ptr -> hours);
  197.  
  198. printf("\nWould you like to add another employee? (y/n): ");
  199. scanf("%s", answer);
  200.  
  201. if ((value = toupper(answer[0])) != 'Y')
  202. {
  203. current_ptr->next = (EMPLOYEE *) NULL;
  204. more_data = 0;
  205. }
  206. else
  207. {
  208. current_ptr->next = (EMPLOYEE *) malloc (sizeof(EMPLOYEE));
  209. current_ptr = current_ptr->next;
  210. }
  211. }
  212. return(head_ptr);
  213. }
  214.  
  215. int isEmployeeSize (EMPLOYEE * head_ptr)
  216. {
  217. EMPLOYEE * current_ptr;
  218. int theSize = 0;
  219.  
  220. if (head_ptr->empName.firstName[0] != '\0')
  221. {
  222. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  223. {
  224. ++theSize;
  225. }
  226. }
  227. return (theSize);
  228. }
  229.  
  230. void printHeader (void)
  231. {
  232. printf ("\n\n*** Pay Calculator ***\n");
  233. printf("\n--------------------------------------------------------------");
  234. printf("-------------------");
  235. printf("\nName Tax Clock# Wage Hours OT Gross ");
  236. printf(" State Fed Net");
  237. printf("\n State Pay ");
  238. printf(" Tax Tax Pay");
  239. printf("\n--------------------------------------------------------------");
  240. printf("-------------------");
  241. }
  242.  
  243. void printEmp (EMPLOYEE * head_ptr)
  244. {
  245. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  246. EMPLOYEE * current_ptr;
  247.  
  248. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  249. {
  250. strcpy (name, current_ptr->empName.firstName);
  251. strcat (name, " ");
  252. strcat (name, current_ptr->empName.lastName);
  253.  
  254. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  255. name, current_ptr->taxState, current_ptr->clockNumber,
  256. current_ptr->wageRate, current_ptr->hours,
  257. current_ptr->overtimeHrs, current_ptr->grossPay,
  258. current_ptr->stateTax, current_ptr->fedTax,
  259. current_ptr->netPay);
  260. }
  261. }
  262.  
  263. void printEmpStatistics (TOTALS * emp_totals_ptr, MIN_MAX * emp_minMax_ptr, int theSize)
  264. {
  265. printf("\n--------------------------------------------------------------");
  266. printf("-------------------");
  267.  
  268. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  269. emp_totals_ptr->total_wageRate,
  270. emp_totals_ptr->total_hours,
  271. emp_totals_ptr->total_overtimeHrs,
  272. emp_totals_ptr->total_grossPay,
  273. emp_totals_ptr->total_stateTax,
  274. emp_totals_ptr->total_fedTax,
  275. emp_totals_ptr->total_netPay);
  276.  
  277. if (theSize > 0)
  278. {
  279. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  280. emp_totals_ptr->total_wageRate/theSize,
  281. emp_totals_ptr->total_hours/theSize,
  282. emp_totals_ptr->total_overtimeHrs/theSize,
  283. emp_totals_ptr->total_grossPay/theSize,
  284. emp_totals_ptr->total_stateTax/theSize,
  285. emp_totals_ptr->total_fedTax/theSize,
  286. emp_totals_ptr->total_netPay/theSize);
  287. }
  288.  
  289. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  290. emp_minMax_ptr->min_wageRate,
  291. emp_minMax_ptr->min_hours,
  292. emp_minMax_ptr->min_overtimeHrs,
  293. emp_minMax_ptr->min_grossPay,
  294. emp_minMax_ptr->min_stateTax,
  295. emp_minMax_ptr->min_fedTax,
  296. emp_minMax_ptr->min_netPay);
  297.  
  298. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  299. emp_minMax_ptr->max_wageRate,
  300. emp_minMax_ptr->max_hours,
  301. emp_minMax_ptr->max_overtimeHrs,
  302. emp_minMax_ptr->max_grossPay,
  303. emp_minMax_ptr->max_stateTax,
  304. emp_minMax_ptr->max_fedTax,
  305. emp_minMax_ptr->max_netPay);
  306.  
  307. printf ("\n\nThe total employees processed was: %i\n", theSize);
  308. }
  309.  
  310. void calcOvertimeHrs (EMPLOYEE * head_ptr)
  311. {
  312. EMPLOYEE * current_ptr;
  313. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  314. {
  315. current_ptr->overtimeHrs = CALC_OT_HOURS(current_ptr->hours);
  316. }
  317. }
  318.  
  319. void calcGrossPay (EMPLOYEE * head_ptr)
  320. {
  321. float theNormalPay, theOvertimePay;
  322. EMPLOYEE * current_ptr;
  323.  
  324. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  325. {
  326. theNormalPay = CALC_NORMAL_PAY(current_ptr->wageRate,
  327. current_ptr->hours,
  328. current_ptr->overtimeHrs);
  329. theOvertimePay = CALC_OT_PAY(current_ptr->wageRate,
  330. current_ptr->overtimeHrs);
  331. current_ptr->grossPay = theNormalPay + theOvertimePay;
  332. }
  333. }
  334.  
  335. void calcStateTax (EMPLOYEE * head_ptr)
  336. {
  337. EMPLOYEE * current_ptr;
  338.  
  339. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  340. {
  341. if (islower(current_ptr->taxState[0]))
  342. current_ptr->taxState[0] = toupper(current_ptr->taxState[0]);
  343. if (islower(current_ptr->taxState[1]))
  344. current_ptr->taxState[1] = toupper(current_ptr->taxState[1]);
  345.  
  346. if (strcmp(current_ptr->taxState, "MA") == 0)
  347. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, MA_TAX_RATE);
  348. else if (strcmp(current_ptr->taxState, "VT") == 0)
  349. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, VT_TAX_RATE);
  350. else if (strcmp(current_ptr->taxState, "NH") == 0)
  351. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, NH_TAX_RATE);
  352. else if (strcmp(current_ptr->taxState, "CA") == 0)
  353. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, CA_TAX_RATE);
  354. else
  355. current_ptr->stateTax = CALC_STATE_TAX(current_ptr->grossPay, DEFAULT_STATE_TAX_RATE);
  356. }
  357. }
  358.  
  359. void calcFedTax (EMPLOYEE * head_ptr)
  360. {
  361. EMPLOYEE * current_ptr;
  362. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  363. {
  364. current_ptr->fedTax = CALC_FED_TAX(current_ptr->grossPay);
  365. }
  366. }
  367.  
  368. void calcNetPay (EMPLOYEE * head_ptr)
  369. {
  370. EMPLOYEE * current_ptr;
  371. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  372. {
  373. current_ptr->netPay = CALC_NET_PAY(current_ptr->grossPay,
  374. current_ptr->stateTax,
  375. current_ptr->fedTax);
  376. }
  377. }
  378.  
  379. void calcEmployeeTotals (EMPLOYEE * head_ptr, TOTALS * emp_totals_ptr)
  380. {
  381. EMPLOYEE * current_ptr;
  382. for (current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next)
  383. {
  384. emp_totals_ptr->total_wageRate += current_ptr->wageRate;
  385. emp_totals_ptr->total_hours += current_ptr->hours;
  386. emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
  387. emp_totals_ptr->total_grossPay += current_ptr->grossPay;
  388. emp_totals_ptr->total_stateTax += current_ptr->stateTax;
  389. emp_totals_ptr->total_fedTax += current_ptr->fedTax;
  390. emp_totals_ptr->total_netPay += current_ptr->netPay;
  391. }
  392. }
  393.  
  394. void calcEmployeeMinMax (EMPLOYEE * head_ptr, MIN_MAX * emp_minMax_ptr)
  395. {
  396. EMPLOYEE * current_ptr = head_ptr;
  397.  
  398. // Initialize with first employee's values
  399. emp_minMax_ptr->min_wageRate = current_ptr->wageRate;
  400. emp_minMax_ptr->min_hours = current_ptr->hours;
  401. emp_minMax_ptr->min_overtimeHrs = current_ptr->overtimeHrs;
  402. emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
  403. emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
  404. emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
  405. emp_minMax_ptr->min_netPay = current_ptr->netPay;
  406.  
  407. emp_minMax_ptr->max_wageRate = current_ptr->wageRate;
  408. emp_minMax_ptr->max_hours = current_ptr->hours;
  409. emp_minMax_ptr->max_overtimeHrs = current_ptr->overtimeHrs;
  410. emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
  411. emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
  412. emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
  413. emp_minMax_ptr->max_netPay = current_ptr->netPay;
  414.  
  415. // Move to next employee
  416. current_ptr = current_ptr->next;
  417.  
  418. // Process remaining employees
  419. for (; current_ptr; current_ptr = current_ptr->next)
  420. {
  421. emp_minMax_ptr->min_wageRate = CALC_MIN(current_ptr->wageRate, emp_minMax_ptr->min_wageRate);
  422. emp_minMax_ptr->max_wageRate = CALC_MAX(current_ptr->wageRate, emp_minMax_ptr->max_wageRate);
  423.  
  424. emp_minMax_ptr->min_hours = CALC_MIN(current_ptr->hours, emp_minMax_ptr->min_hours);
  425. emp_minMax_ptr->max_hours = CALC_MAX(current_ptr->hours, emp_minMax_ptr->max_hours);
  426.  
  427. emp_minMax_ptr->min_overtimeHrs = CALC_MIN(current_ptr->overtimeHrs, emp_minMax_ptr->min_overtimeHrs);
  428. emp_minMax_ptr->max_overtimeHrs = CALC_MAX(current_ptr->overtimeHrs, emp_minMax_ptr->max_overtimeHrs);
  429.  
  430. emp_minMax_ptr->min_grossPay = CALC_MIN(current_ptr->grossPay, emp_minMax_ptr->min_grossPay);
  431. emp_minMax_ptr->max_grossPay = CALC_MAX(current_ptr->grossPay, emp_minMax_ptr->max_grossPay);
  432.  
  433. emp_minMax_ptr->min_stateTax = CALC_MIN(current_ptr->stateTax, emp_minMax_ptr->min_stateTax);
  434. emp_minMax_ptr->max_stateTax = CALC_MAX(current_ptr->stateTax, emp_minMax_ptr->max_stateTax);
  435.  
  436. emp_minMax_ptr->min_fedTax = CALC_MIN(current_ptr->fedTax, emp_minMax_ptr->min_fedTax);
  437. emp_minMax_ptr->max_fedTax = CALC_MAX(current_ptr->fedTax, emp_minMax_ptr->max_fedTax);
  438.  
  439. emp_minMax_ptr->min_netPay = CALC_MIN(current_ptr->netPay, emp_minMax_ptr->min_netPay);
  440. emp_minMax_ptr->max_netPay = CALC_MAX(current_ptr->netPay, emp_minMax_ptr->max_netPay);
  441. }
  442. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
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): 

**** There was no employee input to process ***


 *** End of Program ***