Computation Using A Stack [Update]

I changed up a bit of my code from the last post I made of this assignment. This time, the code actually runs now but when I put in an expression e.g., "3+4-6", I keep getting either 51 or 52.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <cctype>       //isdigit(),
#include <string>       //size(), string, find_first_of
using namespace std;

class Node                                  //CLASS Node
{
private:
    int val;                                //variable for values
    char op;                                //variable for operators
    Node* next;                             //pointer which points to next node
    friend class LinkedStack;               //let class LinkedStack use variables of class Node
};

class LinkedStack                           //CLASS linkedstack
{
public:
    LinkedStack();                          //constructor
    void pushVal(const int&);               //push value
    void pushOp(const char&);               //push operator
    int popVal();                           //pop value
    char popOp();                           //pop operator
    const char& topOp();                    //returns reference of top of opStack
    const int& topVal();                    //returns reference of top of valStack
    bool isValue(const char&);              //checks if there is a value in string
    bool isOp(const char&);                 //checks for operator in string and precedence
    bool empty() const;                     //is stack empty?
private:
    Node* top;                              //points to top of stack
    Node* curr;                             //current pointer points to current node
};

//LINKEDSTACK FUNCTIONS//

LinkedStack::LinkedStack()                  //constructor
: top(NULL){};                              //init top to NULL

void LinkedStack::pushVal(const int& e)     //push value onto valStack
{
    Node* temp = new Node;                  //create new node
    temp->val = e;                          //assign value
    temp->next = top;                       //new node's next pointer points to where top ptr points to
    
    top = temp;                             //top ptr points to new node
}

void LinkedStack::pushOp(const char& e)     //push operator onto opStack
{
    Node* temp = new Node;                  //create new node
    temp->op = e;                           //assign operator
    temp->next = top;                       //new node's next ptr points to where top ptr points to
    
    top = temp;                             //top ptr points to new node
}

int LinkedStack::popVal()                   //pop value from valStack
{
    Node* old = top;                        //old ptr points to the same node as top ptr(the top)
    top = old->next;                        //top points to the same node as old's next ptr
    int e = old->val;                       //assign e with old's value
    delete old;                             //delete old ptr
    return e;                               //return value e
}

char LinkedStack::popOp()                   //pop value from opStack
{
    Node* old = top;                        //old ptr points to the same node as top ptr(the top)
    top = old->next;                        //top points to the same node as old's next ptr
    char e = old->op;                       //assign e with old's operator
    delete old;                             //delete old ptr
    return e;                               //return operator e
}

const char& LinkedStack::topOp()                 //return reference of top operator of opStack
{
    return top->op;                              //return top operator of stack
}

const int& LinkedStack::topVal()                 //return reference of top value of valStack
{
    return top->val;                             //return top value of stack
}

bool LinkedStack::isValue(const char& e)         //checks if there is a value in the string
{
    if(isdigit(e))
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool LinkedStack::isOp(const char& e)     //checks for operator precedence
{
    if((e == '*' || e == '/') && (topOp() == '*' || topOp() == '/'))        //(from string) == (from top of stack)
    {
        return true;
    }
    else if((e == '+' || e == '-') && (topOp() == '+' || topOp() == '-'))   //(from string) == (from top of stack)
    {
        return true;
    }
    else if((e == '*' || e == '/') && (topOp() == '+' || topOp() == '-'))   //(from string) > (from top of stack)
    {
        return false;
    }
    else if((e == '+' || e == '-') && (topOp() == '*' || topOp() == '/'))   //(from string) < (from top of stack)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool LinkedStack::empty() const         //return true if stack is empty, else false
{
    return top == NULL;
}


//MAIN//

int main()
{
    LinkedStack valStack;
    LinkedStack opStack;
    
    string exp;
    cout << "Enter a mathematical expression involving numbers and operators(+,-,*,/):\n";
    cin >> exp;
    
    opStack.pushOp(exp.find_first_of("+-*/"));
    
    for(int i = 0; i < exp.size(); i++)
    {
        if(valStack.isValue(exp[i]) == true)        //checks if there is a value
        {
            valStack.pushVal(exp[i]);
        }
        else if(opStack.isOp(exp[i]) == true)       //checks if there is an operator and checks precedence
        {
            int x = valStack.popVal();
            int y = valStack.popVal();
            char op = opStack.popOp();
            switch(op)
            {
                case '*': valStack.pushVal(y * x);
                    break;
                case '/': valStack.pushVal(y / x);
                    break;
                case '+': valStack.pushVal(y + x);
                    break;
                case '-': valStack.pushVal(y - x);
                    break;
                default: cout << "No operators found.\n";
            }
        }
            else
            {
                opStack.pushOp(exp[i]);
            }
     }
    
    
    cout << valStack.topVal();
    
    return 0;
}
Last edited on
> when I put in an expression e.g., "3+4-6", I keep getting either 51 or 52.
let me see if I understand, you write 3+4-6 as input, and sometimes you've got 51 as output, but other times you've got 52, when it is obvious that you should get 54.

opStack.pushOp(exp.find_first_of("+-*/")); ¿what are you trying to do here?

valStack.pushVal(exp[i]);, you are not pushing 3, but '3', whose ASCII code is 51.

1
2
3
            int x = valStack.popVal();
            int y = valStack.popVal();
            char op = opStack.popOp();
that looks like the algorithm to evaluate a postfix expresion.


> I changed up a bit of my code from the last post I made of this assignment
please don't create new threads, just reply to the old one. That way we can have the history, like knowing what the assignment is about.
Last edited on
My apologies. I'll make sure to reply to an old thread next time.

For opStack.pushOp(exp.find_first_of("+-*/"));, I'm looking for the 1st occurrence of an arithmetic operator to push onto the operator stack. Then I compare the rest of the operators in the string with that operator which is on the top of the stack.

For
1
2
3
int x = valStack.popVal();
            int y = valStack.popVal();
            char op = opStack.popOp();
, yes, I am converting from infix to postfix, computing the two popped values with the popper operator.

valStack.pushVal(exp[i]);, You're right. I totally forgot to convert from character to integer. Is it the int data type parameter in void pushVal that converts '3' to 51? How would I convert the numbers in the string to integer data type then push onto stack?
Topic archived. No new replies allowed.