Advertisement
SIKER_98

book

Jan 25th, 2020
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <cstdlib>
  5. #include <vector>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. class User;
  11.  
  12. class Book {
  13. friend User;
  14. private:
  15. string name;
  16. string surname;
  17. string title;
  18. string produce;
  19. int id;
  20.  
  21. public:
  22. Book(string n, string s, string t, string p, int i) {
  23. name = n;
  24. surname = s;
  25. title = t;
  26. produce = p;
  27. id = i;
  28. }
  29.  
  30.  
  31. };
  32.  
  33. ///
  34.  
  35. class User {
  36. private:
  37. vector<Book *> library;
  38. char opction;
  39.  
  40. public:
  41. char getOpction() {
  42. cout << endl << "/-------------------------------/" << endl;
  43. cout << "1) Dodawanie danych." << endl;
  44. cout << "2) Import danych" << endl;
  45. cout << "3) Wyswietlanie danych" << endl;
  46. cout << "4) Wyswietlanie sortowane po tytule" << endl;
  47. cout << "5) Wyswietlanie sortowane po numerze" << endl;
  48. cout << "6) Zapis do pliku" << endl;
  49. cout << "e) Koniec" << endl;
  50. cout << "/-------------------------------/" << endl;
  51. cin >> opction;
  52. return opction;
  53. }
  54.  
  55. void menu() {
  56. while (opction != 'e') {
  57. opction = getOpction();
  58. if (opction == '1') addBook();
  59. else if (opction == '2') import();
  60. else if (opction == '3') wyswietlanie();
  61. else if (opction == '4') sortowaniePoTytule();
  62. else if (opction == '5') sortowaniePoNumerze();
  63. else if (opction == '6') eksport();
  64. }
  65. }
  66.  
  67. void addBook() {
  68. string name, surname, title, produce, empty;
  69. int id;
  70. cout << "Podaj imie: ";
  71. getline(cin, empty); // synvhronizacja
  72. getline(cin, name); // pobranie linijki znakow
  73. cout << "Podaj nazwisko: ";
  74. getline(cin, surname);
  75. cout << "podaj tytul: ";
  76. getline(cin, title);
  77. cout << "podaj wydawnictwo: ";
  78. getline(cin, produce);
  79. cout << "podaj numer: ";
  80. cin >> id;
  81. Book *b = new Book(name, surname, title, produce, id);
  82. library.push_back(b);
  83. }
  84.  
  85. void import() {
  86. fstream plik;
  87. plik.open("Dane.txt");
  88. if (plik.good()) {
  89. string name, surname, title, produce, empty;
  90. int id;
  91. while (!plik.eof()) {// eof - end of file
  92.  
  93. getline(plik, name);
  94. getline(plik, surname);
  95. getline(plik, title);
  96. getline(plik, produce);
  97. plik >> id;
  98. getline(plik, empty);
  99. Book *b = new Book(name, surname, title, produce, id);
  100. library.push_back(b);
  101.  
  102. }
  103. }
  104. }
  105.  
  106. void wyswietlanie() {
  107. if (!library.empty()) {
  108. for (int i = 0; i < library.size(); i++) {
  109.  
  110. cout << "Imie: " << library[i]->name << endl;
  111. cout << "Nazwisko: " << library[i]->surname << endl;
  112. cout << "Tytul: " << library[i]->title << endl;
  113. cout << "Wydawca: " << library[i]->produce << endl;
  114. cout << "Numer: " << library[i]->id << endl;
  115. cout << "---" << endl;
  116. }
  117. } else cout << "Pamiec jest pusta." << endl;
  118. }
  119.  
  120. void eksport() {// zapis do pliku
  121. if (!library.empty()) {// zabezpieczneie przed pusta pamiecia
  122. fstream plik;
  123. plik.clear(); // wyczyszczenie pliku przed zapisem
  124. plik.open("Dane.txt", ios::out); // w razie nieistnienia pliku zostanie on utworzony
  125.  
  126. // zapisywanie danych o ksiazkach do pliku
  127. for (int i = 0; i < library.size(); i++) {
  128. plik << library[i]->name << endl;
  129. plik << library[i]->surname << endl;
  130. plik << library[i]->title << endl;
  131. plik << library[i]->produce << endl;
  132. plik << library[i]->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. sort(library.begin(), library.end(), liczby);
  157. cout << "Sortowanie zakonczone" << endl;
  158. }
  159.  
  160. void sortowaniePoTytule() {
  161. sort(library.begin(), library.end(), 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