0

I need to sum up or multiplie(it depends on which operation i push in array) first two numbers in stack. If I push operation + and my first two numbers are one and two then my stack value where I pushed + must be 3 , but in my result i get some symbol.

    #include<iostream>

    using namespace std;

    #define MAX 10
    int sp=-1;
    char stack[MAX];

    void push(){
        if(sp==MAX-1){
            cout<<"Error: stack overflow"<<endl;
            return;
        }
        else{
            char x;
            cout<<"Insert value in stack: ";
            cin>>x;
            if(x=='*'){
                if(sp>=1){
                    stack[++sp]=stack[1]*stack[0];
                    return;
                }
                else {
                    stack[++sp]=x;
                    return;
                }
            }
            else if(x=='+'){
                if(sp>=1){
                    stack[++sp]=stack[0]+stack[1];
                    return;
                }
                else {
                    stack[++sp]=x;
                    return;
                }   
            }
            else stack[++sp]=x;
        }
    }

    void pop(){
        if(sp==-1){
            cout<<"Error: Stack empty";
            return;
        }
        else{
            cout<<stack[sp]<<endl;
            sp--;
        }
    }

    void top(){
        if(sp==-1){
            cout<<"Error: Stack empty";
            return;
        }
        else{
            cout<<stack[sp]<<endl;
        }
    }

    void isEmpty(){
        if(sp==-1){
            cout<<"Stack is empty"<<endl;
            return;
        }
        else{
            cout<<"Stack is not empty"<<endl;
        }
    }


    int main(){
        for(int i=0;i<8;i++){
            push();
        }
        top();
        return 0;
    }

Original stack: 3 4 3 + 1 2 * * Stack that I need to get: 3 4 3 7 1 2 12 12 Stack that I get: 3 4 3 \ 1 2 _ _ (let's say something like that)

jtroha
  • 41
  • 5

1 Answers1

0

So you wanted to implement some postfix calculator. Very good that you noticed that a stack is needed.

For calculators you normally parse the input, store some results on a stack and then operate on the stack elements.

In contrast to what timo said, a parse stack can never be implemented with a std::stack because several top stack elements must be matched against the production in the grammar. For that reason a std::vector is the ideal container.

Now to the problem in the program. OP tries to store integer values and operators like "+" and "*" and results of calculations in the same datatype char. That cannot work. char stores characters like '+' or '*' or '1' but not 1. Internally a character is also encoded as a number (for example using ASCII) and a '1' would be equal to 49. And, if you look at results you cannot store '12' (for all the pros. Yes of course I know) in a char. 12 is a number, not a character.

And if you mix these types and do calculations with that then you get wrong results.

The solutions is to use Tokens and implement them as a std::variant. The variant can have a char an an int. Then your parse stack should be a std:::vector of Token.

This the right approach.

Additionally. In all standard recursive descent parsers you do not operate always on elements 0 and 1. You would replace the used values with the newly calculated one.

There is always the same approach

  1. Read next Token and shift it on a stack
  2. Match the top elements of the stack against a production
  3. Reduce. Replace used elements with result

Some example of a different parser: See here.

This is somehow complicated. You may want to read about formal languages and parser.

A M
  • 14,694
  • 5
  • 19
  • 44