Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Lab 3 - Virtual Realities
- //Justin Gonzalez
- //CECS 424
- //Due 3/23/2018
- #include<stdio.h>
- #include<stdlib.h>
- struct Employee {
- void** vtable;
- int age;
- };
- struct HourlyEmployee {
- void** vtable;
- int age;
- double hourly_rate;
- double hours;
- };
- struct CommissionEmployee {
- void** vtable;
- int age;
- double hourly_rate;
- double hours;
- double sales_amount;
- };
- struct SeniorSalesman {
- void** vtable;
- int age;
- double hourly_rate;
- double hours;
- double sales_amount;
- };
- void Speak_Hourly(struct Employee* ptr)
- {
- printf("I work for $%.2lf dollars per hour.\n", ((struct HourlyEmployee*)ptr)->hourly_rate);
- }
- void Speak_Commission(struct Employee* ptr)
- {
- printf("I make commission on $%.2lf dollars in sales!\n", ((struct CommissionEmployee*)ptr)->sales_amount);
- }
- double GetPay_Hourly(struct Employee* ptr)
- {
- return ((struct HourlyEmployee*)ptr)->hours * ((struct HourlyEmployee*)ptr)->hourly_rate;
- }
- double GetPay_Commission(struct Employee* ptr)
- {
- return (((struct CommissionEmployee*)ptr)->sales_amount * .1) + 40000;
- }
- double GetPay_Senior(struct Employee* ptr)
- {
- double srPay = (.2*((struct SeniorSalesman*)ptr)->sales_amount) + 50000;
- if (((struct SeniorSalesman*)ptr)->age >= 40)
- return srPay + ((.05*((struct SeniorSalesman*)ptr)->sales_amount));
- return srPay;
- }
- void* Vtable_Hourly[2] = { Speak_Hourly, GetPay_Hourly };
- void* Vtable_Commission[2] = { Speak_Commission, GetPay_Commission };
- void* Vtable_Senior[2] = { Speak_Commission, GetPay_Senior };
- void Construct_Hourly(struct HourlyEmployee* ptr)
- {
- ptr->age = 0;
- ptr->hourly_rate = 0;
- ptr->hours = 0;
- ptr->vtable = Vtable_Hourly;
- }
- void Construct_Commission(struct CommissionEmployee* ptr)
- {
- ptr->age = 0;
- ptr->hourly_rate = 0;
- ptr->hours = 0;
- ptr->vtable = Vtable_Commission;
- }
- void Construct_Senior(struct SeniorSalesman* ptr)
- {
- ptr->age = 0;
- ptr->hourly_rate = 0;
- ptr->hours = 0;
- ptr->vtable = Vtable_Senior;
- }
- int main()
- {
- struct Employee *eptr;
- int numResp = 0;
- int choice = 0;
- int ageResp = 0;
- double payrateResp = 0.0;
- double hoursResp = 0.0;
- double salesResp = 0.0;
- do {
- printf("Choose one of the options by entering its corresponding number:");
- printf("\n1.Hourly Employee\n2.Commission Employee\n3.Senior Salesman\n");
- scanf_s("%d", &numResp);
- choice = numResp;
- switch (choice)
- {
- case 1:
- eptr = malloc(sizeof(struct HourlyEmployee));
- Construct_Hourly(eptr);
- printf("How old is this employee?\n");
- scanf_s("%d", &ageResp);
- printf("What is this employee's pay rate? (USD Per Hour)\n");
- scanf_s("%lf", &payrateResp);
- printf("How many hours has this employee worked?\n");
- scanf_s("%lf", &hoursResp);
- eptr->age = ageResp;
- ((struct HourlyEmployee*)eptr)->hourly_rate = payrateResp;
- ((struct HourlyEmployee*)eptr)->hours = hoursResp;
- free(eptr);
- break;
- case 2:
- eptr = malloc(sizeof(struct CommissionEmployee));
- Construct_Commission(eptr);
- printf("How old is this employee?\n");
- scanf_s("%d", &ageResp);
- eptr->age = ageResp;
- printf("How much in sales has this employee made?\n");
- scanf_s("%lf", &salesResp);
- ((struct CommissionEmployee*)eptr)->sales_amount = salesResp;
- free(eptr);
- break;
- case 3:
- eptr = malloc(sizeof(struct SeniorSalesman));
- Construct_Senior(eptr);
- printf("How old is this employee?\n");
- scanf_s("%d", &ageResp);
- eptr->age = ageResp;
- printf("How much in sales has this employee made?\n");
- scanf_s("%lf", &salesResp);
- ((struct SeniorSalesman*)eptr)->sales_amount = salesResp;
- free(eptr);
- break;
- default:
- printf("\nPlease choose a listed option.\n");
- break;
- }
- } while ((choice != 1) && (choice != 2) && ((choice != 3)));
- ((void(*)(struct Employee*))eptr->vtable[0])((struct Employee*)eptr);
- printf("I have made %.2lf thus far.", ((double(*)(struct Employee*))eptr->vtable[1])((struct Employee*)eptr));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement