-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch9-1.c
67 lines (66 loc) · 1.34 KB
/
ch9-1.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
#include<stdio.h>
char inputmode(){
char mode;
while(1){
printf("a. add s.subtract\n");
printf("m. mulitply d.divide\n");
printf("q. quit\n");
printf("Enter the operation of your choices:");
scanf("%c", &mode);
fflush(stdin);
switch(mode){
case 'a':case 'A':
case 's':case 'S':
case 'm':case 'M':
case 'd':case 'D':
case 'q':case 'Q':
return mode;
}
printf("Error!\n");
}
}
void ans(char mode, double a, double b){
switch(mode){
case 'a':case 'A':
printf("%lf + %lf = %lf\n", a, b, a+b);
break;
case 's':case 'S':
printf("%lf - %lf = %lf\n", a, b, a-b);
break;
case 'm':case 'M':
printf("%lf * %lf = %lf\n", a, b, a*b);
break;
case 'd':case 'D':
printf("%lf / %lf = %lf\n", a, b, a/b);
break;
}
}
int main(){
char mode;
char s[100];
while(mode=inputmode()){
if(mode=='q'||mode=='Q')break;
double a,b;
while(1){
printf("Enter first number:");
if(scanf("%lf", &a)==1)
break;
scanf("%s", &s);
printf("Error! %s is not a number.\n", s);
}
while(1){
printf("Enter second number:");
if(scanf("%lf", &b)!=1){
scanf("%s", &s);
printf("Error! %s is not a number.\n", s);
} else if((mode=='d'||mode=='D')&&b==0) {
printf("Error! Second number cannot be 0.\n");
} else {
break;
}
}
ans(mode, a, b);
fflush(stdin);
}
printf("Bye!\n");
}