cs11_c++_lab6
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的更多相关文章
- cs11_c++_lab7
wcount.cc #include <iostream> #include <map> #include <string> #include <algori ...
- cs11_c++_lab5待修改
heap.hh #ifndef HEAP_HH #define HEAP_HH #include <iostream> #include <stdexcept> #includ ...
- cs11_c++_lab4b
SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...
- cs11_c++_lab4a
SparseVector.hh class SparseVector { private: //结构体不一定会用到,不用初始化 struct node { int index; int value; ...
- cs11_c++_lab3
Matrix.hh class Matrix { int row; int col; int *p; void copy(const Matrix &m); void clearup(); p ...
- cs11_c++_lab2
Matrix.hh class Matrix { int row; int col; int *p; public: Matrix(); Matrix(int x,int y); ~Matrix(); ...
- cs11_c++_lab1
lab1.cpp #include "Point.hh" #include <iostream> #include <cmath> using namesp ...
随机推荐
- JPanel
class NewPanel extends JPanel{ public NewPanel() {} public void paintComponent(Graphics g) { int x=0 ...
- 开始使用Pyhton
安装 第一个Python程序 首先我们打开python 交互式解释器, 执行如下命令: Python 3.5.1+ (default, Mar 30 2016, 22:46:26) [GCC 5.3. ...
- JSBinding+Bridge.NET:Unity游戏热更新方案
老版本链接如下:http://www.cnblogs.com/answerwinner/p/4469021.html 新用户不要再使用老版本了. 新版本 JSBinding 将抛弃 SharpKit ...
- 使用snmp+mrtg监控CPU、流量、磁盘空间、内存
1.安装snmp rpm -qa|grep snmp* //查看是否安装了snmpyum -y install snmp* //安装snmp #vim /etc/snmp/snmpd.confroco ...
- 转载:bootstrap, boosting, bagging 几种方法的联系
转:http://blog.csdn.net/jlei_apple/article/details/8168856 这两天在看关于boosting算法时,看到一篇不错的文章讲bootstrap, ja ...
- 阿里云的9折推荐码 8DIER4
推荐码: 8DIER4 我有一个阿里云9折推荐码:8DIER4,分享给你,第一次购买云服务器或云数据库可享受原价9折优惠,还可多人使用,拿走不谢. 阿里云地址:http://www.aliyun.co ...
- IIS7下使用4.0框架集成模式URLRewriter重写中文URL乱码问题
解决方法: 1 url中中文先通过Server.UrlEncode编码 2 修改网站编码 3.更改URLRewriter.net中源码 找到RewriterEngine类中 private strin ...
- Fastboot模式和Recovery模式
http://blog.csdn.net/luoshengyang/article/details/29688041 在回答第一个问题之前,我们先来看看Android设备从硬件到系统的结构,如图1所示 ...
- Digests from CG articales
Turtle Talk Prior to the on-set motion capture, the team had the actors perform expressions while be ...
- ios启动载入启动图片
版本判断: 1.首先你要知道这个键值对的key:id key = (id)kCFBundleVersionKey; 2.同过本地的NSBundle取得当前的版本号. 3.在沙盒中取得对应的版本号. ...