Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <istream>
  5.  
  6. using namespace std;
  7.  
  8. struct Book { //Lista ksiazek
  9.     string name;
  10.     Book* next = nullptr;
  11. };
  12.  
  13. struct Label //Lista Etykiet
  14. {
  15.     string name;
  16.     Label* next = nullptr;
  17.  
  18.     Book* booksHead = nullptr; //Glowa listy ksiazek
  19. };
  20.  
  21. //Dodanie Etykieta
  22. Label* pushBack(Label*& head, string const& name)
  23. {
  24.     if (head == nullptr)
  25.     {
  26.         head = new Label{ name };
  27.         return head;
  28.     }
  29.     else
  30.     {
  31.         Label* l = head;
  32.         while (l->next)
  33.             l = l->next;
  34.         l->next = new Label{ name };
  35.         return l->next;
  36.     }
  37. }
  38.  
  39. //Szukanie etykiety
  40. Label* find(Label* head, string const& name)
  41. {
  42.     while (head)
  43.     {
  44.         if (head->name == name)
  45.             return head;
  46.         head = head->next;
  47.     }
  48.     return nullptr;
  49. }
  50.  
  51. //Dodanie Etykiety jesli nie istnieje lub return juz istniejacej etykiety
  52. Label* provideLabel(Label*& head, string const & name)
  53. {
  54.     Label* ret = find(head, name);
  55.     if (ret)
  56.         return ret;
  57.     else
  58.         return pushBack(head, name);
  59. }
  60.  
  61. //Wycina autora z tekstu
  62. string CutAuthor(string book)
  63. {
  64.     int position = book.find(';');
  65.     return book.substr(0, position);
  66. }
  67.  
  68. //Szukanie miejsca do dodania ksiazki alfabetycznie
  69. Book* findAlpBook(Book *head, string name)
  70. {
  71.     while (head)
  72.     {
  73.         if (CutAuthor(head->name) > CutAuthor(name))
  74.             return head;
  75.         head = head->next;
  76.     }
  77.     return nullptr;
  78. }
  79.  
  80. //Dodaj ksiazke na poczatek
  81. Book* pushFrontBook(Book*& head, string name)
  82. {
  83.     Book* l = new Book{ name };
  84.     l->name = name;
  85.     l->next = head;
  86.     head = l;
  87.     return l;
  88. }
  89.  
  90.  
  91. //Dodanie Ksiazki w dane miejsce
  92. Book* insertBeforeBook(Book*& head ,Book* prev, string name)
  93. {
  94.     Book* l = head;
  95.  
  96.     if (l == prev) pushFrontBook(head, name);
  97.     else
  98.     {
  99.         while (l->next != prev)
  100.             l = l->next;
  101.  
  102.         l->next = new Book{ name };
  103.         l->next->next = prev;
  104.         l->next->name = name;
  105.         return l->next;
  106.     }
  107. }
  108.  
  109. //Dodanie ksiazki
  110. Book* pushBook(Book*& head, string name)
  111. {
  112.     if (head == nullptr) //Jesli bedzie puste doda nowwe
  113.     {
  114.         head = new Book{ name };
  115.         return head;
  116.     }
  117.     else //Jesli nie doda ksiezke w dane miejsce sprawdzac kolejnosc alfabetyczna
  118.         insertBeforeBook(head, findAlpBook(head, name), name);
  119. }
  120.  
  121. //Dodawanie ogolne
  122. void push(Label*& labelHead, string const& labelName, string const& bookName)
  123. {
  124.     Label* l = provideLabel(labelHead, labelName);
  125.     pushBook(l->booksHead, bookName);
  126. }
  127.  
  128. //Czyszczenie listy ksiazek
  129. void deleteInsideList(Book*& head)
  130. {
  131.     Book* b = head;
  132.     while (b != nullptr)
  133.     {
  134.         head = b->next;
  135.         delete b;
  136.         b = head;
  137.     }
  138. }
  139.  
  140. //Czyszczenie calkowite
  141. void deleteList(Label*& head)
  142. {
  143.     Label* l = head;
  144.     while (l != nullptr)
  145.     {
  146.         deleteInsideList(l->booksHead);
  147.         head = l->next;
  148.         delete l;
  149.         l = head;
  150.     }
  151. }
  152.  
  153. int main(int argc, char *argv[])
  154. {
  155.     fstream files;
  156.     ofstream saving;
  157.     string parwej = argv[1];
  158.     string parwyj = argv[3];
  159.  
  160.     //Sprawdzanie parametrow uruchomieniowych ewentualne wlaczenie z plikami dymyslnymi
  161.     if (parwej == "-i" && parwyj == "-o")
  162.     {
  163.         files.open(argv[2], ios::in);
  164.         saving.open(argv[4], ofstream::out);
  165.         parwej = argv[2];
  166.         parwyj = argv[4];
  167.     }
  168.     else if (parwej == "-o" && parwyj =="-i")
  169.     {
  170.         files.open(argv[4], ios::in);
  171.         saving.open(argv[2], ofstream::out);
  172.         parwej = argv[4];
  173.         parwyj = argv[2];
  174.     }
  175.     else
  176.     {
  177.         files.open("ksiazki.txt", ios::in);
  178.         saving.open("zapis.txt", ofstream::out);
  179.         parwej = "ksiazki.txt";
  180.         parwyj = "zapis.txt";
  181.     }
  182.  
  183.     Label *head = nullptr; //Utworzenie listy
  184.     string A;
  185.     string B;
  186.     string Labels;
  187.    
  188.     cout << "TRWA ODCZYT..." << endl;
  189.     //======================ODCZYT Z PLIKU==============//
  190.  
  191.     if (files.good() == true)
  192.     {
  193.         while (!files.eof())
  194.         {
  195.             getline(files, A, ';');
  196.             getline(files, B, ';');
  197.             getline(files, Labels);
  198.             B = A + ";" + B;
  199.            
  200.             if (Labels.find(',') != string::npos)
  201.             {
  202.                 do
  203.                 {
  204.                     int Lsize = Labels.find(',');
  205.                     push(head, Labels.substr(0, Lsize), B);
  206.                     Labels.erase(0, Lsize+1);
  207.  
  208.                     if (Labels.find(',') == string::npos)
  209.                         push(head, Labels, B);
  210.  
  211.                 } while (Labels.find(',') != string::npos);
  212.             }
  213.             else
  214.                 push(head, Labels, B);
  215.         }
  216.         files.close(); //koniec odczytu pliku
  217.  
  218.         cout << "Odczytano plik: " << parwej << endl;
  219.  
  220.         //==================ZAPIS DO PLIKU================//
  221.  
  222.         cout << "Zapis do pliku... " << endl;
  223.  
  224.         for (Label* wsk_p = head; wsk_p != nullptr; wsk_p = wsk_p->next)
  225.         {
  226.             saving << wsk_p->name.erase(0, 1) << ':' << endl;
  227.  
  228.             for (Book* wsk_q = wsk_p->booksHead; wsk_q != nullptr; wsk_q = wsk_q->next)
  229.             {
  230.                 saving << wsk_q->name << endl;
  231.             }
  232.             saving << endl;
  233.         }
  234.         saving.close(); //koniec zapisu pliku
  235.  
  236.         cout << "Zapisano do pliku: " << parwyj;
  237.  
  238.         //===========CZYSZCZENIE PAMIECI===============//
  239.         deleteList(head);
  240.  
  241.     }
  242.     else
  243.         cout << "BLAD PLIKU! " << endl; //komunikat w razie bledu odczytu
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement