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

cs11_c++_lab6的更多相关文章

  1. cs11_c++_lab7

    wcount.cc #include <iostream> #include <map> #include <string> #include <algori ...

  2. cs11_c++_lab5待修改

    heap.hh #ifndef HEAP_HH #define HEAP_HH #include <iostream> #include <stdexcept> #includ ...

  3. cs11_c++_lab4b

    SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...

  4. cs11_c++_lab4a

    SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...

  5. cs11_c++_lab3

    Matrix.hh class Matrix { int row; int col; int *p; void copy(const Matrix &m); void clearup(); p ...

  6. cs11_c++_lab2

    Matrix.hh class Matrix { int row; int col; int *p; public: Matrix(); Matrix(int x,int y); ~Matrix(); ...

  7. cs11_c++_lab1

    lab1.cpp #include "Point.hh" #include <iostream> #include <cmath> using namesp ...

随机推荐

  1. range()函数的使用

    坚持每天学一点,每天进步一点,迟早有一点我会成为大神. 在python中range函数可以返回一系列连续增加的整数,也是一个迭代器. 函数用法:range(开始, 结束, 步进值): #步进值默认为1 ...

  2. JSBinding+Bridge.NET:Unity游戏热更新方案

    老版本链接如下:http://www.cnblogs.com/answerwinner/p/4469021.html 新用户不要再使用老版本了. 新版本 JSBinding 将抛弃 SharpKit ...

  3. C# 更新SQL Server数据库备注信息从另一数据库

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. Redis -- 01 入门

    1. Redis是什么 与memcached 和 couchbase类似,redis是非常快速的基于内存的键值数据库,使用标准c编写,是使用最广泛的缓存中间件.利用Redis提供的五种基本数据类型(S ...

  5. 简述cookie

    1.Cookie的概述 * Cookie是客户端的技术(默认把Cookie保存在每个用户的浏览器上) * 程序把每个用户的数据以cookie的形式写给用户各自的浏览器 * 当用户使用浏览器再去访问服务 ...

  6. 工程中建立多个src目录

    android 工程下可以有多个源代码的目录,不一定都要放到src下面.可以在 .classpath 文件中添加. 默认是这样的: <classpath> <classpathent ...

  7. 短链(ShortURL)的Java实现

    什么叫短链或短址? 就是把长的 URL 转成短的 URL, 现在提供这种服务的有很多公司,我们以google家的 URL shortener 服务: http://goo.gl/ 为例. 任何长网址都 ...

  8. protobuf C++ 使用示例

    1.在.proto文件中定义消息格式 2.使用protobuf编译器 3.使用c++ api来读写消息 0.为何使用protobuf? 1.原始内存数据结构,可以以二进制方式sent/saved.这种 ...

  9. javascript Date

    转:http://blog.csdn.net/vbangle/article/details/5643091# // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日( ...

  10. Oracle补习班第三天

    In every triumph, there's a lot of try. 每个胜利背后都有许多尝试 Oracle管理实例组件 主要组件分为两部分例程,与数据库: 例程分为两部分SGA跟进程: S ...