cs11_c++_lab6

时间:2022-02-04 17:52:58

expressions.hh

 #ifndef EXPRESSIONS_HH
#define EXPRESSIONS_HH #include "environment.hh"
#include <string>
#include <stdexcept>
#include <cassert>
#include <cmath> using namespace std; class Expression
{
public:
Expression() {}
virtual double evaluate(const Environment &env) const = ;
virtual ~Expression() {}
}; class Value : public Expression
{
double data_value;
public:
Value(double data):data_value(data) {}
//~Value(){}
double evaluate(const Environment &env) const
{
return data_value;
}
}; class Symbol : public Expression
{
string name_of_symbol;
public:
Symbol(const string &str):name_of_symbol(str) {}
//~Symbol(){}
string accessor_name_of_symbol() const
{
return name_of_symbol;
}
double evaluate(const Environment &env) const
{
return env.getSymbolValue(name_of_symbol);
}
}; class BinaryOperator : public Expression
{
protected:
Expression *pLHS;
Expression *pRHS;
public:
BinaryOperator(Expression *pLHS, Expression *pRHS):pLHS(pLHS),pRHS(pRHS)
{
assert(pLHS != );
assert(pRHS != );
}
virtual ~BinaryOperator()
{
delete pLHS;
delete pRHS;
}
virtual double evaluate(const Environment &env) const = ;
const Expression* accessor_of_left() const
{
return pLHS;
}
const Expression* accessor_of_right() const
{
return pRHS;
} }; class AddOper : public BinaryOperator
{
public:
AddOper(Expression *pLHS, Expression *pRHS):BinaryOperator(pLHS, pRHS) {}
~AddOper() { }
double evaluate(const Environment &env)const
{
return pLHS->evaluate(env) + pRHS->evaluate(env);
}
}; class SubOper : public BinaryOperator
{
public:
SubOper(Expression *pLHS, Expression *pRHS):BinaryOperator(pLHS, pRHS) {}
//~SubOper() { }
double evaluate(const Environment &env) const
{
return pLHS->evaluate(env) - pRHS->evaluate(env);
}
}; class MulOper : public BinaryOperator
{
public:
MulOper(Expression *pLHS, Expression *pRHS):BinaryOperator(pLHS, pRHS) { }
//~MulOper() { }
double evaluate(const Environment &env) const
{
return pLHS->evaluate(env) * pRHS->evaluate(env);
}
}; class DivOper : public BinaryOperator
{
public:
DivOper(Expression *pLHS, Expression *pRHS) : BinaryOperator(pLHS, pRHS) {}
//~DivOper() { }
double evaluate(const Environment &env) const
{
if(pRHS->evaluate(env) == )
{
throw runtime_error("除数为零");
} return pLHS->evaluate(env) / pRHS->evaluate(env);
}
}; class ExpOper : public BinaryOperator
{
public:
ExpOper(Expression *pLHS, Expression *pRHS) : BinaryOperator(pLHS, pRHS) {}
double evaluate(const Environment &env) const
{
return pow(pLHS->evaluate(env), pRHS->evaluate(env));
}
}; class UnaryOperator : public Expression
{
protected:
Expression *pCHILD;
public:
UnaryOperator(Expression *pCHILD) : pCHILD(pCHILD)
{
assert(pCHILD != );
}
virtual ~UnaryOperator()
{
delete pCHILD;
} //virtual double evaluate(const Environment &env) const = 0;
const Expression* accessor_of_child() const
{
return pCHILD;
}
}; class NegOper : public UnaryOperator
{
public:
NegOper(Expression *pCHILD) : UnaryOperator(pCHILD)
{
assert(pCHILD != );
}
//~NegOper() { }
double evaluate(const Environment &env) const
{
return (-) * pCHILD->evaluate(env);
}
}; class FacOper : public UnaryOperator
{
public:
FacOper(Expression *pCHILD) :UnaryOperator(pCHILD) { }
double evaluate(const Environment &env) const
{
double d = pCHILD->evaluate(env);
int i = d;
if(d - i != )
{
throw runtime_error("不为零");
}
int sum =;
for(; i != ; i--)
{
sum *= i;
}
return sum;
}
}; #endif //EXPRESSIONS_HH

commands.hh

 #ifndef COMMANDS_HH
#define COMMANDS_HH #include "environment.hh"
#include "expressions.hh"
#include <iostream>
#include <string>
#include <stdexcept>
#include <cassert> using namespace std; class Command
{
public:
//Command(){}
virtual ~Command(){}
virtual void run(Environment &env) = ;
}; class PrintCommand : public Command
{
Expression *exp;
public:
PrintCommand(Expression *exp):exp(exp)
{
assert(exp != );
}
~PrintCommand()
{
delete exp;
} void run(Environment &env)
{
double d = exp->evaluate(env);
cout << " = " << d<< endl;
}
}; class AssignCommand : public Command
{
Symbol *syp;
Expression *exp;
public:
AssignCommand(Symbol *syp,Expression *exp):syp(syp),exp(exp)
{
assert(syp != );
assert(exp != );
}
~AssignCommand()
{
delete syp;
delete exp;
} void run(Environment &env)
{
double d = exp->evaluate(env);
env.setSymbolValue(syp->accessor_name_of_symbol(), d);
cout << syp->accessor_name_of_symbol() << "=" << d << endl;
}
}; #endif //COMMANDS_H

相关文章