-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost2in.c
More file actions
119 lines (105 loc) · 2.47 KB
/
Copy pathpost2in.c
File metadata and controls
119 lines (105 loc) · 2.47 KB
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
//Postfix to Infix Evaluation
//Input is string
//Output is integer
//by Simply
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
struct stack
{
int a[100];
int top;
int size;
}s;
int pop()
{
int out;
if (s.top==-1)
printf("Underflow");
else
{
out=s.a[s.top];
s.top--;
return out;
}
}
void push(int in)
{
if(s.top==s.size-1)
printf("Overflow");
else
{
s.top++;
s.a[s.top]=in;
}
}
void main()
{
int final,res,num,x,y;
char po[100],ch;
s.top = -1;
printf("Enter Postfix: ");
scanf("%s",po);
s.size=strlen(po);
for (int i=0;i<s.size;i++)
{
ch = po[i];
switch(ch)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
num=ch-'0';
push(num);
break;
case '+':
y = pop();
x = pop();
res = x+y;
push(res);
break;
case '-':
y = pop();
x = pop();
res = x-y;
push(res);
break;
case '*':
y = pop();
x= pop();
res = x*y;
push(res);
break;
case '^':
y = pop();
x= pop();
res = pow(x,y);
push(res);
break;
case '/':
y = pop();
x= pop();
if(y==0)
{
printf("Division by Zero not possible");
return;
}
else
{
res = x/y;
}
push(res);
break;
}
}
final = pop();
printf("Final result: %d",final);
}