coke wrote:
Ok i figured out that i dont need a copy constructor and instead i am
popping out the used variables. if i put in ((8-2)-2) it prings out 4,
which is the correct answer but if i put in ((8-4)-(2+2)) it prints a
crazy number it prints out every step of the way and at the end it
shows ((4-4) but still prints a crazy number... also i had to convert
everything to char instead of int's so it would print correctly.
here is the code if someone could debug and show me what my problem is
i would be most appreciative.
[big piece of code snipped]
Hm, I think that your decision to use two arrays is confusing. In both
arrays some slots are not used. Those slots are uninitialized memory. The
crazy number that you see most likely comes from there. So, I venture the
conjecture that in the pushing and poping the two arrays get out of sync.
May I suggest to use just one stack. That is way more easy to maintain. This
stack would contain tokens, e.g., something like this:
struct Token {
enum Operator { plus, minus, multiplies, divides };
enum Class { num, op, lpar, rpar };
union {
int i;
Operator op;
} value;
Class type;
};
Now, you can parse the expression from left to right and translate it into a
sequence of tokens. Whenever you encounter a right parenthesis, you pop
everything from the stack down to the previous left parenthesis and replace
that subexpression by a num-token representing its value.
Best
Kai-Uwe Bux