1. 程式人生 > 其它 >frp內網穿透服務搭建

frp內網穿透服務搭建

描述
輸入一個表示式(用字串表示),求這個表示式的值。
保證字串中的有效字元包括[‘0’-‘9’],‘+’,‘-’, ‘*’,‘/’ ,‘(’, ‘)’,‘[’, ‘]’,‘{’ ,‘}’。且表示式一定合法。

示例

輸入:
3+2*{1+2*[-4/(8-6)+7]}
輸出:
25
#include<bits/stdc++.h>
using namespace std;
void compute(stack<int>& stk1,stack<char>& stk2)
{
    int b=stk1.top();
    stk1.pop();
    int a=stk1.top();
    stk1.pop();
    char op=stk2.top();
    stk2.pop();
    if(op=='+')a=a+b;
    else if(op=='-')a=a-b;
    else if(op=='*')a=a*b;
    else if(op=='/')a=a/b;
    stk1.push(a);
}
bool check(char op1,char op2)
{
    if(op1=='(')return false;
    else if((op1=='+'||op1=='-')&&(op2=='*'||op2=='/'))return false;
    return true;
}
int main()
{
    string s;
    while(cin>>s)
    {
        stack<int> stk1;
        stack<char> stk2;
        stk2.push('(');
        s+=')';
        bool flag=false;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='('||s[i]=='['||s[i]=='{')stk2.push('(');
            else if(s[i]==')'||s[i]==']'||s[i]=='}'){
                while(stk2.top()!='('){
                    compute(stk1, stk2);
                }
                stk2.pop();
            }else if(flag){
                while(check(stk2.top(), s[i])){
                    compute(stk1,stk2);
                }
                stk2.push(s[i]);
                flag=false;
            }else {
                int j=i;
                if(s[i]=='-'||s[i]=='+')i++;
                while(isdigit(s[i]))i++;
                int num=stoi(s.substr(j,i-j));
                stk1.push(num);
                i--;
                flag=true;
            }
        }
        cout<<stk1.top()<<endl;
    }
    return 0;
    
}