Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <vector>
- #include <string>
- #include <sstream>
- using namespace std;
- void menu()
- {
- cout << "1.Dodaj liste" << endl << "2.Dodaj Kostke" << endl;
- }
- struct Node {
- long int _binary = 0;
- Node* _nastepny = nullptr;
- };
- class lista
- {
- public:
- //Konstruktor
- lista()
- {
- _start = nullptr;
- _last = nullptr;
- }
- //Destruktor
- ~lista()
- {
- Node* kostkaTym = _start;
- Node* kostkaNast;
- while (kostkaTym != 0)
- {
- kostkaNast = kostkaTym->_nastepny;
- delete kostkaTym;
- kostkaTym = kostkaNast;
- }
- }
- //Ustawianie bitu
- long int set(long int x, int y)
- {
- x = x | (0x01 << y);
- return x;
- }
- //Jakie bity uzytkownik chce
- long int getrelation()
- {
- long int relation = 0;
- bool end = false;
- cout << "Prosze podac ktory pojedynczy element nalezacy do zbioru (0 - 63) , podanie czegos innego zakonczy ustalanie relacji" << endl;
- while (end == false)
- {
- int a = 0;
- string input = "";
- getline(cin, input);
- if (!(stringstream(input) >> a)) // Sprawdzanie czy to liczba
- {
- cin.clear();
- end = true;
- }
- else
- if (a > 64)
- cout << "Przekroczono zakres" << endl;
- else
- {
- cout << "Prosze podac ktory pojedynczy element nalezacy do zbioru (0 - 63) , podanie czegos innego zakonczy ustalanie relacji" << endl;
- relation = set(relation, a);
- }
- }
- return relation;
- }
- //Dodanie Kosci
- void addNode()
- {
- if (_start == nullptr)
- {
- Node* nowa = new Node();
- _start = nowa;
- _start->_binary = getrelation();
- _last = _start;
- }
- else
- {
- Node* nowa = new Node();
- _last->_nastepny = nowa;
- _last->_nastepny->_binary = getrelation();
- _last = nowa;
- }
- }
- //Dodawanie z wartoscia
- void addNode(long int x)// Dodawanie na koniec
- {
- if (_start == nullptr)
- {
- Node* nowa = new Node();
- _start = nowa;
- _start->_binary = x;
- _last = _start;
- }
- else
- {
- Node* nowa = new Node();
- _last->_nastepny = nowa;
- _last->_nastepny->_binary =x;
- _last = nowa;
- }
- }
- // Pokaz kosci
- void showNodes()
- {
- if (_start == nullptr)
- {
- cout << "Brak kostek w liscie" << endl;
- }
- else
- {
- if (_start == _last)
- {
- cout << _start->_binary << " " << "Brak nastepnej kostki" << endl;
- }
- else
- {
- Node* temp = _start;
- cout << _start->_binary << " " << _start->_nastepny << endl;
- for (int x = count() - 1; x > 0; x--)
- {
- temp = temp->_nastepny;
- if (temp->_nastepny != nullptr)
- cout << temp->_binary << " " << temp->_nastepny << endl;
- else
- cout << temp->_binary << " " << "Brak nastepnej kostki" << endl;
- }
- }
- }
- }
- //Zlicz kosci
- int count()
- {
- int ilosc = 0;
- Node* kostka = _start;
- while (kostka != nullptr)
- {
- ++ilosc;
- kostka = kostka->_nastepny;
- }
- return ilosc;
- }
- Node* _last;
- Node* _start;
- };
- long int sumBinary(long int first, long int second)
- {
- int sum = first | second;
- return sum;
- }
- long int minusBinary(long int first, long int second) // Gdy jest tylko 1 jedynka zapisuje ja , w obu
- {
- int minus = first ^ second;
- return minus;
- }
- long int bothBinary(long int first, long int second)
- {
- int both = first & second;
- return both;
- }
- lista* suma(lista* x, lista* y)
- {
- lista* newList = new lista();
- int count = 0;
- //Czy listy sa puste
- if(x->count() == 0 || y ->count() == 0)
- {
- return newList;
- }
- else
- {
- int count = 0;
- //Ktora wieksza
- if (x->count() > y->count())
- count = x->count() - 1; // Odejmowanie pierwszej
- else
- count = y->count() - 1; // Odejmowanie pierwszej
- //Tworzenie nowych kostek
- Node* tempx = new Node();
- tempx = x->_start;
- Node* tempy = new Node();
- tempy = y->_start;
- //Sprawdzanie czy ktoras poczatkowe kostki sa puste
- if (tempx->_binary == 0)
- newList->addNode(tempy->_binary);
- else
- if (tempy->_binary == 0)
- newList->addNode(tempx->_binary);
- else
- //Gdy obie nie puste
- newList->addNode( sumBinary(tempx->_binary, tempy->_binary) );
- //Dla reszty listy gdy istnieje
- for (count; count > 0; count--)
- {
- tempx = tempx->_nastepny;
- tempy = tempy->_nastepny;
- //Sprawdzanie czy ktores kostki sa puste
- if (tempx->_binary == 0)
- newList->addNode(tempy->_binary);
- else
- if (tempy->_binary == 0)
- newList->addNode(tempx->_binary);
- else
- //Gdy obie nie puste
- sumBinary(tempx->_binary, tempy->_binary);
- }
- return newList;
- }
- return newList;
- }
- lista* roznica(lista* x, lista* y)
- {
- lista* newList = new lista();
- int count = 0;
- //Czy listy sa puste
- if (x->count() == 0 || y->count() == 0)
- {
- return newList;
- }
- else
- {
- int count = 0;
- //Ktora wieksza
- if (x->count() > y->count())
- count = x->count() - 1; // Odejmowanie pierwszej
- else
- count = y->count() - 1; // Odejmowanie pierwszej
- //Tworzenie nowych kostek
- Node* tempx = new Node();
- tempx = x->_start;
- Node* tempy = new Node();
- tempy = y->_start;
- //Sprawdzanie czy ktoras poczatkowe kostki sa puste
- if (tempx->_binary == 0)
- newList->addNode(-(tempy->_binary) );
- else
- if (tempy->_binary == 0)
- newList->addNode(tempx->_binary);
- else
- //Gdy obie nie puste
- newList->addNode(minusBinary(tempx->_binary, tempy->_binary));
- //Dla reszty listy gdy istnieje
- for (count; count > 0; count--)
- {
- tempx = tempx->_nastepny;
- tempy = tempy->_nastepny;
- //Sprawdzanie czy ktores kostki sa puste
- if (tempx->_binary == 0)
- newList->addNode(-(tempy->_binary) );
- else
- if (tempy->_binary == 0)
- newList->addNode(tempx->_binary);
- else
- //Gdy obie nie puste
- newList->addNode ( minusBinary(tempx->_binary, tempy->_binary) );
- }
- return newList;
- }
- return newList;
- }
- lista* both(lista* x, lista* y)
- {
- lista* newList = new lista();
- int count = 0;
- //Czy listy sa puste
- if (x->count() == 0 || y->count() == 0)
- {
- return newList;
- }
- else
- {
- int count = 0;
- //Ktora wieksza
- if (x->count() > y->count())
- count = x->count() - 1; // Odejmowanie pierwszej
- else
- count = y->count() - 1; // Odejmowanie pierwszej
- //Tworzenie nowych kostek
- Node* tempx = new Node();
- tempx = x->_start;
- Node* tempy = new Node();
- tempy = y->_start;
- //Sprawdzanie czy ktoras poczatkowe kostki sa puste
- if (tempx->_binary == 0 || tempy->_binary == 0)
- {
- }
- else
- //Gdy obie nie puste
- newList->addNode(bothBinary(tempx->_binary, tempy->_binary));
- //Dla reszty listy gdy istnieje
- for (count; count > 0; count--)
- {
- tempx = tempx->_nastepny;
- tempy = tempy->_nastepny;
- //Sprawdzanie czy ktores kostki sa puste
- if (tempx->_binary == 0 || tempy->_binary == 0)
- {
- }
- else
- //Gdy obie nie puste
- newList->addNode(bothBinary(tempx->_binary, tempy->_binary));
- }
- return newList;
- }
- }
- int main()
- {
- lista* list = new lista();
- list->addNode();
- list->showNodes();
- lista* list2 = new lista();
- list2->addNode();
- list2->showNodes();
- lista* lista3 = suma(list, list2); //Dodajemy wynik do 3
- cout << "Wynik sumowania to: " << endl << endl;
- lista3->showNodes();
- lista* lista4 = roznica(list, list2); // Odejmujemy druga liste od pierwszej wynik do 3
- cout << "Wynik roznica to: " << endl << endl;
- lista4->showNodes();
- lista* list5 = both(list, list2);
- cout << "Wynik zawierabua to: " << endl << endl;
- list5->showNodes();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement