C++ Infix to Postfix Stack Vector

  arrays, c++, stack

I have tried different approaches to this using the files that my instructor provided but The one I have listed in this post is the best approach to me. But It just outputs a core dumped. Where did it go wrong?

My Postfix Calculator works correctly so Its not that.

All the files are in this online gdb: https://onlinegdb.com/B1JiSU8Bu

Online gdb does not run make file so copy it and run it on your own ide.

I am doing this program based off of the algorithm for the infix to postfix.

for ( each character ch in the infix expression) 
{ 
switch (ch) 
 { 
case operand: // Append operand to end of postfix expression—step 1 
 postfixExp = postfixExp • ch 
break
case '(': // Save '(' on stack—step 2 
 aStack.push(ch) 
break
case operator: // Process stack operators of greater precedence—step 3 
while (!aStack.isEmpty() and aStack.peek() is not a '(' and
 precedence(ch) <= precedence(aStack.peek())) 
 { 
Append aStack.peek() to the end of postfixExp 
 aStack.pop() 
 } 
 aStack.push(ch) // Save the operator 
break
case ')': // Pop stack until matching '(' —step 4 
while (aStack.peek() is not a '(') 
 { 
Append aStack.peek() to the end of postfixExp 
 aStack.pop() 
 } 
 aStack.pop() // Remove the open parenthesis 
break
 } 
} 
// Append to postfixExp the operators remaining in the stack—step 5 
while (!aStack.isEmpty()) 
{ 
Append aStack.peek() to the end of postfixExp 
 aStack.pop() 
} 

Outputs: Segmentation fault (core dumped)

/**
 * @file InfixCalculator.cpp
 */
#include <sstream>
#include <cstdlib>
#include <stack>
#include <stdexcept>
#include <string>

#include "InfixCalculator.h"
using namespace std;

int opOrder(Token tok){
  switch (tok.o)
        {
                case  ADD : case  SUBTRACT : return 1;
                case MULTIPLY : case  DIVIDE : return 2;
                default : return 0;
        }
}

std::vector<Token> InfixCalculator::infixToPostfix(std::vector<Token> tokens) {
  //TODO: convert infix to postfix
  std::stack<Token>s;
  std::vector<Token>postfix;

  for (unsigned int i = 0; i < tokens.size(); i++){ //for ( each character ch in the infix expression)
          switch (tokens[i].tag){ //switch(ch)
                  case OPERAND: //// Append operand to end of postfix expression—step 1
                  postfix.push_back(tokens[i]); //postfixExp = postfix * ch
                  break; //break;

                  case LPAREN: // Save '(' on stack—step 2
                  s.push(tokens[i]); // aStack.push(ch)
                  break; //break;

                  case OPERATOR: // Process stack operators of greater precedence—step 3

                  while(!s.empty() && tokens[i].tag != LPAREN && opOrder(tokens[i])<= opOrder(s.top())){
                    postfix.push_back(s.top()); //Append aStack.peek() to the end of postfixExp
                    s.pop(); //pop
                  }
                  s.push(tokens[i]); //save the operator
                  break;

                  case RPAREN:
                  while(tokens[i].tag != LPAREN){ // Pop stack until matching '(' —step
                    postfix.push_back(s.top());
                    s.pop();
                  }
                  s.pop(); //remove the open parenthesis
                  break;
              }
            }
          // Append to postfixExp the operators remaining in the stack—step 5
          while (!s.empty()){
                postfix.push_back(s.top());
                s.pop();
          }

  return tokens;
}

double InfixCalculator::eval(std::vector<Token> tokens) {
    return pc.eval(infixToPostfix(tokens));
}

Source: Windows Questions C++

LEAVE A COMMENT