Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4.  
  5.  
  6. class Visitor {
  7. public:
  8.     virtual void visitLogicInput() = 0;
  9.     virtual void visitLogicGate() = 0;
  10.     virtual ~Visitor() {
  11.     };
  12. };
  13.  
  14. class Component {
  15. public:
  16.     virtual bool operation() = 0;
  17.     virtual void accept(Visitor *v) = 0;
  18.     virtual ~Component() {
  19.     };
  20. };
  21.  
  22.  
  23. class LogicInput : public Visitor {
  24.     bool val;
  25. public:
  26.     virtual void visitLogicInput() override {
  27.        
  28.     }
  29.     virtual void visitLogicGate() override {
  30.  
  31.     }
  32.     virtual ~LogicInput() {
  33.  
  34.     }
  35.  
  36. };
  37.  
  38.  
  39.  
  40. enum class LogicOperation {
  41.     AND, OR, XOR
  42. };
  43.  
  44. class LogicGate : public Component {
  45. private:
  46.     Component * a;
  47.     Component* b;
  48.     LogicOperation logicalOperation;
  49. public:
  50.     LogicGate(Component* A, Component* B, LogicOperation logicalOperation) {
  51.         a = A;
  52.         b = B;
  53.         logicalOperation = logicalOperation;
  54.     }
  55.     bool operation() override {
  56.         switch (logicalOperation) {
  57.         case LogicOperation::AND:
  58.             return (a->operation() && b->operation());
  59.             break;
  60.         case LogicOperation::OR:
  61.             return (a->operation() || b->operation());
  62.             break;
  63.         case LogicOperation::XOR:
  64.             return ((a->operation() || b->operation()) || (a->operation() && b->operation()));
  65.             break;
  66.         }
  67.         return false;
  68.     }
  69.  
  70. };
  71.  
  72. int main()
  73. {
  74.     LogicInput a;
  75.     LogicInput b;
  76.  
  77.     LogicGate and_1(&a, &b, LogicOperation::AND);
  78.     LogicGate or_1(&and_1, &b, LogicOperation::OR);
  79.     LogicGate xor_1(&and_1, &or_1, LogicOperation::XOR);
  80.  
  81.     std::cout << "and: " << and_1.operation() << std::endl;
  82.     std::cout << "or: " << or_1.operation() << std::endl;
  83.     std::cout << "xor: " << xor_1.operation() << std::endl;
  84.     std::cin.get();
  85.     return 0;
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement