forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostfix_Evaluation.c
135 lines (115 loc) · 1.85 KB
/
postfix_Evaluation.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*Postfix Evaluation*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define N 20
char stack[N];
int top=-1;
int isEmpty()
{
return top == -1;
}
int isFull()
{
return top == N-1;
}
char peek()
{
return stack[top];
}
void push(int item)
{
if(isFull())
printf("\nStack overflow");
else
{
top++;
stack[top] = item;
}
}
char pop()
{
if(isEmpty())
return -1;
char ch=stack[top];
top--;
return(ch);
}
void EvalPostfix(char stack[])
{
int i,value,data1,data2;
char ch;
for (i = 0; stack[i] != '\0'; i++)
{
ch = stack[i];
if (isdigit(ch))
push(ch - '0');
else if (ch == '+' || ch == '-' || ch == '*' || ch == '/')
{
data1 = pop();data2 = pop();
switch (ch)
{
case '*':
{
value = data2 * data1;
break;
}
case '/':
{
value = data2 / data1;
break;
}
case '+':
{
value = data2 + data1;
break;
}
case '-':
{
value = data2 - data1;
break;
}
}
push(value);
}
}
printf(" %d \n", pop());
}
void main()
{
while(1)
{
char stack[N];
int ch;
printf("\nMENU");
printf("\n1.Enter Postfix Expression\t\t2. Evaluate\t\t3.Exit\t");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("Enter Postfix expression : ");
scanf("%s",stack);
break;
}
case 2:
{
printf("EValuation of Postfix Expression: ");
EvalPostfix(stack);
break;
}
case 3:
{
printf("Succesfully exiting program \n");
exit(0);
}
default:
{
printf("Error,wrong choice input\n");
break;
}
}
}
}