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";