Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <fstream>
  5. #include <cstdlib>
  6. #include <regex>
  7.  
  8. using namespace std;
  9.  
  10. int nr_ID;
  11.  
  12. struct movie {
  13. int id;
  14. char title[50];
  15. unsigned int year;
  16. char desc[1000];
  17. char type[8];
  18. unsigned int number_in;
  19. unsigned int number_out;
  20.  
  21. };
  22.  
  23. void save(vector<movie> &A)
  24. {
  25. ofstream output_file("base.bin", ios::binary);
  26. if (output_file) {
  27. int w = A.size() * sizeof(movie);
  28. output_file.write((char*)&A[0], w);
  29. output_file.close();
  30.  
  31. }
  32. else {
  33. cout << "nie udalo sie otworzyc pliku" << endl;
  34. exit(1);
  35. }
  36.  
  37.  
  38. }
  39. void load(vector<movie> &A) {
  40. ifstream input_file("base.bin", ios::binary);
  41.  
  42. if (input_file) {
  43. input_file.seekg(0, ios::end);
  44. int fileSize = ((input_file.tellg()) / sizeof(movie));
  45. input_file.seekg(0, ios::beg);
  46. A.resize(fileSize);
  47.  
  48. int w = A.size() * sizeof(movie);
  49. input_file.read((char*)&A[0], w);
  50.  
  51.  
  52. input_file.close();
  53. }
  54.  
  55. else {
  56. cout << "Nie dalo sie otworzyc pliku" << endl;
  57.  
  58. }
  59.  
  60. }
  61.  
  62. void dodaj(vector<movie> &A)
  63. {
  64. movie x;
  65. x.id = nr_ID;
  66. cout << "Nowy film o numerze id: " << x.id << endl;
  67. cout << "Tytul: ";
  68. cin.ignore();
  69. cin.getline(x.title, 50);
  70. cout << "Rok wydania: ";
  71. cin >> x.year;
  72. cout << "Opis (max 1000 znakow): ";
  73. cin.ignore();
  74. cin.getline(x.desc, 1000);
  75. int a = 0;
  76. cout << "Jaki nosnik? Wpisz cyfre: \n" << "(1) VHS\n" << "(2) DVD\n" << "(3) BlueRay\n";
  77. cin >> a;
  78. switch (a) {
  79. case 1:
  80. x.type[0] = 'V';
  81. x.type[1] = 'H';
  82. x.type[2] = 'S';
  83. x.type[3] = '\0';
  84. break;
  85. case 2:
  86. x.type[0] = 'D';
  87. x.type[1] = 'V';
  88. x.type[2] = 'D';
  89. x.type[3] = '\0';
  90. break;
  91. case 3:
  92. x.type[0] = 'B';
  93. x.type[1] = 'l';
  94. x.type[2] = 'u';
  95. x.type[3] = 'e';
  96. x.type[4] = 'R';
  97. x.type[5] = 'a';
  98. x.type[6] = 'y';
  99. x.type[7] = '\0';
  100. break;
  101. }
  102. cout << "Ilosc sztuk: ";
  103. cin >> x.number_in;
  104. x.number_out = 0;
  105. nr_ID++;
  106.  
  107. A.push_back(x);
  108. system("CLS");
  109. cout << "Dodano nowy film!" << endl;
  110.  
  111. }
  112. void Edit(vector<movie>& A) {
  113. int k;
  114. cout << "Podaj numer ID filmu, ktory chcesz edytowac: ";
  115. cin >> k;
  116. while (k > nr_ID || k < 0) {
  117. cout << "Niewlasciwy numer ID" << endl;
  118. cout << "ID: ";
  119. cin >> k;
  120. }
  121.  
  122. cout << A[k].title << "(tytul)= ";
  123. cin.ignore();
  124. cin.getline(A[k].title, 50);
  125. cout << A[k].year << "(rok wydania)= ";
  126. cin >> A[k].year;
  127. cout << "Opis (max 1000 znakow): ";
  128. cin.ignore();
  129. cin.getline(A[k].desc, 1000);
  130. int a = 0;
  131. cout << "Jaki nosnik? Wpisz cyfre: \n" << "(1) VHS\n" << "(2) DVD\n" << "(3) BlueRay\n" << A[k].type << "= ";
  132. cin >> a;
  133. switch (a) {
  134. case 1:
  135. A[k].type[0] = 'V';
  136. A[k].type[1] = 'H';
  137. A[k].type[2] = 'S';
  138. A[k].type[3] = '\0';
  139. break;
  140. case 2:
  141. A[k].type[0] = 'D';
  142. A[k].type[1] = 'V';
  143. A[k].type[2] = 'D';
  144. A[k].type[3] = '\0';
  145. break;
  146. case 3:
  147. A[k].type[0] = 'B';
  148. A[k].type[1] = 'l';
  149. A[k].type[2] = 'u';
  150. A[k].type[3] = 'e';
  151. A[k].type[4] = 'R';
  152. A[k].type[5] = 'a';
  153. A[k].type[6] = 'y';
  154. A[k].type[7] = '\0';
  155. break;
  156. }
  157. cout << A[k].number_in << "(ilosc sztuk)= ";
  158. cin >> A[k].number_in;
  159. system("CLS");
  160.  
  161.  
  162. }
  163.  
  164. void Wyp(vector<movie>& A) {
  165. char c = 'n';
  166. int k;
  167. while (c != 'y') {
  168. cout << "Podaj numer ID filmu, ktory chcesz wyporzyczyc: ";
  169. cin >> k;
  170. while (k > nr_ID || k < 0) {
  171. cout << "Niewlasciwy numer ID" << endl;
  172. cout << "ID: ";
  173. cin >> k;
  174. }
  175. cout << A[k].title << endl;
  176. cout << "Rok wydania: " << A[k].year << endl;
  177. cout << "Opis: " << A[k].desc << endl;
  178. cout << "Nosnik: " << A[k].type << endl;
  179. cout << "Ilosc na stanie: " << A[k].number_in << " Wypozyczonych: " << A[k].number_out << endl;
  180. cout << "Chcesz wypozyczyc ten film? y(yes)/n(no)/e(exit) ";
  181. cin >> c;
  182. if (c == 'e') {
  183. system("CLS");
  184. return;
  185. }
  186.  
  187. }
  188. A[k].number_in = A[k].number_in - 1;
  189. A[k].number_out = A[k].number_out + 1;
  190. system("CLS");
  191. cout << "Wypozyczono " << A[k].title << " w wypozyczalni pozostalo: " << A[k].number_in << " kopii " << "wypozyczonych: " << A[k].number_out << " kopii" << endl << endl;
  192.  
  193. }
  194. void Zwr(vector<movie>& A) {
  195. char c = 'n';
  196. int k;
  197. while (c != 'y') {
  198. cout << "Podaj numer ID filmu, ktory chcesz zwrocic: ";
  199. cin >> k;
  200. while (k > nr_ID || k < 0) {
  201. cout << "Niewlasciwy numer ID" << endl;
  202. cout << "ID: ";
  203. cin >> k;
  204. }
  205. cout << A[k].title << " Chcesz zwrocic ten film? y/n ";
  206. cin >> c;
  207.  
  208. }
  209. A[k].number_in = A[k].number_in + 1;
  210. A[k].number_out = A[k].number_out - 1;
  211. system("CLS");
  212. cout << "Zwrocono " << A[k].title << " w wypozyczalni pozostalo: " << A[k].number_in << " kopii " << "wypozyczonych: " << A[k].number_out << " kopii" << endl << endl;
  213.  
  214.  
  215. }
  216.  
  217. void usun(vector<movie>& A) {
  218. int x;
  219. cout << "Podaj nr ID filmu, ktory chcesz usunac: ";
  220. cin >> x;
  221. for (int i = 0; i < (nr_ID - x); i++) {
  222.  
  223. A[x + i] = A[x + i + 1];
  224.  
  225. }
  226. nr_ID--;
  227.  
  228. }
  229. void search(vector<movie>& A) {
  230.  
  231. cout << "Podaj tytul filmu: ";
  232. string tekst;
  233. cin.ignore();
  234. getline(cin, tekst);
  235. regex tyt(tekst, regex_constants::icase);
  236.  
  237. system("CLS");
  238.  
  239. for (int i = 0; i < A.size(); i++) {
  240. int j = 0;
  241. while (A[i].title[j] != NULL) {
  242. j++;
  243. }
  244.  
  245. string tab(A[i].title, j);
  246. smatch wynik;
  247. if (regex_search(tab, wynik, tyt)) {
  248.  
  249. cout <<"Znaleziony film: " << wynik[0] << endl;
  250. cout << "Nr ID: " << A[i].id << endl;
  251. cout << A[i].desc << endl;
  252. cout << "Rok wydania: " << A[i].year << endl;
  253. cout << "Nosnik: " << A[i].type << endl;
  254. cout << "Na stanie: " << A[i].number_in << endl;
  255. cout << "Wypozyczonych: " << A[i].number_out << endl;
  256.  
  257.  
  258. }
  259. else {
  260. cout << "Brak filmu" << endl;
  261. }
  262.  
  263.  
  264.  
  265. }
  266.  
  267. }
  268.  
  269. void Menu() {
  270.  
  271. cout << "------------Wypozyczalnia Filmow------------" << endl;
  272. cout << " Muszynski inc. All rights reserved" << endl << endl;
  273. cout << "><><><><><><><><><><><><><><><><><><><><><><" << endl;
  274. cout << "| ********** (1) Wypozycz film *********** |" << endl;
  275. cout << "| ********** (2) Zwroc film ************** |" << endl;
  276. cout << "| ********** (3) Szukaj ****************** |" << endl;
  277. cout << "| ********** (4) Dodaj film ************** |" << endl;
  278. cout << "| ********** (5) Usun film *************** |" << endl;
  279. cout << "| ********** (6) Edytuj film ************* |" << endl;
  280. cout << "| ********** (9) Zakoncz program ********* |" << endl;
  281. cout << "><><><><><><><><><><><><><><><><><><><><><><" << endl;
  282.  
  283. }
  284.  
  285.  
  286. int main()
  287. {
  288. fstream base;
  289. int d = 0;
  290. vector<movie> A;
  291. fstream P("id.txt", ios::in);
  292. if (P) {
  293. string linia;
  294. getline(P, linia);
  295. nr_ID = atoi(linia.c_str());
  296. }
  297. else {
  298. cout << "Error" << endl;
  299. }
  300.  
  301. load(A);
  302.  
  303.  
  304. while (d != 9) {
  305. Menu();
  306. cin >> d;
  307. switch (d) {
  308. case 1: Wyp(A);
  309. break;
  310. case 2: Zwr(A);
  311. break;
  312. case 3: search(A);
  313. break;
  314. case 4: dodaj(A);
  315. break;
  316. case 5: usun(A);
  317. break;
  318. case 6: Edit(A);
  319. break;
  320.  
  321.  
  322. }
  323.  
  324. }
  325. save(A);
  326. fstream I("id.txt", ios::out);
  327. I << nr_ID;
  328.  
  329. return 0;
  330. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement