dariagalich

Untitled

Dec 1st, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string>
  4. #include <iomanip>
  5. #include <conio.h>
  6. #include <fstream>
  7. #include <locale.h>
  8. #include <windows.h>
  9. using namespace std;
  10. //----КОНСТАНТЫ----------
  11. const int up = 72,
  12. down = 80,
  13. right_btn = 77,
  14. left_btn = 75,
  15. enter = 13,
  16. esc = 27,
  17. del = 83,
  18. d_n = 20;
  19. //-------------------------------------------
  20. struct info {
  21. string autor;
  22. string name;
  23. string izdatelstvo;
  24. string janr;
  25. int d = 0, m = 0, g = 0, cost = 0;
  26. };
  27. struct book {
  28. info inf;
  29. book* next;
  30. book* pred;
  31. };
  32. //-------------------------------------------
  33. book vvod_book();
  34. book* dob(book* end, const book& s);
  35. book* dob_first(const book& s);
  36. book* udal(book* beg);
  37. void print(const book& s);
  38. void prosmotr(book* beg);
  39. void old_book(book beg);
  40. void sort_cost(book **beg);
  41. void sort_data(book* book);
  42. void searchname(book* beg);
  43. void searchautor(book* beg);
  44. int change(book* beg, string& user_autor, string& new_user_autor);
  45. int read_file(char* filename, book** beg, book** end);
  46. int write_file(char* filename, book* temp);
  47. int menu();
  48. int menu2();
  49. //--------основная программа-----------------------------------
  50. int main()
  51. {
  52. ShowWindow(GetConsoleWindow(), SW_MAXIMIZE); // полноэкранный режим
  53. setlocale(LC_ALL, "Rus");
  54. system( "color 8F" );
  55. book* beg = NULL,
  56. * end = NULL, input;
  57. char filename[20];
  58. int Num;
  59. string user_autor, new_user_autor;
  60. while (1) {
  61. switch (menu())
  62. {
  63. case 1://создание списка или добовление нового эл-та в список
  64. if (beg)
  65. end = dob(end, vvod_book());
  66. else
  67. {
  68. beg = dob_first(vvod_book());
  69. end = beg;
  70. }
  71. break;
  72. case 2://удаление всех элементов в списке
  73. beg = udal(beg);
  74. cout << "Нажмите любую клавишу" << endl;
  75. cin.get();
  76. break;
  77. case 3://просмотр
  78. prosmotr(beg);
  79. cin.get();
  80. break;
  81. case 4://запись в файл
  82. write_file(filename, beg);
  83. break;
  84. case 5://чтение из файла
  85. read_file(filename, &beg, &end);
  86. break;
  87. case 6://редактирование списка
  88. cout << "Введите автора, которого вы хотите изменить: " << endl;
  89. cin >> user_autor;
  90. cout << "Введите нового атвора: " << endl;
  91. cin >> new_user_autor;
  92. Num = change(beg, user_autor, new_user_autor);
  93. if (Num == 1) cout << "Автор успешно перезаписан." << endl;
  94. else cout << "Автор не найден!" << endl;
  95. system("pause");
  96. change(beg, user_autor, new_user_autor);
  97. cin.get();
  98. break;
  99. case 7://поиск
  100. {
  101. switch (menu2()) {
  102. case 1://поск по автору
  103. searchautor(beg);
  104. cin.get();
  105. break;
  106. case 2:
  107. searchname(beg);//поиск по названию книги
  108. cin.get();
  109. break;
  110. case 3:
  111. case 9:break;
  112. }
  113. break;
  114. }
  115. case 8: sort_cost(&beg); break;
  116. case 9: return 0;
  117. }
  118. }
  119. return 0;
  120. }
  121.  
  122. //-----------------------------------------------------------------------------
  123. int menu()
  124. {
  125. char buf[7];
  126. int k;
  127. do
  128. {
  129. system("CLS");
  130. cout << "============================" << endl;
  131. cout << " МЕНЮ " << endl;
  132. cout << "============================" << endl;
  133. cout << "Введите номер пункта меню" << endl;
  134. cout << "1 - Создать список или добавить новый эл-т" << endl;
  135. cout << "2 - Удаление всех элементов списка" << endl;
  136. cout << "3 - Просмотр списка" << endl;
  137. cout << "4 - Запись данных в файл" << endl;
  138. cout << "5 - Чтение из файла" << endl;
  139. cout << "6 - Изменить" << endl;
  140. cout << "7 - Поиск" << endl;
  141. cout << "8 - Сортировка по дате" << endl;
  142. cout << "9 - Выход" << endl;
  143. cout << "============================" << endl;
  144. cin >> buf;
  145. cin.get();
  146. k = atoi(buf);
  147. if (!k)
  148. {
  149. cout << "Вам следует вводить число от 1 до 7" << endl;
  150. cin.get();
  151. }
  152. } while (!k);
  153. return k;
  154. }
  155. //-----------------------------------------------------------------------------
  156. int menu2()
  157. {
  158. char buf[7];
  159. int m;
  160. do
  161. {
  162. system("CLS");
  163. cout << "============================" << endl;
  164. cout << "Выберите поле по которому хотите осуществить поиск" << endl;
  165. cout << "1 - Автор книги" << endl;
  166. cout << "2 - Название книги" << endl;
  167. cout << "9 - Выход в главное меню" << endl;
  168. cout << "============================" << endl;
  169. cin >> buf;
  170. cin.get();
  171. m = atoi(buf);
  172. if (!m)
  173. {
  174. cout << "Вам следует вводить число от 1 до 7" << endl;
  175. cin.get();
  176. }
  177. } while (!m);
  178. return m;
  179. }
  180.  
  181. //-----------------------------------------------------------------------------
  182. int change(book* beg, string& user_autor, string& new_user_autor)
  183. {
  184. book* temp = beg;
  185. while (temp != NULL)
  186. {
  187. if (temp->inf.autor == user_autor)
  188. {
  189. temp->inf.autor = new_user_autor;
  190. return 1;
  191. }
  192. else
  193. {
  194. temp = temp->next;
  195.  
  196. }
  197. }
  198. return 0;
  199. }
  200. //------------------------------------------------------------------------------
  201. void searchname(book* beg)
  202. {
  203. book* temp = beg;
  204. char fname[d_n];
  205.  
  206. system("cls");
  207.  
  208. if (!beg)
  209. {
  210. MessageBox(0, L"Список пуст", L"Уведомление", MB_ICONINFORMATION | MB_SETFOREGROUND);
  211. return;
  212. }
  213.  
  214. cout << "Введите название книги для поиска" << endl;
  215. cin >> fname;
  216.  
  217. while (temp)
  218. {
  219. if (fname == temp->inf.name)
  220. {
  221. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  222. cout << "| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |" << endl;
  223. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  224. print(*temp);
  225. system("pause");
  226. return;
  227. }
  228. temp = temp->next;
  229.  
  230. }
  231. cout << "Книги с таким названием не найдено" << endl;
  232. system("pause");
  233. }
  234. //-----------------------------------------------------------------------------
  235. void searchautor(book* beg)
  236. {
  237. book* temp = beg;
  238. char fa[d_n];
  239. system("cls");
  240. if (!beg)
  241. {
  242. MessageBox(0, L"Список пуст", L"Уведомление", MB_ICONINFORMATION | MB_SETFOREGROUND);
  243. return;
  244. }
  245. cout << "Введите автора для поиска" << endl;
  246. cin >> fa;
  247. while (temp)
  248. {
  249. if (fa == temp->inf.autor)
  250. {
  251. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  252. cout << "| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |" << endl;
  253. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  254. print(*temp);
  255. system("pause");
  256. return;
  257. }
  258. temp = temp->next;
  259. }
  260. cout << "Книги с таким автором не найдено" << endl;
  261. system("pause");
  262. }
  263. //-----------------------------------------------------------------------------
  264. void old_book(book beg)
  265. {
  266. sort_data(&beg);
  267. system("cls");
  268. if (!beg)
  269. {
  270. MessageBox(0, L"Список пуст", L"Уведомление",
  271. MB_ICONINFORMATION | MB_SETFOREGROUND);
  272. return;
  273. }
  274. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  275. cout << "| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |" << endl;
  276. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  277. for (int i = 0; i < 5; i++)
  278. print(temp);
  279. system("pause");
  280. return;
  281. temp = temp->next;
  282.  
  283. cout <<"+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+"<<endl;
  284. cin.get();
  285.  
  286. }
  287. //-----------------------------------------------------------------------------
  288. void prosmotr(book* beg)
  289. {
  290. if (!beg)
  291. {
  292. cout << "список пустой" << endl;
  293. cin.get();
  294. return;
  295. }
  296. book* temp = beg;
  297. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  298. cout << "| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |" << endl;
  299. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  300. while (temp)
  301. {
  302. print(*temp);
  303. temp = temp->next;
  304. }
  305. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  306. cin.get();
  307. }
  308. //-----------------------------------------------------------------------------
  309. book vvod_book()
  310. {
  311. book s;
  312. cout << "Введите автора:" << endl;
  313. while (1)
  314. {
  315. cin >> s.inf.autor;
  316. //if (atoi(s.autor)==0)
  317. // if((s.autor[0] <= '0') && (s.autor[0] >= '9' ) )
  318. break;
  319. cout << "Ошибка ввода!" << endl;
  320. }
  321. cout << "Введите название книги:" << endl;
  322. while (1)
  323. {
  324. cin >> s.inf.name;
  325. //if (atoi(s.name)==0)
  326. break;
  327. cout << "Ошибка ввода!" << endl;
  328. }
  329. cout << "Введите издательство:" << endl;
  330. while (1)
  331. {
  332. cin >> s.inf.izdatelstvo;
  333. //if (atoi(s.izdatelstvo) == 0)
  334. break;
  335. cout << "Ошибка ввода!" << endl;
  336. }
  337. cout << "Введите жанр:" << endl;
  338. while (1)
  339. {
  340. cin >> s.inf.janr;
  341. //if (atoi(s.janr) == 0)
  342. break;
  343. cout << "Ошибка ввода!" << endl;
  344. }
  345. cout << "Введите дату поступления:" << endl;
  346. cout << "Введите день:";
  347. while (1)
  348. {
  349. cin >> s.inf.d;
  350. if ((s.inf.d >= 1) && (s.inf.d <= 31))
  351. break;
  352. cout << "Ошибка ввода!Ведите правильную дату!" << endl;
  353. }
  354. cout << "Введите месяц:";
  355. while (1)
  356. {
  357. cin >> s.inf.m;
  358. if ((s.inf.m >= 1) && (s.inf.m <= 12))
  359. break;
  360. cout << "Ошибка ввода!Ведите правильную дату!" << endl;
  361. }
  362. cout << "Введите год:";
  363. while (1)
  364. {
  365. cin >> s.inf.g;
  366. if ((s.inf.g >= 1900) && (s.inf.g <= 2050))
  367. break;
  368. cout << "Ошибка ввода!Ведите правильную дату!" << endl;
  369. }
  370. cout << "Введите стоимость книги:" << endl;
  371. cin >> s.inf.cost;
  372. return s;
  373. }
  374. //-----------------------------------------------------------------------------
  375. void print(const book& s)
  376. {
  377. cout << "|" << s.inf.autor << setw(20 - (s.inf.autor).length()) << "|";
  378. cout << s.inf.name << setw(21 - (s.inf.name).length()) << "|";
  379. cout << s.inf.izdatelstvo << setw(19 - (s.inf.izdatelstvo).length()) << "|";
  380. cout << s.inf.janr << setw(17 - (s.inf.janr).length()) << "|";
  381. if (s.inf.d < 10) cout << 0 << s.inf.d << ".";
  382. if (s.inf.m < 10) cout << 0 << s.inf.m << "." << s.inf.g << setw(19) << "|";
  383. cout << s.inf.cost << setw(16) << "|" << endl;
  384.  
  385. }
  386. //--------------ф-я добавления или создания эл-та-------------------------------
  387. book* dob(book* end, const book& s)
  388. {
  389. book* newE = new book;
  390. *newE = s;
  391. newE->next = 0;
  392. end->next = newE;
  393. end = newE;
  394. return end;
  395. }
  396. //--------------создание первого эл-та------------------------------
  397. book* dob_first(const book& s)
  398. {
  399. book* beg = new book;
  400. *beg = s;
  401. beg->next = 0;
  402. return beg;
  403. }
  404. //-----------------------------------------------------------------------------
  405. int read_file(char* filename, book** beg, book** end)
  406. {
  407. cout << "Введите название файла" << endl;
  408. cin >> filename;
  409. ifstream fin(filename, ios::in);
  410. if (!fin) {
  411.  
  412. MessageBox(0, L"Невозможно открыть файл!", L"Ошибка", MB_ICONERROR | MB_SETFOREGROUND);
  413. return 0;
  414.  
  415. }
  416. book s;
  417. *beg = 0;
  418. fin.seekg(0, ios::beg);
  419. while (fin >> s.inf.autor)
  420. {
  421. fin >> s.inf.name;
  422. fin >> s.inf.izdatelstvo;
  423. fin >> s.inf.janr;
  424. fin >> s.inf.d;
  425. fin >> s.inf.m;
  426. fin >> s.inf.g;
  427. fin >> s.inf.cost;
  428. if (*beg)
  429. *end = dob(*end, s);
  430. else
  431. {
  432. *beg = dob_first(s); *end = *beg;
  433. }
  434. }
  435. cout << "Считывание прошло успешно" << endl;
  436. cin.get();
  437. cin.get();
  438. return 0;
  439. }
  440. //-----------------------------------------------------------------------------
  441. int write_file(char* filename, book* temp)
  442. {
  443. cout << "Введите название файла" << endl;
  444. cin >> filename;
  445. ofstream fout(filename, ios_base::app); // открытие файла
  446. if (!fout)
  447. {
  448. cout << "Не могу открыть файл для записи" << endl;
  449. return 1;
  450. }
  451. while (temp)// пока temp!=0 выводим эл-ты в файл
  452. {
  453. fout << temp->inf.autor << endl;
  454. fout << temp->inf.name << endl;
  455. fout << temp->inf.izdatelstvo << endl;
  456. fout << temp->inf.janr << endl;
  457. fout << temp->inf.d << endl;
  458. fout << temp->inf.m << endl;
  459. fout << temp->inf.g << endl;
  460. fout << temp->inf.cost << endl;
  461. temp = temp->next;
  462. }
  463. cout << "Данные сохранены в файле: " << filename << endl;
  464. cout << "==============================" << endl;
  465. cout << "Нажмите любую клавишу" << endl;
  466. cin.get();
  467. return 0;
  468. }
  469. //-----------------------------------------------------------------------------
  470. book* udal(book* beg)
  471. {
  472. book* temp;
  473. if (!beg)
  474. {
  475. cout << "список пустой" << endl;
  476. cin.get();
  477. return 0;
  478. }
  479. while (beg)
  480. {
  481. temp = beg;
  482. beg = beg->next;
  483. delete temp;
  484. }
  485. cout << "==============================" << endl;
  486. cout << "====удаление прошло успешно===" << endl;
  487. cout << "==============================" << endl;
  488. return beg;
  489. }
  490. //----------------------------------------------------------------
  491. void sort_data(book** beg)
  492. {
  493. book* temp_i = *beg, * temp_j = *beg;
  494. for (; temp_i; temp_i = temp_i->next)
  495. {
  496. for (temp_j = temp_i; temp_j; temp_j = temp_j->next)
  497. {
  498. if (temp_i->inf.g != temp_j->inf.g)
  499. {
  500. if (temp_i->inf.g > temp_j->inf.g)
  501. {
  502. swap(temp_i->inf, temp_j->inf);
  503. continue;
  504. }
  505. }
  506. else if (temp_i->inf.m != temp_j->inf.m)
  507. {
  508. if (temp_i->inf.m > temp_j->inf.m)
  509. {
  510. swap(temp_i->inf, temp_j->inf);
  511. continue;
  512. }
  513. }
  514. else if (temp_i->inf.d > temp_j->inf.d)
  515. {
  516. swap(temp_i->inf, temp_j->inf);
  517. continue;
  518. }
  519. }
  520. }
  521. cout << "Сортировка прошла успешно" << endl;
  522. system("pause");
  523. }
  524. //----------------------------------------
  525. void sort_cost(book** beg) {
  526. book* temp_i = *beg,
  527. * temp_j = *beg;
  528.  
  529. for (; temp_i; temp_i = temp_i->next) {
  530. for (temp_j = temp_i; temp_j; temp_j = temp_j->next) {
  531. if (temp_i->inf.cost > temp_j->inf.cost) {
  532. swap(temp_i->inf, temp_j->inf);
  533. }
  534. }
  535. }
  536.  
  537. cout << "Сортировка прошла успешно" << endl;
  538. system("pause");
  539. }
Advertisement
Add Comment
Please, Sign In to add comment