#include <iostream> #include <stack> #include <stdlib.h> #include <string.h> #include <stdio.h> using namespace std; stack<char> st; stack<int> stlet; int a, b, k = 0, i, n; void st_oper(char s); bool isNumber(char Symbol); int main() { string s; string s1; cout << "Enter The String For Reverse Polish Notation" << endl; getline(cin, s); for (i = 0; i < s.size(); i++) { k = i; n = 0; if (s[i] == '*') { while (st.size()) { if (st.top() == '+' || st.top() == '-' || st.top() == '(')break; st_oper(st.top()); } st.push(s[i]); } else if (s[i] == '/') { while (st.size()) { if (st.top() == '+' || st.top() == '-' || st.top() == '(')break; st_oper(st.top()); } st.push(s[i]); } else if (s[i] == '+') { while (st.size()) { if (st.top() == '(')break; st_oper(st.top()); } st.push(s[i]); } else if (s[i] == '-') { while (st.size()) { if (st.top() == '(')break; st_oper(st.top()); } st.push(s[i]); } else if (s[i] == '(') { st.push(s[i]); } else if (s[i] == ')') { while (st.size()) { if (st.top() == '(') { st.pop(); break; } st_oper(st.top()); } } else if (isNumber(s[i])) { while (isNumber(s[k])) { if (k == s.size())break; s1[n] = s[k]; n++; k++; } k--; i = k; stlet.push(atoi(s1.c_str())); } //считывает посимвольно } while (st.size())st_oper(st.top()); cout << stlet.top(); } void st_oper(char s) { if (stlet.size() > 1) { a = stlet.top(); stlet.pop(); b = stlet.top(); stlet.pop(); if (s == '+') { b += a; st.pop(); stlet.push(b); } else if (s == '-') { b -= a; st.pop(); stlet.push(b); } else if (s == '*') { b *= a; st.pop(); stlet.push(b); } else if (s == '/') { b /= a; st.pop(); stlet.push(b); } } } bool isNumber(char Symbol) { if (Symbol >= '0' && Symbol <= '9') return true; return false; } 

With single digits everything works fine, but with double digits, it doesn’t display anything, I’m dumb for a couple of hours, can you tell me a mistake?

Closed due to the fact that off-topic participants Kromster , αλεχολυτ , Kirill Stoianov , aleksandr barakin , cheops 4 Oct '16 at 17:52 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Kromster, αλεχολυτ, Kirill Stoianov, cheops
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Well, the formatting of the code ... And did you try to see what was wrong with the debugger? - pavel
  • @pavel, something is not particularly better :-) - Grundy
  • and what happens in this block? if(isNumber(s[i])) { while(isNumber(s[k])) { if(k==s.size())break;s1[n]=s[k];n++;k++; } k--;i=k;stlet.push(atoi(s1.c_str())); } if(isNumber(s[i])) { while(isNumber(s[k])) { if(k==s.size())break;s1[n]=s[k];n++;k++; } k--;i=k;stlet.push(atoi(s1.c_str())); } ? - Grundy
  • if the character is from '0' to '9', that is, a digit, then until you meet + - * /, we will go forward along the line and enter one digit in the string s1, as soon as we met NOT a digit - send, convert The resulting number in integer was pushed onto the stack (there’s a problem in this line) - GerrDott
  • Frankly, I don’t use the debugger very well, so I didn’t dare to check it. - GerrDott

1 answer 1

Firstly, isNumber replace library isdigit , and secondly, rewrite "horror flying on the wings of the night" (c), for example, like this:

 else if (isdigit(s[i])) { int val = 0; while (isdigit(s[i])) { val = val*10 + (s[i] - '0'); ++i; } --i; stlet.push(val); } 
  • Fuh, thanks for the decision, I suffered a lot) - GerrDott
  • Well, if it suits you - click the "bird" to the left of the answer. - Harry