Advertisement
SIKER_98

listy

Jan 31st, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <list>
  8.  
  9. using namespace std;
  10.  
  11. class User;
  12.  
  13. class Book {
  14. public:
  15. string name;
  16. string surname;
  17. string title;
  18. string produce;
  19. int id;
  20.  
  21. Book(string n, string s, string t, string p, int i) {
  22. name = n;
  23. surname = s;
  24. title = t;
  25. produce = p;
  26. id = i;
  27. }
  28. };
  29.  
  30. ///
  31.  
  32. class User {
  33. private:
  34. list<Book *> library;
  35. char opction;
  36.  
  37. public:
  38. char getOpction() {
  39. cout << endl << "/-------------------------------/" << endl;
  40. cout << "1) Dodawanie danych." << endl;
  41. cout << "2) Import danych" << endl;
  42. cout << "3) Wyswietlanie danych" << endl;
  43. cout << "4) Wyswietlanie sortowane po tytule" << endl;
  44. cout << "5) Wyswietlanie sortowane po numerze" << endl;
  45. cout << "6) Zapis do pliku" << endl;
  46. cout << "e) Koniec" << endl;
  47. cout << "/-------------------------------/" << endl;
  48. cin >> opction;
  49. return opction;
  50. }
  51.  
  52. void menu() {
  53. while (opction != 'e') {
  54. opction = getOpction();
  55. if (opction == '1') addBook();
  56. else if (opction == '2') import();
  57. else if (opction == '3') wyswietlanie();
  58. else if (opction == '4') sortowaniePoTytule();
  59. else if (opction == '5') sortowaniePoNumerze();
  60. else if (opction == '6') eksport();
  61. }
  62. }
  63.  
  64. void addBook() {
  65. string name, surname, title, produce, empty;
  66. int id;
  67. cout << "Podaj imie: ";
  68. getline(cin, empty); // synvhronizacja
  69. getline(cin, name); // pobranie linijki znakow
  70. cout << "Podaj nazwisko: ";
  71. getline(cin, surname);
  72. cout << "podaj tytul: ";
  73. getline(cin, title);
  74. cout << "podaj wydawnictwo: ";
  75. getline(cin, produce);
  76. cout << "podaj numer: ";
  77. cin >> id;
  78. Book *b = new Book(name, surname, title, produce, id);
  79. library.push_back(b);
  80. }
  81.  
  82. void import() {
  83. fstream plik;
  84. plik.open("Dane.txt");
  85. if (plik.good()) {
  86. string name, surname, title, produce, empty;
  87. int id;
  88. while (!plik.eof()) {// eof - end of file
  89.  
  90. getline(plik, name);
  91. getline(plik, surname);
  92. getline(plik, title);
  93. getline(plik, produce);
  94. plik >> id;
  95. getline(plik, empty);
  96. Book *b = new Book(name, surname, title, produce, id);
  97. library.push_back(b);
  98.  
  99. }
  100. }
  101. }
  102.  
  103. void wyswietlanie() {
  104. if (!library.empty()) {
  105. Book *tmp;
  106. for (list<Book *>::iterator it = library.begin(); it != library.end(); it++) {
  107. tmp = *it;
  108. cout << "Imie: " << tmp->name << endl;
  109. cout << "Nazwisko: " << tmp->surname << endl;
  110. cout << "Tytul: " << tmp->title << endl;
  111. cout << "Wydawca: " << tmp->produce << endl;
  112. cout << "Numer: " << tmp->id << endl;
  113. cout << "---" << endl;
  114. }
  115. } else cout << "Pamiec jest pusta." << endl;
  116. }
  117.  
  118. void eksport() {// zapis do pliku
  119. if (!library.empty()) {// zabezpieczneie przed pusta pamiecia
  120. fstream plik;
  121. plik.clear(); // wyczyszczenie pliku przed zapisem
  122. plik.open("Dane.txt", ios::out); // w razie nieistnienia pliku zostanie on utworzony
  123.  
  124. // zapisywanie danych o ksiazkach do pliku
  125. Book *tmp = *library.begin();
  126. for (list<Book *>::iterator it = library.begin(); it != library.end(); it++) {
  127. tmp = *it;
  128. plik << tmp->name << endl;
  129. plik << tmp->surname << endl;
  130. plik << tmp->title << endl;
  131. plik << tmp->produce << endl;
  132. plik << tmp->id << endl;
  133. }
  134. plik.close();
  135. cout << "Zapis wykonano" << endl;
  136. } else cout << "Pamiec pusta." << endl;
  137. }
  138.  
  139. static bool liczby(Book *mniejszy, Book *wiekszy) {// regula sortowania dla liczb
  140. return mniejszy->id < wiekszy->id;
  141. }
  142.  
  143. static string changeCharSize(string before) { // zmienianie liter na male
  144. for (int i = 0; i < before.length(); i++) {
  145. if (before[i] >= 'A' && before[i] <= 'Z')before[i] += 32;
  146. }
  147. return before;
  148. }
  149.  
  150. static bool znaki(Book *mniejszy, Book *wiekszy) { // regula sortowanie dla znakow
  151. string maly = changeCharSize(mniejszy->title), duzy = changeCharSize(wiekszy->title);
  152. return maly < duzy;
  153. }
  154.  
  155. void sortowaniePoNumerze() {
  156. library.sort(liczby);
  157. cout << "Sortowanie zakonczone" << endl;
  158. }
  159.  
  160. void sortowaniePoTytule() {
  161. library.sort(znaki);
  162. cout << "Sortowanie zakonczone" << endl;
  163. }
  164. };
  165.  
  166.  
  167. int main() {
  168. User *admin = new User;
  169. admin->menu();
  170. return 0;
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement