Evaluation of four expressions

Posted by jokerfool on Sun, 05 Apr 2020 05:35:34 +0200

Convert infix expression to suffix expression

Rules: traverse each number and symbol of infix expression from left to right. If the number is output, it will become a part of suffix expression. If the symbol is output, it will judge the priority of it and stack top symbol. If the right bracket is output, the stack element above the left bracket will be output. If the left bracket is added or subtracted, it will see whether there is multiplication or division at the top of stack. If there is, it needs to be multiplied or subtracted out of stack, and then add or subtracted or left bracket Stack.

Example: 9 + (3-1) × 3 + 10 / 2
The suffix expression is: 9 3 1 - 3 * 10 2 / ++

Calculate the suffix expression to get the result

Rules: traverse each number and symbol of suffix expression from left to right. If it is a number, it will enter the stack. If it is a symbol, it will exit the two numbers at the top of the stack for operation. The operation result will enter the stack until the result is obtained.

I have forgotten that there may be three or more digits in this program. Maybe I need to add a while() and change it again when I have time.

#include <iostream>
#include <stack>
#include <string>
#include <sstream>
using namespace std;

stack<char>s1;
stack<int>s2;
string target;
string t;

bool det(char a){
    if(s1.empty())
        return false;
    else{
        if((s1.top()=='*'||s1.top()=='/') && (a=='+' || a == '-' || a=='('))
            return true;
        else return false;
    }
}
void cal1(string tar){
    int flag=0;
    for(int i=0;i<tar.size();i++){
        if(flag == 1){
            flag = !flag;
            continue;
        }
        if(isdigit(tar[i]) && isdigit(tar[i+1])){
            t +=tar[i];
            t +=tar[i+1];
            t+= ' ';
            flag = !flag;
            continue;
        }
        if(isdigit(tar[i])){
            t+=tar[i];t+=' ';}
        else if(tar[i] == '*' || tar[i] == '/')
            s1.push(tar[i]);
        else if(tar[i]=='(' || tar[i] == '+' || tar[i] == '-'){
            if(det(tar[i]))
                while(s1.top()=='*' ||s1.top()=='/'){
                    t+=s1.top();
                    t+=' ';
                    s1.pop();
                }
            s1.push(tar[i]);
        }
        else if(tar[i] == ')'){
            while(s1.top()!='('){
                t+=s1.top();
                t+=' ';
                s1.pop();
            }
            s1.pop();
        }
    }
    while(!s1.empty()){
        t+=s1.top();
        t+=' ';
        s1.pop();
    }
}
//Traverse each number and symbol of suffix expression from left to right. If it is a number, it will enter the stack. If it is a symbol, it will exit the two numbers at the top of the stack for operation. The operation result will enter the stack until the result is obtained.
int cal3(int a,int b,char c){
    if(c == '+')
        return a+b;
    else if(c == '-')
        return a-b;
    else if(c== '*')
        return a*b;
    else
        return a/b;
}
int cal2(){
    int a,b;
    string temp;
    stringstream ss;
    int flag = 0;

    for(int i=0;i<t.size();i++){
        if(flag == 1){
            flag = !flag;
            continue;
        }
        if(t[i]==' ')
            continue;
        if(isdigit(t[i]) && isdigit(t[i+1])){
            temp+=t[i];
            temp+=t[i+1];
            ss << temp;
            ss >> a;
            s2.push(a);
            flag = !flag;
            continue;
        }
        if(isdigit(t[i])){
            s2.push(t[i]-'0');
        }
        else{
            a = s2.top();
            s2.pop();
            b = s2.top();
            s2.pop();
            a = cal3(b,a,t[i]);
            s2.push(a);
        }
    }
    return s2.top();
}

int main(){
    cin >> target;
    cal1(target);
    cout << cal2() << endl;
}