dariagalich

Untitled

Nov 14th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.72 KB | None | 0 0
  1. #include <fstream>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <iomanip>
  5. #include <conio.h>
  6. #include <iostream>
  7. #include <locale.h>
  8. #include <windows.h>
  9. using namespace std;
  10. const int d_n = 20;
  11. //-------------------------------------------
  12. struct book{
  13. char autor[d_n];
  14. char name[d_n];
  15. char izdatelstvo[d_n];
  16. char janr[d_n];
  17. int d,m,g,cost;
  18. book *next;
  19. book *pred;
  20. };
  21. //-------------------------------------------
  22. book* dob(book* end, const book& s);
  23. book* dob_first(const book& s);
  24. book* udal(book* beg);
  25. book vvod_book();
  26. book*variant(book* beg,const book& s);
  27. void print(const book& s);
  28. void prosmotr(book* beg);
  29. void sort (const book*s);
  30. void searchname(book * beg);
  31. int read_file(char* filename, book** beg, book** end);
  32. int write_file(char* filename, book* temp);
  33. int menu();
  34. //--------основная программа-----------------------------------
  35. int main(){
  36. ShowWindow(GetConsoleWindow(), SW_MAXIMIZE); // полноэкранный режим
  37. setlocale(LC_ALL, "Rus");
  38.  
  39. book *beg = NULL,
  40. *end = NULL,input;
  41. char filename[20];
  42. int n=0;
  43. while (1){
  44. switch (menu())
  45. {
  46. case 1:
  47. if (beg)
  48. end = dob(end,vvod_book());
  49. else
  50. {
  51. beg = dob_first(vvod_book());
  52. end = beg;
  53. }
  54. n++;
  55. break;
  56. case 2:
  57. beg = udal(beg);
  58. cout << "Нажмите любую клавишу" << endl;
  59. n=0;
  60. cin.get();
  61. break;
  62. case 3:
  63. prosmotr(beg);
  64. cout<<"!!!"<<n;
  65. cin.get();
  66. break;
  67. case 4:
  68. write_file(filename,beg);
  69. break;
  70. case 5:
  71. read_file(filename, &beg, &end);
  72. break;
  73. case 6:
  74. // beg = variant();
  75. searchname(book * beg);
  76. cin.get();
  77. break;
  78. case 7: return 0;
  79. default:
  80. cout << "Вам следует ввести номер от 1 до 5" << endl;
  81. cin.get();
  82. break;
  83. }
  84. }
  85. return 0;
  86. }
  87.  
  88. //-----------------------------------------------------------------------------
  89. void searchname(book * beg)
  90. {
  91. book * temp = beg;
  92. char fname;
  93.  
  94. system ("cls");
  95.  
  96. if (!beg) {
  97. MessageBox(0, L"Список пуст", L"Уведомление", MB_ICONINFORMATION | MB_SETFOREGROUND);
  98. return;
  99. }
  100.  
  101. cout << "Введите название книги для поиска" << endl;
  102. cin >> fname;
  103.  
  104. while (temp) {
  105. if (fname == temp->s.book) {
  106. cout<<"+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+"<<endl;
  107. cout<<"| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |"<<endl;
  108. cout<<"+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+"<<endl;
  109. print(*temp);
  110. system ("pause");
  111. return;
  112. }
  113. temp = temp->next;
  114.  
  115. }
  116. cout << "Книги с таким названием не найдено" << endl;
  117. system ("pause");
  118. }
  119. //----------------------------------------------------------------
  120. sort(const book& s,book* beg){
  121. /* if (!beg){
  122. book*temp=new book;
  123. if (!temp)
  124. {
  125. cout <<"Не хватает памяти!"<<endl;
  126. return 0;
  127. }
  128. *temp=s;
  129. temp->next=NULL;
  130. beg=temp;
  131. return beg;
  132. }*/
  133.  
  134. // book*T=new book;
  135. /* book *temp=new book;
  136. *temp=*beg;
  137. book *temp2=new book;
  138. *temp2=*beg->next;
  139. for (temp;temp->next!=NULL;temp->next)
  140. {
  141. for (temp2;temp2!=NULL;temp2->next)
  142. {
  143. if (s[temp].g != s[temp2].g)
  144. {
  145. if(s[temp].g > s[temp2].g)
  146. {
  147. T=s[temp];
  148. s[temp]=s[temp2];
  149. s[temp2]=T;
  150. continue;
  151. }
  152. }
  153. else if (s[temp].m != s[temp2].m)
  154. {
  155. if (s[temp].m > s[temp2].m)
  156. {
  157. T=s[temp];
  158. s[temp]=s[temp2];
  159. s[temp2]=T;
  160. continue;
  161. }
  162. }
  163. else if (s[temp].d > s[temp2].d)
  164. {
  165. T=s[temp];
  166. s[temp]=s[temp2];
  167. s[temp2]=temp;
  168. }
  169. }
  170. }*/
  171. }
  172.  
  173. //-----------------------------------------------------------------------------
  174. void prosmotr(book *beg)
  175. {
  176. if (!beg)
  177. {
  178. cout << "список пустой" << endl;
  179. cin.get();
  180. return ;
  181. }
  182. book *temp = beg;
  183.  
  184. cout<<"+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+"<<endl;
  185. cout<<"| Автор | Название | Издательство | Жанр | Дата поступления | Стоимость |"<<endl;
  186. cout<<"+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+"<<endl;
  187.  
  188. while (temp)
  189. {
  190. print(*temp);
  191. temp = temp->next;
  192. }
  193.  
  194. cout<<"+———————————————————+————————————————————+——————————————————+————————————————+——————————————————+———————————————+"<<endl;
  195. cin.get();
  196. }
  197.  
  198. //-----------------------------------------------------------------------------
  199. void print(const book& s)
  200. {
  201.  
  202. cout << "|" << s.autor << setw( 20 - strlen(s.autor) ) << "|";
  203. cout << s.name << setw( 21 - strlen(s.name) ) << "|";
  204. cout << s.izdatelstvo << setw( 19 - strlen(s.izdatelstvo) ) << "|";
  205. cout << s.janr << setw( 17 - strlen(s.izdatelstvo) ) << "|";
  206. if (s.d<10) cout << 0 << s.d << ".";
  207. if (s.m<10) cout << 0 << s.m << "." << s.g << setw( 19 ) << "|";
  208. cout << s.cost << setw( 16 ) << "|" << endl;
  209.  
  210. }
  211. //--------------ф-я добавления или создания эл-та-------------------------------
  212. book* dob(book *end,const book& s)
  213. {
  214. book *newE = new book;
  215. *newE = s;
  216. newE->next = 0;
  217. end->next = newE;
  218. end = newE;
  219. return end;
  220. }
  221. //--------------создание первого эл-та------------------------------
  222. book* dob_first(const book& s)
  223. {
  224. book *beg = new book;
  225. *beg = s;
  226. beg->next = 0;
  227. return beg;
  228. }
  229. //-----------------------------------------------------------------------------
  230. book vvod_book()
  231. {
  232. char buf[10];
  233. book s;
  234. cout << "Введите автора:" << endl;
  235. while (1){
  236. cin>>s.autor;
  237. if (atoi(s.autor)==0)
  238. break;
  239. cout<<"Ошибка ввода!"<<endl;
  240. }
  241. cout << "Введите название книги:" << endl;
  242. while (1){
  243. cin>>s.name;
  244. if (atoi(s.name)==0)
  245. break;
  246. cout<<"Ошибка ввода!"<<endl;
  247. }
  248. cout << "Введите издательство:" << endl;
  249. while (1){
  250. cin>>s.izdatelstvo;
  251. if (atoi(s.izdatelstvo)==0)
  252. break;
  253. cout<<"Ошибка ввода!"<<endl;
  254. }
  255. cout << "Введите жанр:" << endl;
  256.  
  257. while (1){
  258. cin>>s.janr;
  259. if (atoi(s.janr)==0)
  260. break;
  261. cout<<"Ошибка ввода!"<<endl;
  262. }
  263. cout << "Введите дату поступления:" << endl;
  264. cout <<"Введите день:";
  265. while(1){
  266. cin>>s.d;
  267. if ((s.d>=1)&&(s.d<=31))
  268. break;
  269. cout<<"Ошибка ввода!Ведите правильную дату!"<<endl;
  270. }
  271. cout <<"Введите месяц:";
  272. while(1){
  273. cin>>s.m;
  274. if ((s.m>=1)&&(s.m<=12))
  275. break;
  276. cout<<"Ошибка ввода!Ведите правильную дату!"<<endl;
  277. }
  278. cout <<"Введите год:";
  279. while(1){
  280. cin>>s.g;
  281. if ((s.g>=1900)&&(s.g<=2050))
  282. break;
  283. cout<<"Ошибка ввода!Ведите правильную дату!"<<endl;
  284. }
  285. cout << "Введите стоимость книги:" << endl;
  286. cin>>s.cost;
  287. return s;
  288. }
  289.  
  290. //-----------------------------------------------------------------------------
  291. /*book* variant(book* beg,const book& s)
  292. {
  293.  
  294. if (!beg)
  295. {
  296. book* temp=new book;
  297. if(!temp)
  298. {
  299. cout<<"Не хватает памяти"<<endl;
  300. return NULL;
  301. }
  302. *temp = s;
  303. temp->next=NULL;
  304. beg=temp;
  305. return beg;
  306. }
  307. book*new_book=new book;
  308. if (!new_book)
  309. {
  310. cout<<"Не хватает памяти"<<endl;
  311. return beg;
  312. }
  313. book* temp=beg;
  314. *new_book=s;
  315. new_book->next=NULL;
  316. if (strcmp(new_book->name,temp->name)<=0)
  317. {
  318. new_book->next=temp;
  319. return new_book;
  320. }
  321. else
  322. {
  323. if(!temp->next&&strcmp(new_book->name,temp->name)>=0)
  324. {
  325. temp->next=new_book;
  326. return beg;
  327. }
  328. }
  329. while (temp->next->next && strcmp(new_book->name,temp->next->name)>=0)
  330. {
  331. temp = temp->next;
  332. }
  333. if(!temp->next->next)
  334. {
  335. if(strcmp(new_book->name,temp->next->name)>=0)
  336. {
  337. temp->next->next=new_book;
  338. return beg;
  339. }
  340. else
  341. {
  342. new_book->next=temp->next;
  343. temp->next=new_book;
  344. return beg;
  345. }
  346. }
  347. else
  348. {
  349. new_book->next=temp->next;
  350. temp->next=new_book;
  351. return beg;
  352. }
  353. }*/
  354. //-----------------------------------------------------------------------------
  355.  
  356. //-----------------------------------------------------------------------------
  357. int read_file(char* filename,book** beg, book** end)
  358. {
  359. cout<<"Введите название файла"<<endl;
  360. cin>>filename;
  361. ifstream fin(filename,ios::in);
  362. if (!fin)
  363. {
  364. cout<<"Нет файла"<<filename<<endl;
  365. return 1;
  366. }
  367. book s;
  368. *beg = 0;
  369. fin.seekg(0, ios::beg);
  370. while (fin>>s.autor)
  371. {
  372. fin>>s.name;
  373. fin>>s.izdatelstvo;
  374. fin>>s.janr;
  375. fin>>s.d;
  376. fin>>s.m;
  377. fin>>s.g;
  378. fin>>s.cost;
  379. if (*beg)
  380. *end=dob(*end,s);
  381. else
  382. {
  383. *beg=dob_first(s); *end=*beg;
  384. }
  385. }
  386. cout<<"Считывание прошло успешно"<<endl;
  387. cin.get();
  388. cin.get();
  389. return 0;
  390. }
  391. //-----------------------------------------------------------------------------
  392. int write_file(char* filename, book* temp)
  393. {
  394. cout<<"Введите название файла"<<endl;
  395. cin>>filename;
  396. ofstream fout(filename, ios_base::app); // открытие файла
  397. if (!fout)
  398. {
  399. cout << "Не могу открыть файл для записи" << endl;
  400. return 1;
  401. }
  402. while (temp)// пока temp!=0 выводим эл-ты в файл
  403. {
  404. fout << temp->autor << endl;
  405. fout << temp->name << endl;
  406. fout << temp->izdatelstvo << endl;
  407. fout << temp->janr << endl;
  408. fout << temp->d<< endl;
  409. fout <<temp->m<< endl;
  410. fout <<temp->g<< endl;
  411. fout << temp->cost << endl;
  412. temp = temp->next;
  413. }
  414. cout << "Данные сохранены в файле: " << filename << endl;
  415. cout << "==============================" << endl;
  416. cout << "Нажмите любую клавишу" << endl;
  417. cin.get();
  418. return 0;
  419. }
  420. //-----------------------------------------------------------------------------
  421. //-----------------------------------------------------------------------------
  422. int menu()
  423. {
  424. char buf[10];
  425. int item;
  426. do
  427. {
  428. system("CLS");
  429. cout << "1 - Создать список или добавить новый эл-т" << endl;
  430. cout << "2 - Удаление всех элементов списка" << endl;
  431. cout << "3 - Просмотр списка" << endl;
  432. cout << "4 - Запись данных в файл" << endl;
  433. cout << "5 - Чтение из файла" << endl;
  434. cout << "6 - 5 самых старых книг" << endl;
  435. cout << "7 - Выход"<< endl;
  436. cout << "============================" << endl;
  437. cout << "Введите номер пункта меню" << endl;
  438. cin >> buf;
  439. cin.get();
  440. item = atoi(buf);
  441. if (!item)
  442. {
  443. cout << "Вам следует вводить число от 1 до 7" << endl;
  444. cin.get();
  445. }
  446. }
  447. while (!item);
  448. return item;
  449. }
  450. //-----------------------------------------------------------------------------
  451. book* udal(book *beg)
  452. {
  453. book *temp;
  454. if (!beg)
  455. {
  456. cout << "список пустой" << endl;
  457. cin.get();
  458. return 0;
  459. }
  460. while (beg)
  461. {
  462. temp = beg;
  463. beg = beg->next;
  464. delete temp;
  465. }
  466. cout << "==============================" << endl;
  467. cout << "====удаление прошло успешно===" << endl;
  468. cout << "==============================" << endl;
  469. return beg;
  470. }
Advertisement
Add Comment
Please, Sign In to add comment