C++四則運算解析器

// design by novus 2009
// some right reserved
#include <iostream>
#include <cstdlib>

using namespace std;

double Eval2(istream& iss) {
    double Eval0(istream& iss);
    double res=0;
    if (iss.peek() == '(' && iss.get()) {
        res = Eval0(iss);
        iss.peek() == ')' && iss.get();
    } else { iss >> res; }
    return res;
}

double Eval1(istream& iss) {
    double res = Eval2(iss);
    while (iss.peek() == '*' || iss.peek() == '/') 
        (iss.get() == '*')? (res*=Eval2(iss)): (res/=Eval2(iss));
    return res;
}

double Eval0(istream& iss) { 
    double res = Eval1(iss);
    while (iss.peek() == '+' || iss.peek() == '-')
        res +=(iss.get() == '+')? Eval1(iss): -Eval1(iss);
    return res;
}

int main() {
    cout << Eval0(cin) << endl;
    system("pause");
}

http://novus.pixnet.net/blog/post/20243253-c%2B%2B四則運算解析器

来源: http://novus.pixnet.net/blog/post/20243253-c%2B%2B四則運算解析器

posted @ 2018-10-12 20:22  clq.lib  阅读(187)  评论(0编辑  收藏  举报