Experiment Title.: Infix To Postfix Notation
Experiment Title.: Infix To Postfix Notation
Write a program to demonstrate the use of stack (Implemented using linear array) in
converting arithmetic expression from infix notation to postfix notation.
2. Task to be done:
We have to write a code which take input infix notation and after converting it into
Postfix notation display it.
3. Algorithm/Flowchart :
#include<iostream>
#include<string>
#define MAX 50
using namespace std;
class Stack
{
public:
char s[50];
int top;
Stack()
{
top=-1;
}
void push(char o)
{
if(top==MAX-1)
{
cout<<"Over Flow !!!";
}
else
{
top++;
s[top]=o;
}
}
char pop()
{
char ch;
if(top==-1)
{
cout<<"Under Flow !!!";
}
else
{
ch=s[top];
s[top]='\0';
top--;
return(ch);
}
return 0;
}
char Top()
{
if(top<0)
{
return 0;
}
else
{
char c=s[top];
return c;
}
}
};
return 0;
}
if(in[i]=='}')
{
while(o.Top()!='{')
{
p.insert(p.end(),o.pop());
}
o.pop();
i++;
}
}
else
{
if(o.top==-1)
{
o.push(in[i]);
i++;
}
while(priority(o.Top()) == priority(in[i]))
{
p.insert(p.end(),o.pop());
if(o.top < 0)
{
break;
}
}
o.push(in[i]);
i++;
}
else if(priority(in[i]) > priority(o.Top()))
{
o.push(in[i]);
i++;
}
}
}
while(o.top!=-1)
{
p.insert(p.end(),o.pop());
}
return p;
}
int main()
{
string in,p;
cout<<"Enter the infix expression:"<<endl;
cin>>in;
p = convert(in);
cout<<"The Converted postfix string is:"<<endl<<p;
return 0;
}
5.Discussion /Observation/Complexity :
6. Result/Output/Writing Summary:
Evaluation Grid: