Advertisement
Guest User

CVRs

a guest
Mar 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. //Lab 3 - Virtual Realities
  2. //Justin Gonzalez
  3. //CECS 424
  4. //Due 3/23/2018
  5.  
  6. #include<stdio.h>
  7. #include<stdlib.h>
  8.  
  9. struct Employee {
  10. void** vtable;
  11. int age;
  12. };
  13.  
  14. struct HourlyEmployee {
  15. void** vtable;
  16. int age;
  17. double hourly_rate;
  18. double hours;
  19. };
  20.  
  21. struct CommissionEmployee {
  22. void** vtable;
  23. int age;
  24. double hourly_rate;
  25. double hours;
  26. double sales_amount;
  27. };
  28.  
  29. struct SeniorSalesman {
  30. void** vtable;
  31. int age;
  32. double hourly_rate;
  33. double hours;
  34. double sales_amount;
  35. };
  36.  
  37. void Speak_Hourly(struct Employee* ptr)
  38. {
  39. printf("I work for $%.2lf dollars per hour.\n", ((struct HourlyEmployee*)ptr)->hourly_rate);
  40. }
  41.  
  42. void Speak_Commission(struct Employee* ptr)
  43. {
  44. printf("I make commission on $%.2lf dollars in sales!\n", ((struct CommissionEmployee*)ptr)->sales_amount);
  45. }
  46.  
  47. double GetPay_Hourly(struct Employee* ptr)
  48. {
  49. return ((struct HourlyEmployee*)ptr)->hours * ((struct HourlyEmployee*)ptr)->hourly_rate;
  50. }
  51.  
  52. double GetPay_Commission(struct Employee* ptr)
  53. {
  54. return (((struct CommissionEmployee*)ptr)->sales_amount * .1) + 40000;
  55. }
  56.  
  57. double GetPay_Senior(struct Employee* ptr)
  58. {
  59. double srPay = (.2*((struct SeniorSalesman*)ptr)->sales_amount) + 50000;
  60. if (((struct SeniorSalesman*)ptr)->age >= 40)
  61. return srPay + ((.05*((struct SeniorSalesman*)ptr)->sales_amount));
  62. return srPay;
  63. }
  64. void* Vtable_Hourly[2] = { Speak_Hourly, GetPay_Hourly };
  65. void* Vtable_Commission[2] = { Speak_Commission, GetPay_Commission };
  66. void* Vtable_Senior[2] = { Speak_Commission, GetPay_Senior };
  67.  
  68. void Construct_Hourly(struct HourlyEmployee* ptr)
  69. {
  70. ptr->age = 0;
  71. ptr->hourly_rate = 0;
  72. ptr->hours = 0;
  73. ptr->vtable = Vtable_Hourly;
  74. }
  75.  
  76. void Construct_Commission(struct CommissionEmployee* ptr)
  77. {
  78. ptr->age = 0;
  79. ptr->hourly_rate = 0;
  80. ptr->hours = 0;
  81. ptr->vtable = Vtable_Commission;
  82. }
  83.  
  84. void Construct_Senior(struct SeniorSalesman* ptr)
  85. {
  86. ptr->age = 0;
  87. ptr->hourly_rate = 0;
  88. ptr->hours = 0;
  89. ptr->vtable = Vtable_Senior;
  90. }
  91.  
  92. int main()
  93. {
  94. struct Employee *eptr;
  95. int numResp = 0;
  96. int choice = 0;
  97. int ageResp = 0;
  98. double payrateResp = 0.0;
  99. double hoursResp = 0.0;
  100. double salesResp = 0.0;
  101. do {
  102. printf("Choose one of the options by entering its corresponding number:");
  103. printf("\n1.Hourly Employee\n2.Commission Employee\n3.Senior Salesman\n");
  104. scanf_s("%d", &numResp);
  105. choice = numResp;
  106. switch (choice)
  107. {
  108. case 1:
  109. eptr = malloc(sizeof(struct HourlyEmployee));
  110. Construct_Hourly(eptr);
  111. printf("How old is this employee?\n");
  112. scanf_s("%d", &ageResp);
  113. printf("What is this employee's pay rate? (USD Per Hour)\n");
  114. scanf_s("%lf", &payrateResp);
  115. printf("How many hours has this employee worked?\n");
  116. scanf_s("%lf", &hoursResp);
  117. eptr->age = ageResp;
  118. ((struct HourlyEmployee*)eptr)->hourly_rate = payrateResp;
  119. ((struct HourlyEmployee*)eptr)->hours = hoursResp;
  120. free(eptr);
  121. break;
  122. case 2:
  123. eptr = malloc(sizeof(struct CommissionEmployee));
  124. Construct_Commission(eptr);
  125. printf("How old is this employee?\n");
  126. scanf_s("%d", &ageResp);
  127. eptr->age = ageResp;
  128. printf("How much in sales has this employee made?\n");
  129. scanf_s("%lf", &salesResp);
  130. ((struct CommissionEmployee*)eptr)->sales_amount = salesResp;
  131. free(eptr);
  132. break;
  133. case 3:
  134. eptr = malloc(sizeof(struct SeniorSalesman));
  135. Construct_Senior(eptr);
  136. printf("How old is this employee?\n");
  137. scanf_s("%d", &ageResp);
  138. eptr->age = ageResp;
  139. printf("How much in sales has this employee made?\n");
  140. scanf_s("%lf", &salesResp);
  141. ((struct SeniorSalesman*)eptr)->sales_amount = salesResp;
  142. free(eptr);
  143. break;
  144. default:
  145. printf("\nPlease choose a listed option.\n");
  146. break;
  147. }
  148. } while ((choice != 1) && (choice != 2) && ((choice != 3)));
  149. ((void(*)(struct Employee*))eptr->vtable[0])((struct Employee*)eptr);
  150. printf("I have made %.2lf thus far.", ((double(*)(struct Employee*))eptr->vtable[1])((struct Employee*)eptr));
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement