How would I make it to where this program doesnt get messed up by parenthesis cu
ID: 3531908 • Letter: H
Question
How would I make it to where this program doesnt get messed up by parenthesis currently if you enter something in infix form with parenthesis it doesnt work properly. Please use same code simply edit to make it work with parenthesis #include <iostream> #include <sstream> #include <stack> #include <limits> #include <string> using namespace std; int priority(char a) { int temp; if (a == '*' || a == '/' || a == '%') temp = 2; else if (a == '+' || a == '-') temp = 1; return temp; } //start int main() { //declare a string called "infix" string infix; stringstream output; stack<char> s1, s2; cout << "Enter an arithmetic expression with no perenthesis: " << endl; getline(cin, infix); //this loops through backwards searching for the operators for(int i = infix.length() - 1; i >= 0; i--) { //check the input against +,-,/,*,% if (infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/' || infix[i] == '%') { while(!s1.empty() && priority(s1.top()) > priority(infix[i])) { output << s1.top(); s2.push(s1.top()); s1.pop(); } s1.push(infix[i]); } //I think i need to add an else if to check for parenthesis, not sure how else { output << infix[i]; s2.push(infix[i]); } } while(!s1.empty()) { output << s1.top(); s2.push(s1.top()); s1.pop(); } cout << " Answer: "; while(!s2.empty()) { cout << s2.top(); s2.pop(); } cout <<" Press enter to exit" << endl; }
Explanation / Answer
Please rate my answer....Heres your program:
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.