Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- class Visitor {
- public:
- virtual void visitLogicInput() = 0;
- virtual void visitLogicGate() = 0;
- virtual ~Visitor() {
- };
- };
- class Component {
- public:
- virtual bool operation() = 0;
- virtual void accept(Visitor *v) = 0;
- virtual ~Component() {
- };
- };
- class LogicInput : public Visitor {
- bool val;
- public:
- virtual void visitLogicInput() override {
- }
- virtual void visitLogicGate() override {
- }
- virtual ~LogicInput() {
- }
- };
- enum class LogicOperation {
- AND, OR, XOR
- };
- class LogicGate : public Component {
- private:
- Component * a;
- Component* b;
- LogicOperation logicalOperation;
- public:
- LogicGate(Component* A, Component* B, LogicOperation logicalOperation) {
- a = A;
- b = B;
- logicalOperation = logicalOperation;
- }
- bool operation() override {
- switch (logicalOperation) {
- case LogicOperation::AND:
- return (a->operation() && b->operation());
- break;
- case LogicOperation::OR:
- return (a->operation() || b->operation());
- break;
- case LogicOperation::XOR:
- return ((a->operation() || b->operation()) || (a->operation() && b->operation()));
- break;
- }
- return false;
- }
- };
- int main()
- {
- LogicInput a;
- LogicInput b;
- LogicGate and_1(&a, &b, LogicOperation::AND);
- LogicGate or_1(&and_1, &b, LogicOperation::OR);
- LogicGate xor_1(&and_1, &or_1, LogicOperation::XOR);
- std::cout << "and: " << and_1.operation() << std::endl;
- std::cout << "or: " << or_1.operation() << std::endl;
- std::cout << "xor: " << xor_1.operation() << std::endl;
- std::cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement