Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <istream>
- using namespace std;
- struct Book { //Lista ksiazek
- string name;
- Book* next = nullptr;
- };
- struct Label //Lista Etykiet
- {
- string name;
- Label* next = nullptr;
- Book* booksHead = nullptr; //Glowa listy ksiazek
- };
- //Dodanie Etykieta
- Label* pushBack(Label*& head, string const& name)
- {
- if (head == nullptr)
- {
- head = new Label{ name };
- return head;
- }
- else
- {
- Label* l = head;
- while (l->next)
- l = l->next;
- l->next = new Label{ name };
- return l->next;
- }
- }
- //Szukanie etykiety
- Label* find(Label* head, string const& name)
- {
- while (head)
- {
- if (head->name == name)
- return head;
- head = head->next;
- }
- return nullptr;
- }
- //Dodanie Etykiety jesli nie istnieje lub return juz istniejacej etykiety
- Label* provideLabel(Label*& head, string const & name)
- {
- Label* ret = find(head, name);
- if (ret)
- return ret;
- else
- return pushBack(head, name);
- }
- //Wycina autora z tekstu
- string CutAuthor(string book)
- {
- int position = book.find(';');
- return book.substr(0, position);
- }
- //Szukanie miejsca do dodania ksiazki alfabetycznie
- Book* findAlpBook(Book *head, string name)
- {
- while (head)
- {
- if (CutAuthor(head->name) > CutAuthor(name))
- return head;
- head = head->next;
- }
- return nullptr;
- }
- //Dodaj ksiazke na poczatek
- Book* pushFrontBook(Book*& head, string name)
- {
- Book* l = new Book{ name };
- l->name = name;
- l->next = head;
- head = l;
- return l;
- }
- //Dodanie Ksiazki w dane miejsce
- Book* insertBeforeBook(Book*& head ,Book* prev, string name)
- {
- Book* l = head;
- if (l == prev) pushFrontBook(head, name);
- else
- {
- while (l->next != prev)
- l = l->next;
- l->next = new Book{ name };
- l->next->next = prev;
- l->next->name = name;
- return l->next;
- }
- }
- //Dodanie ksiazki
- Book* pushBook(Book*& head, string name)
- {
- if (head == nullptr) //Jesli bedzie puste doda nowwe
- {
- head = new Book{ name };
- return head;
- }
- else //Jesli nie doda ksiezke w dane miejsce sprawdzac kolejnosc alfabetyczna
- insertBeforeBook(head, findAlpBook(head, name), name);
- }
- //Dodawanie ogolne
- void push(Label*& labelHead, string const& labelName, string const& bookName)
- {
- Label* l = provideLabel(labelHead, labelName);
- pushBook(l->booksHead, bookName);
- }
- //Czyszczenie listy ksiazek
- void deleteInsideList(Book*& head)
- {
- Book* b = head;
- while (b != nullptr)
- {
- head = b->next;
- delete b;
- b = head;
- }
- }
- //Czyszczenie calkowite
- void deleteList(Label*& head)
- {
- Label* l = head;
- while (l != nullptr)
- {
- deleteInsideList(l->booksHead);
- head = l->next;
- delete l;
- l = head;
- }
- }
- int main(int argc, char *argv[])
- {
- fstream files;
- ofstream saving;
- string parwej = argv[1];
- string parwyj = argv[3];
- //Sprawdzanie parametrow uruchomieniowych ewentualne wlaczenie z plikami dymyslnymi
- if (parwej == "-i" && parwyj == "-o")
- {
- files.open(argv[2], ios::in);
- saving.open(argv[4], ofstream::out);
- parwej = argv[2];
- parwyj = argv[4];
- }
- else if (parwej == "-o" && parwyj =="-i")
- {
- files.open(argv[4], ios::in);
- saving.open(argv[2], ofstream::out);
- parwej = argv[4];
- parwyj = argv[2];
- }
- else
- {
- files.open("ksiazki.txt", ios::in);
- saving.open("zapis.txt", ofstream::out);
- parwej = "ksiazki.txt";
- parwyj = "zapis.txt";
- }
- Label *head = nullptr; //Utworzenie listy
- string A;
- string B;
- string Labels;
- cout << "TRWA ODCZYT..." << endl;
- //======================ODCZYT Z PLIKU==============//
- if (files.good() == true)
- {
- while (!files.eof())
- {
- getline(files, A, ';');
- getline(files, B, ';');
- getline(files, Labels);
- B = A + ";" + B;
- if (Labels.find(',') != string::npos)
- {
- do
- {
- int Lsize = Labels.find(',');
- push(head, Labels.substr(0, Lsize), B);
- Labels.erase(0, Lsize+1);
- if (Labels.find(',') == string::npos)
- push(head, Labels, B);
- } while (Labels.find(',') != string::npos);
- }
- else
- push(head, Labels, B);
- }
- files.close(); //koniec odczytu pliku
- cout << "Odczytano plik: " << parwej << endl;
- //==================ZAPIS DO PLIKU================//
- cout << "Zapis do pliku... " << endl;
- for (Label* wsk_p = head; wsk_p != nullptr; wsk_p = wsk_p->next)
- {
- saving << wsk_p->name.erase(0, 1) << ':' << endl;
- for (Book* wsk_q = wsk_p->booksHead; wsk_q != nullptr; wsk_q = wsk_q->next)
- {
- saving << wsk_q->name << endl;
- }
- saving << endl;
- }
- saving.close(); //koniec zapisu pliku
- cout << "Zapisano do pliku: " << parwyj;
- //===========CZYSZCZENIE PAMIECI===============//
- deleteList(head);
- }
- else
- cout << "BLAD PLIKU! " << endl; //komunikat w razie bledu odczytu
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement