-
Notifications
You must be signed in to change notification settings - Fork 110
/
no_stacks.c
186 lines (164 loc) · 4.25 KB
/
no_stacks.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <stdio.h>
#include<string.h>
void WritePostfixBrackets(char expr[]);
void PostNoBrackets(char expr[], int start, int end);
void GetLeftOperand(char expr[], char left[], int pos, int start);
void GetRightOperand(char expr[], char right[], int pos, int end);
void WritePostfix(char expr[], int startIndex, char left[], char right[], char op);
int converted[50];
int main()
{
char expr[50];
printf("Enter infix expression \n");
scanf("%s",expr);
char st[52];
st[0]='(';
st[1]='\0';
strcat(st,expr);
strcpy(expr,st);
st[0]=')';
st[1]='\0';
strcat(expr,st);
int l =strlen(expr);
int i;
for(i=0; i<l; i++)
{
converted[i]=0;
}
WritePostfixBrackets(expr);
return 0;
}
void WritePostfixBrackets(char expr[])
{
int l = strlen(expr),i,startIndex,endIndex,k=0;
do
{
i=0;
int c=0;
startIndex=0;
endIndex = l;
while(i<l && expr[i]!=')')
{
if(expr[i]=='(')
{
c=1;
startIndex = i+1;
i++;
}
else
++i;
}
if(c==1)
{
i=0;
while(i<l)
{
if(expr[i]==')')
{
endIndex=i-1;
PostNoBrackets(expr, startIndex, endIndex);
expr[startIndex-1]=expr[endIndex+1]='#';
converted[startIndex-1]=converted[endIndex+1]=1;
c=0;
break;
}
i++;
}
}
}while(startIndex!=0);
char newExp[50];
for(i=0; i<l; i++)
{
if(expr[i]!='#')
{
newExp[k++]=expr[i];
}
}
newExp[k]='\0';
printf("Equivalent postfix expression = %s",newExp);
}
void PostNoBrackets(char expr[], int start, int end)
{
int i,l;
char left[30];
char right[30];
for(i=end; i>=start; i--)
{
if (expr[i] == '^' && converted[i] != 1)
{
converted[i]=1;
converted[i-1]=converted[i+1]=1;
GetLeftOperand(expr, left, i, start);
GetRightOperand(expr, right, i, end);
l=strlen(left);
WritePostfix(expr, i-l, left, right, expr[i]);
}
}
for(i=start; i<=end; i++)
{
if ((expr[i] == '*' || expr[i] == '/') && converted[i] != 1)
{
converted[i]=1;
converted[i-1]=converted[i+1]=1;
GetLeftOperand(expr, left, i, start);
GetRightOperand(expr, right, i, end);
l=strlen(left);
WritePostfix(expr, i-l, left, right, expr[i]);
}
}
for(i=start; i<=end; i++)
{
if ((expr[i] == '+' || expr[i] == '-') && converted[i] != 1)
{
converted[i]=1;
converted[i-1]=converted[i+1]=1;
GetLeftOperand(expr, left, i, start);
GetRightOperand(expr, right, i, end);
l=strlen(left);
WritePostfix(expr, i-l, left, right, expr[i]);
}
}
}
void GetLeftOperand(char expr[], char left[], int pos, int start)
{
left[0]='\0';
int i = pos-1;int j,d;
while ( i>= start && converted[i] != 0)
{ char st[20];
st[0]=expr[i];
st[1]='\0';
strcat(st,left);
strcpy(left,st);
i = i-1;
}
}
void GetRightOperand(char expr[], char right[], int pos, int end)
{
right[0]='\0';
int i = pos+1;
while ( i<= end && converted[i] != 0)
{
char st[2];
st[0]=expr[i];
st[1]='\0';
strcat(right, st);
i = i+1;
}
}
void WritePostfix(char expr[], int startIndex, char left[], char right[], char op)
{
int i = 0;
while (i < strlen(left))
{
expr[startIndex+i] = left[i];
i = i+1;
}
int j = 0;
while (j < strlen(right))
{
expr[startIndex+i+j] = right[j];
//converted[i+j] = 1;
j = j+1;
}
expr[startIndex+i+j] = op;
}