-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path004_Prefix.c
115 lines (62 loc) · 1.58 KB
/
004_Prefix.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//Author:Kavya Dhar // © Mr.dhar 25-June-2021
// Purpose ; // Prefix expressions
// 1) Enter prefix Expression along with operation
// 3) Result
// 4) $$ Exit $$
#include<stdio.h> // Pre-Processive to include standard input and output funtion header files
#include<conio.h> // Pre-Processive to include console input and output funtion header files
#include<string.h> // Pre-Processive to include string funtions header files
int James[20]; // Maximum size of Stack i.e 20
int top=-1; // stack is empty
int A,B,i; // Global Declratation
void push(int value)
{
top=top+1;
James[top]=value;
}
int pop()
{
return(James[top--]);
}
int ope(char c)
{
if(c=='+'||c=='-'||c=='*'||c=='/')
return 1;
else
return 0;
}
void main()
{
int exp1;
int exp2;
int res;
char prefix[10];
printf("Enter the prefix Expression: ");
scanf("%s",prefix);
A=strlen(prefix);
for(i=A-1;i>=0;i--)
{
switch(ope(prefix[i]))
{
case 0:
B=prefix[i];
push(B);
break;
case 1: exp1=pop();
exp2=pop();
switch(prefix[i])
{
case '+': res=exp1+ exp2;
break;
case '-': res=exp1- exp2;
break;
case '*': res=exp1* exp2;
break;
case '/': res=exp1/ exp2;
break;
}
push(res);
}
}
printf("Result is %d",James[top]);
}