expressions.hh
1 #ifndef EXPRESSIONS_HH 2 #define EXPRESSIONS_HH 3 4 #include "environment.hh" 5 #include <string> 6 #include <stdexcept> 7 #include <cassert> 8 #include <cmath> 9 10 using namespace std; 11 12 class Expression 13 { 14 public: 15 Expression() {} 16 virtual double evaluate(const Environment &env) const = 0; 17 virtual ~Expression() {} 18 }; 19 20 class Value : public Expression 21 { 22 double data_value; 23 public: 24 Value(double data):data_value(data) {} 25 //~Value(){} 26 double evaluate(const Environment &env) const 27 { 28 return data_value; 29 } 30 }; 31 32 class Symbol : public Expression 33 { 34 string name_of_symbol; 35 public: 36 Symbol(const string &str):name_of_symbol(str) {} 37 //~Symbol(){} 38 string accessor_name_of_symbol() const 39 { 40 return name_of_symbol; 41 } 42 double evaluate(const Environment &env) const 43 { 44 return env.getSymbolValue(name_of_symbol); 45 } 46 }; 47 48 class BinaryOperator : public Expression 49 { 50 protected: 51 Expression *pLHS; 52 Expression *pRHS; 53 public: 54 BinaryOperator(Expression *pLHS, Expression *pRHS):pLHS(pLHS),pRHS(pRHS) 55 { 56 assert(pLHS != 0); 57 assert(pRHS != 0); 58 } 59 virtual ~BinaryOperator() 60 { 61 delete pLHS; 62 delete pRHS; 63 } 64 virtual double evaluate(const Environment &env) const = 0; 65 const Expression* accessor_of_left() const 66 { 67 return pLHS; 68 } 69 const Expression* accessor_of_right() const 70 { 71 return pRHS; 72 } 73 74 }; 75 76 77 class AddOper : public BinaryOperator 78 { 79 public: 80 AddOper(Expression *pLHS, Expression *pRHS):BinaryOperator(pLHS, pRHS) {} 81 ~AddOper() { } 82 double evaluate(const Environment &env)const 83 { 84 return pLHS->evaluate(env) + pRHS->evaluate(env); 85 } 86 }; 87 88 class SubOper : public BinaryOperator 89 { 90 public: 91 SubOper(Expression *pLHS, Expression *pRHS):BinaryOperator(pLHS, pRHS) {} 92 //~SubOper() { } 93 double evaluate(const Environment &env) const 94 { 95 return pLHS->evaluate(env) - pRHS->evaluate(env); 96 } 97 }; 98 99 class MulOper : public BinaryOperator 100 { 101 public: 102 MulOper(Expression *pLHS, Expression *pRHS):BinaryOperator(pLHS, pRHS) { } 103 //~MulOper() { } 104 double evaluate(const Environment &env) const 105 { 106 return pLHS->evaluate(env) * pRHS->evaluate(env); 107 } 108 }; 109 110 class DivOper : public BinaryOperator 111 { 112 public: 113 DivOper(Expression *pLHS, Expression *pRHS) : BinaryOperator(pLHS, pRHS) {} 114 //~DivOper() { } 115 double evaluate(const Environment &env) const 116 { 117 if(pRHS->evaluate(env) == 0) 118 { 119 throw runtime_error("除数为零"); 120 } 121 122 return pLHS->evaluate(env) / pRHS->evaluate(env); 123 } 124 }; 125 126 class ExpOper : public BinaryOperator 127 { 128 public: 129 ExpOper(Expression *pLHS, Expression *pRHS) : BinaryOperator(pLHS, pRHS) {} 130 double evaluate(const Environment &env) const 131 { 132 return pow(pLHS->evaluate(env), pRHS->evaluate(env)); 133 } 134 }; 135 136 class UnaryOperator : public Expression 137 { 138 protected: 139 Expression *pCHILD; 140 public: 141 UnaryOperator(Expression *pCHILD) : pCHILD(pCHILD) 142 { 143 assert(pCHILD != 0); 144 } 145 virtual ~UnaryOperator() 146 { 147 delete pCHILD; 148 } 149 150 //virtual double evaluate(const Environment &env) const = 0; 151 const Expression* accessor_of_child() const 152 { 153 return pCHILD; 154 } 155 }; 156 157 class NegOper : public UnaryOperator 158 { 159 public: 160 NegOper(Expression *pCHILD) : UnaryOperator(pCHILD) 161 { 162 assert(pCHILD != 0); 163 } 164 //~NegOper() { } 165 double evaluate(const Environment &env) const 166 { 167 return (-1) * pCHILD->evaluate(env); 168 } 169 }; 170 171 class FacOper : public UnaryOperator 172 { 173 public: 174 FacOper(Expression *pCHILD) :UnaryOperator(pCHILD) { } 175 double evaluate(const Environment &env) const 176 { 177 double d = pCHILD->evaluate(env); 178 int i = d; 179 if(d - i != 0) 180 { 181 throw runtime_error("不为零"); 182 } 183 int sum =1; 184 for(; i != 0; i--) 185 { 186 sum *= i; 187 } 188 return sum; 189 } 190 }; 191 192 193 194 #endif //EXPRESSIONS_HH
commands.hh
1 #ifndef COMMANDS_HH 2 #define COMMANDS_HH 3 4 #include "environment.hh" 5 #include "expressions.hh" 6 #include <iostream> 7 #include <string> 8 #include <stdexcept> 9 #include <cassert> 10 11 using namespace std; 12 13 class Command 14 { 15 public: 16 //Command(){} 17 virtual ~Command(){} 18 virtual void run(Environment &env) = 0; 19 }; 20 21 22 class PrintCommand : public Command 23 { 24 Expression *exp; 25 public: 26 PrintCommand(Expression *exp):exp(exp) 27 { 28 assert(exp != 0); 29 } 30 ~PrintCommand() 31 { 32 delete exp; 33 } 34 35 void run(Environment &env) 36 { 37 double d = exp->evaluate(env); 38 cout << " = " << d<< endl; 39 } 40 }; 41 42 43 class AssignCommand : public Command 44 { 45 Symbol *syp; 46 Expression *exp; 47 public: 48 AssignCommand(Symbol *syp,Expression *exp):syp(syp),exp(exp) 49 { 50 assert(syp != 0); 51 assert(exp != 0); 52 } 53 ~AssignCommand() 54 { 55 delete syp; 56 delete exp; 57 } 58 59 void run(Environment &env) 60 { 61 double d = exp->evaluate(env); 62 env.setSymbolValue(syp->accessor_name_of_symbol(), d); 63 cout << syp->accessor_name_of_symbol() << "=" << d << endl; 64 } 65 }; 66 67 #endif //COMMANDS_H