dariagalich

Untitled

Dec 1st, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.47 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. //======================
  21.  
  22. // ГЛОБАЛЬНЫЕ ПЕРЕМЕННЫЕ
  23.  
  24. //======================
  25.  
  26. string filename;
  27.  
  28. string All_bd = "mainBD.txt";
  29.  
  30. float total_el = 0;
  31.  
  32. int num_pages = 5, // Кол-во элементов на одной странице
  33.  
  34. width = 0, // Ширина окна
  35.  
  36. height = 0; // Высота окна
  37.  
  38.  
  39.  
  40.  
  41. const string items[10] = {
  42. "Создать список или добавить новый эл-т ",
  43. "Удаление всех элементов списка",
  44. "Просмотр списка",
  45. "Запись данных в файл",
  46. "Чтение из файла",
  47. "Изменить",
  48. "Поиск",
  49. "Сортировка по дате",
  50. "Старые книги",
  51. "Выход" };
  52.  
  53.  
  54. const string items2[10] = {
  55. "Поиск по автору книги ",
  56. "Поиск по названию книги",
  57. "Выход" };
  58.  
  59. //-------------------------------------------
  60. struct info {
  61. string autor;
  62. string name;
  63. string izdatelstvo;
  64. string janr;
  65. int d = 0, m = 0, g = 0, cost = 0;
  66. };
  67. struct book {
  68. info inf;
  69. book* next;
  70. book* pred;
  71. };
  72. //-------------------------------------------
  73. book vvod_book();
  74. book* dob(book* end, const book& s);
  75. book* dob_first(const book& s);
  76. book* udal(book* beg);
  77. void gotoxy(int xpos, int ypos);
  78. void print(const book& s);
  79. void prosmotr(book* beg);
  80. void old_book(book beg);
  81. void sort_cost(book** beg);
  82. void sort_data(book* beg);
  83. void searchname(book* beg);
  84. void searchautor(book* beg);
  85. int change(book* beg, string& user_autor, string& new_user_autor);
  86. int read_file(char* filename, book** beg, book** end);
  87. int write_file(char* filename, book* temp);
  88. int menu(int& active, const string items[], int num_el);
  89. int menu2();
  90. void SetColor(int text, int bg);
  91. //--------основная программа-----------------------------------
  92. int main()
  93. {
  94. HANDLE hCon;
  95.  
  96.  
  97.  
  98. // вытаскиваем ширину и высоту
  99.  
  100. hCon = GetStdHandle(-12);
  101.  
  102. CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
  103.  
  104. if (GetConsoleScreenBufferInfo(hCon, &consoleInfo))
  105.  
  106. {
  107.  
  108. width = consoleInfo.srWindow.Right - consoleInfo.srWindow.Left + 1;
  109.  
  110. height = consoleInfo.srWindow.Bottom - consoleInfo.srWindow.Top + 1;
  111.  
  112. }
  113.  
  114.  
  115.  
  116. // меняем размер шрифта
  117.  
  118. CONSOLE_FONT_INFOEX cfi;
  119.  
  120. cfi.cbSize = sizeof(cfi);
  121.  
  122. cfi.nFont = 0;
  123.  
  124. cfi.dwFontSize.X = 0; // Width of each character in the font
  125.  
  126. cfi.dwFontSize.Y = 24; // Height
  127.  
  128. cfi.FontFamily = FF_DONTCARE;
  129.  
  130. cfi.FontWeight = FW_NORMAL;
  131.  
  132. SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);
  133.  
  134. //========================
  135.  
  136.  
  137. ShowWindow(GetConsoleWindow(), SW_MAXIMIZE); // полноэкранный режим
  138. setlocale(LC_ALL, "Rus");
  139. system("color 8F");
  140. book* beg = NULL,
  141. * end = NULL, input;
  142. char filename[20];
  143. int Num, current = 1;
  144. string user_autor, new_user_autor;
  145.  
  146. while (1) {
  147. switch (menu(current, items, 10))
  148. {
  149. case 1://создание списка или добовление нового эл-та в список
  150. if (beg)
  151. end = dob(end, vvod_book());
  152. else
  153. {
  154. beg = dob_first(vvod_book());
  155. end = beg;
  156. }
  157. break;
  158. case 2://удаление всех элементов в списке
  159. beg = udal(beg);
  160. cout << "Нажмите любую клавишу" << endl;
  161. cin.get();
  162. break;
  163. case 3://просмотр
  164. gotoxy(10, 10);
  165. prosmotr(beg);
  166. break;
  167. case 4://запись в файл
  168. write_file(filename, beg);
  169. break;
  170. case 5://чтение из файла
  171. read_file(filename, &beg, &end);
  172. break;
  173. case 6://редактирование списка
  174. cout << "Введите автора, которого вы хотите изменить: " << endl;
  175. cin >> user_autor;
  176. cout << "Введите нового атвора: " << endl;
  177. cin >> new_user_autor;
  178. Num = change(beg, user_autor, new_user_autor);
  179. if (Num == 1) cout << "Автор успешно перезаписан." << endl;
  180. else cout << "Автор не найден!" << endl;
  181. system("pause");
  182. change(beg, user_autor, new_user_autor);
  183. cin.get();
  184. break;
  185. case 7://поиск
  186. {
  187. int Num, current = 1;
  188. switch (menu(current, items2, 3))
  189. {
  190. case 1://поск по автору
  191. searchautor(beg);
  192. cin.get();
  193. break;
  194. case 2:
  195. searchname(beg);//поиск по названию книги
  196. cin.get();
  197. break;
  198. case 3:break;
  199. }
  200. break;
  201. }
  202. case 8:
  203. {
  204. int Num, current = 1;
  205. switch (menu(current, items2, 3))
  206. {
  207. case 1://поск по автору
  208. sort_cost(&beg); break;
  209. cin.get();
  210. break;
  211. case 2:
  212. searchname(beg);//поиск по названию книги
  213. cin.get();
  214. break;
  215. case 3:break;
  216. }
  217. break;
  218. }
  219. case 9:
  220. if (!beg) {
  221. MessageBox(0, L"Список пуст", L"Уведомление", MB_ICONINFORMATION | MB_SETFOREGROUND);
  222. break;
  223. }
  224. old_book(*beg);
  225. break;
  226. case 10: return 0;
  227. }
  228. }
  229. return 0;
  230. }
  231.  
  232.  
  233. int menu2()
  234. {
  235. char buf[7];
  236. int m;
  237. do
  238. {
  239. system("CLS");
  240. cout << "============================" << endl;
  241. cout << "Выберите поле по которому хотите осуществить поиск" << endl;
  242. cout << "1 - Автор книги" << endl;
  243. cout << "2 - Название книги" << endl;
  244. cout << "9 - Выход в главное меню" << endl;
  245. cout << "============================" << endl;
  246. cin >> buf;
  247. cin.get();
  248. m = atoi(buf);
  249. if (!m)
  250. {
  251. cout << "Вам следует вводить число от 1 до 7" << endl;
  252. cin.get();
  253. }
  254. } while (!m);
  255. return m;
  256. }
  257.  
  258. //-----------------------------------------------------------------------------
  259. int change(book* beg, string& user_autor, string& new_user_autor)
  260. {
  261. book* temp = beg;
  262. while (temp != NULL)
  263. {
  264. if (temp->inf.autor == user_autor)
  265. {
  266. temp->inf.autor = new_user_autor;
  267. return 1;
  268. }
  269. else
  270. {
  271. temp = temp->next;
  272.  
  273. }
  274. }
  275. return 0;
  276. }
  277. //------------------------------------------------------------------------------
  278. void searchname(book* beg)
  279. {
  280. book* temp = beg;
  281. char fname[d_n];
  282. int fl = 0;
  283.  
  284. system("cls");
  285.  
  286. if (!beg)
  287. {
  288. MessageBox(0, L"Список пуст", L"Уведомление", MB_ICONINFORMATION | MB_SETFOREGROUND);
  289. return;
  290. }
  291.  
  292. cout << "Введите название книги для поиска" << endl;
  293. cin >> fname;
  294. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  295. cout << "| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |" << endl;
  296. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  297. while (temp)
  298. {
  299. if (fname == temp->inf.name)
  300. {
  301. print(*temp);
  302. fl = 1;
  303. }
  304. temp = temp->next;
  305. }
  306. if (fl != 1)
  307. {
  308. cout << "Книги с таким названием не найдено" << endl;
  309. }
  310. system("pause");
  311. }
  312. //-----------------------------------------------------------------------------
  313. void searchautor(book* beg)
  314. {
  315. book* temp = beg;
  316. int fl = 0;
  317. char fa[d_n];
  318. system("cls");
  319. if (!beg)
  320. {
  321. MessageBox(0, L"Список пуст", L"Уведомление", MB_ICONINFORMATION | MB_SETFOREGROUND);
  322. return;
  323. }
  324. cout << "Введите автора для поиска" << endl;
  325. cin >> fa;
  326.  
  327. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  328. cout << "| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |" << endl;
  329. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  330. while (temp)
  331. {
  332. if (fa == temp->inf.autor)
  333. {
  334. print(*temp);
  335. fl = 1;
  336. }
  337. temp = temp->next;
  338. }
  339. if (fl != 1) {
  340. cout << "Книги с таким автором не найдено" << endl;
  341. }
  342. system("pause");
  343. }
  344. //-----------------------------------------------------------------------------
  345. void old_book(book beg)
  346. {
  347. book* temp = &beg;
  348. sort_data(&beg);
  349. system("cls");
  350.  
  351. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  352. cout << "| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |" << endl;
  353. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  354. for (int i = 0; i < 5; i++) {
  355. print(*temp);
  356. temp = temp->next;
  357. }
  358.  
  359. system("pause");
  360. return;
  361.  
  362. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  363. cin.get();
  364. }
  365. //-----------------------------------------------------------------------------
  366. void prosmotr(book* beg)
  367. {
  368. if (!beg)
  369. {
  370. cout << "список пустой" << endl;
  371. cin.get();
  372. return;
  373. }
  374. book* temp = beg;
  375. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  376. cout << "| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |" << endl;
  377. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  378. while (temp)
  379. {
  380. print(*temp);
  381. temp = temp->next;
  382. }
  383. cout << "+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+" << endl;
  384. cin.get();
  385. }
  386. //-----------------------------------------------------------------------------
  387. book vvod_book()
  388. {
  389. book s;
  390. cout << "Введите автора:" << endl;
  391. while (1)
  392. {
  393. cin >> s.inf.autor;
  394. //if (atoi(s.autor)==0)
  395. // if((s.autor[0] <= '0') && (s.autor[0] >= '9' ) )
  396. break;
  397. cout << "Ошибка ввода!" << endl;
  398. }
  399. cout << "Введите название книги:" << endl;
  400. while (1)
  401. {
  402. cin >> s.inf.name;
  403. //if (atoi(s.name)==0)
  404. break;
  405. cout << "Ошибка ввода!" << endl;
  406. }
  407. cout << "Введите издательство:" << endl;
  408. while (1)
  409. {
  410. cin >> s.inf.izdatelstvo;
  411. //if (atoi(s.izdatelstvo) == 0)
  412. break;
  413. cout << "Ошибка ввода!" << endl;
  414. }
  415. cout << "Введите жанр:" << endl;
  416. while (1)
  417. {
  418. cin >> s.inf.janr;
  419. //if (atoi(s.janr) == 0)
  420. break;
  421. cout << "Ошибка ввода!" << endl;
  422. }
  423. cout << "Введите дату поступления:" << endl;
  424. cout << "Введите день:";
  425. while (1)
  426. {
  427. cin >> s.inf.d;
  428. if ((s.inf.d >= 1) && (s.inf.d <= 31))
  429. break;
  430. cout << "Ошибка ввода!Ведите правильную дату!" << endl;
  431. }
  432. cout << "Введите месяц:";
  433. while (1)
  434. {
  435. cin >> s.inf.m;
  436. if ((s.inf.m >= 1) && (s.inf.m <= 12))
  437. break;
  438. cout << "Ошибка ввода!Ведите правильную дату!" << endl;
  439. }
  440. cout << "Введите год:";
  441. while (1)
  442. {
  443. cin >> s.inf.g;
  444. if ((s.inf.g >= 1900) && (s.inf.g <= 2050))
  445. break;
  446. cout << "Ошибка ввода!Ведите правильную дату!" << endl;
  447. }
  448. cout << "Введите стоимость книги:" << endl;
  449. cin >> s.inf.cost;
  450. return s;
  451. }
  452. //-----------------------------------------------------------------------------
  453. void print(const book& s)
  454. {
  455. cout << "|" << s.inf.autor << setw(20 - (s.inf.autor).length()) << "|";
  456. cout << s.inf.name << setw(21 - (s.inf.name).length()) << "|";
  457. cout << s.inf.izdatelstvo << setw(19 - (s.inf.izdatelstvo).length()) << "|";
  458. cout << s.inf.janr << setw(17 - (s.inf.janr).length()) << "|";
  459. if (s.inf.d < 10) cout << 0 << s.inf.d << ".";
  460. if (s.inf.m < 10) cout << 0 << s.inf.m << "." << s.inf.g << setw(9) << "|";
  461. cout << s.inf.cost << setw(16) << "|" << endl;
  462.  
  463. }
  464. //--------------ф-я добавления или создания эл-та-------------------------------
  465. book* dob(book* end, const book& s)
  466. {
  467. book* newE = new book;
  468. *newE = s;
  469. newE->next = 0;
  470. end->next = newE;
  471. end = newE;
  472. return end;
  473. }
  474. //--------------создание первого эл-та------------------------------
  475. book* dob_first(const book& s)
  476. {
  477. book* beg = new book;
  478. *beg = s;
  479. beg->next = 0;
  480. return beg;
  481. }
  482. //-----------------------------------------------------------------------------
  483. int read_file(char* filename, book** beg, book** end)
  484. {
  485. cout << "Введите название файла" << endl;
  486. cin >> filename;
  487. ifstream fin(filename, ios::in);
  488. if (!fin) {
  489.  
  490. MessageBox(0, L"Невозможно открыть файл!", L"Ошибка", MB_ICONERROR | MB_SETFOREGROUND);
  491. return 0;
  492.  
  493. }
  494. book s;
  495. *beg = 0;
  496. fin.seekg(0, ios::beg);
  497. while (fin >> s.inf.autor)
  498. {
  499. fin >> s.inf.name;
  500. fin >> s.inf.izdatelstvo;
  501. fin >> s.inf.janr;
  502. fin >> s.inf.d;
  503. fin >> s.inf.m;
  504. fin >> s.inf.g;
  505. fin >> s.inf.cost;
  506. if (*beg)
  507. *end = dob(*end, s);
  508. else
  509. {
  510. *beg = dob_first(s); *end = *beg;
  511. }
  512. }
  513. cout << "Считывание прошло успешно" << endl;
  514. cin.get();
  515. cin.get();
  516. return 0;
  517. }
  518. //-----------------------------------------------------------------------------
  519. int write_file(char* filename, book* temp)
  520. {
  521. cout << "Введите название файла" << endl;
  522. cin >> filename;
  523. ofstream fout(filename, ios_base::app); // открытие файла
  524. if (!fout)
  525. {
  526. cout << "Не могу открыть файл для записи" << endl;
  527. return 1;
  528. }
  529. while (temp)// пока temp!=0 выводим эл-ты в файл
  530. {
  531. fout << temp->inf.autor << endl;
  532. fout << temp->inf.name << endl;
  533. fout << temp->inf.izdatelstvo << endl;
  534. fout << temp->inf.janr << endl;
  535. fout << temp->inf.d << endl;
  536. fout << temp->inf.m << endl;
  537. fout << temp->inf.g << endl;
  538. fout << temp->inf.cost << endl;
  539. temp = temp->next;
  540. }
  541. cout << "Данные сохранены в файле: " << filename << endl;
  542. cout << "==============================" << endl;
  543. cout << "Нажмите любую клавишу" << endl;
  544. cin.get();
  545. return 0;
  546. }
  547. //-----------------------------------------------------------------------------
  548. book* udal(book* beg)
  549. {
  550. book* temp;
  551. if (!beg)
  552. {
  553. cout << "список пустой" << endl;
  554. cin.get();
  555. return 0;
  556. }
  557. while (beg)
  558. {
  559. temp = beg;
  560. beg = beg->next;
  561. delete temp;
  562. }
  563. cout << "==============================" << endl;
  564. cout << "====удаление прошло успешно===" << endl;
  565. cout << "==============================" << endl;
  566. return beg;
  567. }
  568. //----------------------------------------------------------------
  569. void sort_data(book* beg)
  570. {
  571. book* temp_i = beg, * temp_j = beg;
  572. for (; temp_i; temp_i = temp_i->next)
  573. {
  574. for (temp_j = temp_i; temp_j; temp_j = temp_j->next)
  575. {
  576. if (temp_i->inf.g != temp_j->inf.g)
  577. {
  578. if (temp_i->inf.g > temp_j->inf.g)
  579. {
  580. swap(temp_i->inf, temp_j->inf);
  581. continue;
  582. }
  583. }
  584. else if (temp_i->inf.m != temp_j->inf.m)
  585. {
  586. if (temp_i->inf.m > temp_j->inf.m)
  587. {
  588. swap(temp_i->inf, temp_j->inf);
  589. continue;
  590. }
  591. }
  592. else if (temp_i->inf.d > temp_j->inf.d)
  593. {
  594. swap(temp_i->inf, temp_j->inf);
  595. continue;
  596. }
  597. }
  598. }
  599. cout << "Сортировка прошла успешно" << endl;
  600. system("pause");
  601. }
  602. //----------------------------------------
  603. void sort_cost(book** beg) {
  604. book* temp_i = *beg,
  605. * temp_j = *beg;
  606.  
  607. for (; temp_i; temp_i = temp_i->next) {
  608. for (temp_j = temp_i; temp_j; temp_j = temp_j->next) {
  609. if (temp_i->inf.cost > temp_j->inf.cost) {
  610. swap(temp_i->inf, temp_j->inf);
  611. }
  612. }
  613. }
  614.  
  615. cout << "Сортировка прошла успешно" << endl;
  616. system("pause");
  617. }
  618. // ==========ШАБЛОН ПЕЧАТИ МЕНЮ==========
  619. void print_menu(int sym, const string items[], const int N_ITEMS) {
  620. for (int i = 1; i <= N_ITEMS; i++) {
  621. SetColor(15, 8);
  622. gotoxy((width/2) - 6, (height/2) + i - 3); // ставим меню в центр
  623. if (i == sym) {
  624. SetColor(15, 5);
  625. }
  626. cout << items[i - 1] << endl;
  627. SetColor(15, 8);
  628. }
  629. HANDLE hCon;
  630.  
  631. // вытаскиваем ширину и высоту
  632. hCon = GetStdHandle(-12);
  633. CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
  634. if (GetConsoleScreenBufferInfo(hCon, &consoleInfo))
  635. {
  636. width = consoleInfo.srWindow.Right - consoleInfo.srWindow.Left + 1;
  637. height = consoleInfo.srWindow.Bottom - consoleInfo.srWindow.Top + 1;
  638. }
  639. }
  640.  
  641.  
  642. void print_menu2(int sym, const string items[], const int N_ITEMS) {
  643. for (int i = 1; i <= N_ITEMS; i++) {
  644. SetColor(15, 8);
  645. if (i == sym) {
  646. SetColor(15, 5);
  647. }
  648. cout << items[i - 1] << endl;
  649. SetColor(15, 8);
  650. }
  651. }
  652.  
  653. // ==========МЕНЮ==========
  654. int menu(int& active, const string items[], int num_el) {
  655. wint_t buf;
  656.  
  657. do {
  658. system("cls");
  659. print_menu(active, items, num_el);
  660.  
  661. buf = _getwch();
  662. switch (buf) {
  663. case up: // клавиша вверх
  664. if (active > 1) active--;
  665. break;
  666. case down: // клавиша вниз
  667. if (active < num_el) active++;
  668. break;
  669. case enter: // клавиша enter
  670. return active;
  671. case esc: // клавиша escape
  672. return -1;
  673. }
  674. } while (1);
  675. }
  676.  
  677.  
  678. // ==========УСТАНОВКА ЦВЕТА ТЕКСТА И ФОНА==========
  679. void SetColor(int text, int bg) {
  680. HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  681. SetConsoleTextAttribute(hStdOut, (WORD)((bg << 4) | text));
  682. }
  683. // ==========ПЕРЕМЕЩЕНИЕ КУРСОРА НА ВЫБРАННУЮ ПОЗИЦИЮ==========
  684.  
  685. void gotoxy(int xpos, int ypos)
  686.  
  687. {
  688.  
  689. COORD scrn;
  690.  
  691. HANDLE hOuput = GetStdHandle(STD_OUTPUT_HANDLE);
  692.  
  693. scrn.X = xpos; scrn.Y = ypos;
  694.  
  695. SetConsoleCursorPosition(hOuput, scrn);
  696.  
  697. }
Advertisement
Add Comment
Please, Sign In to add comment