Sunday 20 August 2017

INFIX to RPN conversion ,Stack example 3



INFIX to RPN conversion……
ALGORITHM
C++ CODE
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
///////////////////////////////// DEFINITION OF STACK ///////////////////////////////////
class stack
{
            int top;
            char opers[100];
public:
            stack( ) : top(-1) { }
            void push(char a)
                        {           opers[++top] = a;        }
            char pop( )
                        {           return opers[top--];      }
            char topele( )
                        {           return opers[top];         }
            bool isempty( )
            {
                        if(top == -1)                 return true;
                        else                              return false;
            }
};
///////////////////////////////// DECLARATION OF RPN () ////////////////////////////////
void RPN(char*);
/////////////////////////////////////////////////////////////////////////////////////////
void main( )
{
            char exp[100];
            cout << "Enter an INFIX expression: ";
            cin.getline(exp,100);
            RPN(exp);
            system("pause");
}
///////////////////////////////// DEFINITION OF RPN () /////////////////////////////////
void RPN(char* exp)
{
            char token, RPNexp[100] = "";
            const char * c;
            stack s;
            for(int i = 0; i < strlen(exp); i++)
            {
                        token = exp[i];
                        switch(token)
                        {
                        case ' ':            break;
                        case '(':            s.push(token);             break;
                        case ')':
                                    for(; ;)
                                    {
                                                if(s.isempty( ))
                                                            {           s.push(token);             break;              }
                                                if( s.topele( ) == '(' )
                                                            {           s.pop( );                       break;              }
                                                else
                                                            {           RPNexp[strlen(RPNexp)] = s.pop( ); }
                                    }
                                    break;
                        case '+':           case '-':            case '*':           case '/':
                                                for(; ;)
                                                {
                                                            if(s.isempty( ) || s.topele( ) == '(' || (token == '*' || token == '/') &&
                                                              (s.topele( ) == '+' || s.topele( ) == '-'))
                                                                        {           s.push(token);             break;              }
                                                            if((token == '+' || token == '-') && (s.topele( ) == '*' || s.topele( ) == '/'))
                                                                        {           RPNexp[strlen(RPNexp)] = s.pop( );             }
                                                            else
                                                                        {           RPNexp[strlen(RPNexp)] = s.pop( );             }
                                                }
                                                break;
                        default:
                                    RPNexp[strlen(RPNexp)] = token;
                                    break;
                        }
            }
            for(; ;)
            {
                        if(s.isempty( ))
                                    {           cout << RPNexp << endl;                  break;              }
                        if(s.topele( ) != '(' && s.topele( ) != ')')
                                    RPNexp[strlen(RPNexp)] = s.pop( );
                        else
                                    {           cout << "Error in INFIX expression..." << endl;          break;              }
            }
}
OUTPUT:

0 comments: