-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheval_postfix.c
160 lines (118 loc) · 4.01 KB
/
eval_postfix.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*This is the program to evaluate the postfix expression, user will enter the postfix expression
and the program will evaluate it and will give the result*/
/*Header files*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
/*Defining the size of stack*/
#define SIZE 100
/*Structure for stack*/
typedef struct
{
double item[SIZE];
int top;
}operand_stack;
/*Function prototype*/
void push (operand_stack *, double);
double pop (operand_stack *);
void init(operand_stack *);
int isEmpty(operand_stack *); /*To check whether the stack is empty or not*/
double operate (double, double, char);/*For operation*/
double eval(char[]); /*For evaluation*/
void push(operand_stack *sp, double value)
{
if (sp->top == SIZE -1)
{
printf("Stack overflow\n");
return;
}
else
sp->top++;
sp->item[sp->top] = value;
}
double pop(operand_stack *sp)
{
if(isEmpty(sp))
{
printf("Stack underflow\n");
return -9999;
}
int value;
value = sp->item[sp->top];
sp->top--;
return value;
}
void init(operand_stack *sp) /*For initializing the stack*/
{
sp->top = -1;
}
int isEmpty(operand_stack *sp) /*It will return '1' if the stack is empty and will return '0' if not*/
{
return (sp->top == -1);
}
/*This function will evaluate and return the value of the entered postfix string*/
double eval(char postfix[])
{
operand_stack s1; /*declaring the variable of type stack*/
init(&s1); /*Initializing the stack*/
int i=0;
double result = 0.00;
while (postfix[i]!= '\0')
{
char next_token = postfix[i];
if (next_token >= '0' && next_token <= '9') /*If the next token will be an operand*/
{
int v = next_token- '0'; /*As we are dealing with ASCII values, so subtracting
it with the ASCII value of 0 will give the required integer*/
push(&s1, (double) v);
}
/*If the next token is an operator , assign the last two operands in variable opnd1 and opnd2 and operate them*/
/*and then push the result into the stack until and unless we will not reach the end of the expression*/
else if(next_token == '+' || next_token == '-' || next_token == '*' || next_token == '/' || next_token == '$')
{
double opnd1 = pop(&s1);
double opnd2 = pop(&s1);
result = operate(opnd2, opnd1, next_token);
push(&s1, result);
}
else
{
printf("Invalid symbol encountered\n"); /*If the entered symbol is not among the mentioned above*/
exit(1);
}
i++;
}
/*When we will reach the end of the expression, just pop out the result from the stack*/
return pop(&s1);
}
/*This function is for operation when the next token will be operator and we will pop out the lat two operands*/
double operate (double left_opnd, double right_opnd, char oprtr)
{
double result = 0.0;
switch(oprtr)
{
case '+': result = left_opnd + right_opnd;
break;
case '-': result = left_opnd - right_opnd;
break;
case '*': result = left_opnd * right_opnd;
break;
case '/': result = left_opnd / right_opnd;
break;
case '$': result = pow(left_opnd, right_opnd);
break;
default : printf("Invalid operator encountered\n");
exit(1);
}
return result;
}
int main()
{
char postfix[SIZE];
double result;
printf("Enter postfix expression : ");
scanf("%s", postfix);
result = eval(postfix);
printf("Result is %lf \n", result);
return 0;
}