四则运算表达式处理


//#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;

const int N = 100;

bool isnum(char c){
    if(c>='0' && c<='9')
        return true;
    return false;
}
bool priority_op(char a, char p){
    if( (p=='*'||p=='/') && (a=='+'||a=='-') )
        return true;
    return false;
}
bool same_op(char a, char p){
    if( (p=='*'||p=='/') && (a=='/'||a=='*') )
        return true;
    if( (p=='+'||p=='-') && (a=='-'||a=='+') )
        return true;
    return false;
}

stack<char> st;
stack<int> st2;
char s[N];
struct Node{
    int val;
    char op;
}temp[N];

int main()
{
    while(~scanf("%s",s)){
        //中缀表达式 转 后缀表达式
        while(!st.empty()) st.pop();
        int len = strlen(s);
        int k = 0,tmp = 0,op;
        for(int i=0; i<len; i++){
            if(isnum(s[i])){
                tmp = tmp*10+s[i]-'0';
                op = 1;
            }
            else{
                if(op){
                    temp[k].op = '?';
                    temp[k++].val = tmp;
                    tmp = 0;
                    op = 0;
                }
                if(s[i]==')'){
                    while( !st.empty() && st.top()!='(' ){
                        temp[k++].op = st.top();
                        st.pop();
                    }
                    st.pop();
                }
                else if(s[i]=='(')
                    st.push(s[i]);
                else{
                    while( !st.empty() && (same_op(st.top(),s[i])||(priority_op(s[i],st.top()))) ){
                        temp[k++].op = st.top();
                        st.pop();
                    }
                    st.push(s[i]);
                }
            }
        }
        temp[k].op = '?';
        temp[k++].val = tmp;
        while(!st.empty()){
            temp[k++].op = st.top();
            st.pop();
        }
//        for(int i=0; i<k; i++)
//            printf("%d %c\n",temp[i].val,temp[i].op);

        // 后缀表达式求值
        while(!st2.empty()) st2.pop();
        for(int i=0; i<k; i++){
            if(temp[i].op=='?')
                st2.push(temp[i].val);
            else{
                int t1 = st2.top();
                st2.pop();
                int t2 = st2.top();
                st2.pop();
                if(temp[i].op=='+')
                    st2.push(t2+t1);
                else if(temp[i].op=='-')
                    st2.push(t2-t1);
                else if(temp[i].op=='*')
                    st2.push(t2*t1);
                else if(temp[i].op=='/')
                    st2.push(t2/t1);
            }
        }
        printf("%d\n",st2.top());
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值