-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solve arithematic expressions.txt
77 lines (65 loc) · 1.18 KB
/
Solve arithematic expressions.txt
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
#include <stdio.h>
#include<string.h>
int addsub();
int muldiv();
int term();
char input[101];
int pos = 0;
int term(){
int n = 0;
if(input[pos] == '('){
pos++;
n = addsub();
if(input[pos] == ')'){
pos++;
return n;
}
}else{
while('0' <= input[pos] && input[pos] <= '9'){
n = n*10 + (input[pos] - '0');
pos++;
}
}
return n;
}
int muldiv(){
int first,second;
first = term();
for(;;){
if(input[pos] == '*'){
pos++;
second = term();
first *= second;
}else if(input[pos] == '/'){
pos++;
second = term();
first /= second;
}else{
return first;
}
}
}
int addsub(){
int first,second;
first = muldiv();
for(;;){
if(input[pos] == '+'){
pos++;
second = muldiv();
first += second;
}else if(input[pos] == '-'){
pos++;
second = muldiv();
first -= second;
}else{
return first;
}
}
}
int main(){
int n,i,j;
printf("Input an expression using +, -, *, / operators:\n");
scanf("%s",input);
printf("%d\n",addsub());
return 0;
}