中缀,后缀表达式的计算以及转换

文章目录

    • 中缀表达式直接计算(带括号)
    • 中缀转后缀与后缀表达式计算(带括号)

中缀表达式直接计算(带括号)

#include<bits/stdc++.h>

using namespace std;
//中缀表达式求值

string mid;
stack<double> data_stack;
stack<char> op_stack;


int Priority(char op){
    if(op=='+'||op=='-') return 1;
    else if(op=='*'||op=='/') return 2;
    else if(op=='(') return 0;//注意
}

double Calculate(double a,double b,char op){
    if(op=='+') return a+b;
    else if(op=='-') return a-b;
    else if(op=='*') return a*b;
    else if(op=='/') return a/b;
}

double getNumber(string str,int& pos){//pos是共享的
    double ans=0.0;
    while(isdigit(str[pos])){
        ans=ans*10+(double)(str[pos++]-'0');
    }
    pos--;
    return ans;
}

//1+2*3
int main(){
    cin>>mid;
    for(int i=0;i<mid.size();i++){
        if(isdigit(mid[i])){
            //cout<<(double)(mid[i]-'0')<
            //cout<<"data "<
            double number=getNumber(mid,i);
            //cout<<"数字:"<
            data_stack.push(number);
        }
        else if(mid[i]=='('){
            op_stack.push(mid[i]);
        }
        else if(mid[i]==')'){
            while(op_stack.top()!='('){
                char op=op_stack.top();  op_stack.pop();
                double b=data_stack.top(); data_stack.pop();
                double a=data_stack.top(); data_stack.pop();
                double temp=Calculate(a,b,op);
                data_stack.push(temp);
            }
            op_stack.pop();
        }
        else{
            //cout<<"op "<
            while((!op_stack.empty())&&Priority(op_stack.top())>=Priority(mid[i])){//顺序不能变,一定是先判栈空
                char op=op_stack.top();  op_stack.pop();
                double b=data_stack.top(); data_stack.pop();
                double a=data_stack.top(); data_stack.pop();
                double temp=Calculate(a,b,op);
                //cout<
                data_stack.push(temp);
            }
            op_stack.push(mid[i]);
        }
    }
    while(!op_stack.empty()){//别忘了将op清空
        char op=op_stack.top();  op_stack.pop();
        double b=data_stack.top(); data_stack.pop();
        double a=data_stack.top(); data_stack.pop();
        double temp=Calculate(a,b,op);
        data_stack.push(temp);
    }
    cout<<data_stack.top()<<endl;
}

中缀转后缀与后缀表达式计算(带括号)

PS:后缀表达式中两两之间一定要用空格隔开,不然getNumber函数就不起作用了

#include<bits/stdc++.h>

using namespace std;
//中缀转后缀+后缀求值
string post="",mid;
stack<char> op_stack;
stack<double> data_stack;
int Priority(char op){
    if(op=='+'||op=='-') return 1;
    else if(op=='*'||op=='/') return 2;
    else if(op=='(') return 0;
}

double Calculate(double a,double b,char op){
    if(op=='+') return a+b;
    else if(op=='-') return a-b;
    else if(op=='*') return a*b;
    else if(op=='/') return a/b;
}

double getNumber(string str,int& pos){//pos是共享的
    double ans=0.0;
    while(isdigit(str[pos])){
        ans=ans*10+(double)(str[pos++]-'0');
    }
    pos--;//不然会越过一个位置
    return ans;
}

//9+(3-1)*3+10/2,转换的话不会用到getnumber得到多位的整数
void Transform(){
    for(int i=0;i<mid.size();i++){
        //cout<
        if(isdigit(mid[i])){//这里得到完整数直接写就行!!!
            while(isdigit(mid[i])){
                post=post+mid[i++];
            }
            i--;//不然会把下一个覆盖掉
            post=post+" ";
        }
        else if(mid[i]==' '){
            continue;
        }
        else if(mid[i]=='('){
            op_stack.push(mid[i]);
        }
        else if(mid[i]==')'){
            while(op_stack.top()!='('){
                char op=op_stack.top();
                op_stack.pop();
                post=post+op+" ";
            }
            op_stack.pop();//将'('pop出来
        }
        else{//为操作符
            //cout<<"操作符 "<
            if(op_stack.empty()){
                op_stack.push(mid[i]);
                continue;
            }
            else{
                //别忘了判栈空
                while(!op_stack.empty()&&Priority(op_stack.top())>=Priority(mid[i])){
                    //cout<
                    char op=op_stack.top();
                    op_stack.pop();
                    post=post+op+" ";
                }
                op_stack.push(mid[i]);
            }
        }
    }
    while(!op_stack.empty()){
        char op=op_stack.top();
        op_stack.pop();
        post=post+op;
    }
}

int  main(){
    cin>>mid;
    Transform();
    cout<<"后缀表达式:"<<post<<endl;
    for(int pos=0;pos<post.size();pos++){
        if(isdigit(post[pos])){
            double number=getNumber(post,pos);
            //cout<<"数字:"<
            data_stack.push(number);
        }
        else if(post[pos]==' ') continue;
        else{
            char op=post[pos];
            double b=data_stack.top(); data_stack.pop();
            double a=data_stack.top(); data_stack.pop();
            double temp=Calculate(a,b,op);
            //cout<<"运算后 "<
            data_stack.push(temp);
        }
    }
    cout<<"运算结果:"<<data_stack.top()<<endl;
}

你可能感兴趣的:(算法)