kielbasior

BRAMKI LOGICZNE

Jul 4th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <string>
  4. #include <vector>
  5. #include <iostream>
  6. #include <fstream>
  7.  
  8. using std::vector;
  9. using std::string;
  10.  
  11. class ZlyNumerWejscia: public std::out_of_range
  12. {
  13. public:
  14.     ZlyNumerWejscia(): std::out_of_range("Poza zakresem!"){}
  15. };
  16.  
  17. class Bramka
  18. {
  19. protected:
  20.     int liczba_wejsc;
  21.     bool *stan_wejscia;
  22. public:
  23.     Bramka(int l_wejsc): liczba_wejsc(l_wejsc)
  24.     {
  25.         stan_wejscia=new bool[liczba_wejsc];
  26.         for (int i=0;i<liczba_wejsc;i++)
  27.         {
  28.             stan_wejscia[i]=false;
  29.         }
  30.     }
  31.     virtual void Zeruj()
  32.     {
  33.         for (int i=0;i<liczba_wejsc;i++)
  34.         {
  35.             stan_wejscia[i]=false;
  36.         }
  37.     }
  38.     virtual int LiczbaWejsc()
  39.     {
  40.         return liczba_wejsc;
  41.     }
  42.     virtual void UstawWejscie(int i, int x)
  43.     {
  44.         if (i<0 || i>liczba_wejsc)
  45.             throw ZlyNumerWejscia();
  46.         stan_wejscia[i]=x;
  47.     }
  48.     virtual bool Wejscie(int i)
  49.     {
  50.         if (i<0 || i>liczba_wejsc)
  51.             throw ZlyNumerWejscia();
  52.         return stan_wejscia[i];
  53.     }
  54.     virtual bool Wyjscie()=0;
  55.     virtual string ZwrocNazwe()=0;
  56. };
  57.  
  58. class NOT: public Bramka
  59. {
  60. public:
  61.     NOT(): Bramka(1){}
  62.     bool Wyjscie()
  63.     {
  64.         return !stan_wejscia;
  65.     }
  66.     string ZwrocNazwe()
  67.     {
  68.         return " NOT ";
  69.     }
  70. };
  71.  
  72. class OR: public Bramka
  73. {
  74. public:
  75.     OR(int l_wejsc): Bramka(l_wejsc){}
  76.     bool Wyjscie()
  77.     {
  78.         for (int i=0;i<liczba_wejsc;i++)
  79.         {
  80.         if (stan_wejscia[i]==1)
  81.         {
  82.             return true;
  83.         }
  84.         else
  85.         return false;
  86.         }
  87.     }
  88.         string ZwrocNazwe()
  89.     {
  90.         return " OR ";
  91.     }
  92. };
  93.  
  94. class AND: public Bramka
  95. {
  96. public:
  97.     AND(int l_wejsc): Bramka(l_wejsc){}
  98.     bool Wyjscie()
  99.     {
  100.         for (int i=0;i<liczba_wejsc;i++)
  101.         {
  102.         if (stan_wejscia[0]!=stan_wejscia[i])
  103.         {
  104.             return false;
  105.         }
  106.         else
  107.         return true;
  108.         }
  109.     }
  110.         string ZwrocNazwe()
  111.     {
  112.         return " AND ";
  113.     }
  114. };
  115.  
  116. class XOR: public Bramka
  117. {
  118. public:
  119.     XOR(int l_wejsc): Bramka(l_wejsc){}
  120.     bool Wyjscie()
  121.     {
  122.         for (int i=0;i<liczba_wejsc;i++)
  123.         {
  124.             if (stan_wejscia[0]!=stan_wejscia[i])
  125.             {
  126.                 return true;
  127.             }
  128.             else
  129.             return false;
  130.         }
  131.     }
  132.         string ZwrocNazwe()
  133.     {
  134.         return " XOR ";
  135.     }
  136. };
  137.  
  138. int main()
  139. {
  140.     vector<Bramka*> Nazwa;
  141.     vector<Bramka*>::iterator it;
  142.  
  143.     string nazwa;
  144.     int liczba, stan;
  145.     std::ifstream p;
  146.     p.open("bramki.txt");
  147.     try
  148.     {
  149.         while (!p.eof())
  150.         {
  151.             p >> nazwa;
  152.             if (nazwa == "NOT")
  153.             {
  154.                 p >> stan;
  155.                 Nazwa.push_back(new NOT());
  156.                 it=Nazwa.end()-1;
  157.                 (*it)->UstawWejscie(0,stan);
  158.             }
  159.             if (nazwa == "OR")
  160.             {
  161.                 p >> liczba;
  162.                 Nazwa.push_back(new OR(liczba));
  163.                 it=Nazwa.end()-1;
  164.                 for (int i=0;i<liczba;i++)
  165.                 {
  166.                 p >> stan;
  167.                 (*it)->UstawWejscie(i,stan);
  168.                 }
  169.             }
  170.             if (nazwa == "AND")
  171.             {
  172.                 p >> liczba;
  173.                 Nazwa.push_back(new AND(liczba));
  174.                 it=Nazwa.end()-1;
  175.                 for (int i=0;i<liczba;i++)
  176.                 {
  177.                 p >> stan;
  178.                 (*it)->UstawWejscie(i,stan);
  179.                 }
  180.             }
  181.             if (nazwa == "XOR")
  182.             {
  183.                 p >> liczba;
  184.                 Nazwa.push_back(new XOR(liczba));
  185.                 it=Nazwa.end()-1;
  186.                 for (int i=0;i<liczba;i++)
  187.                 {
  188.                 p >> stan;
  189.                 (*it)->UstawWejscie(i,stan);
  190.                 }
  191.             }
  192.         }
  193.  
  194.         for (it=Nazwa.begin(); it<Nazwa.end(); it++)
  195.         {
  196.             printf("%s %d ", (*it)->ZwrocNazwe().c_str(), (*it)->LiczbaWejsc());
  197.             for (int i=0;i<(*it)->LiczbaWejsc(); i++)
  198.             {
  199.                 printf("%d ", (*it)->Wejscie(i));
  200.             }
  201.             printf("%d ", (*it)->Wyjscie());
  202.             printf("\n");
  203.         }
  204.     }
  205.     catch (ZlyNumerWejscia &k)
  206.     {
  207.         printf(" ", k.what());
  208.     }
  209.     _getch();
  210.     return 0;
  211. }
  212.  
  213. NOT 0
  214. NOT 1
  215. OR 3 0 0 0
  216. OR 3 0 1 1
  217. OR 5 1 0 0 0 0
  218. OR 2 1 0
  219. AND 4 1 1 1 1
  220. AND 4 0 0 0 0
  221. AND 3 0 1 1
  222. XOR 4 1 0 1 0
  223. XOR 4 1 1 1 0
  224. XOR 3 0 0 0
Add Comment
Please, Sign In to add comment