Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.38 KB | None | 0 0
  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <Windows.h>
  6. #include <math.h>
  7.  
  8. #define pause system("pause")
  9. #define clrcmd system("CLS")
  10.  
  11. // Define macros that are used later in the program to calculate federal tax and other tax
  12. #define FED_TAX 0.3;
  13. #define OTH_TAX 0.088;
  14.  
  15. // Structs
  16. typedef struct NAME
  17. {
  18. char* firstName; // String for first name
  19. char middleInitial; // Character for middle initial
  20. char* lastName; // String for last name
  21. }NAME;
  22.  
  23. typedef struct STRDATE
  24. {
  25. int month, day, year;
  26. }STRDATE;
  27.  
  28. typedef struct PAYRECORD
  29. {
  30. int EmployeeID;
  31. NAME name;
  32. STRDATE payDate;
  33. float hoursWorked;
  34. float hourlyRate;
  35. float regPay;
  36. float otPay;
  37. float grossPay;
  38. float fedTax;
  39. float otherTax;
  40. float netPay;
  41. }PAYRECORD;
  42.  
  43. // Function Prototypes
  44.  
  45. // Display Functions
  46. void mainMenu();
  47. void displaySummary(PAYRECORD *payroll, int inputStubs);
  48. void addSpaces(int inputSpaces, int inputNum);
  49.  
  50. // Input Functions
  51. void getUserChoice(int* ptrUserChoice);
  52. void getPayInfor(PAYRECORD *payroll, int inputStub);
  53.  
  54. // Calculate Functions
  55. void calcPayroll(PAYRECORD *payroll, int inputStub);
  56. void calcRegPay(PAYRECORD *payroll, int inputStub);
  57. void calcOtPay(PAYRECORD *payroll, int inputStub);
  58. void calcGrossPay(PAYRECORD *payroll, int inputStub);
  59. void calcFedTax(PAYRECORD *payroll, int inputStub);
  60. void calcOtherTax(PAYRECORD *payroll, int inputStub);
  61. void calcNetPay(PAYRECORD *payroll, int inputStub);
  62.  
  63. void main()
  64. {
  65. ShowWindow(GetConsoleWindow(), SW_MAXIMIZE); // Maximizes console window
  66.  
  67. // Initialize variables
  68. int userChoice = 0, stub = 0;
  69. PAYRECORD payroll[500];
  70.  
  71. mainMenu(); // Prints the main menu
  72. do { // The loop that runs the menu detection system
  73. getUserChoice(&userChoice); // Calls the function getUserChoice with the input of &userChoice so that it can receive the pointer from the function
  74. switch (userChoice) { // Switch statement based on what the user has entered
  75. case 1: // Enter new paycheck information
  76. if (stub == 499) // Stop if there are 500 stubs
  77. {
  78. printf("\nMax stubs inputted.\n");
  79. system("pause");
  80. mainMenu();
  81. break;
  82. }
  83. getPayInfor(payroll, stub); // Call the function getPayInfor with the input of the 2D array positions so that they can receive the pointer from the function
  84. calcPayroll(payroll, stub);
  85. stub++; // Increase the stub
  86. mainMenu(); // Resets the menu
  87. break;
  88. case 2: // Display Payroll Summary for all data saved
  89. displaySummary(payroll, stub); // Calls the displaySummary function with the input of the payroll and stub so that the table can be displayed with all of the necessary information
  90. mainMenu(); // Resets the menu
  91. break;
  92. case 3: // Quit
  93. break;
  94. case '\n': // Don't trigger default if you press enter, prevents the default from catching when swapping from menus
  95. break;
  96. default: // Catch anything else incase the wrong letter is typed
  97. printf("\nWrong Number\n"); // Error message
  98. system("pause"); // Waiting for user to realize their mistake
  99. break;
  100. }
  101. } while (userChoice != 3); // Ends the do-while loop with the exit being 3 for quit, since you want the loop to end when the user would like to quit
  102. }
  103.  
  104. void getUserChoice(int* ptrUserChoice) // Function that points the user's input back to the called variable for which menu they would like to go in to
  105. {
  106. scanf_s(" %d", &*ptrUserChoice); // point the input from the user back to the input
  107. }
  108.  
  109. void mainMenu() // Base menu, clears the console and outputs all of the menu choices
  110. {
  111. clrcmd; // Clears the window
  112. printf("\n *** PAYROLL MAIN MENU *** \n"
  113. "\n 1. Enter new paycheck information \n"
  114. "\n 2. Display Payroll Summary for all data saved \n"
  115. "\n 3. Quit \n\n");
  116. }
  117.  
  118. void getPayInfor(PAYRECORD* payroll, int inputStub) // Function that points back the user's inputs for the employee information
  119. {
  120. fflush(stdin); // Flush the standard input
  121. clrcmd; // Clears the window
  122.  
  123. // Get the first name
  124. printf("\n\n Input the employee's First Name: ");
  125. //fgets(payroll[inputStub].name.firstName, sizeof(payroll[inputStub].name.firstName), stdin);
  126.  
  127. // Get the middle initial
  128. printf("\n\n Input the employee's Middle Initial: ");
  129. //scanf_s("%c", payroll[inputStub].name.middleInitial);
  130.  
  131. // Get the last name
  132. printf("\n\n Input the employee's Last Name: ");
  133. //fgets(payroll[inputStub].name.lastName, sizeof(payroll[inputStub].name.firstName), stdin);
  134.  
  135. // Get the employee number
  136. printf("\n\n Input the employee number: ");
  137. scanf_s(" %f", payroll[inputStub].EmployeeID);
  138.  
  139. // Get the hours worked
  140. printf("\n\n Input the amount of hours worked: ");
  141. scanf_s(" %f", payroll[inputStub].hoursWorked);
  142.  
  143. // Get the hourly rate
  144. printf("\n\n Input the hourly rate: ");
  145. scanf_s(" %f", payroll[inputStub].hourlyRate);
  146. }
  147.  
  148. void displaySummary(PAYRECORD* payroll, int inputStubs) // Function that displays the summary table for the payroll
  149. {
  150.  
  151. clrcmd; // Clears the console
  152. printf("\n" // Prints Heading
  153. "Employee Number"
  154. " Hourly Rate"
  155. " Hours Worked"
  156. " Regular Pay"
  157. " Overtime Pay"
  158. " Gross Pay"
  159. " Federal Tax"
  160. " Other Tax"
  161. " Net Pay\n");
  162. for (int curStub = 0; curStub < inputStubs; curStub++) // Prints each row for each stub in payroll
  163. {
  164. // Begin to print each value in the column for the current row
  165. printf("\n");
  166. printf("%.0f", payroll[curStub].EmployeeID); // Display Employee Number
  167.  
  168. addSpaces(23, payroll[curStub].EmployeeID); // The function addSpaces gives enough spaces to evenly space out all values so that nothing is misaligned
  169. printf("$%.2f", payroll[curStub].hourlyRate); // Display Hourly Pay
  170.  
  171. addSpaces(15, payroll[curStub].hourlyRate);
  172. printf("%.2f", payroll[curStub].hoursWorked); // Display Hours Worked
  173.  
  174. addSpaces(17, payroll[curStub].hoursWorked);
  175. printf("$%.2f", payroll[curStub].regPay); // Display Reg Pay
  176.  
  177. addSpaces(15, payroll[curStub].regPay);
  178. printf("$%.2f", payroll[curStub].otPay); // Display OT Pay
  179.  
  180. addSpaces(16, payroll[curStub].otPay);
  181. printf("$%.2f", payroll[curStub].grossPay); // Display Gross Pay
  182.  
  183. addSpaces(13, payroll[curStub].grossPay);
  184. printf("$%.2f", payroll[curStub].fedTax); // Display Fed Tax
  185.  
  186. addSpaces(15, payroll[curStub].fedTax);
  187. printf("$%.2f", payroll[curStub].otherTax); // Display Other Tax
  188.  
  189. addSpaces(13, payroll[curStub].otherTax);
  190. printf("$%.2f", payroll[curStub].netPay); // Display Net Pay
  191. }
  192. printf("\n");
  193. pause;
  194. }
  195.  
  196. void addSpaces(int inputSpaces, int inputNum) /* Function that adds spaces based on the length of each value in the table
  197. I had to make this function instead of using /t or %10.2f in my printf because:
  198. If the values were too large than the tabs would not space properly
  199. and %10.2 would space the number 10 times between the number and the $ which I did not want. */
  200. {
  201. int tempLength = floor(log10(abs(inputNum))) + 1; /* Outputs the digits from inputNum (the values from the table). Example would be inputNum = 1000, tempLength = 4.
  202. I tried using other methods, but they weren't working so I decided to do some googling on how to do it.
  203. Found this off of stackoverflow, from what I can understand the purpose of each command is:
  204. - the floor rounds the value to lowest whole number
  205. - log10 specifies that it is in base 10
  206. - the abs takes the absolute value of the inputNum
  207. - + 1 because the log 10 is (n-1), so you have to fix that with a + 1
  208. All of this gives how many digits there are in inputNum, which is needed to calculate how many spaces are required.
  209. This does not account for decimals and it does not need to because at most each value will go to 2 decimal places.
  210. https://stackoverflow.com/questions/3068397/finding-the-length-of-an-integer-in-c
  211. https://stackoverflow.com/questions/24176789/how-does-using-log10-correctly-calculate-the-length-of-a-integer
  212. */
  213. if (inputNum == 0) // Run this code if inputNum is 0, because if it is than tempLength is a large value that breaks everything
  214. {
  215. for (int spaces = 0; spaces < inputSpaces - 1; spaces++)
  216. {
  217. printf(" ");
  218. }
  219. }
  220. else // Run this code if inputNum is not 0 since tempLength will be a proper value then
  221. {
  222. for (int spaces = 0; spaces < (inputSpaces - tempLength); spaces++)
  223. {
  224. printf(" ");
  225. }
  226. }
  227. }
  228.  
  229. void calcPayroll(PAYRECORD* payroll, int inputStub)
  230. {
  231. calcRegPay(payroll, inputStub);
  232. calcOtPay(payroll, inputStub);
  233. calcGrossPay(payroll, inputStub);
  234. calcFedTax(payroll, inputStub);
  235. calcOtherTax(payroll, inputStub);
  236. calcNetPay(payroll, inputStub);
  237. }
  238.  
  239. void calcRegPay(PAYRECORD* payroll, int inputStub) // Function that calculates regPay and points it back to the variable that called it
  240. {
  241. if (payroll[inputStub].hoursWorked <= 40)
  242. {
  243. payroll[inputStub].regPay = payroll[inputStub].hoursWorked * payroll[inputStub].hourlyRate;
  244. }
  245. else if (payroll[inputStub].hoursWorked > 40)
  246. {
  247. payroll[inputStub].regPay = 40 * payroll[inputStub].hourlyRate;
  248. }
  249.  
  250. }
  251.  
  252. void calcOtPay(PAYRECORD* payroll, int inputStub) // Function that calculates otPay and points it back to the variable that called it
  253. {
  254. if (payroll[inputStub].hoursWorked <= 40)
  255. {
  256. payroll[inputStub].otPay = 0;
  257. return;
  258. }
  259. else
  260. {
  261. payroll[inputStub].otPay = (payroll[inputStub].hoursWorked - 40) * (payroll[inputStub].hourlyRate * 1.5);
  262. }
  263. }
  264.  
  265. void calcGrossPay(PAYRECORD* payroll, int inputStub)// Function that calculates grossPay and points it back to the variable that called it
  266. {
  267. payroll[inputStub].grossPay = payroll[inputStub].regPay + payroll[inputStub].otPay;
  268. }
  269.  
  270. void calcFedTax(PAYRECORD* payroll, int inputStub) // Function that calculates fedTax and points it back to the variable that called it
  271. {
  272. payroll[inputStub].fedTax = payroll[inputStub].grossPay * FED_TAX;
  273. }
  274.  
  275. void calcOtherTax(PAYRECORD* payroll, int inputStub) // Function that calculates otherTax and points it back to the variable that called it
  276. {
  277. payroll[inputStub].otherTax = payroll[inputStub].grossPay * OTH_TAX;
  278. }
  279.  
  280. void calcNetPay(PAYRECORD* payroll, int inputStub) // Function that calculates netPay and points it back to the variable that called it
  281. {
  282. payroll[inputStub].netPay = payroll[inputStub].grossPay - payroll[inputStub].fedTax - payroll[inputStub].otherTax;
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement