Guest User

Untitled

a guest
Jun 21st, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.31 KB | None | 0 0
  1. /*
  2. Курсовая 2 семестра по программированию
  3.  
  4. Томар Киран
  5. гр. 4308
  6. СПБГЭТУ "ЛЭТИ"
  7. 2015 год
  8.  
  9. Список контактов a.k.a записная книжка
  10.  
  11. TO DO:
  12. search, setcursor position
  13. */
  14. #include "stdafx.h"
  15. #include <iostream>
  16. #include <conio.h>
  17. #include <Windows.h>
  18. #include <time.h>
  19. using namespace std;
  20.  
  21. typedef struct user{
  22. char*LOGIN;
  23. char*password;
  24. user*next;
  25. }Users;
  26.  
  27. typedef struct list{
  28. user*login;
  29. char*name;
  30. char*surname;
  31. char*phonenumber;
  32. char*email;
  33. list*next;
  34. list*prev;
  35. }ST;
  36.  
  37. ST* File(Users*user);
  38. ST* DeleteContact(ST*head, int n, Users*user);
  39. char* GetElement();
  40. ST* AddNewContact(ST* head, Users*user);
  41. ST* ClearAll(ST* head);
  42. ST* Swap(ST* p);
  43. ST* Sorting1(ST* head);
  44. ST* Sorting2(ST* head);
  45. Users* Register();
  46. char* enterpass();
  47. Users* UserIdentify();
  48. Users* scanUser();
  49. Users* login();
  50. void NewOutPut(ST*head, int n);
  51. void PrintContacts(ST*p, int i);
  52. int CounterOfContacts(ST*head);
  53. void Greeting(char*str);
  54. void Search(ST*head);
  55.  
  56. int main()
  57. {
  58. ::SetConsoleTitle(_T("Adress book contacts v 1.0"));
  59. system("color 0f");
  60. SetConsoleCP(1251);
  61. SetConsoleOutputCP(1251);
  62. time_t t = time(NULL);
  63. struct tm *t_m = localtime(&t);
  64. int CurrentTime = t_m->tm_hour, a = 30, b = 10; // a- горизонталь, b - вертикаль
  65. COORD x = { 30, 10 };
  66. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  67. SetConsoleCursorPosition(hConsole, x);
  68. switch (CurrentTime)
  69. {
  70. case 0:case 1:case 2:case 3:case 4: case 5:
  71. {
  72. char str[] = "Доброй ночи!";
  73. Greeting(str);
  74. break;
  75. }
  76. case 6:case 7:case 8:case 9:case 10:case 11:
  77. {
  78. char str[] = "Доброе утро!";
  79. Greeting(str);
  80. break;
  81. }
  82. case 12:case 13:case 14:case 15:case 16:case 17:
  83. {
  84. char str[] = "Добрый день!";
  85. Greeting(str);
  86. break;
  87. }
  88. case 18:case 19:case 20:case 21:case 22:case 23:
  89. {
  90. char str[] = "Добрый вечер!";
  91. Greeting(str);
  92. break;
  93.  
  94. }
  95. }
  96. _getch();
  97. system("cls");
  98. Users*user = UserIdentify();
  99. ST*head = File(user);
  100. head = Sorting1(head);
  101. int active_record = 1, i, max = 6, flag = 0, sorting = 1;
  102. char symb;
  103. do
  104. {
  105. i = 1;
  106. system("cls");
  107. do
  108. {
  109.  
  110. if (i == active_record)
  111. {
  112. SetConsoleTextAttribute(hConsole, (WORD)((1 << 3 | 7)));
  113. }
  114. else
  115. {
  116. SetConsoleTextAttribute(hConsole, (WORD)((0 << 4 | 7)));
  117. }
  118. if (i == 1)
  119. printf("1.Вывести список контактов\n");
  120. if (i == 2)
  121. printf("2.Добавить новый контакт\n");
  122. if (i == 3)
  123. printf("3.Удалить контакт\n");
  124. if (i == 4)
  125. printf("4.Изменить порядок отображения контактов\n");
  126. if (i == 5)
  127. printf("5.Поиск контактов\n");
  128. if (i == 6)
  129. printf("5.Выход");
  130. i++;
  131. } while (i < 7);
  132. SetConsoleTextAttribute(hConsole, (WORD)(0 << 4 | 7));
  133. cin.clear();
  134. cin.sync();
  135. symb = _getch();
  136. switch (symb)
  137. {
  138. case 13:
  139. {
  140. if (active_record == 1)
  141. {
  142. system("cls");
  143. int n = CounterOfContacts(head);
  144. NewOutPut(head, n);
  145. break;
  146. }
  147. if (active_record == 2)
  148. {
  149. system("cls");
  150. head = AddNewContact(head, user);
  151. if (sorting == 1)
  152. head = Sorting1(head);
  153. else
  154. head = Sorting2(head);
  155. system("pause");
  156. break;
  157. }
  158. if (active_record == 3)
  159. {
  160. system("cls");
  161. int n;
  162. if (head != NULL)
  163. {
  164. cout << "Введите порядковый номер контакта, который вы хотите удалить(Если 0, то все): ";
  165. cin >> n;
  166. }
  167. if (n == 0)
  168. head = ClearAll(head);
  169. else
  170. head = DeleteContact(head, n, user);
  171. if (sorting == 1)
  172. head = Sorting1(head);
  173. else
  174. head = Sorting2(head);
  175. system("pause");
  176. break;
  177. }
  178. if (active_record == 4)
  179. {
  180. int active_record = 1, i, max = 3, flag = 0;
  181. char symb;
  182. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  183. do
  184. {
  185. i = 1;
  186. system("cls");
  187. do
  188. {
  189. if (i == active_record)
  190. SetConsoleTextAttribute(hConsole, (WORD)(1 << 3 | 7));
  191. else
  192. SetConsoleTextAttribute(hConsole, (WORD)(0 << 4 | 7));
  193. if (i == 1)
  194. cout << "Упорядочить в прямом порядке(от А до Я)\n";
  195. if (i == 2)
  196. cout << "Упорядочить в обратном порядке(от Я до А)\n";
  197. if (i == 3)
  198. cout << "Назад";
  199. i++;
  200. } while (i < 4);
  201. SetConsoleTextAttribute(hConsole, (WORD)(0 << 4 | 7));
  202. cin.clear();
  203. cin.sync();
  204. symb = _getch();
  205. switch (symb)
  206. {
  207. case 13:
  208. {
  209. if (active_record == 1)
  210. {
  211. system("cls");
  212. head = Sorting1(head);
  213. sorting = 1;
  214. flag = 1;
  215. break;
  216. }
  217. if (active_record == 2)
  218. {
  219. system("cls");
  220. head = Sorting2(head);
  221. sorting = 2;
  222. flag = 1;
  223. break;
  224. }
  225. if (active_record == 3)
  226. {
  227. system("cls");
  228. flag = 1;
  229. break;
  230.  
  231. }
  232. }
  233. case -32:
  234. {
  235. symb = _getch();
  236. switch (symb)
  237. {
  238. case 80:
  239. {
  240. if (active_record != max)
  241. active_record++;
  242. else
  243. active_record = 1;
  244. break;
  245. }
  246. case 72:
  247. {
  248. if (active_record != 1)
  249. active_record--;
  250. else
  251. active_record = max;
  252. break;
  253. }
  254. default:
  255. cout << "\nВыберите нужный пункт меню, используя стрелки вверх, вниз и клавишу Enter";
  256. Sleep(500);
  257. break;
  258. }
  259. break;
  260. }
  261. default:
  262. cout << "\nВыберите нужный пункт меню, используя стрелки вверх, вниз и клавишу Enter";
  263. Sleep(500);
  264. break;
  265. }
  266. } while (flag != 1);
  267. break;
  268. }
  269. if (active_record == 5)
  270. {
  271. system("cls");
  272. Search(head);
  273. break;
  274. }
  275. if (active_record == 6)
  276. {
  277. system("cls");
  278. system("color 0f");
  279. COORD x = { 30, 10 };
  280. SetConsoleCursorPosition(hConsole, x);
  281. cout << "Спасибо за работу!\n";
  282. ClearAll(head);
  283. _getch();
  284. return 0;
  285.  
  286. }
  287. }
  288. //Выход из программы по ESC'ейпу
  289. //case 27:
  290. //{
  291. // flag = 1;
  292. // return NULL;
  293. // break;
  294. //}
  295. case -32:
  296. {
  297. symb = _getch();
  298. switch (symb)
  299. {
  300. case 80:
  301. {
  302. if (active_record != max)
  303. active_record++;
  304. else
  305. active_record = 1;
  306. break;
  307. }
  308. case 72:
  309. {
  310. if (active_record != 1)
  311. active_record--;
  312. else
  313. active_record = max;
  314. break;
  315. }
  316. default:
  317. {
  318. puts("\nВыберите нужный пункт меню, используя стрелки вверх, вниз и клавишу Enter");
  319. Sleep(500);
  320. break;
  321. }
  322. }
  323. break;
  324. }
  325. default:
  326. {
  327. puts("\nВыберите нужный пункт меню, используя стрелки вверх, вниз и клавишу Enter");
  328. Sleep(500);
  329. break;
  330. }
  331. }
  332. } while (flag != 1);
  333. system("pause");
  334. return 0;
  335. }
  336.  
  337. ST* File(Users*user)
  338. {
  339. FILE *file;
  340. ST*start = NULL;
  341. char buff[256];
  342. if (user == NULL)
  343. {
  344. file = fopen("Temporary", "w");
  345. if (file == 0)
  346. {
  347. printf("Не могу открыть файл '%s'\n", "Temporary");
  348. return NULL;
  349. }
  350. }
  351. else
  352. {
  353. char fname1[4] = "D:\\";
  354. char fname3[5] = ".txt";
  355. char *fname2 = user->LOGIN;
  356. char *fname = (char*)malloc(sizeof(char));
  357. strcpy(fname, fname1);
  358. strcat(fname, fname2);
  359. strcat(fname, fname3);
  360. file = fopen(fname, "r");
  361. if (file == 0)
  362. {
  363. printf("Не могу открыть файл '%s'\n", fname);
  364. return NULL;
  365. }
  366. }
  367. if ((fscanf(file, "%s256", buff) != EOF))
  368. {
  369. start = new ST();
  370. start->surname = (char*)malloc(strlen(buff)*sizeof(char));
  371. strcpy(start->surname, buff);
  372. start->next = NULL;
  373. start->prev = NULL;
  374. }
  375. else
  376. {
  377. cout << "Ошибка в формировании списка!\n";
  378. return NULL;
  379. }
  380. if ((fscanf(file, "%s256", buff) != EOF))
  381. {
  382. start->name = (char*)malloc(strlen(buff)*sizeof(char));
  383. strcpy(start->name, buff);
  384. }
  385. else
  386. {
  387. cout << "Ошибка в формировании списка!\n";
  388. return NULL;
  389. }
  390. if ((fscanf(file, "%s256", buff) != EOF))
  391. {
  392. start->email = (char*)malloc(strlen(buff)*sizeof(char));
  393. strcpy(start->email, buff);
  394. }
  395. else
  396. {
  397. cout << "Ошибка в формировании списка!\n";
  398. return NULL;
  399. }
  400. if ((fscanf(file, "%s256", buff) != EOF))
  401. {
  402. start->phonenumber = (char*)malloc(strlen(buff)*sizeof(char));
  403. strcpy(start->phonenumber, buff);
  404. }
  405. else
  406. {
  407. cout << "Ошибка в формировании списка!\n";
  408. return NULL;
  409. }
  410. while ((fscanf(file, "%s256", buff) != EOF))
  411. {
  412. ST*field = start;
  413.  
  414. while (field->next != NULL)
  415. {
  416. field = field->next;
  417. };
  418. ST*tmp = field;
  419. field->next = new ST();
  420. field = field->next;
  421. field->surname = (char*)malloc(strlen(buff)*sizeof(char));
  422. strcpy(field->surname, buff);
  423. field->next = NULL;
  424. field->prev = tmp;
  425. if ((fscanf(file, "%s256", buff) != EOF))
  426. {
  427. field->name = (char*)malloc(strlen(buff)*sizeof(char));
  428. strcpy(field->name, buff);
  429. }
  430. else
  431. {
  432. cout << "Ошибка в формировании списка!\n";
  433. return NULL;
  434. }
  435. if ((fscanf(file, "%s256", buff) != EOF))
  436. {
  437. field->email = (char*)malloc(strlen(buff)*sizeof(char));
  438. strcpy(field->email, buff);
  439. }
  440. else
  441. {
  442. cout << "Ошибка в формировании списка!\n";
  443. return NULL;
  444. }
  445. if ((fscanf(file, "%s256", buff) != EOF))
  446. {
  447. field->phonenumber = (char*)malloc(strlen(buff)*sizeof(char));
  448. strcpy(field->phonenumber, buff);
  449. }
  450. else
  451. {
  452. cout << "Ошибка в формировании списка!\n";
  453. return NULL;
  454. }
  455. }
  456. fclose(file);
  457. return start;
  458. }
  459.  
  460. ST* DeleteContact(ST* head, int n, Users*user)
  461. {
  462. if (head == NULL)
  463. {
  464. cout << "Список контактов пуст.\n";
  465. return head;
  466. }
  467. ST*p = head;
  468. int i = 1;
  469. while (i != n && p != NULL)
  470. {
  471. i++;
  472. p = p->next;
  473. }
  474. if (p == NULL)
  475. {
  476. cout << "Нет заданного номера контакта.\n";
  477. }
  478. else
  479. {
  480. if (i == 1)
  481. {
  482. head = p->next;
  483. free(p);
  484. cout << "Контакт успешно удален!\n";
  485. }
  486. else
  487. {
  488. if (p->next == NULL)
  489. {
  490. p->prev->next = NULL;
  491. free(p);
  492. cout << "Контакт успешно удален!\n";
  493. }
  494. else
  495. {
  496. p->prev->next = p->next;
  497. p->next->prev = p->prev;
  498. free(p);
  499. cout << "Контакт успешно удален!\n";
  500. }
  501. }
  502. }
  503. if (head != NULL)
  504. {
  505. if (user == NULL)
  506. {
  507. user = (Users*)malloc(sizeof(Users));
  508. user->LOGIN = "Temporary";
  509. }
  510. ST*start = NULL;
  511. FILE *file;
  512. char fname1[4] = "D:\\";
  513. char fname3[5] = ".txt";
  514. char *fname2 = user->LOGIN;
  515. char *fname = (char*)malloc(sizeof(char));
  516. strcpy(fname, fname1);
  517. strcat(fname, fname2);
  518. strcat(fname, fname3);
  519. file = fopen(fname, "w+");
  520. if (file == 0)
  521. return head;
  522. p = head;
  523. do
  524. {
  525. fputs(p->surname, file);
  526. fputs(" ", file);
  527. fputs(p->name, file);
  528. fputs(" ", file);
  529. fputs(p->email, file);
  530. fputs(" ", file);
  531. fputs(p->phonenumber, file);
  532. fputs(" ", file);
  533. p = p->next;
  534. } while (p != NULL);
  535. fclose(file);
  536. }
  537. return head;
  538. }
  539.  
  540. char* GetElement()
  541. {
  542. int len = 0;
  543. char c, *str = nullptr;
  544. do
  545. {
  546. str = (char*)realloc(str, (len + 1)*sizeof(char));
  547. scanf("%c", &c);
  548. str[len++] = c;
  549. } while (c != '\n');
  550. str[len - 1] = '\0';
  551. return str;
  552. }
  553.  
  554. ST*AddNewContact(ST*head, Users*user)
  555. {
  556. if (user == NULL)
  557. {
  558. user = (Users*)malloc(sizeof(Users));
  559. user->LOGIN = "Temporary";
  560. }
  561. FILE *file;
  562. char fname1[4] = "D:\\";
  563. char fname3[5] = ".txt";
  564. char *fname2 = user->LOGIN;
  565. char *fname = (char*)malloc(sizeof(char));
  566. strcpy(fname, fname1);
  567. strcat(fname, fname2);
  568. strcat(fname, fname3);
  569. file = fopen(fname, "a");
  570. fputs(" ", file);
  571. if (file == 0)
  572. {
  573. printf("Не могу открыть файл '%s'", fname);
  574. return head;
  575. }
  576. cin.clear();
  577. cin.sync();
  578. if (head == NULL)
  579. {
  580. head = (ST*)malloc(sizeof(ST));
  581. cout << "Введите Фамилию контакта: ";
  582. head->surname = GetElement();
  583. fputs(head->surname, file);
  584. fputs(" ", file);
  585. cout << "Введите Имя контакта: ";
  586. head->name = GetElement();
  587. fputs(head->name, file);
  588. fputs(" ", file);
  589. cout << "Введите E-mail адрес контакта: ";
  590. head->email = GetElement();
  591. fputs(head->email, file);
  592. fputs(" ", file);
  593. cout << "Введите Номер телефона контакта: ";
  594. head->phonenumber = GetElement();
  595. fputs(head->phonenumber, file);
  596. fputs(" ", file);
  597. cout << endl;
  598. head->next = NULL;
  599. head->prev = NULL;
  600. return head;
  601. }
  602. ST*p = head;
  603. while (p->next != NULL)
  604. p = p->next;
  605. ST*q = (ST*)malloc(sizeof(ST));
  606. p->next = q;
  607. q->next = NULL;
  608. q->prev = p;
  609. cout << "Введите Фамилию контакта: ";
  610. q->surname = GetElement();
  611. fputs(q->surname, file);
  612. fputs(" ", file);
  613. cout << "Введите Имя контакта: ";
  614. q->name = GetElement();
  615. fputs(q->name, file);
  616. fputs(" ", file);
  617. cout << "Введите E-mail адрес контакта: ";
  618. q->email = GetElement();
  619. fputs(q->email, file);
  620. fputs(" ", file);
  621. cout << "Введите Номер телефона контакта: ";
  622. q->phonenumber = GetElement();
  623. fputs(q->phonenumber, file);
  624. fputs(" ", file);
  625. cout << endl;
  626. fclose(file);
  627. return head;
  628. }
  629.  
  630. ST* ClearAll(ST*head)
  631. {
  632. if (head != NULL)
  633. {
  634. ST *tmp = head;
  635. do
  636. {
  637. head = tmp->next;
  638. free(tmp);
  639.  
  640. tmp = head;
  641. } while (tmp != NULL);
  642. }
  643. return NULL;
  644. }
  645.  
  646. ST* Swap(ST*p)
  647. {
  648. if (p == NULL || p->next == NULL)
  649. return p;
  650. ST*q = p;
  651. if (p->prev == NULL && p->next->next == NULL)
  652. {
  653. p = p->next;
  654. p->next = q;
  655. p->prev = NULL;
  656. q->prev = p;
  657. q->next = NULL;
  658. return p;
  659. }
  660. if (p->prev == NULL)
  661. {
  662. p = p->next;
  663. p->next->prev = q;
  664. p->prev = NULL;
  665. q->next = p->next;
  666. q->prev = p;
  667. p->next = q;
  668. return p;
  669. }
  670. if (p->next->next == NULL)
  671. {
  672. p = p->next;
  673. p->next = q;
  674. p->prev = q->prev;
  675. q->prev->next = p;
  676. q->prev = p;
  677. q->next = NULL;
  678. return p;
  679. }
  680. p = p->next;
  681. p->next->prev = q;
  682. q->prev->next = p;
  683. p->prev = q->prev;
  684. q->next = p->next;
  685. p->next = q;
  686. q->prev = p;
  687. return p;
  688. }
  689.  
  690. ST* Sorting1(ST*head)
  691. {
  692. if (head == NULL || head->next == NULL)
  693. return head;
  694. bool flag = 1;
  695. while (flag == 1)
  696. {
  697. ST*p = head;
  698. flag = 0;
  699. while (p->next)
  700. {
  701. if (strcmp(p->surname, p->next->surname) >0)
  702. {
  703. if (p->prev == NULL)
  704. {
  705. head = Swap(p);
  706. p = head;
  707. }
  708. else
  709. {
  710. p = Swap(p);
  711. }
  712.  
  713. flag = 1;
  714. }
  715. else if (strcmp(p->surname, p->next->surname) == 0)
  716. {
  717. if (strcmp(p->name, p->next->name) > 0)
  718. if (p->prev == NULL)
  719. {
  720. head = Swap(p);
  721. p = head;
  722. }
  723. else
  724. {
  725. p = Swap(p);
  726. }
  727. }
  728. p = p->next;
  729. }
  730. }
  731. return head;
  732. }
  733.  
  734. ST* Sorting2(ST*head)
  735. {
  736. if (head == NULL || head->next == NULL)
  737. return head;
  738. bool flag = 1;
  739. while (flag == 1)
  740. {
  741. ST*p = head;
  742. flag = 0;
  743. while (p->next)
  744. {
  745. if (strcmp(p->surname, p->next->surname) < 0)
  746. {
  747. if (p->prev == NULL)
  748. {
  749. head = Swap(p);
  750. p = head;
  751. }
  752. else
  753. {
  754. p = Swap(p);
  755. }
  756.  
  757. flag = 1;
  758. }
  759. else if (strcmp(p->surname, p->next->surname) == 0)
  760. {
  761. if (strcmp(p->name, p->next->name) < 0)
  762. if (p->prev == NULL)
  763. {
  764. head = Swap(p);
  765. p = head;
  766. }
  767. else
  768. {
  769. p = Swap(p);
  770. }
  771. }
  772. p = p->next;
  773. }
  774. }
  775. return head;
  776. }
  777.  
  778. Users* Register()
  779. {
  780. Users* start;
  781. Users* field;
  782. char*login, *password;
  783. int check;
  784. do
  785. {
  786. check = 0;
  787. system("cls");
  788. fflush(stdin);
  789. cout << "Регистрация\n";
  790. cout << "Введите логин: ";
  791. login = GetElement();
  792. start = scanUser();
  793. field = start;
  794. while (field != NULL)
  795. {
  796. if (strcmp(field->LOGIN, login) == 0)
  797. {
  798. cout << "\nТакой логин уже существует. Попробуйте другой.\n";
  799. system("pause");
  800. check = 1;
  801. }
  802. field = field->next;
  803. }
  804. } while (check == 1);
  805. cout << "Введите пароль: ";
  806. password = GetElement();
  807. FILE* UsH;
  808. char*fname = "D:\\userbase.txt";
  809. UsH = fopen(fname, "a");
  810. fputs("\n", UsH);
  811. fputs(login, UsH);
  812. fputs(" ", UsH);
  813. fputs(password, UsH);
  814. fclose(UsH);
  815. Users*head = scanUser();
  816. return head;
  817. }
  818.  
  819. char* enterpass()
  820. {
  821. char* st = new char[1];
  822. int len = 0;
  823. char c = _getch();
  824. _putch('*');
  825. while (c != '\r')
  826. {
  827. char* tmp = new char[len + 2];
  828. for (int i = 0; i < len; i++)
  829. {
  830. tmp[i] = st[i];
  831. }
  832. tmp[len] = c;
  833. len++;
  834. delete[] st;
  835. st = tmp;
  836. c = _getch();
  837. _putch('*');
  838. }
  839. st[len] = '\0';
  840. return st;
  841. }
  842. //08-backspace
  843.  
  844. Users* UserIdentify()
  845. {
  846. Users* UserInfo;
  847. int active_record = 1, i, max, flag = 0;
  848. char symb;
  849. max = 3;
  850. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  851. do
  852. {
  853. i = 1;
  854. system("cls");
  855. do
  856. {
  857.  
  858. if (i == active_record)
  859. {
  860. SetConsoleTextAttribute(hConsole, (WORD)((1 << 3 | 7)));
  861. }
  862. else
  863. {
  864. SetConsoleTextAttribute(hConsole, (WORD)((0 << 4 | 7)));
  865. }
  866. if (i == 1)
  867. printf("1.Войти как зарегистрированный пользователь\n");
  868. if (i == 2)
  869. printf("2.Войти без регистрации\n");
  870. if (i == 3)
  871. printf("3.Зарегистрироваться\n");
  872. i++;
  873. } while (i < 4);
  874. SetConsoleTextAttribute(hConsole, (WORD)(0 << 4 | 7));
  875. fflush(stdin);
  876.  
  877. symb = _getch();
  878. switch (symb)
  879. {
  880. case 13:
  881. {
  882. if (active_record == 1)
  883. {
  884. UserInfo = login();
  885. if (UserInfo != NULL)
  886. return UserInfo;
  887. break;
  888. }
  889. if (active_record == 2)
  890. {
  891. return NULL;
  892. break;
  893. }
  894. if (active_record == 3)
  895. {
  896. UserInfo = Register();
  897. while (UserInfo->next != NULL)
  898. UserInfo = UserInfo->next;
  899. return UserInfo;
  900. break;
  901. }
  902. }
  903. case -32:
  904. {
  905. symb = _getch();
  906. switch (symb)
  907. {
  908. case 80:
  909. {
  910. if (active_record != max)
  911. active_record++;
  912. else
  913. active_record = 1;
  914. break;
  915. }
  916. case 72:
  917. {
  918. if (active_record != 1)
  919. active_record--;
  920. else
  921. active_record = max;
  922. break;
  923. }
  924. default:
  925. {
  926. puts("\nВыберете нужный пункт меню, используя стрелки вверх, вниз и клавишу Enter");
  927. Sleep(500);
  928. break;
  929. }
  930. }
  931. break;
  932. }
  933. default:
  934. {
  935. puts("\nВыберете нужный пункт меню, используя стрелки вверх, вниз и клавишу Enter");
  936. Sleep(500);
  937. break;
  938. }
  939. }
  940. } while (flag != 1);
  941.  
  942. }
  943.  
  944. Users* login()
  945. {
  946. Users*head = scanUser();
  947. char*LOGIN;
  948. char*PASS;
  949. system("cls");
  950. cout << "Введите логин: ";
  951. LOGIN = GetElement();
  952. cout << endl;
  953. cout << "Введите пароль: ";
  954. PASS = enterpass();
  955. do
  956. {
  957. if ((strcmp(LOGIN, head->LOGIN) == 0) && (strcmp(PASS, head->password) == 0))
  958. return head;
  959. head = head->next;
  960. } while (head != NULL);
  961. return NULL;
  962. }
  963.  
  964. Users* scanUser()
  965. {
  966.  
  967. Users*start = NULL;
  968. FILE *file;
  969. char buff[256];
  970. char *fname = "D:\\userbase.txt";
  971. file = fopen(fname, "r");
  972. if (file == 0)
  973. {
  974. printf("Не могу открыть файл '%s'\n", fname);
  975. return NULL;
  976. }
  977. if ((fscanf(file, "%s256", buff) != EOF))
  978. {
  979. start = new Users();
  980. start->LOGIN = (char*)malloc(strlen(buff)*sizeof(char));
  981. strcpy(start->LOGIN, buff);
  982. start->next = NULL;
  983. }
  984. else
  985. {
  986. cout << "Ошибка в формировании списка!\n";
  987. return NULL;
  988. }
  989. if ((fscanf(file, "%s256", buff) != EOF))
  990. {
  991. start->password = (char*)malloc(strlen(buff)*sizeof(char));
  992. strcpy(start->password, buff);
  993. }
  994. else
  995. {
  996. cout << "Ошибка в формировании списка!\n";
  997. return NULL;
  998. }
  999. Users*field = start;
  1000. while ((fscanf(file, "%s256", buff) != EOF))
  1001. {
  1002.  
  1003.  
  1004. while (field->next != NULL)
  1005. {
  1006. field = field->next;
  1007. };
  1008. Users*tmp = field;
  1009. field->next = new Users();
  1010. field = field->next;
  1011. field->LOGIN = (char*)malloc(strlen(buff)*sizeof(char));
  1012. strcpy(field->LOGIN, buff);
  1013. field->next = NULL;
  1014. if ((fscanf(file, "%s256", buff) != EOF))
  1015. {
  1016. field->password = (char*)malloc(strlen(buff)*sizeof(char));
  1017. strcpy(field->password, buff);
  1018. }
  1019. else
  1020. {
  1021. cout << "Ошибка в формировании списка!\n";
  1022. return NULL;
  1023. }
  1024. }
  1025. fclose(file);
  1026. return start;
  1027.  
  1028. }
  1029.  
  1030. void NewOutPut(ST*head, int n)
  1031. {
  1032. if (head == NULL)
  1033. {
  1034. cout << "Список контактов пуст!\n";
  1035. system("pause");
  1036. }
  1037. else
  1038. {
  1039. int active_record = 1, i, flag = 0;
  1040. char symb;
  1041. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  1042. ST*p;
  1043. do
  1044. {
  1045. p = head;
  1046. i = 1;
  1047. system("cls");
  1048. for (int i = 1; i < n + 1; i++)
  1049. {
  1050. if (i == active_record)
  1051. {
  1052. SetConsoleTextAttribute(hConsole, (WORD)((1 << 3 | 7)));
  1053. }
  1054. else
  1055. {
  1056. SetConsoleTextAttribute(hConsole, (WORD)((0 << 4 | 7)));
  1057. }
  1058. cout << p->surname;
  1059. cout << " ";
  1060. cout << p->name;
  1061. cout << endl;
  1062. p = p->next;
  1063. }
  1064. SetConsoleTextAttribute(hConsole, (WORD)(0 << 4 | 7));
  1065. fflush(stdin);
  1066. p = head;
  1067. symb = _getch();
  1068. switch (symb)
  1069. {
  1070. case 13:
  1071. {
  1072. for (int x = 1; x < n + 1; x++, p = p->next)
  1073. {
  1074. if (active_record == x)
  1075. {
  1076. system("cls");
  1077. PrintContacts(p,x);
  1078. system("pause");
  1079. }
  1080. }
  1081. break;
  1082. }
  1083. case 27:
  1084. {
  1085. flag = 1;
  1086. break;
  1087. }
  1088. case -32:
  1089. {
  1090. symb = _getch();
  1091. switch (symb)
  1092. {
  1093. case 80:
  1094. {
  1095. if (active_record != n)
  1096. active_record++;
  1097. else
  1098. active_record = 1;
  1099. break;
  1100. }
  1101. case 72:
  1102. {
  1103. if (active_record != 1)
  1104. active_record--;
  1105. else
  1106. active_record = n;
  1107. break;
  1108. }
  1109. default:
  1110. {
  1111. puts("\nВыберете нужный пункт меню, используя стрелки вверх, вниз и клавишу Enter");
  1112. Sleep(500);
  1113. break;
  1114. }
  1115. }
  1116. break;
  1117. }
  1118. default:
  1119. {
  1120. puts("\nВыберете нужный пункт меню, используя стрелки вверх, вниз и клавишу Enter");
  1121. Sleep(500);
  1122. break;
  1123. }
  1124. }
  1125. } while (flag != 1);
  1126. }
  1127. }
  1128.  
  1129. void PrintContacts(ST*p,int i)
  1130. {
  1131. cout << "Контакт #" << i << endl;
  1132. cout << "Фамилия: " << p->surname << endl;
  1133. cout << "Имя: " << p->name << endl;
  1134. cout << "E-mail адрес: " << p->email << endl;
  1135. cout << "Номер телефона: " << p->phonenumber << endl;
  1136.  
  1137. }
  1138.  
  1139. int CounterOfContacts(ST*head)
  1140. {
  1141. int n = 0;
  1142. while (head != NULL)
  1143. {
  1144. n++;
  1145. head = head->next;
  1146. }
  1147. return n;
  1148. }
  1149.  
  1150. void Greeting(char* str)
  1151. {
  1152. int b = 10;
  1153. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  1154. for (int a = 0; a < 30; a++)
  1155. {
  1156. system("cls");
  1157. COORD d = { a, b };
  1158. SetConsoleCursorPosition(hConsole, d);
  1159. for (int i = 0, j = strlen(str); i < j; i++)
  1160. {
  1161. cout << str[i];
  1162. }
  1163. Sleep(20);
  1164. }
  1165. }
  1166.  
  1167. void Search(ST*head)
  1168. {
  1169. system("cls");
  1170. if (head == NULL)
  1171. {
  1172. cout << "Список контактов пуст.\n";
  1173. system("pause");
  1174. }
  1175. else
  1176. {
  1177. char C;
  1178. int check = 0;
  1179. do
  1180. {
  1181. ST*p = head;
  1182. int flag = 0, flagi = 1, flagj = 1;
  1183. if (check != 0)
  1184. {
  1185. getchar();
  1186. }
  1187. check = 1;
  1188. system("cls");
  1189. cout << "Введите искомое: ";
  1190. C = _getch();
  1191. cout << C << endl;
  1192. while (p != NULL)
  1193. {
  1194. for (int i = 0, j = 0; i < strlen(p->surname) || j < strlen(p->name); i++, j++)
  1195. {
  1196. if (C == p->surname[i] || C == p->name[j])
  1197. {
  1198. cout << p->surname << " " << p->name << endl;
  1199. flag = 1;
  1200. }
  1201. }
  1202. if (p)
  1203. p = p->next;
  1204. }
  1205. if (flag == 0 && C != 27)
  1206. cout << "Ничего не найдено.\n";
  1207. cout << "_________________\nНажмите Enter для продолжения, Enter->ESC для выхода.\n";
  1208. } while (C != 27);
  1209. }
  1210. }
Advertisement
Add Comment
Please, Sign In to add comment