Advertisement
TheSpookyGhost

Kind of set form Pascal in C++

Nov 18th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.42 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <sstream>
  6. using namespace std;
  7.  
  8.  
  9. void menu()
  10. {
  11.     cout << "1.Dodaj liste" << endl << "2.Dodaj Kostke" << endl;
  12. }
  13.  
  14. struct Node {
  15.     long int _binary = 0;
  16.     Node* _nastepny = nullptr;
  17. };
  18.  
  19. class lista
  20. {
  21. public:
  22.    
  23.     //Konstruktor
  24.     lista()
  25.     {
  26.         _start = nullptr;
  27.         _last = nullptr;
  28.     }
  29.     //Destruktor
  30.     ~lista()
  31.     {
  32.         Node* kostkaTym = _start;
  33.         Node* kostkaNast;
  34.         while (kostkaTym != 0)
  35.         {
  36.             kostkaNast = kostkaTym->_nastepny;
  37.             delete kostkaTym;
  38.             kostkaTym = kostkaNast;
  39.         }
  40.     }
  41.     //Ustawianie bitu
  42.     long int set(long int x, int y)
  43.     {
  44.         x = x | (0x01 << y);
  45.         return x;
  46.     }
  47.     //Jakie bity uzytkownik chce
  48.     long int getrelation()
  49.     {
  50.         long int relation = 0;
  51.         bool end = false;
  52.         cout << "Prosze podac ktory pojedynczy element nalezacy do zbioru (0 - 63) , podanie czegos innego zakonczy ustalanie relacji" << endl;
  53.         while (end == false)
  54.         {
  55.             int a = 0;
  56.             string input = "";
  57.             getline(cin, input);   
  58.             if (!(stringstream(input) >> a)) // Sprawdzanie czy to liczba
  59.             {
  60.                 cin.clear();
  61.                 end = true;                
  62.             }
  63.             else
  64.                 if (a > 64)
  65.                     cout << "Przekroczono zakres" << endl;
  66.                 else
  67.                 {
  68.                     cout << "Prosze podac ktory pojedynczy element nalezacy do zbioru (0 - 63) , podanie czegos innego zakonczy ustalanie relacji" << endl;
  69.                     relation = set(relation, a);
  70.                 }
  71.         }
  72.         return relation;
  73.     }
  74.     //Dodanie Kosci
  75.     void addNode()
  76.     {
  77.         if (_start == nullptr)
  78.         {
  79.             Node* nowa = new  Node();
  80.             _start = nowa;
  81.             _start->_binary = getrelation();
  82.             _last = _start;
  83.         }
  84.         else
  85.         {
  86.             Node* nowa = new  Node();
  87.             _last->_nastepny = nowa;
  88.             _last->_nastepny->_binary = getrelation();
  89.             _last = nowa;
  90.         }
  91.     }
  92.     //Dodawanie z wartoscia
  93.     void addNode(long int x)// Dodawanie na koniec
  94.     {
  95.         if (_start == nullptr)
  96.         {
  97.             Node* nowa = new  Node();
  98.             _start = nowa;
  99.             _start->_binary = x;
  100.             _last = _start;
  101.         }
  102.         else
  103.         {
  104.             Node* nowa = new  Node();
  105.             _last->_nastepny = nowa;
  106.             _last->_nastepny->_binary =x;
  107.             _last = nowa;
  108.         }
  109.     }
  110.     // Pokaz kosci
  111.     void showNodes()
  112.     {
  113.         if (_start == nullptr)
  114.         {
  115.             cout << "Brak kostek w liscie" << endl;
  116.         }
  117.         else
  118.         {
  119.             if (_start == _last)
  120.             {
  121.                 cout << _start->_binary << "  " << "Brak nastepnej kostki" << endl;
  122.             }
  123.             else
  124.             {
  125.                 Node* temp = _start;
  126.                 cout << _start->_binary << "  " << _start->_nastepny << endl;
  127.                 for (int x = count() - 1; x > 0; x--)
  128.                 {
  129.                     temp = temp->_nastepny;
  130.                     if (temp->_nastepny != nullptr)
  131.                         cout << temp->_binary << "  " << temp->_nastepny << endl;
  132.                     else
  133.                         cout << temp->_binary << "  " << "Brak nastepnej kostki" << endl;
  134.                 }      
  135.             }
  136.         }
  137.     }
  138.     //Zlicz kosci
  139.     int count()
  140.     {
  141.         int ilosc = 0;
  142.         Node* kostka = _start;
  143.         while (kostka != nullptr)
  144.         {
  145.             ++ilosc;
  146.             kostka = kostka->_nastepny;
  147.         }
  148.         return ilosc;
  149.     }
  150.         Node* _last;
  151.         Node* _start;  
  152. };
  153.  
  154. long int sumBinary(long int first, long int second)
  155. {
  156.     int sum = first | second;
  157.     return sum;
  158. }
  159. long int minusBinary(long int first, long int second) // Gdy jest tylko 1 jedynka zapisuje ja , w obu
  160. {
  161.     int minus = first ^ second;
  162.     return minus;
  163. }
  164. long int bothBinary(long int first, long int second)
  165. {
  166.     int both = first & second;
  167.     return both;
  168. }
  169.  
  170.  
  171. lista* suma(lista* x, lista* y)
  172. {
  173.     lista* newList = new lista();
  174.     int count = 0;
  175.     //Czy listy sa puste
  176.     if(x->count() == 0 || y ->count() == 0)
  177.     {
  178.         return newList;
  179.     }
  180.     else
  181.     {
  182.         int count = 0;
  183.         //Ktora wieksza
  184.         if (x->count() > y->count())
  185.             count = x->count() - 1; // Odejmowanie pierwszej
  186.         else
  187.             count = y->count() - 1; // Odejmowanie pierwszej
  188.         //Tworzenie nowych kostek
  189.         Node* tempx = new Node();
  190.         tempx = x->_start;
  191.         Node* tempy = new Node();
  192.         tempy = y->_start;
  193.         //Sprawdzanie czy ktoras poczatkowe kostki sa puste
  194.         if (tempx->_binary == 0)
  195.             newList->addNode(tempy->_binary);
  196.         else
  197.             if (tempy->_binary == 0)
  198.                 newList->addNode(tempx->_binary);
  199.             else
  200.                 //Gdy obie nie puste
  201.                 newList->addNode( sumBinary(tempx->_binary, tempy->_binary) );
  202.         //Dla reszty listy gdy istnieje
  203.         for (count; count > 0; count--)
  204.         {
  205.             tempx = tempx->_nastepny;
  206.             tempy = tempy->_nastepny;
  207.             //Sprawdzanie czy ktores kostki sa puste
  208.             if (tempx->_binary == 0)
  209.                 newList->addNode(tempy->_binary);
  210.             else
  211.                 if (tempy->_binary == 0)
  212.                     newList->addNode(tempx->_binary);
  213.                 else
  214.                     //Gdy obie nie puste
  215.                     sumBinary(tempx->_binary, tempy->_binary);
  216.         }
  217.         return newList;
  218.     }
  219.     return newList;
  220. }
  221.  
  222. lista* roznica(lista* x, lista* y)
  223. {
  224.     lista* newList = new lista();
  225.     int count = 0;
  226.     //Czy listy sa puste
  227.     if (x->count() == 0 || y->count() == 0)
  228.     {
  229.         return newList;
  230.     }
  231.     else
  232.     {
  233.         int count = 0;
  234.         //Ktora wieksza
  235.         if (x->count() > y->count())
  236.             count = x->count() - 1; // Odejmowanie pierwszej
  237.         else
  238.             count = y->count() - 1; // Odejmowanie pierwszej
  239.         //Tworzenie nowych kostek
  240.         Node* tempx = new Node();
  241.         tempx = x->_start;
  242.         Node* tempy = new Node();
  243.         tempy = y->_start;
  244.         //Sprawdzanie czy ktoras poczatkowe kostki sa puste
  245.         if (tempx->_binary == 0)
  246.             newList->addNode(-(tempy->_binary) );
  247.         else
  248.             if (tempy->_binary == 0)
  249.                 newList->addNode(tempx->_binary);
  250.             else
  251.                 //Gdy obie nie puste
  252.                 newList->addNode(minusBinary(tempx->_binary, tempy->_binary));
  253.         //Dla reszty listy gdy istnieje
  254.         for (count; count > 0; count--)
  255.         {
  256.             tempx = tempx->_nastepny;
  257.             tempy = tempy->_nastepny;
  258.             //Sprawdzanie czy ktores kostki sa puste
  259.             if (tempx->_binary == 0)
  260.                 newList->addNode(-(tempy->_binary) );
  261.             else
  262.                 if (tempy->_binary == 0)
  263.                     newList->addNode(tempx->_binary);
  264.                 else
  265.                     //Gdy obie nie puste
  266.                     newList->addNode ( minusBinary(tempx->_binary, tempy->_binary) );
  267.         }
  268.         return newList;
  269.     }
  270.  
  271.     return newList;
  272. }
  273.  
  274. lista* both(lista* x, lista* y)
  275. {
  276.     lista* newList = new lista();
  277.     int count = 0;
  278.     //Czy listy sa puste
  279.     if (x->count() == 0 || y->count() == 0)
  280.     {
  281.         return newList;
  282.     }
  283.     else
  284.     {
  285.         int count = 0;
  286.         //Ktora wieksza
  287.         if (x->count() > y->count())
  288.             count = x->count() - 1; // Odejmowanie pierwszej
  289.         else
  290.             count = y->count() - 1; // Odejmowanie pierwszej
  291.         //Tworzenie nowych kostek
  292.         Node* tempx = new Node();
  293.         tempx = x->_start;
  294.         Node* tempy = new Node();
  295.         tempy = y->_start;
  296.         //Sprawdzanie czy ktoras poczatkowe kostki sa puste
  297.         if (tempx->_binary == 0 || tempy->_binary == 0)
  298.         {
  299.         }
  300.         else
  301.             //Gdy obie nie puste
  302.             newList->addNode(bothBinary(tempx->_binary, tempy->_binary));
  303.         //Dla reszty listy gdy istnieje
  304.         for (count; count > 0; count--)
  305.         {
  306.             tempx = tempx->_nastepny;
  307.             tempy = tempy->_nastepny;
  308.             //Sprawdzanie czy ktores kostki sa puste
  309.             if (tempx->_binary == 0 || tempy->_binary == 0)
  310.             {
  311.             }
  312.             else
  313.                 //Gdy obie nie puste
  314.                 newList->addNode(bothBinary(tempx->_binary, tempy->_binary));
  315.         }
  316.         return newList;
  317.     }
  318. }
  319. int main()
  320. {
  321.     lista* list = new lista();
  322.     list->addNode();
  323.     list->showNodes();
  324.  
  325.     lista* list2 = new lista();
  326.     list2->addNode();
  327.     list2->showNodes();
  328.      
  329.     lista* lista3 = suma(list, list2); //Dodajemy wynik do 3
  330.     cout << "Wynik sumowania to: " << endl << endl;
  331.     lista3->showNodes();
  332.  
  333.     lista* lista4 = roznica(list, list2); // Odejmujemy druga liste od pierwszej wynik do 3
  334.     cout << "Wynik roznica to: " << endl << endl;
  335.     lista4->showNodes();
  336.  
  337.     lista* list5 = both(list, list2);
  338.     cout << "Wynik zawierabua to: " << endl << endl;
  339.     list5->showNodes();
  340.  
  341.  
  342.  
  343.     return 0;
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement