C++语言程序设计

| 例11-36c

class PowExpr : public ArithExpr {
private:
ArithExpr* e1;
ArithExpr* e2;
public:
PowExpr(ArithExpr* base, ArithExpr* exp): e1(base), e2(exp) {}
~PowExpr() { delete e1; delete e2; }
virtual double valueAt(double v)
{ return pow(e1->valueAt(v), e2->valueAt(v)); }
};

typedef double (*UnaryFunction)(double);
// Pointer to function that takes one arg of type double
// and returns a value of type double

class FunExpr: public ArithExpr {
private:
double (*f)(double);
ArithExpr* a;
public:
FunExpr(UnaryFunction fn, ArithExpr* arg): f(fn),a(arg) {}
~FunExpr() { delete a; }
virtual double valueAt(double v) { return f(a->valueAt(v)); }
};
#endif

输入Menu:A(dd),D(elete),S(ort),Q(uit)
然后由用户输入A,D,S,Q
然后再相应的执行程序