-
Notifications
You must be signed in to change notification settings - Fork 0
/
06-09.c
35 lines (28 loc) · 777 Bytes
/
06-09.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
// evaluate simple expression with switch-case, 06-08A modified
#include <stdio.h>
int main(void) {
float value1, value2;
char operator;
printf("expression: ");
scanf("%f %c %f", &value1, &operator, &value2);
switch (operator) {
case '+':
printf("%.2f\n", value1 + value2);
break;
case '-':
printf("%.2f\n", value1 - value2);
break;
case '*':
case 'x':
printf("%.2f\n", value1 * value2);
break;
case '/':
if (value2 == 0) printf("division by zero\n");
else printf("%.2f\n", value1 / value2);
break;
default:
printf("unknown operator\n");
break;
}
return 0;
}