-
Notifications
You must be signed in to change notification settings - Fork 1
/
ass4.c
207 lines (195 loc) · 3 KB
/
ass4.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include "stack.h"
#include "stackstring.h"
void in_post(char in[20],char post[20]);
int isp(char ch)
{
if(ch=='^')
{
return 3;
}
else if(ch=='*' || ch=='/')
{
return 2;
}
else if(ch=='+' || ch=='-')
{
return 1;
}
else
{
return 0;
}
}
int icp(char ch)
{
if(ch=='^')
{
return 4;
}
else if(ch=='*' || ch=='/')
{
return 2;
}
else if(ch=='+' || ch=='-')
{
return 1;
}
else
{
return 0;
}
}
void in_post(char in[20],char post[20])
{
//char a[50];
//int top=-1;
int i=0,j=0;
int len=strlen(in);
for(i=0;i<=len;i++)
{
if(in[i]=='\0')
{
while(isempty()==1)
{
post[j]=pop();
j++;
}
}
else if(isalnum(in[i]))
{
post[j]=in[i];
j++;
}
else if(in[i]==')')
{
while(topele()!='(')
{
post[j]=pop();
j++;
}
pop();
}
else
{
if(in[i]=='(')
{
push(in[i]);
}
else
{
while(isp(topele()) >= icp(in[i]))
{
post[j]=pop();
j++;
if(isempty()==0)
{
break;
}
}
push(in[i]);
}
}
}
post[j]='\0';
}
void post_in(char pos[20],char pre[20])
{
int j;
char op1[30],op2[30],res[30],res1[30],str[30];
int len=strlen(pos);
for(int i=0;i<=len;i++)
{
if(isalnum(pos[i]))
{
res1[0]=pos[i];
res1[1]='\0';
push1(res1);
}
else if(pos[i]=='+' || pos[i]=='-' || pos[i]=='*' || pos[i]=='/' || pos[i]=='^')
{
strcpy(op1,pop1());
strcpy(op2,pop1());
str[0]='(';//start making expression
str[1]='\0';
strcat(str,op2);// get (a
int l=strlen(str);
str[l]=pos[i];//add operator, .: get (a+
str[l+1]='\0';
strcat(str,op1);//.: get (a+b
l=strlen(str);
str[l]=')';//.: get (a+b)
str[l+1]='\0';
//top++;
push1(str);//push (a+b)
}
}
strcpy(pre,pop1());
}
int main()
{
char in[20],post[20];
char pos[20],pre[20];
int ch;
do
{
printf("enter the choice below \n1.infix to postfix \n2.postfix to infix \n3.exit \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the infix expression :");
scanf("%s",in);
in_post(in,post);
printf("the postfix expression is :");
printf("%s\n",post);
break;
case 2:
printf("enter the postfix expression :");
scanf("%s",pos);
post_in(pos,pre);
printf("the infix expression is :");
printf("%s\n",pre);
break;
}
}while(ch!=3);
return 0;
}
/*
OUTPUT:
enter the choice below
1.infix to postfix
2.postfix to infix
3.exit
1
enter the infix expression :(A+B^C)*D+E^5
the postfix expression is :ABC^+D*E5^+
enter the choice below
1.infix to postfix
2.postfix to infix
3.exit
2
enter the postfix expression :ABC^+D*E5^+
the infix expression is :(((A+(B^C))*D)+(E^5))
enter the choice below
1.infix to postfix
2.postfix to infix
3.exit
1
enter the infix expression :A^B^C
the postfix expression is :ABC^^
enter the choice below
1.infix to postfix
2.postfix to infix
3.exit
2
enter the postfix expression :ABC^^
the infix expression is :(A^(B^C))
enter the choice below
1.infix to postfix
2.postfix to infix
3.exit
3
*/