dariagalich

Untitled

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