Professional Documents
Culture Documents
Class: IT-A
Roll Number: 201106013
Date: 25/11/2021
Experiment 2
Infix Expression
Postfix Expression
• If the operator has precedence greater than or equal to the top of the stack,
push the operator to the stack.
• If the operator has precedence lesser than the top of the stack, pop the
operator and output it to the prefix notation output and then check the
above condition again with the new top of the stack.
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#define MAX 20
class stack{
private:
int top,a;
int arr[MAX];
public:
stack(){
top =-1;
}
if (isfull())
cout<<"stack is full!!!\n";
else{
top++;
arr[top]=a;
void pop(){
if (isempty()){
cout<<"stack is empty!!!\n";
else{
top--;
int gettop(){
return arr[top];
bool isempty(){
if (top==-1)
return true;
else
return false;
bool isfull(){
if (top==MAX-1)
return true;
else
return false;
if(op=='+'||op=='-'||op=='*'||op=='/'||op=='^')
return true;
if(c=='^')
return 3;
return 2;
return 1;
for(int i=0;i<s.length();i++){
if(isdigit(s[i])){
push(s[i]-'0');
else{
int op2=gettop();
pop();
int op1=gettop();
pop();
switch(s[i]){
case '+':push((op1+op2));break;
case '-':push((op1-op2));break;
case '*':push((op1*op2));break;
case '/':push((op1/op2));break;
case '^':push((pow(op1,op2)));break;
if(gettop()!=NULL)
return gettop();
for(int i=s.length()-1;i>=0;i--){
if(isdigit(s[i])){
push(s[i]-'0');
else{
int op1=gettop();
pop();
int op2=gettop();
pop();
switch(s[i]){
case '+':push((op1+op2));break;
case '-':push((op1-op2));break;
case '*':push((op1*op2));break;
case '/':push((op1/op2));break;
case '^':push((pow(op1,op2)));break;
if(gettop()!=NULL)
return gettop();
string result;
for(int i=0;i<inp.length();i++){
if(isalpha(inp[i])||isdigit(inp[i])){
result+=inp[i];
}else if(inp[i]=='('){
push(inp[i]);
}else if(inp[i]==')'){
while(gettop()!='('){
result+=gettop();
pop();
pop();
}else{
result+=gettop();
pop();
push(inp[i]);
while(!isempty()){
pop();
int res=postfixevaluation(result);
if(res<-150){
}else
cout<<endl<<"Solution:"<<res<<endl<<endl;
return result;
}
string result;
for(int i=inp.length()-1;i>=0;i--){
if((isalpha(inp[i]))||isdigit(inp[i])){
result+=inp[i];
}else if(inp[i]==')'){
push(inp[i]);
}else if(inp[i]=='('){
while(gettop()!=')'){
result+=gettop();
pop();
}
pop();
}else{
result+=gettop();
pop();
push(inp[i]);
while(!isempty()){
pop();
reverse(result.begin(),result.end());
int reso=prefixevaluation(result);
if(reso<-150){
}else
cout<<endl<<"Solution:"<<reso<<endl<<endl;
return result;
}
};
int main(){
stack y;
int x;
do{
cout<<"1.Infix to Postfix"<<endl;
cout<<"2.Infix to Prefix"<<endl;
cout<<"0.Exit"<<endl;
cin>>x;
switch(x){
char c;
case 1: {
string str;
cin>>str;
cout<<"Postfix Expression:"<<endl<<y.infixtopostfix(str)<<endl<<endl;
break;
case 2:
string str;
cin>>str;
cout<<"Prefix Expression:"<<endl<<y.infixtoprefix(str)<<endl<<endl;
break;
case 0:
exit(1);
default :
cout<<"\nInvalid input!!"<<endl;
}while(x!=0);
}
7. After all the characters are scanned, reverse the prefix notation output.
CONCLUSION:
The C++ programs were successfully studied and
implemented.