Infix to Postfix
PROBLEM :
Given an infix expression. Conver the infix expression to postfix expression.
Infix expression:The expression of the form a op b. When an operator is in-between every pair of operands.
Postfix expression:The expression of the form a b op. When an operator is followed for every pair of operands.
Input:
The input contains an infix expression.The expression should contain all characters and ^,*,/,+,-.
Output:
Output the infix expression to postfix expression.
Constraints:
1<=T<=100
1<=length of expression<=30
Example:
Input:
2
a+b*(c^d-e)^(f+g*h)-i
A*(B+C)/D
Output:
abcd^e-fgh*+^*+i-
ABC+*D/
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<stack>
bool isChar(char ch){
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
return true ;
return false ;
}
int check(char op){
switch(op){
case '(' : return 0 ;
case '+' : return 1 ;
case '-' : return 1 ;
case '*' : return 2 ;
case '/' : return 2 ;
case '^' : return 3 ;
}
}
string Infix2Postfix(string str){
string ans="" ;
stack<char> s ;
for(int i=0;i<str.length();i++){
if(str[i]=='(')
s.push(str[i]) ;
else if(str[i]==isChar(str[i]))
ans+=str[i] ;
else if(str[i]==')'){
while(!s.empty()&&s.top()!='('){
ans+=s.top() ;
s.pop() ;
}
s.pop() ;
}
else{
while(!s.empty()&&check(str[i])<=check(s.top())){
ans+=s.top() ;
s.pop() ;
}
s.push(str[i]) ;
}
}
while(!s.empty()){
ans+=s.top() ;
s.pop() ;
}
return ans ;
}
int main(){
int t ;
cin>>t ;
while(t--){
string str ;
cin>>str ;
str=Infix2Postfix(str) ;
cout<<str<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
Given an infix expression. Conver the infix expression to postfix expression.
Infix expression:The expression of the form a op b. When an operator is in-between every pair of operands.
Postfix expression:The expression of the form a b op. When an operator is followed for every pair of operands.
Input:
The input contains an infix expression.The expression should contain all characters and ^,*,/,+,-.
Output:
Output the infix expression to postfix expression.
Constraints:
1<=T<=100
1<=length of expression<=30
Example:
Input:
2
a+b*(c^d-e)^(f+g*h)-i
A*(B+C)/D
Output:
abcd^e-fgh*+^*+i-
ABC+*D/
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<stack>
bool isChar(char ch){
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
return true ;
return false ;
}
int check(char op){
switch(op){
case '(' : return 0 ;
case '+' : return 1 ;
case '-' : return 1 ;
case '*' : return 2 ;
case '/' : return 2 ;
case '^' : return 3 ;
}
}
string Infix2Postfix(string str){
string ans="" ;
stack<char> s ;
for(int i=0;i<str.length();i++){
if(str[i]=='(')
s.push(str[i]) ;
else if(str[i]==isChar(str[i]))
ans+=str[i] ;
else if(str[i]==')'){
while(!s.empty()&&s.top()!='('){
ans+=s.top() ;
s.pop() ;
}
s.pop() ;
}
else{
while(!s.empty()&&check(str[i])<=check(s.top())){
ans+=s.top() ;
s.pop() ;
}
s.push(str[i]) ;
}
}
while(!s.empty()){
ans+=s.top() ;
s.pop() ;
}
return ans ;
}
int main(){
int t ;
cin>>t ;
while(t--){
string str ;
cin>>str ;
str=Infix2Postfix(str) ;
cout<<str<<endl ;
}
return 0;
}
--------------------------------------------------------------------------------
Comments
Post a Comment