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 ...
随机推荐
- OpenGL顶点数组
概述 作为在立即模式(glBegin()与glEnd()之间)下指定单个顶点数据的替代,你可以保存顶点数据在一组列表中,包括顶点位置.法线.纹理坐标与颜色信息.并且你可以通过索引数组解引用数组元素绘制 ...
- Visual Studio+TFS--强大的项目管理工具
一.前言 微软的Visual Studio非常强大,可以无缝结合Git或自家的TFS(Team Foundation Server),进行项目管理非常方便,从需求分析.开发.测试.维护,几乎可以贯穿软 ...
- os and shutil
# os 模块 os.sep 可以取代操作系统特定的路径分隔符.windows下为 '\\'os.name 字符串指示你正在使用的平台.比如对于Windows,它是'nt',而对于Linux/Unix ...
- asp.net gridview动态添加列,并获取其数据;
1,绑定数据前先动态添加列,见方法CreateGridColumn(只在第一次加载动态添加): 2,gvlist_RowDataBound为对应列添加控件: 前台代码: <%@ Page Lan ...
- YII rules常见规则
public function rules() { return array( //必须填写 array('email, username, password, ...
- AVL的删除写法的一个错误
今天在写AVL删除的时候犯了一个傻逼错误,调了很久,教训惨痛,引以为鉴. 树中允许有重复节点,如果删除的节点有重复,则只删除1个. AVL删除采取的方法是首先判断待删除节点是否存在,如果存在,那么判断 ...
- Python torndoa mysql 模块安装
pip install torndb pip install pip install mysql-python #不支持3.x版本 ln -s /usr/local/mysql/lib/libmysq ...
- Nginx图片剪裁模块探究
http://nginx.org/en/docs/http/ngx_http_image_filter_module.html http://cwtea.blog.51cto.com/4500217/ ...
- debian下安装zendframework
第一步,打开apache的rewrite模块,因为在debian下使用apache必须执行这一步 a2enmod rewrite #激活rewrite模块 /etc/init.d/apache2 re ...
- jquery “做页面滚动到某屏时改变状态标题” 所用知识点记录
浏览器滚动条滚动时触发事件 //浏览器滚动条滚动时触发事件 $(window).scroll(function(){}); 浏览器窗口大小改变时触发事件 //浏览器窗口大小改变时触发事件 $(wind ...