Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
GoF interpreter
//g++ 5.4.0 #include <iostream> #include <string> #include <unordered_map> #include <algorithm> #include <cstring> class Context; class BooleanExp { public: virtual ~BooleanExp() {} virtual bool Evaluate(Context &) = 0; virtual BooleanExp * Replace(const std::string &, BooleanExp &) = 0; virtual BooleanExp * Copy() const = 0; }; class VariableExp; class Context { public: bool Lookup(const std::string & name) const { auto it = std::find_if(expressions_.begin(), expressions_.end(), [&] (auto it) { return it.first->name() == name; }); return it == expressions_.end() ? false : it->second; } void Assign(VariableExp * exp, bool val) { expressions_[exp] = val; } private: std::unordered_map<VariableExp*, bool> expressions_; }; class VariableExp : public BooleanExp { public: VariableExp(const std::string & name) : name_(strdup(name.c_str())) {} virtual ~VariableExp() override {} virtual bool Evaluate(Context & context) override { return context.Lookup(name_); } virtual BooleanExp * Replace(const std::string & name, BooleanExp & exp) override { if (name == name_) return exp.Copy(); else return new VariableExp(name_); } virtual BooleanExp * Copy() const override { return new VariableExp(name_); } const std::string & name() const { return name_; } private: std::string name_; }; class AndExp : public BooleanExp { public: AndExp(BooleanExp * op1, BooleanExp * op2) : op1_(op1), op2_(op2) {} virtual ~AndExp() override { delete op1_; delete op2_; } virtual bool Evaluate(Context & c) override { return op1_->Evaluate(c) && op2_->Evaluate(c); } virtual BooleanExp * Replace(const std::string & name, BooleanExp & exp) override { return new AndExp(op1_->Replace(name, exp), op2_->Replace(name, exp)); } virtual BooleanExp * Copy() const override { return new AndExp(op1_->Copy(), op2_->Copy()); } private: BooleanExp * op1_; BooleanExp * op2_; }; class OrExp : public BooleanExp { public: OrExp(BooleanExp * op1, BooleanExp * op2) : op1_(op1), op2_(op2) {} virtual ~OrExp() override { delete op1_; delete op2_; } virtual bool Evaluate(Context & c) override { return op1_->Evaluate(c) || op2_->Evaluate(c); } virtual BooleanExp * Replace(const std::string & name, BooleanExp & exp) override { return new OrExp(op1_->Replace(name, exp), op2_->Replace(name, exp)); } virtual BooleanExp * Copy() const override { return new OrExp(op1_->Copy(), op2_->Copy()); } private: BooleanExp * op1_; BooleanExp * op2_; }; class NotExp : public BooleanExp { public: NotExp(BooleanExp * op) : op_(op) {} virtual ~NotExp() override { delete op_; } virtual bool Evaluate(Context & c) override { return !op_->Evaluate(c); } virtual BooleanExp * Replace(const std::string & name, BooleanExp & exp) override { return new NotExp(op_->Replace(name, exp)); } virtual BooleanExp * Copy() const override { return new NotExp(op_->Copy()); } private: BooleanExp * op_; }; class Constant : public BooleanExp { public: Constant(bool state) : state_(state) {} virtual bool Evaluate(Context & c) override { return state_; } virtual BooleanExp * Replace(const std::string & name, BooleanExp & exp) override { return new Constant(state_); } virtual BooleanExp * Copy() const override { return new Constant(state_); } private: bool state_; }; int main() { BooleanExp * exp; Context cont; VariableExp * x = new VariableExp("X"); VariableExp * y = new VariableExp("Y"); exp = new AndExp( new AndExp(new Constant(true), x), new AndExp(y, x) ); cont.Assign(x, true); cont.Assign(y, true); bool res = exp->Evaluate(cont); std::cout << std::boolalpha << res << "\n"; VariableExp * z = new VariableExp("Z"); cont.Assign(z, false); NotExp notZ(z); BooleanExp * repl = exp->Replace("Y", notZ); res = repl->Evaluate(cont); std::cout << res << "\n"; delete y; delete repl; }
run
|
edit
|
history
|
help
0
mutable constexpr
Find the max and min number in array
Undefined behaviour
Sort array of 0's and 1's
Counting top students
Ss
Problem: binary
BinSearch
CPP - Pointers - Ex.1
IAR compiler bug test code