-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputation.c
More file actions
80 lines (76 loc) · 1.83 KB
/
computation.c
File metadata and controls
80 lines (76 loc) · 1.83 KB
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
#include <stdio.h>
#include <string.h>
int calculator()
{
char ch;
int x,y,tmp;
int sum,sub,mul,dev,rd;
printf("Please input the computation methods: a for addition, s for substraction, m for multiplication, d for devision, r for rouding, c for comparing\n");
scanf("%c",&ch);
switch (ch) {
/* addition */
case 'a':
printf("Please input num x and y.\n");
scanf("%d%d",&x,&y);
sum=x+y;
printf("%d + %d = %d\n",x,y,sum);
break;
/* substraction */
case 's':
printf("Please input num x and y\n");
scanf("%d%d",&x,&y);
if (x>y){
sub=x-y;
printf("%d - %d = %d\n",x,y,sub);
}
else{
sub=y-x;
printf("%d - %d = %d\n",y,x,sub);
}
break;
/* multiplication */
case 'm':
printf("Please input num x and y.\n");
scanf("%d%d",&x,&y);
mul=x*y;
printf("%d * %d = %d\n",x,y,mul);
break;
/* devision */
case 'd':
printf("Please input num x and y.\n");
scanf("%d%d",&x,&y);
if (x>y){
dev=x/y;
printf("%d / %d = %d\n",x,y,dev);
}
else{
dev=y/x;
printf("%d / %d = %d\n",y,x,dev);
}
break;
/* rounding */
case 'r':
printf("Please input num x.\n");
scanf("%d",&x);
if ((x % 10) >= 5){
rd= (x/10+1)*10;
printf("round(%d)=%d\n",x,rd);
}
else{
rd= (x/10)*10;
printf("round(%d)=%d\n",x,rd);
}
break;
/* compare two num*/
case 'c':
printf("Please input x and y");
scanf("%d%d",&x,&y);
tmp=x>y?x:y;
printf("The big one is %d, in %d and %d\n",tmp,x,y);
break;
default:
printf("Please input correct letter!\n");
break;
}
return 0;
}