0

I'm having trouble creating a program that will convert infix to postfix. My code is as follows:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

#define DEFAULT_SIZE 20

/*
*
*/

class Stack {
    char *arr;
    int tos, capacity;
public:
    //Constructors
    Stack();
    Stack(int size);

    //Destructor
    ~Stack();

    //Methods
    void push(char a);
    char pop();
    int get_size();
    bool is_empty();
    bool is_full();
    void display();
    char get_top();


};


Stack::Stack() {
    arr = new char[DEFAULT_SIZE];
    tos = 0;
    capacity = DEFAULT_SIZE;
}

Stack::Stack(int size) {
    arr = new char[size];
    tos = 0;
    capacity = size;
}

Stack::~Stack() {
    delete[] arr;
}

void Stack::push(char a) {
    if (!is_full())
        arr[tos++] = a;
    else
        cout << "Sorry, the stack is full. Push failed!" << endl;

}
char Stack::pop() {
    if (!is_empty())
        return arr[--tos];
    else {
        cout << "Sorry, the stack is empty. Pop failed!" << endl;
        return -1;
    }


}


char Stack::get_top() {
    if (!is_empty())
        return arr[tos - 1];
    else {
        cout << "Sorry, the stack is empty. Pop failed!" << endl;
        return 'E';
    }
}


int Stack::get_size() {
    return tos;

}
bool Stack::is_empty() {
    if (tos == 0)
        return true;
    else
        return false;


}
bool Stack::is_full() {
    if (tos == capacity)
        return true;
    else
        return false;

}

void Stack::display() {
    if (tos == 0)
        cout << "The stack is empty" << endl;
    else {
        for (int i = 0; i<tos;i++)
            cout << arr[i] << " ";
        cout << endl;
    }


}

int main() {
    Stack stack(50);
    string infix = "(1+3)*2/(6-4)^2";
    stringstream ss;


    for (char c : infix) {
        if ('0' <= c && c <= '9') {
            ss << c;
        }
        else if (c == '(') {
            continue;

        }
        else if (c == ')') {
            ss << stack.pop();
            stack.pop();
        }

        else if (c == '^' || c == '*' || c == '/' || c == '+' || c == '-') {
            stack.push(c);
        }

    }

    string postfix = ss.str();
    cout << postfix;

I know what my issue is, I just dont understand or comprehend how to solve it. This code currently outputs 13+264-2. It needs to output 13+2*64-2^/. I know my issues is with my last else if statement in int main(). I dont understand how to rearrange the operators behind the operands.

Anything in parentheses is passed into the stream correctly, because I can wait until the closing parenthesis is hit to add in the operator. I can't visualize how to make that work for things not in parentheses though. Can anyone offer any advice?

J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
CabooseMSG
  • 51
  • 10
  • 1
    Why do you pop twice when you see closing paren? You discard every other operator this way. You should also see some "pop failed" messages. – Igor Tandetnik Jul 19 '16 at 01:36
  • Obvious [rule of three](http://stackoverflow.com/a/4172724/636019) violation. – ildjarn Jul 19 '16 at 07:39
  • @ildjarn Where? The only class object here is the stack, and it isn't being copied anywhere. – user207421 Jul 12 '19 at 02:46
  • @user207421 : I didn't say this example usage was falling afoul of said violation, only that the class is implemented wrong. – ildjarn Jul 12 '19 at 08:18

0 Answers0