The problem is in a string calculator, I read numbers from a string character by character and write them into an array, BUT when a two-digit number is found in a string, it is naturally written with different indices rather than a single number.

What happens in the code? Reads a string and converts the expression to a postfix view, then it performs the evaluation.

int prior (char s) { int a = 0; switch (s) { case '*': a = 3; break; case '/': a = 3; break; case '-': a = 2; break; case '+': a = 2; break; case '(': a = 1; break; } return a; } void st_machine (string text) { stack<char> st; stack<int>val_stack; string a; int outputi = 0; char outstr[20]; int point = 0, n1=0, n2=0, res; int p = 0; while (p < text.length()) { char curr = text.at(p); if (isdigit(curr)){ outstr[point++] = curr; } if (curr == '+' || curr == '-' || curr == '/' || curr == '*'){ if (st.empty()){ st.push(curr); } else { if (prior(st.top()) < prior(curr)){ st.push(curr); } else { while (prior(st.top()) >= prior(curr)){ outstr[point++] = st.top(); st.pop(); } st.push(curr); } } } if (curr == '('){ st.push(curr); } if (curr == ')'){ while (st.top() != '('){ outstr[point++] = st.top(); st.pop(); } if (st.top() == '('){ st.pop(); } } p++; } while (st.empty() != true) { outstr[point++] = st.top(); st.pop(); } //a = outstr; point = 0; for (int i = 0; i<strlen(outstr); i++){ //cout<<outstr[i]; if (isdigit(outstr[i])){ val_stack.push((int)outstr[i]-48); } else { n2 = val_stack.top(); val_stack.pop(); n1 = val_stack.top(); val_stack.pop(); switch (outstr[i]){ case '+': res = n1+n2; break; case '-': res = n1-n2; break; case '*': res = n1*n2; break; case '/': res = n1/n2; break; } val_stack.push(res); } } outputi = val_stack.top(); cout<<"output: " << outputi << "\n"; } 

In the text, I pass the string string f = "(8+ (6/2 + 10) * (3 + 1)) / 2";

    2 answers 2

    Combine the numbers in a number (if I was not mistaken in the order of numbers):

     if (isdigit(outstr[i])){ int t = (int)outstr[i]-48; while (isdigit(outstr[++i]) t = t * 10 + (int)outstr[i]-48; val_stack.push(t); 

    But in general - outstr actually plays the role of a backup stack - so it’s better to have one stack instead of outstr and val_stack, into which structures containing either a number, or an operation, and a sign - this number or operation is put. To form numbers according to the principle that I wrote, only to take data from the input string

    • outstr = 862/10 + 31 + * + 2 / if you combine the numbers, the number 862 is put on the stack, but you need to have it separately, and 10 together you need so that the outstr '10' is in the same index, but now it turns out [4] = 1 [5] = 0 - Lil Exi
    • while (p < text.length()) { char curr = text.at(p); if (isdigit(curr)){ outstr[point++] = curr; } while (p < text.length()) { char curr = text.at(p); if (isdigit(curr)){ outstr[point++] = curr; } you need to somehow read the string not character by character, but the whole number, it is possible to convert the input string to a string with spaces and how to use it - Lil Exi

    Decide if anyone needs it.

     #include <stack> #include <stdlib.h> #include <string.h> #include <iostream> using namespace std; int prior (char s) { int a = 0; switch (s) { case '*': a = 3; break; case '/': a = 3; break; case '-': a = 2; break; case '+': a = 2; break; case '(': a = 1; break; } return a; } void st_machine (string text) { stack<char> st; stack<int> val_stack; //string a; string tmp = "", numbers = ""; int count = 0, pp = 0, temp = 0, outputi = 0, point = 0, n1=0, n2=0, res, p = 0, i = 0; p = 0; while (p < text.length()){ tmp = ""; while (isdigit(text[p])) { tmp += text[p]; count++; p++; } if (count > 0) { numbers += tmp; numbers += " "; p += count; temp = count; count = 0; p -= temp; } if (text[p] == '+' || text[p] == '-' || text[p] == '/' || text[p] == '*'){ if (st.empty()){ st.push(text[p]); } else { if (prior(st.top()) < prior(text[p])){ st.push(text[p]); } else { while (prior(st.top()) >= prior(text[p])){ numbers += st.top(); st.pop(); numbers += " "; } st.push(text[p]); } } } if ( text[p] == '('){ st.push( text[p]); } if ( text[p] == ')'){ while (st.top() != '('){ numbers += st.top(); st.pop(); numbers += " "; } if (st.top() == '('){ st.pop(); } } p++; } while (st.empty() != true) { numbers += st.top(); st.pop(); numbers += " "; } // for (i = 0; i<numbers.length(); i++){ tmp = ""; pp = i; point = 0; while (numbers[pp] != ' '){ tmp += numbers[pp]; pp++; point++; } switch (point){ case 2: i+=1; break; case 3: i+=2; break; case 4: i+=3; break; } if (isdigit(tmp[0])){ val_stack.push(atoi(tmp.c_str())); } else if (tmp[0] == '/' || tmp[0] == '+' || tmp[0] == '-' || tmp[0] == '*') { n2 = val_stack.top(); val_stack.pop(); n1 = val_stack.top(); val_stack.pop(); switch (numbers[i]){ case '+': res = n1+n2; break; case '-': res = n1-n2; break; case '*': res = n1*n2; break; case '/': res = n1/n2; break; } val_stack.push(res); } } outputi = val_stack.top(); cout<< outputi << "\n"; }