-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPREFIX.C
More file actions
55 lines (46 loc) · 853 Bytes
/
PREFIX.C
File metadata and controls
55 lines (46 loc) · 853 Bytes
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
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
int stack[50];
int top = -1;
void push(int x) {
stack[++top] = x;
}
int pop() {
return stack[top--];
}
void main() {
char exp[50];
int n1,n2,n3,num,len,i;
clrscr();
printf("\nEnter the expression: ");
scanf("%s",exp);
len = strlen(exp);
for(i=len;i>=0;i--) {
if( isdigit(exp[i]) ) {
num = exp[i] - 48; //converts from ASCII to int
push(num);
}
else {
n1 = pop();
n2 = pop();
switch(exp[i]) {
case '+': n3 = n1 + n2;
break;
case '-': n3 = n2 - n1;
break;
case '*': n3 = n2 * n1;
break;
case '/': n3 = n2 / n1;
break;
case '^': n3 = pow(n1,n2);
break;
}
push(n3);
}
}
num = pop();
printf("\nRESULT\n %s = %d", exp, num);
getch();
}