infix to postfix program in c 2b 2b

Solutions on MaxInterview for infix to postfix program in c 2b 2b by the best coders in the world

showing results for - "infix to postfix program in c 2b 2b"
Ailsa
05 May 2016
1/*https://github.com/Sudhanshu1304/Stack-Application*/
2
3#include <iostream>
4#include<string>
5using namespace std;
6
7
8
9class Stack{
10
11
12private:
13
14
15    char A[5];
16    int Size;
17
18public:
19    int top;
20    Stack(){
21
22        top=-1;
23        Size=sizeof(A)/sizeof(char);
24
25    }
26
27
28
29    bool IsFull(){
30
31        if(top==Size-1){
32            return true;
33        }
34        else{
35            return false;
36        }
37    }
38
39    bool IsEmpty(){
40
41        if(top==-1){
42            return true;
43        }
44        else{
45            return false;
46        }
47    }
48
49    char peek(){
50
51        return A[top];
52    }
53
54    void Push(char val){
55
56        if (IsFull()==false){
57            top++;
58            A[top]=val;
59        }
60        else{
61            cout<<"\nThe Stack is Full"<<endl;
62        }
63    }
64
65    char Pop(){
66
67        if(IsEmpty()==false){
68            char temp=A[top];
69            A[top]='0';
70            top--;
71            return temp;
72        }
73        else{
74            return '-1';
75        }
76
77    }
78
79    void Show_Stack(){
80
81
82        for(int i=0;i<top+1;i++){
83            cout<<A[i];
84        }
85
86
87    }
88
89};
90
91
92int Search(char A){
93
94
95    string CHAR[]={"([","{)","]}","+-","*/","^$"};
96    int Size=(sizeof(CHAR)/sizeof(string));
97
98    for(int i=0;i<Size;i++){
99
100        if(A==CHAR[i][0]){
101                if(i+i>=6){
102                    return i+i;
103                }
104                else{
105                    return i+i+0;
106                }
107
108        }
109        else if(CHAR[i][1]==A){
110             if(i+i>=6){
111                    return i+i;
112                }
113                else{
114                    return i+i+1;
115                }
116        }
117    }
118    return -1;
119
120}
121
122
123
124
125void Display(char ch,string vari, Stack &s){
126
127    int Size=s.top+1;
128
129
130    cout<<"\n   "<<ch<<"           ";
131    s.Show_Stack();
132    for(int i=0;i<10-Size;i++){
133        cout<<" ";
134    }
135    cout<<vari<<endl;
136
137}
138
139
140int main(){
141
142    Stack STACK;
143    char temp;
144    string exp;//"A+B*C";
145    cout<<"Enter Your Expression :";
146    cin>>exp;
147
148    string out="";
149    cout<<"\n\nExpression   Stack   Postfix\n"<<endl;
150    for(int i=0;i<exp.size();i++){
151
152        temp=exp[i];
153
154        int ab=Search(temp);
155
156        if (ab!=-1){
157
158            /* If We ENCOUNTER CLOSING BRACKETS*/
159            if(ab<=5 && ab>=3){
160
161
162
163                while(Search(STACK.peek())>2){
164                    char val=STACK.Pop();
165                        out=out+val;
166
167                    Display(temp,out,STACK);
168                }
169                STACK.Pop();
170                Display(temp,out,STACK);
171                }
172            /* Search Precedence*/
173            else{
174                if (Search(temp)>=0 && Search(temp)<=2){
175                    STACK.Push(temp);
176                    Display(temp,out,STACK);
177                }
178
179                /* If TOP < Temp */
180
181                else if(Search(STACK.peek())<ab){
182
183                        STACK.Push(temp);
184                        Display(temp,out,STACK);
185                }
186                else{
187                    /* if STACK= +,* and temp= + then we have to remove two times */
188
189
190                    while(Search(STACK.peek())>=ab){
191
192                        char val=STACK.Pop();
193                            out=out+val;
194                        Display(temp,out,STACK);
195                    }
196                    STACK.Push(temp);
197                    Display(temp,out,STACK);
198                }
199            }
200        }
201        /* If an Alphabet */
202        else{
203
204            out=out+temp;
205            Display(temp,out,STACK);
206
207            }
208
209
210    }
211    while(STACK.IsEmpty()==false){
212
213        char val=STACK.Pop();
214
215            out=out+val;
216        Display(temp,out,STACK);
217
218    }
219    cout<<"\n\nFINAL STRING : "<<out<<endl;
220
221
222}
Delfina
06 Sep 2020
1//Easiet way to solve infix to postfix expression
2bool isOperator(char c)
3{
4	if(c=='+'||c=='-'||c=='*'||c=='/'||c=='^')
5	{
6		return true;
7	}
8	else
9	{
10		return false;
11	}
12}
13
14int precedence(char c) 
15{ 
16    if(c == '^') 
17    return 3; 
18    else if(c == '*' || c == '/') 
19    return 2; 
20    else if(c == '+' || c == '-') 
21    return 1; 
22    else
23    return -1; 
24} 
25
26string InfixToPostfix(stack<char> s, string infix)
27{
28	string postfix;
29	for(int i=0;i<infix.length();i++)
30	{
31		if((infix[i] >= 'a' && infix[i] <= 'z')
32		||(infix[i] >= 'A' && infix[i] <= 'Z'))
33		{
34			postfix+=infix[i];
35		}
36		else if(infix[i] == '(')
37		{
38			s.push(infix[i]);
39		}
40		else if(infix[i] == ')')
41		{
42			while((s.top()!='(') && (!s.empty()))
43			{
44				char temp=s.top();
45				postfix+=temp;
46				s.pop();
47			}
48			if(s.top()=='(')
49			{
50				s.pop();
51			}
52		}
53		else if(isOperator(infix[i]))
54		{
55			if(s.empty())
56			{
57				s.push(infix[i]);
58			}
59			else
60			{
61				if(precedence(infix[i])>precedence(s.top()))
62				{
63					s.push(infix[i]);
64				}	
65				else if((precedence(infix[i])==precedence(s.top()))&&(infix[i]=='^'))
66				{
67					s.push(infix[i]);
68				}
69				else
70				{
71					while((!s.empty())&&( precedence(infix[i])<=precedence(s.top())))
72					{
73						postfix+=s.top();
74						s.pop();
75					}
76					s.push(infix[i]);
77				}
78			}
79		}
80	}
81	while(!s.empty())
82	{
83		postfix+=s.top();
84		s.pop();
85	}
86	
87	return postfix;
88}
89
90int main() 
91{  
92
93  	string infix_exp, postfix_exp;
94  	cout<<"Enter a Infix Expression :"<<endl;
95  	cin>>infix_exp;
96  	stack <char> stack;
97	cout<<"INFIX EXPRESSION: "<<infix_exp<<endl;
98  	postfix_exp = InfixToPostfix(stack, infix_exp);
99  	cout<<endl<<"POSTFIX EXPRESSION: "<<postfix_exp;
100	  
101	return 0;
102}
queries leading to this page
indlix to postflix using stack in c 2b 2boutput the final postfix expression after converting it to postfix from infix infix to postfix in cpppostfix to infix cppgeeks for geeks c infix to postfix programc 2b 2b program infix to postfixconvert the infix to postfix for a 2bb 2ac 2b 28d 2ae 29infix to postfix in c 2b 2binfix and postfix in cinfix to postfix conversioninfix to postfix converter in cinfix to postfix cinvresion in c 2b 2binfix to postfix java programinfix to postfix conversion using stackpostfix to infix program in c 2b 2bif a operator is encountered while scanning the expressionwhat is infix to postfix c 2b 2bconvert the infix to postfix for a 28b 2bc 29 2a 28d 2fe 29mplement and use a stack adt to convert infix mathematical expressions to postfix 2c and then evaluate the postfix expressions input will be from a text file 2c and output will be written to a file java program to convert infix to postfix expression using stackinfix to postfix function in cpostfix infix in c programminginfix to postfix code in cinfix to postfix in c programconvert infix to postfix in cinfix to postfix using stack in cc infix to postfixinfix to postfix using stack c 2b 2b codepostfix to infix in c write a program in to convert the entered expression to postfix forminfix to postfix conversion c 2b 2bconversion of infix to postfixcpp code infix to postfix conversiona 2bb 2ac infix to postfixconvert postfix to infix c 2b 2binfix o postfix converter in c 2b 2bprogram to convert infix to postfixinfix to postfix implementation cpphow to convert infix to postfix in cinfix to postfix in cconvert infix to postfix c 2b 2binfix to postfix unary c 2b 2binfix to postfix cppinfix to postfix in c 2b 2b using stackinfix to postfix using cppinfix to postfix c 2b 2binfix to postfix expression in cinfix to postfix program in cpp using stackinfix to postfix converter code stackinfix and postfixwrite a c program for implementing infix to postfix conversion postfix to infix cstack and expression conversionconverting postfix to infix c 2b 2binfix to postfix evaluation using stack step by step answer in c 2b 2binfix to postfix conversion examples in c 2b 2bprogram for infix to postfix conversion using c 2b 2binfix to postfix and prefix conversion using stack in c 2b 2bc 2b 2b infix to postfixinfix to postfix conversion onlineinfix to postfix algorithminfix to postfic 3dx using cpostfix conversion c 2b 2binfix to postfix program in cppprogram for infix to postfixc 2b 2b program to convert infix to postfixinfix to postfix with stack c 2b 2bpostfix to infix in compiler designinfix to postfix program in c languagec programto convert infix to postfix expressioninfix to postfix progrma in cinfix to postfix c programinfix and postfix c 2b 2binfix notation to postfix notation program codeinfix to postfix program in javainfix to postfix inx 2b 2bstack infix to postfix algoinfix to postfix expression of a 2bb 2a 28c 2bd 29 2fe 2bfc program for infix to postfixinfix to postfix conversion program in cinfix to postfix conversion program write a program to convert an infix expression to a postfix expression using stackinfix postfix concept in dsa programimplementing infix to postfix conversion in c 2b 2binfix to postfix c 2b 2b programc 2b 2b code for infix to postfix conversionc program for infix to postfix conversionhow to change infix equation to postfix equation using pythonin this program you have to convert an expression given in e2 80 9cinfix e2 80 9d form to e2 80 9cpostfix e2 80 9d formpostfix t infix using stack implementation c 2b 2bpostfix program in c 2b 2bjava infix to postfix programinfix to postfix conversion in compiler designinfix to postfix using stack implementation cpppostfix to infixinfix to postfix c 2b 2b using arrayconvert infix to postfix a 2fb 5ec 2bd 2ae a 2acconvert a given infix expression to postfix expression and then evaluate this postfix expression 2ainfix to postfix converter javainfix to postfix translation cpp convert infix expression to postfix expression code in c 2b 2bconvert infix to postfix 2b 2a 2bpostfix in c 2b 2binfix to postfix converter c 2b 2bpostfix to infix conversioninfix to postfix conversion cinfix to postfix c 2b 2b codeimplementation of infix to postfix conversion in cc program to onvert nfix to postfix expression c infix to postfix programinfix to postfix conversion examplesinfix to postfix cinfix to postfix programinfix to postfix using stack in cppwrite a program to convert an infix expression to postfix and prefix conversion c 2b 2bc 2b 2b program to convert infix to postfix using stackpostfix to infix program in c 2b 2b step by step executioninfix to postfix in c using stackinfix to postfix convertion in cinfix to postfix using stack programinfix to postfix using stack 28d 2b 28c e 29 2af 29 infix to postfixwrite a program for expression conversionconvert the infix to postfix for a 28b 2bc 29 2a 28d 2fe 29 mcqinfix to postfix program cinfix to postfix program in c 2b 2binfix to postfix using c programpostfix to infix c 2b 2bc program to convert infix to postfix25 program for converting infix to postfix form in cppprogram for infix to postfix cinfix to postfix conversion in cinfix to postfix algorithm in c 2b 2binfix to postfix pseudocodeinfix to postfix in compiler designinfix to postfix conversion using stack in c 2b 2bprogram to convert infix to postfix using stackwrite a c program to convert infix to postfixinfix into postfix in c 2b 2bimplement stack as an adt and apply it for conversions infix to postfixinfix to postfix c codeinfix expression to a parenthesis free postfix expression with c 2b 2b stackconvert infix to postfix expression using linear queueinfix to postfix program in cinfix to postfix conversion code in compiler designwrite a program to convert infix to postfix expression using stack in c 2b 2binfix to postfixinfix to postfix program in c 2b 2b