Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <cstdlib>
- #include <vector>
- #include <algorithm>
- using namespace std;
- class User;
- class Book {
- friend User;
- private:
- string name;
- string surname;
- string title;
- string produce;
- int id;
- public:
- Book(string n, string s, string t, string p, int i) {
- name = n;
- surname = s;
- title = t;
- produce = p;
- id = i;
- }
- };
- ///
- class User {
- private:
- vector<Book *> library;
- char opction;
- public:
- char getOpction() {
- cout << endl << "/-------------------------------/" << endl;
- cout << "1) Dodawanie danych." << endl;
- cout << "2) Import danych" << endl;
- cout << "3) Wyswietlanie danych" << endl;
- cout << "4) Wyswietlanie sortowane po tytule" << endl;
- cout << "5) Wyswietlanie sortowane po numerze" << endl;
- cout << "6) Zapis do pliku" << endl;
- cout << "e) Koniec" << endl;
- cout << "/-------------------------------/" << endl;
- cin >> opction;
- return opction;
- }
- void menu() {
- while (opction != 'e') {
- opction = getOpction();
- if (opction == '1') addBook();
- else if (opction == '2') import();
- else if (opction == '3') wyswietlanie();
- else if (opction == '4') sortowaniePoTytule();
- else if (opction == '5') sortowaniePoNumerze();
- else if (opction == '6') eksport();
- }
- }
- void addBook() {
- string name, surname, title, produce, empty;
- int id;
- cout << "Podaj imie: ";
- getline(cin, empty); // synvhronizacja
- getline(cin, name); // pobranie linijki znakow
- cout << "Podaj nazwisko: ";
- getline(cin, surname);
- cout << "podaj tytul: ";
- getline(cin, title);
- cout << "podaj wydawnictwo: ";
- getline(cin, produce);
- cout << "podaj numer: ";
- cin >> id;
- Book *b = new Book(name, surname, title, produce, id);
- library.push_back(b);
- }
- void import() {
- fstream plik;
- plik.open("Dane.txt");
- if (plik.good()) {
- string name, surname, title, produce, empty;
- int id;
- while (!plik.eof()) {// eof - end of file
- getline(plik, name);
- getline(plik, surname);
- getline(plik, title);
- getline(plik, produce);
- plik >> id;
- getline(plik, empty);
- Book *b = new Book(name, surname, title, produce, id);
- library.push_back(b);
- }
- }
- }
- void wyswietlanie() {
- if (!library.empty()) {
- for (int i = 0; i < library.size(); i++) {
- cout << "Imie: " << library[i]->name << endl;
- cout << "Nazwisko: " << library[i]->surname << endl;
- cout << "Tytul: " << library[i]->title << endl;
- cout << "Wydawca: " << library[i]->produce << endl;
- cout << "Numer: " << library[i]->id << endl;
- cout << "---" << endl;
- }
- } else cout << "Pamiec jest pusta." << endl;
- }
- void eksport() {// zapis do pliku
- if (!library.empty()) {// zabezpieczneie przed pusta pamiecia
- fstream plik;
- plik.clear(); // wyczyszczenie pliku przed zapisem
- plik.open("Dane.txt", ios::out); // w razie nieistnienia pliku zostanie on utworzony
- // zapisywanie danych o ksiazkach do pliku
- for (int i = 0; i < library.size(); i++) {
- plik << library[i]->name << endl;
- plik << library[i]->surname << endl;
- plik << library[i]->title << endl;
- plik << library[i]->produce << endl;
- plik << library[i]->id << endl;
- }
- plik.close();
- cout << "Zapis wykonano" << endl;
- } else cout << "Pamiec pusta." << endl;
- }
- static bool liczby(Book *mniejszy, Book *wiekszy) {// regula sortowania dla liczb
- return mniejszy->id < wiekszy->id;
- }
- static string changeCharSize(string before) { // zmienianie liter na male
- for (int i = 0; i < before.length(); i++) {
- if (before[i] >= 'A' && before[i] <= 'Z')before[i] += 32;
- }
- return before;
- }
- static bool znaki(Book *mniejszy, Book *wiekszy) { // regula sortowanie dla znakow
- string maly = changeCharSize(mniejszy->title), duzy = changeCharSize(wiekszy->title);
- return maly < duzy;
- }
- void sortowaniePoNumerze() {
- sort(library.begin(), library.end(), liczby);
- cout << "Sortowanie zakonczone" << endl;
- }
- void sortowaniePoTytule() {
- sort(library.begin(), library.end(), znaki);
- cout << "Sortowanie zakonczone" << endl;
- }
- };
- int main() {
- User *admin = new User;
- admin->menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement