-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex12.c
150 lines (129 loc) · 4.74 KB
/
ex12.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Rewrite the previous ex11 program by replacing the switch statement with a nested if…else statement; be careful to deal with the default case properly. Then rewrite this new version by replacing the nested if…else statement with a series of if statements; here, too, be careful to deal with the default case properly (this is more difficult than in the nested if…else version). This exercise demonstrates that switch is a convenience and that any switch statement can be written with only single-selection statements.
//////////////////////////////
// Using Nested if else //
/////////////////////////////
#include <stdio.h>
int main()
{
int paycode;
// Prompt for employee type
printf("Enter paycode (-1 to end): ");
scanf("%d", &paycode);
while (paycode != -1)
{
if (paycode == 1)
{ // Manager
double weeklySalary;
printf("Enter weekly salary: ");
scanf("%lf", &weeklySalary);
printf("Manager's pay is $%.2lf\n", weeklySalary);
}
else if (paycode == 2)
{ // Hourly worker
double hourlyWage, hoursWorked, overtime = 0.0, totalPay;
printf("Enter hourly wage: ");
scanf("%lf", &hourlyWage);
printf("Enter total hours worked: ");
scanf("%lf", &hoursWorked);
if (hoursWorked > 40)
{
overtime = (hoursWorked - 40) * (hourlyWage * 1.5);
hoursWorked = 40;
}
totalPay = (hoursWorked * hourlyWage) + overtime;
printf("Hourly worker's pay is $%.2lf\n", totalPay);
}
else if (paycode == 3)
{ // Commission worker
double grossSales, totalPay;
printf("Enter gross weekly sales: ");
scanf("%lf", &grossSales);
totalPay = 250 + (grossSales * 0.057);
printf("Commission worker's pay is $%.2lf\n", totalPay);
}
else if (paycode == 4)
{ // Pieceworker
double amountPerItem, numberOfItems, totalPay;
printf("Enter amount paid per item: ");
scanf("%lf", &amountPerItem);
printf("Enter number of items produced: ");
scanf("%lf", &numberOfItems);
totalPay = amountPerItem * numberOfItems;
printf("Pieceworker's pay is $%.2lf\n", totalPay);
}
else
{ // Invalid paycode
printf("Invalid paycode entered.\n");
}
// Prompt for next employee type or to exit
printf("\nEnter paycode (-1 to end): ");
scanf("%d", &paycode);
}
return 0;
}
//////////////////////////////
// Using Series of if //
/////////////////////////////
#include <stdio.h>
int main()
{
int paycode;
// Prompt for employee type
printf("Enter paycode (-1 to end): ");
scanf("%d", &paycode);
while (paycode != -1)
{
int validPaycode = 0;
if (paycode == 1)
{
validPaycode = 1;
double weeklySalary;
printf("Enter weekly salary: ");
scanf("%lf", &weeklySalary);
printf("Manager's pay is $%.2lf\n", weeklySalary);
}
if (paycode == 2)
{
validPaycode = 1;
double hourlyWage, hoursWorked, overtime = 0.0, totalPay;
printf("Enter hourly wage: ");
scanf("%lf", &hourlyWage);
printf("Enter total hours worked: ");
scanf("%lf", &hoursWorked);
if (hoursWorked > 40)
{
overtime = (hoursWorked - 40) * (hourlyWage * 1.5);
}
totalPay = (hoursWorked > 40 ? 40 : hoursWorked) * hourlyWage + overtime;
printf("Hourly worker's pay is $%.2lf\n", totalPay);
}
if (paycode == 3)
{
validPaycode = 1;
double grossSales, totalPay;
printf("Enter gross weekly sales: ");
scanf("%lf", &grossSales);
totalPay = 250 + (grossSales * 0.057);
printf("Commission worker's pay is $%.2lf\n", totalPay);
}
if (paycode == 4)
{
validPaycode = 1;
double amountPerItem, numberOfItems, totalPay;
printf("Enter amount paid per item: ");
scanf("%lf", &amountPerItem);
printf("Enter number of items produced: ");
scanf("%lf", &numberOfItems);
totalPay = amountPerItem * numberOfItems;
printf("Pieceworker's pay is $%.2lf\n", totalPay);
}
if (!validPaycode)
{
printf("Invalid paycode entered.\n");
}
// Prompt for next employee type or to exit
printf("\nEnter paycode (-1 to end): ");
scanf("%d", &paycode);
}
return 0;
}