### Stacks and Precedence

Im having trouble wrapping my head around this concept. My current mindset is have a string to read in the variables. If its a letter it goes onto one stack and if is a operator it goes onto another stack. I want to take A+B-C*D and turn it into :
*CD
+AB
2-1 (where 1 is a+b and 2 is C*D)

I have an outline but im not sure what to do about "(" and ")" either.

Im not asking for the whole thing to make made out for me(which would be somewhat helpful because I have a harder version of this to do after this), all I'm askin is for hints or fixes to my code that can help me get to a point that I can fix it on my own at the very least.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384`` ``````/*Write an object oriented C++ program to translate a C++ assignment statement into triplet form. You may assume that variables are single characters. Use a precedence table to decide if operators get pushed or popped. Be sure to give clear directions so that a person unfamiliar with this process will understand how to execute your solution.*/ #include #include #include using namespace std; class tripletform { public: void process(const string& x); void precedence(const string& x); private: stack letters; stack operators; }; void tripletform::precedence(const string& x) { } void tripletform::process(const string& x) { //int count = x.length(); for(int i = x.length(); i > -1; i--) { if (i <= 'A' || i >= 'z'){ letters.push(x[i]); } } for(int j = x.length(); j > -1; j--) { if(j == '('){ operators.push(x[j]); } if (j == '='){ operators.push(x[j]); } if(j == '*' || j == '/') { operators.push(x[j]); } if (j == '+' || j == '-' ) { operators.push(x[j]); } if(j == ')'){ operators.push(x[j]); } } while(!letters.empty()){ cout << letters.top(); letters.pop(); } while(!operators.empty()){ cout << operators.top(); operators.pop(); } } int main () { string x; getline(cin, x); tripletform test; test.process(x); cin >> x; }``````
Topic archived. No new replies allowed.