Advertisement
dariagalich

Untitled

Dec 15th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.49 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. #include <io.h>
  10. #include <fcntl.h>
  11. using namespace std;
  12. //----КОНСТАНТЫ----------
  13. const int up = 72,
  14. down = 80,
  15. right_btn = 77,
  16. left_btn = 75,
  17. enter = 13,
  18. esc = 27,
  19. del = 83,
  20. d_n = 20;
  21. const string items[10] =
  22. {
  23. "Создать список или добавить новый эл-т ",
  24. " Удаление всех элементов списка ",
  25. " Просмотр списка ",
  26. " Запись данных в файл ",
  27. " Чтение из файла ",
  28. " Изменить ",
  29. " Поиск ",
  30. " Старые книги ",
  31. " Сортировка ",
  32. " Выход "
  33. };
  34. const string items2[4] =
  35. {
  36. "Поиск по автору книги ",
  37. "Поиск по названию книги",
  38. "Поиск по жанру книги",
  39. "Выход(esc)"
  40. };
  41. const string items3[3] =
  42. {
  43. "Запись в текстовый файл",
  44. "Запись в бинарный файл"
  45. "Выход(esc)"
  46. };
  47. const string items4[3] =
  48. {
  49. "Чтение из текстового файла",
  50. "Чтение из бинарного файла",
  51. "Выход(esc)"
  52. };
  53. const string items5[4] =
  54. {
  55. "Сортировка по стоимости книги (по возрастанию) ",
  56. "Сортировка по стоимости книги (по убыванию)",
  57. "Сортировка по дате поступления(от старых к новым)",
  58. "Выход(esc)"
  59. };
  60. const string items6[3] =
  61. {
  62. "Удаление одного элемента списка",
  63. "Удаление всех элементов списка",
  64. "Выход(esc)"
  65. };
  66. const string items2[6] =
  67. {
  68. "Изменить автора книги ",
  69. "Изменить название книги",
  70. "Изменить издательство книги",
  71. "Изменить жанр книги",
  72. "Изменить стоимость книги",
  73. "Выход(esc)"
  74. };
  75. //=======ГЛОБАЛЬНЫЕ ПЕРЕМЕННЫЕ===============
  76. string filename;
  77. int num_pages = 5, // Кол-во элементов на одной странице
  78. width = 0, // Ширина окна
  79. height = 0; // Высота окна
  80. int kol_el = 1,// Кол-во элементов
  81. kol=0;
  82. //-------------------------------------------
  83. struct info
  84. {
  85. string autor;
  86. string name;
  87. string izdatelstvo;
  88. string janr;
  89. string cost;
  90. int d = 0, m = 0, g = 0;
  91. };
  92. struct book
  93. {
  94. info inf;
  95. book* next = 0;
  96. book* pred = 0;
  97. };
  98. //-------------------------------------------
  99. book vvod_book();
  100. book* dob(book* end, const book& s);
  101. book* dob_first(const book& s);
  102. book* del_all(book* beg);
  103. book* delete_el(book* beg,int num_del);
  104. book* list(book* beg, int active, int page, int k);
  105. void cls();
  106. void gotoxy(int xpos, int ypos);
  107. void print(const book& s);
  108. void prosmotr(book* beg);
  109. void old_book(book beg);
  110. void sort_cost(book** beg);
  111. void sort_cost_2(book** beg);
  112. void sort_data(book* beg);
  113. void searchname(book* beg);
  114. void searchautor(book* beg);
  115. void searchjanr(book* beg);
  116. void SetColor(int text, int bg);
  117. int change_autor(book* beg, string& user_autor, string& new_user_autor);
  118. int read_file(string filename, book** beg, book** end);
  119. int write_file(string filename, book* temp);
  120. int write_file_binary(string filename, book* beg);
  121. int read_file_binary(string filename, book** beg, book** end);
  122. int menu(int& active, const string items[], int num_el);
  123. wchar_t* UnicodeString(string input);
  124.  
  125. //===========основная программа===================================================
  126. int main()
  127. {
  128. // меняем размер шрифта
  129. CONSOLE_FONT_INFOEX cfi;
  130. cfi.cbSize = sizeof(cfi);
  131. cfi.nFont = 0;
  132. cfi.dwFontSize.X = 0; // Width of each character in the font
  133. cfi.dwFontSize.Y = 24; // Height
  134. cfi.FontFamily = FF_DONTCARE;
  135. cfi.FontWeight = FW_NORMAL;
  136. SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
  137. //----------------------------------
  138. HANDLE hCon;
  139. // вытаскиваем ширину и высоту
  140. hCon = GetStdHandle(-12);
  141. CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
  142. if (GetConsoleScreenBufferInfo(hCon, &consoleInfo))
  143. {
  144. width = consoleInfo.srWindow.Right - consoleInfo.srWindow.Left + 1;
  145. height = consoleInfo.srWindow.Bottom - consoleInfo.srWindow.Top + 1;
  146. }
  147. //======================================================================
  148. system("cls");
  149. ShowWindow(GetConsoleWindow(), SW_MAXIMIZE); // полноэкранный режим
  150. setlocale(LC_ALL, "Rus");
  151. system("color 8F");
  152. book* beg = NULL,
  153. * end = NULL, input;
  154. string filename;
  155. int Num, current = 1;
  156. string user_autor, new_user_autor;
  157. int num_e = 0;
  158. int k = 0;
  159. while (1) {
  160. system("cls");
  161. switch (menu(current, items, 10))
  162. {
  163. case 1://создание списка или добовление нового эл-та в список
  164. system("cls");
  165. if (beg)
  166. end = dob(end, vvod_book());
  167. else
  168. {
  169. beg = dob_first(vvod_book());
  170. end = beg;
  171. }
  172. kol_el++;
  173. break;
  174. case 2://удаление всех элементов в списке
  175. system("cls");
  176. current = 1;
  177. switch (menu(current, items6, 3))
  178. {
  179. case 1:
  180. delete_el(beg, 0);
  181. break;
  182. case 2:
  183. beg=del_all(beg);
  184. case esc:break;
  185. }
  186. break;
  187. case 3://просмотр
  188. system("cls");
  189. //prosmotr(beg);
  190. list(beg, 0, 0, 0);
  191. break;
  192. case 4://запись в файл
  193. {
  194. system("cls");
  195. int current = 1;
  196. cout << "Выберите формат файла в который хотите записать:";
  197. switch (menu(current, items3, 3))
  198. {
  199. case 1://Запись в текстовый файл
  200. system("cls");
  201. write_file(filename, beg);
  202. break;
  203. case 2://Запись в бинарный файл
  204. write_file_binary(filename, beg);
  205. cin.get();
  206. break;
  207. case 27:break;
  208. }
  209. }
  210. break;
  211. case 5://чтение из файла
  212. {
  213. system("cls");
  214. int current = 1;
  215. cout << "Выберите формат файла в который хотите записать:";
  216. switch (menu(current, items4, 3))
  217. {
  218. case 1://чтение из текстового файла
  219. system("cls");
  220. read_file(filename, &beg, &end);
  221. break;
  222. case 2://чтение из бинарного файла
  223. read_file_binary(filename, &beg, &end);
  224. cin.get();
  225. break;
  226. case 27:break;
  227. }
  228. }
  229.  
  230. break;
  231. /*
  232. "Изменить название книги",
  233. "Изменить издательство книги",
  234. "Изменить жанр книги",
  235. "Изменить стоимость книги",*/
  236. case 6://редактирование списка
  237. system("cls");
  238. int current = 1;
  239. switch (menu(current, items6, 6))
  240. {
  241. case 1://===========Изменить автора книги
  242. Num = change_autor(beg, user_autor, new_user_autor);
  243. if (Num == 1)
  244. cout << "Автор успешно перезаписан." << endl;
  245. else
  246. cout << "Автор не найден!" << endl;
  247. system("pause");
  248. break;
  249. case 2:
  250.  
  251. }
  252.  
  253.  
  254. break;
  255. case 7://поиск
  256. {
  257. system("cls");
  258. int current = 1;
  259. switch (menu(current, items2, 4))
  260. {
  261. case 1://поск по автору
  262. system("cls");
  263. searchautor(beg);
  264. cin.get();
  265. break;
  266. case 2:
  267. system("cls");
  268. searchname(beg);//поиск по названию книги
  269. cin.get();
  270. break;
  271. case 3:
  272. searchjanr(beg);
  273. break;
  274. case 27:break;
  275. }
  276. break;
  277. }
  278. case 8://старые книги
  279. if (!beg)
  280. {
  281. MessageBox(0, L"Список пуст", L"Уведомление", MB_ICONINFORMATION | MB_SETFOREGROUND);
  282. break;
  283. }
  284. old_book(*beg);
  285. break;
  286. case 9:
  287. {
  288. system("cls");
  289. int current = 1;
  290. switch (menu(current, items5, 4))
  291. {
  292. case 1://сортировка стоимости(по возрастанию)
  293. system("cls");
  294. sort_cost(&beg);
  295. cin.get();
  296. break;
  297. case 2:
  298. system("cls");
  299. sort_cost_2(&beg);//сортировка стоимости(по убыванию)
  300. cin.get();
  301. break;
  302. case 27:break;
  303. }
  304. break;
  305. }
  306. case 10:
  307. del_all(beg);
  308. return 0;
  309. }
  310. }
  311. return 0;
  312. }
  313.  
  314. //-----------------------------------------------------------------------------
  315. int change_autor(book* beg, string& user_autor, string& new_user_autor)
  316. {
  317. book* temp = beg;
  318.  
  319. cout << "Введите автора, которого вы хотите изменить " << endl;
  320. cin >> user_autor;
  321. cout << "Введите нового атвора: " << endl;
  322. cin >> new_user_autor;
  323.  
  324. while (temp != NULL)
  325. {
  326. if (temp->inf.autor == user_autor)
  327. {
  328. temp->inf.autor = new_user_autor;
  329. return 1;
  330. }
  331. else
  332. {
  333. temp = temp->next;
  334.  
  335. }
  336. }
  337. return 0;
  338. }
  339. //------------------------------------------------------------------------------
  340. int change_name(book* beg, string& user_name, string& new_user_name)
  341. {
  342. book* temp = beg;
  343.  
  344. cout << "Введите автора, которого вы хотите изменить " << endl;
  345. cin >> user_name;
  346. cout << "Введите нового атвора: " << endl;
  347. cin >> new_user_name;
  348.  
  349. while (temp != NULL)
  350. {
  351. if (temp->inf.autor == user_name)
  352. {
  353. temp->inf.autor = new_user_name;
  354. return 1;
  355. }
  356. else
  357. {
  358. temp = temp->next;
  359.  
  360. }
  361. }
  362. return 0;
  363. }
  364. //------------------------------------------------------------------------------
  365. void searchjanr(book* beg)
  366. {
  367. book* temp = beg;
  368. char fname[d_n];
  369. int fl = 0;
  370.  
  371. system("cls");
  372.  
  373. if (!beg)
  374. {
  375. MessageBox(0, L"Список пуст", L"Уведомление", MB_ICONINFORMATION | MB_SETFOREGROUND);
  376. return;
  377. }
  378.  
  379. cout << "Введите название жанра для поиска" << endl;
  380. cin >> fname;
  381. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  382. cout << "| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |" << endl;
  383. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  384. while (temp)
  385. {
  386. if (fname == temp->inf.janr)
  387. {
  388. print(*temp);
  389. fl = 1;
  390. }
  391. temp = temp->next;
  392. }
  393. if (fl != 1)
  394. {
  395. cout << "Книги с таким жанром не найдено" << endl;
  396. }
  397. system("pause");
  398. }
  399. //------------------------------------------------------------------------------
  400. void searchname(book* beg)
  401. {
  402. book* temp = beg;
  403. char fname[d_n];
  404. int fl = 0;
  405.  
  406. system("cls");
  407.  
  408. if (!beg)
  409. {
  410. MessageBox(0, L"Список пуст", L"Уведомление", MB_ICONINFORMATION | MB_SETFOREGROUND);
  411. return;
  412. }
  413.  
  414. cout << "Введите название книги для поиска" << endl;
  415. cin >> fname;
  416. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  417. cout << "| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |" << endl;
  418. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  419. while (temp)
  420. {
  421. if (fname == temp->inf.name)
  422. {
  423. print(*temp);
  424. fl = 1;
  425. }
  426. temp = temp->next;
  427. }
  428. if (fl != 1)
  429. {
  430. cout << "Книги с таким названием не найдено" << endl;
  431. }
  432. system("pause");
  433. }
  434. //-----------------------------------------------------------------------------
  435. void searchautor(book* beg)
  436. {
  437. book* temp = beg;
  438. int fl = 0;
  439. char fa[d_n];
  440. system("cls");
  441. if (!beg)
  442. {
  443. MessageBox(0, L"Список пуст", L"Уведомление", MB_ICONINFORMATION | MB_SETFOREGROUND);
  444. return;
  445. }
  446. cout << "Введите автора для поиска" << endl;
  447. cin >> fa;
  448.  
  449. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  450. cout << "| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |" << endl;
  451. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  452. while (temp)
  453. {
  454. if (fa == temp->inf.autor)
  455. {
  456. print(*temp);
  457. fl = 1;
  458. }
  459. temp = temp->next;
  460. }
  461. if (fl != 1) {
  462. cout << "Книги с таким автором не найдено" << endl;
  463. }
  464. system("pause");
  465. }
  466. //-----------------------------------------------------------------------------
  467. void old_book(book beg)
  468. {
  469. book* temp = &beg;
  470. sort_data(&beg);
  471. system("cls");
  472.  
  473. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  474. cout << "| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |" << endl;
  475. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  476. for (int i = 0; i < 5; i++) {
  477. print(*temp);
  478. temp = temp->next;
  479. }
  480.  
  481. system("pause");
  482. return;
  483.  
  484. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  485. cin.get();
  486. }
  487. //-----------------------------------------------------------------------------
  488. void prosmotr(book* beg)
  489. {
  490. int k = 1;
  491. if (!beg)
  492. {
  493. cout << "список пустой" << endl;
  494. cin.get();
  495. return;
  496. }
  497. book* temp = beg;
  498. cout << "+—|—————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  499. cout << "|№| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |" << endl;
  500. cout << "+—|—————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  501. while (temp)
  502. {
  503. cout << "|" << k++ << "|";
  504. print(*temp);
  505. temp = temp->next;
  506.  
  507. }
  508. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  509. cin.get();
  510. }
  511. //-----------------------------------------------------------------------------
  512. book vvod_book()
  513. {
  514. book s;
  515. cout << "Введите автора:" << endl;
  516. while (1)
  517. {
  518. cin >> s.inf.autor;
  519. //if (atoi(s.autor)==0)
  520. if ((s.inf.autor[0] >= '0') && (s.inf.autor[0] <= '9'))
  521. break;
  522. cout << "Ошибка ввода!" << endl;
  523. }
  524. cout << "Введите название книги:" << endl;
  525. while (1)
  526. {
  527. cin >> s.inf.name;
  528. //if (atoi(s.name)==0)
  529. break;
  530. cout << "Ошибка ввода!" << endl;
  531. }
  532. cout << "Введите издательство:" << endl;
  533. while (1)
  534. {
  535. cin >> s.inf.izdatelstvo;
  536. //if (atoi(s.izdatelstvo) == 0)
  537. break;
  538. cout << "Ошибка ввода!" << endl;
  539. }
  540. cout << "Введите жанр:" << endl;
  541. while (1)
  542. {
  543. cin >> s.inf.janr;
  544. //if (atoi(s.janr) == 0)
  545. break;
  546. cout << "Ошибка ввода!" << endl;
  547. }
  548. cout << "Введите дату поступления:" << endl;
  549. cout << "Введите день:";
  550. while (1)
  551. {
  552. cin >> s.inf.d;
  553. if ((s.inf.d >= 1) && (s.inf.d <= 31))
  554. break;
  555. cout << "Ошибка ввода!Ведите правильную дату!" << endl;
  556. }
  557. cout << "Введите месяц:";
  558. while (1)
  559. {
  560. cin >> s.inf.m;
  561. if ((s.inf.m >= 1) && (s.inf.m <= 12))
  562. break;
  563. cout << "Ошибка ввода!Ведите правильную дату!" << endl;
  564. }
  565. cout << "Введите год:";
  566. while (1)
  567. {
  568. cin >> s.inf.g;
  569. if ((s.inf.g >= 1900) && (s.inf.g <= 2050))
  570. break;
  571. cout << "Ошибка ввода!Ведите правильную дату!" << endl;
  572. }
  573. cout << "Введите стоимость книги:" << endl;
  574. cin >> s.inf.cost;
  575.  
  576. kol_el++;
  577. return s;
  578. }
  579. //-----------------------------------------------------------------------------
  580. void print(const book& s)
  581. {
  582. cout << s.inf.autor << setw(18 - (s.inf.autor).length()) << "|";
  583. cout << s.inf.name << setw(21 - (s.inf.name).length()) << "|";
  584. cout << s.inf.izdatelstvo << setw(19 - (s.inf.izdatelstvo).length()) << "|";
  585. cout << s.inf.janr << setw(17 - (s.inf.janr).length()) << "|";
  586. if (s.inf.d < 10) {
  587. cout << 0 << s.inf.d << ".";
  588. }
  589. else {
  590. cout << s.inf.d << ".";
  591. }
  592. if (s.inf.m < 10) {
  593. cout << 0 << s.inf.m << ".";
  594. }
  595. else {
  596. cout << s.inf.m << ".";
  597. }
  598. cout << s.inf.g << setw(9) << "|";
  599. cout << s.inf.cost << setw(16 - (s.inf.cost).length()) << "|" << endl;
  600.  
  601. }
  602. //--------------ф-я добавления или создания эл-та-------------------------------
  603. book* dob(book* end, const book& s)
  604. {
  605. book* newE = new book;
  606. *newE = s;
  607. newE->next = 0;
  608. newE->pred = end;
  609. end->next = newE;
  610. end = newE;
  611. return end;
  612. }
  613. //--------------создание первого эл-та------------------------------
  614. book* dob_first(const book& s)
  615. {
  616. book* beg = new book;
  617. *beg = s;
  618. beg->next = 0;
  619. beg->pred = 0;
  620. return beg;
  621. }
  622. //-----------------------------------------------------------------------------
  623. int read_file(string filename, book** beg, book** end)
  624. {
  625. cout << "Введите название файла" << endl;
  626. cin >> filename;
  627. ifstream fin(filename, ios::in);
  628. if (!fin) {
  629.  
  630. MessageBox(0, L"Невозможно открыть файл!", L"Ошибка", MB_ICONERROR | MB_SETFOREGROUND);
  631. return 0;
  632.  
  633. }
  634. book s;
  635. *beg = 0;
  636. fin.seekg(0, ios::beg);
  637. while (fin >> s.inf.autor)
  638. {
  639. fin >> s.inf.name;
  640. fin >> s.inf.izdatelstvo;
  641. fin >> s.inf.janr;
  642. fin >> s.inf.d;
  643. fin >> s.inf.m;
  644. fin >> s.inf.g;
  645. fin >> s.inf.cost;
  646.  
  647.  
  648. if (*beg) {
  649. *end = dob(*end, s);
  650. }
  651. else {
  652. *beg = dob_first(s); *end = *beg;
  653. }
  654.  
  655. kol_el++;
  656. }
  657. cout << "Считывание прошло успешно" << endl;
  658. cin.get();
  659. cin.get();
  660. return 0;
  661. }
  662. //-----------------------------------------------------------------------------
  663. int write_file(string filename, book* temp)
  664. {
  665. cout << "Введите название файла" << endl;
  666. cin >> filename;
  667. ofstream fout(filename, ios_base::app); // открытие файла
  668. if (!fout)
  669. {
  670. cout << "Не могу открыть файл для записи" << endl;
  671. return 1;
  672. }
  673. while (temp)// пока temp!=0 выводим эл-ты в файл
  674. {
  675. fout << temp->inf.autor << endl;
  676. fout << temp->inf.name << endl;
  677. fout << temp->inf.izdatelstvo << endl;
  678. fout << temp->inf.janr << endl;
  679. fout << temp->inf.d << endl;
  680. fout << temp->inf.m << endl;
  681. fout << temp->inf.g << endl;
  682. fout << temp->inf.cost << endl;
  683. temp = temp->next;
  684. }
  685. cout << "Данные сохранены в файле: " << filename << endl;
  686. cout << "==============================" << endl;
  687. cout << "Нажмите любую клавишу" << endl;
  688. cin.get();
  689. return 0;
  690. }
  691. //-----------------------------------------------------------------------------
  692. int write_file_binary(string filename, book* beg)
  693. {
  694. cout << "Введите название файла" << endl;
  695. cin >> filename;
  696. book* temp = beg;
  697. ofstream fout(filename + ".dat", ios::binary); // открытие файла
  698. if (!fout)
  699. {
  700. cout << "Не могу открыть файл для записи" << endl;
  701. return 1;
  702. }
  703. while (temp) {
  704. fout.write((char*)&temp->inf, sizeof temp->inf);
  705. temp = temp->next;
  706. }
  707. fout.close();
  708. cout << "Данные сохранены в файле: " << filename << endl;
  709. cout << "==============================" << endl;
  710. cout << "Нажмите любую клавишу" << endl;
  711. cin.get();
  712. return 0;
  713. }
  714.  
  715. // ==========ЧТЕНИЕ ИЗ БИНАРНОГО ФАЙЛА==========
  716. int read_file_binary(string filename, book** beg, book** end)
  717. {
  718. cout << "Введите название файла" << endl;
  719. cin >> filename;
  720. ifstream fin(filename + ".dat", ios::binary);
  721.  
  722. if (!fin) {
  723. MessageBox(0, L"Нет файла!", L"Ошибка", MB_ICONERROR | MB_SETFOREGROUND);
  724. return 1;
  725. }
  726.  
  727. fin.seekg(ios_base::beg);
  728. book* t = new book;
  729. t->next = NULL;
  730. t->pred = NULL;
  731. *beg = 0;
  732.  
  733. while (fin.read((char*)&t->inf, sizeof t->inf))
  734. {
  735. if (*beg)
  736. *end = dob(*end, *t);
  737. else {
  738. *beg = dob_first(*t);
  739. *end = *beg;
  740. }
  741. }
  742. fin.close();
  743. cout << "Чтене прошло успешно" << endl;
  744. cout << "==============================" << endl;
  745. cout << "Нажмите любую клавишу" << endl;
  746. return 0;
  747. }
  748.  
  749. //-----------------------------------------------------------------------------
  750. book* del_all(book* beg)
  751. {
  752. book* temp;
  753. if (!beg) {
  754. MessageBox(0, L"Список пуст", L"Уведомление", MB_ICONINFORMATION | MB_SETFOREGROUND);
  755. return 0;
  756. }
  757. while (beg)
  758. {
  759. temp = beg;
  760. beg = beg->next;
  761. delete temp;
  762. }
  763. system("cls");
  764. cout << "==============================" << endl;
  765. cout << "====удаление прошло успешно===" << endl;
  766. cout << "==============================" << endl;
  767. cin.get();
  768. return beg;
  769. }
  770. //----------------------------------------------------------------
  771. void sort_data(book* beg)
  772. {
  773. book* temp_i = beg, * temp_j = beg;
  774. for (; temp_i; temp_i = temp_i->next)
  775. {
  776. for (temp_j = temp_i; temp_j; temp_j = temp_j->next)
  777. {
  778. if (temp_i->inf.g != temp_j->inf.g)
  779. {
  780. if (temp_i->inf.g > temp_j->inf.g)
  781. {
  782. swap(temp_i->inf, temp_j->inf);
  783. continue;
  784. }
  785. }
  786. else if (temp_i->inf.m != temp_j->inf.m)
  787. {
  788. if (temp_i->inf.m > temp_j->inf.m)
  789. {
  790. swap(temp_i->inf, temp_j->inf);
  791. continue;
  792. }
  793. }
  794. else if (temp_i->inf.d > temp_j->inf.d)
  795. {
  796. swap(temp_i->inf, temp_j->inf);
  797. continue;
  798. }
  799. }
  800. }
  801. cout << "Сортировка прошла успешно" << endl;
  802. system("pause");
  803. }
  804. //----------------------------------------
  805. void sort_cost(book** beg) {
  806. book* temp_i = *beg,
  807. * temp_j = *beg;
  808.  
  809. for (; temp_i; temp_i = temp_i->next) {
  810. for (temp_j = temp_i; temp_j; temp_j = temp_j->next) {
  811. if (stoi(temp_i->inf.cost) > stoi(temp_j->inf.cost)) {
  812. swap(temp_i->inf, temp_j->inf);
  813. }
  814. }
  815. }
  816.  
  817. cout << "Сортировка прошла успешно" << endl;
  818. system("pause");
  819. }
  820. void sort_cost_2(book** beg) {
  821. book* temp_i = *beg,
  822. * temp_j = *beg;
  823.  
  824. for (; temp_i; temp_i = temp_i->next) {
  825. for (temp_j = temp_i; temp_j; temp_j = temp_j->next) {
  826. if (stoi(temp_i->inf.cost) < stoi(temp_j->inf.cost)) {
  827. swap(temp_i->inf, temp_j->inf);
  828. }
  829. }
  830. }
  831.  
  832. cout << "Сортировка прошла успешно" << endl;
  833. system("pause");
  834. }
  835. // ==========ШАБЛОН ПЕЧАТИ МЕНЮ==========
  836. void print_menu(int sym, const string items[], const int N_ITEMS)
  837. {
  838. for (int i = 1; i <= N_ITEMS; i++)
  839. {
  840. SetColor(15, 8);
  841. gotoxy((width / 2) - 10, (height / 2) + i - 3); // ставим меню в центр
  842. if (i == sym)
  843. {
  844. SetColor(16, 3);
  845. }
  846. cout << items[i - 1] << endl;
  847. SetColor(15, 8);
  848. }
  849. }
  850. // ==========МЕНЮ==========
  851. int menu(int& active, const string items[], int num_el)
  852. {
  853. wint_t buf;
  854. while (1)
  855. {
  856. cls();
  857. print_menu(active, items, num_el);
  858.  
  859. buf = _getwch();
  860. switch (buf)
  861. {
  862. case up: // клавиша вверх
  863. if (active > 1) active--;
  864. break;
  865. case down: // клавиша вниз
  866. if (active < num_el) active++;
  867. break;
  868. case enter: // клавиша enter
  869. return active;
  870. case esc: // клавиша escape
  871. return -1;
  872. }
  873. }
  874. }
  875. // ==========УСТАНОВКА ЦВЕТА ТЕКСТА И ФОНА==========
  876. void SetColor(int text, int bg)
  877. {
  878. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  879. SetConsoleTextAttribute(hStdOut, (WORD)((bg << 4) | text));
  880. }
  881. // ==========ПЕРЕМЕЩЕНИЕ КУРСОРА НА ВЫБРАННУЮ ПОЗИЦИЮ==========
  882.  
  883. void gotoxy(int xpos, int ypos)
  884. {
  885. COORD scrn;
  886. HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
  887. scrn.X = xpos; scrn.Y = ypos;
  888. SetConsoleCursorPosition(hOuput, scrn);
  889. }
  890. //=====================================
  891. wchar_t* UnicodeString(string input) {
  892. const char* orig = input.c_str();
  893. size_t origsize = strlen(orig) + 1;
  894. const size_t newsize = 100;
  895. size_t convertedChars = 0;
  896. wchar_t* wcstring = new wchar_t[newsize];
  897. mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);
  898. return wcstring;
  899. }
  900. // ==========ОЧИСТКА ЭКРАНА БЕЗ МЕРЦАНИЯ==========
  901. void cls() {
  902. HANDLE hd = GetStdHandle(STD_OUTPUT_HANDLE);
  903. COORD cd;
  904. cd.X = 0;
  905. cd.Y = 0;
  906. SetConsoleCursorPosition(hd, cd);
  907. }
  908. // ==========УДАЛЕНИЕ==========
  909. book* delete_el(book* beg,int num_del)
  910. {
  911. book* temp;
  912. book* buf;
  913.  
  914. if (!beg) {
  915. MessageBox(0, L"Список пуст", L"Уведомление", MB_ICONINFORMATION | MB_SETFOREGROUND);
  916. return 0;
  917. }
  918.  
  919. temp = beg;
  920.  
  921. cout << "Введите номер элемента,который хотите удалить" << endl;
  922. cin >> num_del;
  923.  
  924. // если один элемент в списке
  925. if (beg->next == 0) {
  926. delete temp;
  927. return 0;
  928. }
  929.  
  930. while (temp) {
  931. if (num_del == kol) { // если введённый номер совпал с шифром задания
  932. buf = temp->next;
  933.  
  934. // если удаляется второй элемент
  935. if (temp->pred == 0) {
  936. buf->pred = 0;
  937. delete temp;
  938. return buf;
  939. }
  940.  
  941. if (buf != 0) buf->pred = temp->pred; // если следующий элемент не 0
  942.  
  943. buf = temp->pred;
  944. buf->next = temp->next;
  945. delete temp;
  946. return beg;
  947. }
  948. temp = temp->next;
  949. }
  950. MessageBox(0, L"Произошла ошибка!", L"Ошибка", MB_ICONERROR | MB_SETFOREGROUND);
  951. cout << "==============================" << endl;
  952. cout << "====удаление прошло успешно===" << endl;
  953. cout << "==============================" << endl;
  954. kol = 0;
  955. return beg;
  956.  
  957. }
  958.  
  959. book* list(book* beg, int active, int page, int k)
  960. {
  961. book* temp = beg,
  962. * buf_temp = beg,
  963. * buf_el = beg;
  964.  
  965. int c = 0,
  966. key = 0,
  967. first_page = 2; // переменная для первой страницы, если это первая страница, тогда 1, в остальных случаях = 2
  968.  
  969. int num_del = 0; // номер для удаления
  970. k = 0;
  971. page = 1;
  972. active = 1;
  973.  
  974. while (1)
  975. {
  976. if (!beg)
  977. {
  978. MessageBox(0, L"Список пуст", L"Уведомление", MB_ICONINFORMATION | MB_SETFOREGROUND);
  979. return beg;
  980. }
  981.  
  982. if (k != kol_el + 1 && first_page != 1)
  983. {
  984. cout << "+—|—————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  985. cout << "|№| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |" << endl;
  986. cout << "+—|—————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  987.  
  988. do {
  989.  
  990. // находим первый элемент с предыдущей страницы
  991. if (k % 5 == 0 && page != 1) {
  992. buf_el = temp;
  993.  
  994. for (int j = 0; j < 5; j++, temp = temp->pred) {
  995. buf_temp = temp->pred;
  996. }
  997. temp = buf_el;
  998. }
  999.  
  1000. cout << "|" << ++k << "|";
  1001. print(*temp);
  1002. temp = temp->next;
  1003. } while (temp && k % 5 != 0);
  1004.  
  1005. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  1006. }
  1007.  
  1008. c = _getwch();
  1009. key = _getwch();
  1010.  
  1011. switch (key) {
  1012. case right_btn:
  1013. first_page = 2;
  1014. if (k != kol_el + 1) {
  1015. page++;
  1016. system("cls");
  1017. }
  1018. break;
  1019. case left_btn:
  1020. if (page != 1) {
  1021. system("cls");
  1022. k -= 10;
  1023. page--;
  1024. temp = buf_temp;
  1025. }
  1026. else first_page = 1;
  1027. break;
  1028. case del:
  1029. delete_el(beg, 0);
  1030. break;
  1031. case esc:
  1032. return 0;
  1033. }
  1034. }
  1035. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement