0% found this document useful (0 votes)
59 views

Infix To Postfix Code

This C++ program defines a class called stack to implement a stack data structure. It contains functions to push, pop, check if empty/full and access the top element. The main function takes an infix expression as input, uses the stack to convert it to postfix notation by following precedence rules and operator order. It displays the output postfix expression and step-by-step workings including stack operations.

Uploaded by

NIGHT D3vil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Infix To Postfix Code

This C++ program defines a class called stack to implement a stack data structure. It contains functions to push, pop, check if empty/full and access the top element. The main function takes an infix expression as input, uses the stack to convert it to postfix notation by following precedence rules and operator order. It displays the output postfix expression and step-by-step workings including stack operations.

Uploaded by

NIGHT D3vil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

/* C++ implementation to convert

infix expression to postfix*/

// Note that here we use std::stack

// for Stack operations

//#include<bits/stdc++.h>

#include<iostream>

using namespace std;

class stack {

public:

char a[50];

char mytop;

stack()

mytop = -1;

void push(char num);

bool isempty();

bool isfull();

void pop();

char top();

};

void stack::push(char num)

if (isfull() == true)

cout << "Stack is full" << endl;

else

mytop++;

a[mytop] = num;

}
bool stack::isempty()

if (mytop == -1)

return true;

else

return false;

bool stack::isfull()

if (mytop == 49)

return true;

else

return false;

void stack::pop()

if (isempty() == true)

cout << "Stack is empty " << endl;

else

a[mytop] = 0;

mytop--;

}
}

char stack::top()

return a[mytop];

//Function to return precedence of operators

int prec(char c)

if(c == '*' || c == '/')

return 2;

else if(c == '+' || c == '-')

return 1;

else

return -1;

// The main function to convert infix expression

//to postfix expression

void infixToPostfix(string s)

stack st;

int l = s.length();

string ns;

int z=0;

string strStack="";

for(int i = 0; i < l; i++)

// If the scanned character is an


// ‘(‘, push it to the stack.

if(s[i] == '(')

strStack+=s[i];

st.push('(');

cout << "Token: '" << ns<< "'\tpush '" << s[i]<<"'\t\tStack: "<<strStack<< "\n";

// If the scanned character is an ‘)’,

// pop and to output string from the stack

// until an ‘(‘ is encountered.

else if(s[i] == ')')

cout <<"Token: '" <<ns<<endl;

while(!st.isempty() && st.top() != '(')

cout << "Token: '" <<ns << "\n";

char c = st.top();

z=strStack.length()-1;

string ss =strStack;

strStack = "";

for (int i = 0; i < z; i++)

strStack += ss[i];

ns += c;

cout << "\t\tpop & Display '" << st.top() ;

st.pop();

cout<< "'\tStack:" << strStack << "\n";

if(st.top() == '(')

char c = st.top();

cout << "\t\tpop '" <<st.top();

st.pop();
z=strStack.length()-1;

string ss =strStack;

strStack = "";

for (int i = 0; i < z; i++)

strStack += ss[i];

cout<< "'\t\t\tStack: " << strStack<< "\n";

//If an operator is scanned

else if (s[i]=='+'||s[i]=='-'||s[i]=='*'||s[i]=='/')

while(st.isempty() != true && prec(s[i]) <= prec(st.top()))

char c = st.top();

z=strStack.length()-1;

string ss =strStack;

strStack = "";

for (int i = 0; i < z; i++)

strStack += ss[i];

ns += c;

cout << "Token: '" << ns << "'\tpop & Display '" << st.top();

st.pop();

cout<< "'\tStack:" << strStack << "\n";}

strStack+=s[i];

st.push(s[i]);

cout << "Token: '" << ns << "'\tpush '"<< s[i] << "'\t\tStack: " << strStack << "\n";

// If the scanned character is

// an operand, add it to output string.

else

{
ns+=s[i];

cout << "Token: '" << ns << "'\tDisplay '"<<s[i]<<"'\t\tStack: " << strStack << "\n";

// Pop all the remaining elements from the stack

while(st.isempty() != true)

char c = st.top();

z=strStack.length()-1;

string ss =strStack;

strStack = "";

for (int i = 0; i < z; i++)

strStack += ss[i];

st.pop();

ns += c;

cout << "Token: '" << ns << "'\tpop & Display '" << st.top();

st.pop();

cout<< "'\tStack:" << strStack << "\n";

cout << ns << endl;

int main()

string exp;

cout<<"enter string"<<endl;

cin>>exp;

infixToPostfix(exp);

return 0;
//((A*(B+D)/E)-F*(G+H/K)))

//ABD+*E/FGHK/+*-

You might also like