这题要分词!

P1449 后缀表达式

Alextokc @ 2016-12-20 20:11:34

完全就是一道分词模拟题。

表不满。。。


by Alextokc @ 2016-12-21 13:41:14

#include <bits/stdc++.h>
using namespace std;
typedef long long int64;
stack<int> s;
vector<char> cun;
vector<string> shai;
inline int64 stoi(string n){ int64 s = 0;if (n[0]=='-')
{for ( int64 i=1;i<n.size();++i)s = s * 10 + (n[i]-'0');
s = -s;return s; }else { for (int64 i=0;i<n.size();++i)
s = s * 10 + (n[i]-'0');return s; } }
int main(){
    char n;
    string sum , x;
    int a, b;
    while (cin >> n){
        if (n=='@') break;
        cun.push_back(n);
    }
    for (int i=0;i<cun.size();++i){
        sum.push_back(cun[i]);
        if (cun[i]=='+') shai.push_back("+") , sum.clear();
        else if (cun[i]=='-') shai.push_back("-") , sum.clear();
        else if (cun[i]=='*') shai.push_back("*") , sum.clear();
        else if (cun[i]=='/') shai.push_back("/") , sum.clear();
        else if (cun[i] == '.') shai.push_back(sum) , sum.clear();
    }
    for (int i=0;i<shai.size();++i) 
        if (shai[i][shai[i].size()-1]=='.') shai[i] = shai[i].substr(0,shai[i].size()-1);
    for (int i=0;i<shai.size();++i){
        x=shai[i];
        if (x[0]=='+'){
            b=s.top();s.pop();
            a=s.top();s.pop();
            s.push(a+b);
        }
        else if (x[0]=='-'){
            b=s.top();s.pop();
            a=s.top();s.pop();
            s.push(a-b);
        }
        else if (x[0]=='*'){
            b=s.top();s.pop();
            a=s.top();s.pop();
            s.push(a*b);
        }
        else if (x[0]=='/'){
            b=s.top();s.pop();
            a=s.top();s.pop();
            s.push(a/b);
        }
        else s.push(stoi(x.c_str()));
    }
    cout << s.top() << endl;
    return 0;
}

by Alextokc @ 2016-12-21 13:42:31

良心的分词:

//ac 代码(真的想贡献给大家,不要误会)
while (cin >> n){
        if (n=='@') break;
        cun.push_back(n);
    }
    for (int i=0;i<cun.size();++i){
        sum.push_back(cun[i]);
        if (cun[i]=='+') shai.push_back("+") , sum.clear();
        else if (cun[i]=='-') shai.push_back("-") , sum.clear();
        else if (cun[i]=='*') shai.push_back("*") , sum.clear();
        else if (cun[i]=='/') shai.push_back("/") , sum.clear();
        else if (cun[i] == '.') shai.push_back(sum) , sum.clear();
    }
    for (int i=0;i<shai.size();++i) 
if (shai[i][shai[i].size()-1]=='.') shai[i] = shai[i].substr(0,shai[i].size()-1);

by y_kx_b @ 2022-08-22 11:33:30

tlqtj 烤谷


|