#ifndef EXPR_H #define EXPR_H #include<iostream> #include<string> using namespace std; class Expr_node//抽象基类 { friend class Expr; int use; friend ostream & operator << (ostream &, const Expr_node &); protected: Expr_node() :use(1){} virtual int eval()const=0; virtual ~Expr_node(){}; public: virtual void print(ostream &)const = 0; }; class Expr//具体类 { friend class Expr_node; friend ostream& operator <<(ostream & o, const Expr &); Expr_node * p; public: Expr(int); Expr(const string & op, Expr); Expr(const string &op, Expr, Expr); Expr(const string &op, Expr, Expr,Expr); Expr(const Expr &t){ p = t.p; ++p->use; } Expr& operator=(const Expr &); ~Expr(){ if (--p->use == 0)delete p; } int eval()const { return p->eval(); } }; class Int_node :public Expr_node { friend class Expr; int n; Int_node(int k) :n(k){} void print(ostream & o)const{ o << n; } int eval()const{ return n; } }; class Unary_node :public Expr_node { friend class Expr; string op; Expr opnd; Unary_node(const string & a, Expr b) :op(a), opnd(b){} int eval()const; void print(ostream & o)const { o << "(" << op << opnd << ")"; } }; class Binary_node :public Expr_node { friend class Expr; string op; Expr left, right; Binary_node(const string & a, Expr b, Expr c) :op(a), left(b), right(c){} int eval()const; void print(ostream & o)const{ o << "(" << left << op << right << ")"; } }; class Ternary_node :public Expr_node { friend class Expr; string op; Expr left; Expr middle; Expr right; Ternary_node(const string & s, Expr l, Expr m, Expr r) :op(s), left(l), middle(m), right(r){} void print(ostream & o)const; int eval()const; }; int Binary_node::eval()const { int l = left.eval(); int r = right.eval(); if (op == "+")return l + r; if (op == "-")return l - r; if (op == "*")return l * r; if (op == "/"&&r!=0)return l / r; throw"error, bad op " + op + " int Binarynode"; } int Unary_node::eval()const { if (op == "-")return -opnd.eval(); throw"error, bad op " + op + " int Unarynode"; } Expr::Expr(int t) { p = new Int_node(t); } Expr::Expr(const string & op, Expr e) { p = new Unary_node(op, e); } Expr::Expr(const string &op, Expr d, Expr e) { p = new Binary_node(op, d, e); } Expr::Expr(const string &op, Expr a, Expr b, Expr c) { p = new Ternary_node(op, a, b, c); } Expr& Expr::operator=(const Expr &r) { r.p->use++; if (--p->use == 0)delete p; p = r.p; return *this; } ostream & operator << (ostream &o, const Expr_node &t) { t.print(o); return o; } ostream& operator <<(ostream & o, const Expr &r) { r.p->print(o); return o; } void Ternary_node::print(ostream & o)const { o << "( " << left << " ? " << middle << " : " << right << " ) "; } int Ternary_node::eval()const { if (left.eval()) return middle.eval(); else return right.eval(); } #endif
#include<iostream> #include"EXPR.h" using namespace std; int main() { Expr t = Expr("*", Expr("-", 5), Expr("+", 3, 4)); cout << t <<"="<<t.eval()<< endl; //t = Expr("*", t, t); //cout << t << "=" << t.eval() << endl; Expr m = Expr("?",-1, 99,26); cout << m << "=" << m.eval() << endl; return 0; }
原文地址:http://blog.csdn.net/shiwazone/article/details/45192479