Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <Windows.h>
  4. #include <stdio.h>
  5. #include <cstring>
  6. using namespace std;
  7. const unsigned int NAME_SIZE = 30;
  8. const unsigned int CITY_SIZE = 20;
  9.  
  10. struct Address
  11. {
  12. char name[NAME_SIZE];
  13. char city[CITY_SIZE];
  14. Address* next;
  15. Address* prev;
  16. };
  17. //-----------------------------------------------------------
  18. int menu(void)
  19. {
  20. SetConsoleCP(1251);
  21. SetConsoleOutputCP(1251);
  22. char s[80]; int c;
  23. cout << endl;
  24. cout << "1. Ввод имени в список" << endl;
  25. cout << "2. Удаление имени списка" << endl;
  26. cout << "3. Вывод на экран списка" << endl;
  27. cout << "4. Поиск в списке" << endl;
  28. cout << "5. Чтение списка из файла" << endl;
  29. cout << "6. Запись списка в файл" << endl;
  30. cout << "7. " << endl;
  31. cout << "8. Выход" << endl;
  32. cout << endl;
  33. do
  34. {
  35. cout << "Ваш выбор: ";
  36. cin.sync();
  37. gets_s(s);
  38. cout << endl;
  39. c = atoi(s);
  40. } while (c < 0 || c > 8);
  41. return c;
  42. }
  43. //-----------------------------------------------------------
  44. void insert(Address* e, Address** phead, Address** plast) //Добавление в конец списка
  45. {
  46. SetConsoleCP(1251);
  47. SetConsoleOutputCP(1251);
  48. Address* p = *plast;
  49. if (*plast == NULL)
  50. {
  51. e->next = NULL;
  52. e->prev = NULL;
  53. *plast = e;
  54. *phead = e;
  55. return;
  56. }
  57. else
  58. {
  59. p->next = e;
  60. e->next = NULL;
  61. e->prev = p;
  62. *plast = e;
  63. }
  64. }
  65. //-----------------------------------------------------------
  66. Address* setElement(bool bList) // Создание элемента и ввод его значений с клавиатуры
  67. {
  68. SetConsoleCP(1251);
  69. SetConsoleOutputCP(1251);
  70. Address* temp1 = new Address();
  71. Address* temp2 = new Address();
  72. if (bList) {
  73. if (!temp1)
  74. {
  75. cerr << "Ошибка выделения памяти памяти";
  76. return NULL;
  77. }
  78. cout << "Введите имя: ";
  79. cin.getline(temp1->name, NAME_SIZE - 1, '\n');
  80. cin.ignore(cin.rdbuf()->in_avail());
  81. cin.clear();
  82. cout << "Введите город: ";
  83. cin.getline(temp1->city, CITY_SIZE - 1, '\n');
  84. cin.ignore(cin.rdbuf()->in_avail());
  85. cin.clear();
  86. temp1->next = NULL;
  87. temp1->prev = NULL;
  88. return temp1;
  89. }
  90. else {
  91. if (!temp2)
  92. {
  93. cerr << "Ошибка выделения памяти памяти";
  94. return NULL;
  95. }
  96. cout << "Введите имя: ";
  97. cin.getline(temp2->name, NAME_SIZE - 1, '\n');
  98. cin.ignore(cin.rdbuf()->in_avail());
  99. cin.clear();
  100. cout << "Введите город: ";
  101. cin.getline(temp2->city, CITY_SIZE - 1, '\n');
  102. cin.ignore(cin.rdbuf()->in_avail());
  103. cin.clear();
  104. temp2->next = NULL;
  105. temp2->prev = NULL;
  106. return temp2;
  107. }
  108. }
  109. //-----------------------------------------------------------
  110. void outputList(Address** phead, Address** plast) //Вывод списка на экран
  111. {
  112. SetConsoleCP(1251);
  113. SetConsoleOutputCP(1251);
  114. Address* t = *phead;
  115. while (t)
  116. {
  117. cout << t->name << ' ' << t->city << endl;
  118. t = t->next;
  119. }
  120. cout << "" << endl;
  121. }
  122. //-----------------------------------------------------------
  123. void find(char name[NAME_SIZE], Address** phead) // Поиск имени в списке
  124. {
  125. SetConsoleCP(1251);
  126. SetConsoleOutputCP(1251);
  127. Address* t = *phead;
  128. while (t)
  129. {
  130. if (!strcmp(name, t->name)) break;
  131. t = t->next;
  132. }
  133. if (!t)
  134. cerr << "Имя не найдено" << endl;
  135. else
  136. cout << t->name << ' ' << t->city << endl;
  137. }
  138. //-----------------------------------------------------------
  139. void delet(char name[NAME_SIZE], Address** phead, Address** plast) // Удаление имени
  140. {
  141. SetConsoleCP(1251);
  142. SetConsoleOutputCP(1251);
  143. struct Address* t = *phead;
  144. while (t)
  145. {
  146. if (!strcmp(name, t->name)) break;
  147. t = t->next;
  148. }
  149. if (!t)
  150. cerr << "Имя не найдено" << endl;
  151. else
  152. {
  153. if (*phead == t)
  154. {
  155. *phead = t->next;
  156. if (*phead)
  157. (*phead)->prev = NULL;
  158. else
  159. *plast = NULL;
  160. }
  161. else
  162. {
  163. t->prev->next = t->next;
  164. if (t != *plast)
  165. t->next->prev = t->prev;
  166. else
  167. *plast = t->prev;
  168. }
  169. delete t;
  170. cout << "Элемент удален" << endl;
  171. }
  172. }
  173.  
  174. void writeToFile(Address** phead1, Address** phead2, Address** phead3, int bList) //Запись в файл
  175. {
  176. FILE* fp;
  177. char filename[12];
  178. switch (bList) {
  179. case 0: {
  180. struct Address* t = *phead1;
  181. strcpy_s(filename, "mList1.bin");
  182. errno_t err = fopen_s(&fp, filename, "wb");
  183. if (err)
  184. {
  185. cerr << "Файл не открывается" << endl;
  186. exit(1);
  187. }
  188. cout << "Сохранение в файл" << endl;
  189. while (t)
  190. {
  191. fwrite(t, sizeof(struct Address), 1, fp);
  192. t = t->next;
  193. }
  194. fclose(fp);
  195. break;
  196. }
  197. case 1: {
  198. struct Address* t = *phead2;
  199. strcpy_s(filename, "mList2.bin");
  200. errno_t err = fopen_s(&fp, filename, "wb");
  201. if (err)
  202. {
  203. cerr << "Файл не открывается" << endl;
  204. exit(1);
  205. }
  206. cout << "Сохранение в файл" << endl;
  207. while (t)
  208. {
  209. fwrite(t, sizeof(struct Address), 1, fp);
  210. t = t->next;
  211. }
  212. fclose(fp);
  213. break;
  214. }
  215. case 2: {
  216. struct Address* t = *phead3;
  217. strcpy_s(filename, "mList3.bin");
  218. errno_t err = fopen_s(&fp, filename, "wb");
  219. if (err)
  220. {
  221. cerr << "Файл не открывается" << endl;
  222. exit(1);
  223. }
  224. cout << "Сохранение в файл" << endl;
  225. while (t)
  226. {
  227. fwrite(t, sizeof(struct Address), 1, fp);
  228. t = t->next;
  229. }
  230. fclose(fp);
  231. break;
  232. }
  233. }
  234.  
  235. }
  236. //-----------------------------------------------------------
  237. void readFromFile(Address** phead, Address** plast, bool bList) //Считывание из файла
  238. {
  239. struct Address* t;
  240. FILE* fp;
  241. char filename[15];
  242. if (bList) {
  243. strcpy_s(filename, "mList2.bin");
  244. }
  245. else strcpy_s(filename, "mList1.bin");
  246. errno_t err = fopen_s(&fp, filename, "rb");
  247. if (err)
  248. {
  249. cerr << "Файл не открывается" << endl;
  250. exit(1);
  251. }
  252. while (*phead)
  253. {
  254. *plast = (*phead)->next;
  255. delete* phead;
  256. *phead = *plast;
  257. }
  258. *phead = *plast = NULL;
  259. cout << "Загрузка из файла" << endl;
  260. while (!feof(fp))
  261. {
  262. t = new Address();
  263. if (!t)
  264. {
  265. cerr << "Ошибка выделения памяти" << endl;
  266. return;
  267. }
  268. if (1 != fread(t, sizeof(struct Address), 1, fp)) break;
  269. insert(t, phead, plast);
  270. }
  271. fclose(fp);
  272. }
  273.  
  274. void oneByOne(Address** phead1, Address** phead2, Address** phead3) {
  275. char cName[30], cCity[20];
  276. struct Address* t = *phead1;
  277. struct Address* l = *phead2;
  278. //struct Address* m = *phead3;
  279. Address* m = new Address();
  280. while (t || l)
  281. {
  282.  
  283. //strcpy_s(cName, t->name);
  284.  
  285. for (int i = 0; t->name[i] !=0; i++) {
  286. m->name[i] = t->name[i];
  287. }
  288. for (int i = 0; t->city[i] != 0; i++) {
  289. m->city[i] = t->city[i];
  290. }
  291. t = t->next;
  292.  
  293. m = m->next;
  294. Address* m = new Address();
  295. for (int i = 0; l->name[i] != 0; i++) {
  296. m->name[i] = l->name[i];
  297. }
  298. for (int i = 0; l->city[i] != 0; i++) {
  299. m->city[i] = l->city[i];
  300. }
  301. m = m->next;
  302. l = l->next;
  303.  
  304. }
  305. writeToFile(phead1, phead2, phead3, 2);
  306. }
  307.  
  308. //-----------------------------------------------------------
  309. int main(void)
  310. {
  311. char s[80];
  312. SetConsoleCP(1251);
  313. SetConsoleOutputCP(1251);
  314. Address* head1 = NULL;
  315. Address* last1 = NULL;
  316. Address* head2 = NULL;
  317. Address* last2 = NULL;
  318. Address* head3 = NULL;
  319. Address* last3 = NULL;
  320. int bList;
  321. setlocale(LC_CTYPE, "Rus");
  322. while (true)
  323. {
  324. cout << "С каким списком будем работать?\n";
  325. cin.sync();
  326. gets_s(s);
  327. cout << endl;
  328. bList = atoi(s);
  329. switch (menu())
  330. {
  331. case 1: if (bList - 1) insert(setElement(bList - 1), &head2, &last2);
  332. else
  333. insert(setElement(bList - 1), &head1, &last1);
  334. break;
  335. case 2: { char dname[NAME_SIZE];
  336. cout << "Введите имя: ";
  337. cin.getline(dname, NAME_SIZE - 1, '\n');
  338. cin.ignore(cin.rdbuf()->in_avail());
  339. cin.sync();
  340. if (bList - 1)
  341. delet(dname, &head2, &last2);
  342. else delet(dname, &head1, &last1);
  343. break;
  344. }
  345. break;
  346. case 3:
  347. if (bList - 1)
  348. outputList(&head2, &last2);
  349. else outputList(&head1, &last1);
  350. break;
  351. case 4: { char fname[NAME_SIZE];
  352. cout << "Введите имя: ";
  353. cin.getline(fname, NAME_SIZE - 1, '\n');
  354. cin.ignore(cin.rdbuf()->in_avail());
  355. cin.sync();
  356. if (bList - 1)
  357. find(fname, &head2);
  358. else find(fname, &head1);
  359. }
  360. break;
  361. case 5: {
  362. if (bList - 1)
  363. readFromFile(&head2, &last2, bList - 1);
  364. else readFromFile(&head1, &last1, bList - 1);
  365. break;
  366. }
  367. case 6:
  368. {
  369. writeToFile(&head1, &head2, &head3, bList-1);
  370. break;
  371. }
  372. case 7: oneByOne(&head1, &head2, &head3); break;
  373. case 8: exit(0);
  374. default: exit(1);
  375. }
  376. }
  377. return 0;
  378. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement