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 ...
随机推荐
- LeetCode 【347. Top K Frequent Elements】
Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...
- 实现DevExpress GridControl 只有鼠标双击后才进行修改数据
1. 实现DevExpress GridControl 只有鼠标双击后才进行修改数据:修改GridView.OptionsBehavior.EditorShowMode属性为Click 2. 实现De ...
- ASP.NET(转自wiki)
ASP.NET是由微软在.NET Framework框架中所提供,开发Web应用程序的类库,封装在System.Web.dll文件中,显露出System.Web名字空间,并提供ASP.NET网页处理. ...
- css3实现酷炫的3D盒子翻转效果
简介 运用css3先在平面空间组成立方体盒子,再让整个盒子翻转起来,先来张效果图: 步骤 1.先用css将6张图片摆成下图的样子: 下面就是通过css3的3D变换将每个面进行翻转,使之成为一个立体的盒 ...
- 自动运维:Ansible -ansible tower
文档主页:http://docs.ansible.com/参考文档:http://docs.ansible.com/ansible/参考文档:http://docs.ansible.com/ansib ...
- mongodb 对内嵌文档(数组) group分页查询,并设置查询条件
文档示例Account的其中一条记录: // collection: Account { "_id" : ObjectId("5843e38e535f3708f759b2 ...
- apache 使用 .htaccess 导致500错误
今天在win主机上配置了一个apache+mysql+php 的环境,一切看似正常了.结果将程序转移过来,打开网站的时候,出现了500错误.于是乎查原因: 首先,怀疑的是连接mysql出错了,找出配置 ...
- Python字符串处理
字符串输入: my_string = raw_input("please input a word:") 字符串判断: (1) 判断是不是纯字母 my_string.isalpha ...
- zookeeper在集群负载均衡中的应用
zookeeper本身是不提供负载均衡的策略,需要自己来实现,所以这里确切的说,是在负载均衡中应用到了zookeeper做集群的协调. 对于HTTP请求的负载均衡,成熟的解决方案是Nginx(或Hap ...
- [转载]ARM协处理器CP15寄存器详解
用于系统存储管理的协处理器CP15 原地址:http://blog.csdn.net/gameit/article/details/13169405 MCR{cond} coproc,opc ...