Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <fstream>
- #include <cstdlib>
- #include <regex>
- using namespace std;
- int nr_ID;
- struct movie {
- int id;
- char title[50];
- unsigned int year;
- char desc[1000];
- char type[8];
- unsigned int number_in;
- unsigned int number_out;
- };
- void save(vector<movie> &A)
- {
- ofstream output_file("base.bin", ios::binary);
- if (output_file) {
- int w = A.size() * sizeof(movie);
- output_file.write((char*)&A[0], w);
- output_file.close();
- }
- else {
- cout << "nie udalo sie otworzyc pliku" << endl;
- exit(1);
- }
- }
- void load(vector<movie> &A) {
- ifstream input_file("base.bin", ios::binary);
- if (input_file) {
- input_file.seekg(0, ios::end);
- int fileSize = ((input_file.tellg()) / sizeof(movie));
- input_file.seekg(0, ios::beg);
- A.resize(fileSize);
- int w = A.size() * sizeof(movie);
- input_file.read((char*)&A[0], w);
- input_file.close();
- }
- else {
- cout << "Nie dalo sie otworzyc pliku" << endl;
- }
- }
- void dodaj(vector<movie> &A)
- {
- movie x;
- x.id = nr_ID;
- cout << "Nowy film o numerze id: " << x.id << endl;
- cout << "Tytul: ";
- cin.ignore();
- cin.getline(x.title, 50);
- cout << "Rok wydania: ";
- cin >> x.year;
- cout << "Opis (max 1000 znakow): ";
- cin.ignore();
- cin.getline(x.desc, 1000);
- int a = 0;
- cout << "Jaki nosnik? Wpisz cyfre: \n" << "(1) VHS\n" << "(2) DVD\n" << "(3) BlueRay\n";
- cin >> a;
- switch (a) {
- case 1:
- x.type[0] = 'V';
- x.type[1] = 'H';
- x.type[2] = 'S';
- x.type[3] = '\0';
- break;
- case 2:
- x.type[0] = 'D';
- x.type[1] = 'V';
- x.type[2] = 'D';
- x.type[3] = '\0';
- break;
- case 3:
- x.type[0] = 'B';
- x.type[1] = 'l';
- x.type[2] = 'u';
- x.type[3] = 'e';
- x.type[4] = 'R';
- x.type[5] = 'a';
- x.type[6] = 'y';
- x.type[7] = '\0';
- break;
- }
- cout << "Ilosc sztuk: ";
- cin >> x.number_in;
- x.number_out = 0;
- nr_ID++;
- A.push_back(x);
- system("CLS");
- cout << "Dodano nowy film!" << endl;
- }
- void Edit(vector<movie>& A) {
- int k;
- cout << "Podaj numer ID filmu, ktory chcesz edytowac: ";
- cin >> k;
- while (k > nr_ID || k < 0) {
- cout << "Niewlasciwy numer ID" << endl;
- cout << "ID: ";
- cin >> k;
- }
- cout << A[k].title << "(tytul)= ";
- cin.ignore();
- cin.getline(A[k].title, 50);
- cout << A[k].year << "(rok wydania)= ";
- cin >> A[k].year;
- cout << "Opis (max 1000 znakow): ";
- cin.ignore();
- cin.getline(A[k].desc, 1000);
- int a = 0;
- cout << "Jaki nosnik? Wpisz cyfre: \n" << "(1) VHS\n" << "(2) DVD\n" << "(3) BlueRay\n" << A[k].type << "= ";
- cin >> a;
- switch (a) {
- case 1:
- A[k].type[0] = 'V';
- A[k].type[1] = 'H';
- A[k].type[2] = 'S';
- A[k].type[3] = '\0';
- break;
- case 2:
- A[k].type[0] = 'D';
- A[k].type[1] = 'V';
- A[k].type[2] = 'D';
- A[k].type[3] = '\0';
- break;
- case 3:
- A[k].type[0] = 'B';
- A[k].type[1] = 'l';
- A[k].type[2] = 'u';
- A[k].type[3] = 'e';
- A[k].type[4] = 'R';
- A[k].type[5] = 'a';
- A[k].type[6] = 'y';
- A[k].type[7] = '\0';
- break;
- }
- cout << A[k].number_in << "(ilosc sztuk)= ";
- cin >> A[k].number_in;
- system("CLS");
- }
- void Wyp(vector<movie>& A) {
- char c = 'n';
- int k;
- while (c != 'y') {
- cout << "Podaj numer ID filmu, ktory chcesz wyporzyczyc: ";
- cin >> k;
- while (k > nr_ID || k < 0) {
- cout << "Niewlasciwy numer ID" << endl;
- cout << "ID: ";
- cin >> k;
- }
- cout << A[k].title << endl;
- cout << "Rok wydania: " << A[k].year << endl;
- cout << "Opis: " << A[k].desc << endl;
- cout << "Nosnik: " << A[k].type << endl;
- cout << "Ilosc na stanie: " << A[k].number_in << " Wypozyczonych: " << A[k].number_out << endl;
- cout << "Chcesz wypozyczyc ten film? y(yes)/n(no)/e(exit) ";
- cin >> c;
- if (c == 'e') {
- system("CLS");
- return;
- }
- }
- A[k].number_in = A[k].number_in - 1;
- A[k].number_out = A[k].number_out + 1;
- system("CLS");
- cout << "Wypozyczono " << A[k].title << " w wypozyczalni pozostalo: " << A[k].number_in << " kopii " << "wypozyczonych: " << A[k].number_out << " kopii" << endl << endl;
- }
- void Zwr(vector<movie>& A) {
- char c = 'n';
- int k;
- while (c != 'y') {
- cout << "Podaj numer ID filmu, ktory chcesz zwrocic: ";
- cin >> k;
- while (k > nr_ID || k < 0) {
- cout << "Niewlasciwy numer ID" << endl;
- cout << "ID: ";
- cin >> k;
- }
- cout << A[k].title << " Chcesz zwrocic ten film? y/n ";
- cin >> c;
- }
- A[k].number_in = A[k].number_in + 1;
- A[k].number_out = A[k].number_out - 1;
- system("CLS");
- cout << "Zwrocono " << A[k].title << " w wypozyczalni pozostalo: " << A[k].number_in << " kopii " << "wypozyczonych: " << A[k].number_out << " kopii" << endl << endl;
- }
- void usun(vector<movie>& A) {
- int x;
- cout << "Podaj nr ID filmu, ktory chcesz usunac: ";
- cin >> x;
- for (int i = 0; i < (nr_ID - x); i++) {
- A[x + i] = A[x + i + 1];
- }
- nr_ID--;
- }
- void search(vector<movie>& A) {
- cout << "Podaj tytul filmu: ";
- string tekst;
- cin.ignore();
- getline(cin, tekst);
- regex tyt(tekst, regex_constants::icase);
- system("CLS");
- for (int i = 0; i < A.size(); i++) {
- int j = 0;
- while (A[i].title[j] != NULL) {
- j++;
- }
- string tab(A[i].title, j);
- smatch wynik;
- if (regex_search(tab, wynik, tyt)) {
- cout <<"Znaleziony film: " << wynik[0] << endl;
- cout << "Nr ID: " << A[i].id << endl;
- cout << A[i].desc << endl;
- cout << "Rok wydania: " << A[i].year << endl;
- cout << "Nosnik: " << A[i].type << endl;
- cout << "Na stanie: " << A[i].number_in << endl;
- cout << "Wypozyczonych: " << A[i].number_out << endl;
- }
- else {
- cout << "Brak filmu" << endl;
- }
- }
- }
- void Menu() {
- cout << "------------Wypozyczalnia Filmow------------" << endl;
- cout << " Muszynski inc. All rights reserved" << endl << endl;
- cout << "><><><><><><><><><><><><><><><><><><><><><><" << endl;
- cout << "| ********** (1) Wypozycz film *********** |" << endl;
- cout << "| ********** (2) Zwroc film ************** |" << endl;
- cout << "| ********** (3) Szukaj ****************** |" << endl;
- cout << "| ********** (4) Dodaj film ************** |" << endl;
- cout << "| ********** (5) Usun film *************** |" << endl;
- cout << "| ********** (6) Edytuj film ************* |" << endl;
- cout << "| ********** (9) Zakoncz program ********* |" << endl;
- cout << "><><><><><><><><><><><><><><><><><><><><><><" << endl;
- }
- int main()
- {
- fstream base;
- int d = 0;
- vector<movie> A;
- fstream P("id.txt", ios::in);
- if (P) {
- string linia;
- getline(P, linia);
- nr_ID = atoi(linia.c_str());
- }
- else {
- cout << "Error" << endl;
- }
- load(A);
- while (d != 9) {
- Menu();
- cin >> d;
- switch (d) {
- case 1: Wyp(A);
- break;
- case 2: Zwr(A);
- break;
- case 3: search(A);
- break;
- case 4: dodaj(A);
- break;
- case 5: usun(A);
- break;
- case 6: Edit(A);
- break;
- }
- }
- save(A);
- fstream I("id.txt", ios::out);
- I << nr_ID;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement