Hello everyone

i'am tring to write a program that convert expression from infix to postfix using stack.

but i have a problem with my code it terminate the program i can't determine what is wrong but it seems to be in the (precedence) function . so can anyone help me plz;


her is my code :

#include <iostream>
#include <assert.h>
#include <string>
using namespace std;

const int maxSize = 1000;
typedef char StackElementType;

class Stack {
public:
   Stack();
   void push(char x);
   char pop();
   char top();
   bool isEmpty();
   int stackArray[maxSize]; 
   int topIndex;
};

Stack::Stack()
{
   topIndex = -1;
}

void Stack::push(char x)
{
   ++topIndex;
 
   assert(topIndex < maxSize);
   stackArray[topIndex] = x;
}

char Stack::pop()
{

   assert(topIndex >= 0);
   int returnIndex(topIndex);
   --topIndex;
   return stackArray[returnIndex];
}

char Stack::top()
{
  
   assert(topIndex >= 0);
   return stackArray[topIndex];
}

bool Stack::isEmpty()
{
   return bool(topIndex == -1);
}

///////////////////////////////////////////////////////////////////////////////////////////
int precedence(char symbol)           
{ 
        if(symbol == '+' || symbol == '-') 
                return 1; 
        else if(symbol == '*' || symbol == '/') 
                return 2; 
        else 
                return -1; 
}


//////////////////////////////////////////////////////////////////////////////////////////
int main () {
     Stack s;
    
    int y;

    string exp ;
    
    cout <<"Enter the expression you want to convert\n";
    cin >> exp;
    
    
  
            char ch;
            string     postfixExpression = "";
          for (int i=0;i<exp.size();i++){
               ch= exp[i];
               
               if(ch == '+' || ch == '-' || ch == '*' || ch == '/')  
                {
                          while(s.top() != '(' && precedence(s.top()) >= precedence(ch) && !s.isEmpty()) 
                                          { 
                                        postfixExpression += s.pop();             
                                           }  
                                s.push(ch);
                }
         
         else if(ch == '(') 
                              {  s.push(ch); }
         
         else if (ch == ')')
         {
                while(s.top() != '(') 
                                { 
                                        postfixExpression += s.pop();            
                                } 
                                s.pop();                       
         
         else  
         {
              postfixExpression += ch;
              }
            
            }
            
                   
                   while(!s.isEmpty()) 
                { 
                        postfixExpression += s.pop();                           
                } 
                
                cout <<"postfix :" <<postfixExpression << endl;
                
                
         
            cin>>y;
            return 0;
    }

i can define my proble as when i write an expression like 2+3*(4-2) and press enter the windows automatically closed.

Member Avatar for iamthwee

That doesn't even compile?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.