Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.23 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <string>
  6. #include <string.h>
  7. #include <Windows.h>
  8. #include <algorithm>
  9. using namespace std;
  10.  
  11. int n;
  12.  
  13. struct date
  14. {
  15. int day;
  16. int mounth;
  17. int year;
  18. };
  19.  
  20. struct student
  21. {
  22. string name;
  23. string inits;
  24. date datedsar;
  25. };
  26.  
  27. struct dayyear
  28. {
  29. date dr;
  30. int nm;
  31. };
  32.  
  33. bool sortyear(student a, student b); //исходный // сортировка по годам
  34. bool sortvozrast(student a, student b);//выходной //сортировка от самого старшего к самому младшему
  35. bool sortalf(student a, student b);//выходной
  36. void vivod(student *a,int n); //Вывод массива на экран
  37. void zapisvfile(student *a, int n); //Запись массива в файл
  38. void searchhh(student *a, student b[], int n, int& chek); //поиск
  39. void pechatchapki(); //Печать шапки
  40. void addstudent(student a[], int &n); //добавить студента
  41. void deletestudent(student a[], int& n); //удалить студента
  42. void pere4en(student *a, int n, dayyear b[], int& k);
  43. void vivodpere4n(dayyear *a, int n);
  44. void zapisvfileper4(dayyear *a, int n);
  45. bool sortpere42(dayyear a, dayyear b);
  46. bool sortpere41(dayyear a, dayyear b);
  47.  
  48. int main()
  49. {
  50. SetConsoleCP(1251);
  51. SetConsoleOutputCP(1251);
  52. int vibormenu, sortirovka, n, chek, c4k;
  53. chek = 0, c4k = 0;
  54. const int N=1000;
  55. student srch, gg;
  56. cout << "Введите колличество студентов" << endl; cin >> n;
  57. /*student* real = new student[n];*/
  58. student real[N], poiskk[N];
  59. dayyear pr4n[N] = { 0 };
  60. ifstream filevhod("C:/Users/Vadim/Desktop/studen1.txt");
  61. for (int i = 0; i < n; i++)
  62. filevhod >> real[i].name >> real[i].inits >> real[i].datedsar.day >> real[i].datedsar.mounth >> real[i].datedsar.year; //Считывание массива
  63. //
  64. //sort(real, real + n, sortyear); //изначальная сортировка
  65. menu:
  66. system("cls");
  67. cout << "Выберите пункт меню(1,2,3,4,5)" << endl;
  68. cout << "1 - Вывод массива на экран" << endl;
  69. cout << "2 - Сортировка" << endl;
  70. cout << "3 - Поиск" << endl;
  71. cout << "4 - Новый студент" << endl;
  72. cout << "5 - Удалить студента" << endl;
  73. cout << "6 - сформировать перечень" << endl;
  74. cout << "Для выхода из программы нажмите на любую другу клавишу" << endl;
  75. cin >> vibormenu;
  76. switch (vibormenu)
  77. {
  78. case 1:
  79. {
  80. system("cls");
  81. vivod(real, n);
  82. system("pause");
  83. goto menu;
  84. }
  85. case 2:
  86. {
  87. system("cls");
  88. cout << "Выберите способ сортировки(1,2,3)" << endl;
  89. cout << "1 - Сортировка по году рождения по возрастанию" << endl;
  90. cout << "2 - Сортировка по возрасту, в порядке убывания" << endl;
  91. cout << "3 - Сортировка по по фамилии и инциалам в алфавитном порядке" << endl;
  92. cout << "Нажмите любую клавишу для возврата в меню" << endl;
  93. cin >> sortirovka;
  94. switch (sortirovka)
  95. {
  96. case 1:
  97. {
  98. sort(real, real + n, sortyear);
  99. vivod(real, n);
  100. zapisvfile(real, n);
  101. system("pause");
  102. goto menu;
  103. }
  104. case 2:
  105. {
  106. sort(real, real + n, sortvozrast);
  107. vivod(real, n);
  108. zapisvfile(real, n);
  109. system("pause");
  110. goto menu;
  111. }
  112. case 3:
  113. {
  114. sort(real, real + n, sortalf);
  115. vivod(real, n);
  116. zapisvfile(real, n);
  117. system("pause");
  118. goto menu;
  119.  
  120. }
  121. default:
  122. {
  123. goto menu;
  124. }
  125. }
  126. }
  127. case 3:
  128. {
  129. int psk;
  130. chek = 0;
  131. searchhh(real, poiskk, n, chek);
  132. vivod(poiskk, chek);
  133. cout << "Как отсортировать массив?" << endl;
  134. cout << "1 - по возраству; 2 - по алфавиту; 3 - выход без сортировки" << endl;
  135. cin >> psk;
  136. switch (psk)
  137. {
  138. case 1:
  139. {
  140. sort(poiskk, poiskk + chek, sortvozrast);
  141. vivod(poiskk, chek);
  142. zapisvfile(poiskk, chek);
  143. system("pause");
  144. break;
  145. }
  146. case 2:
  147. {
  148. sort(poiskk, poiskk + chek, sortalf);
  149. vivod(poiskk, chek);
  150. zapisvfile(poiskk, chek);
  151. system("pause");
  152. break;
  153. }
  154. case 3:
  155. {
  156. goto menu;
  157. }
  158. }
  159.  
  160.  
  161.  
  162. system("pause");
  163. goto menu;
  164. }
  165. case 4:
  166. {
  167. system("cls");
  168. addstudent(real, n);
  169. vivod(real, n);
  170. zapisvfile(real, n);
  171. goto menu;
  172. break;
  173. }
  174. case 5:
  175. {
  176. system("cls");
  177. vivod(real, n);
  178. deletestudent(real, n);
  179. zapisvfile(real, n);
  180. goto menu; // Вшзврат к меню
  181. }
  182. case 6:
  183. {
  184. system("cls");
  185. pere4en(real, n, pr4n, c4k);
  186. vivodpere4n(pr4n, c4k);
  187. zapisvfileper4(pr4n, c4k);
  188. int otvet2;
  189. cout << "Выберите способ сортировки перечня" << endl;
  190. cout << "1 - по году рождения в порядке возрастания" << endl;
  191. cout << "2 - по количеству студентов в порядке убывания" << endl;
  192. cout << "3 - выход без сортировки" << endl;
  193. cin >> otvet2;
  194. switch (otvet2)
  195. {
  196. case 1:
  197. {
  198. system("cls");
  199. sort(pr4n, pr4n + c4k,sortpere41);
  200. vivodpere4n(pr4n, c4k);
  201. zapisvfileper4(pr4n, c4k);
  202. system("pause");
  203. goto menu;
  204. }
  205. case 2:
  206. {
  207. system("cls");
  208. sort(pr4n, pr4n + c4k, sortpere42);
  209. vivodpere4n(pr4n, c4k);
  210. zapisvfileper4(pr4n, c4k);
  211. system("pause");
  212. goto menu;
  213. }
  214. case 3:
  215. {
  216. goto menu;
  217. }
  218. }
  219. }
  220. }
  221.  
  222. }
  223.  
  224.  
  225. void vivod(student *a, int n)
  226. {
  227. cout << "__________________________________________" << endl;
  228. cout << "| № | Фамилия и инициалы | Дата рождения |" << endl;
  229. cout << "__________________________________________" << endl;
  230. for (int i = 0; i < n; i++)
  231. {
  232. cout << "|" << setw(3) << i+1 << "|" << setw(15) << a[i].name << " " << setw(4) << a[i].inits << "|" << setw(7) << a[i].datedsar.day << "." << setw(2) << a[i].datedsar.mounth << "." << setw(4) << a[i].datedsar.year << "|" << endl;
  233. }
  234. }
  235.  
  236. void zapisvfile(student *a, int n)
  237. {
  238. int otvet;
  239. cout << "Сохранить в файл?(1-да/2-нет)" << endl; cin >> otvet;
  240. if (otvet == 1)
  241. {
  242. string filename, kone4niipyt;
  243. string pytrabstol = "C:/Users/Vadim/Desktop/";
  244. string txt = ".txt";
  245. cout << "Введите имя конечного файла" << endl; cin >> filename;
  246. kone4niipyt = pytrabstol + filename;
  247. kone4niipyt = kone4niipyt + txt;
  248. ofstream fout(kone4niipyt);
  249. if (!fout.is_open()) cout << "Ошибка";
  250. fout << "__________________________________________" << endl;
  251. fout << "| № | Фамилия и инициалы | Дата рождения |" << endl;
  252. fout << "__________________________________________" << endl;
  253. for (int i = 0; i < n; i++)
  254. {
  255. fout << "|" << setw(3) << i+1 << "|" << setw(15) << a[i].name << " " << setw(4) << a[i].inits << "|" << setw(7) << a[i].datedsar.day << "." << setw(2) << a[i].datedsar.mounth << "." << setw(4) << a[i].datedsar.year << "|" << endl;
  256. }
  257. }
  258. else return;
  259.  
  260. }
  261.  
  262. bool sortyear(student a, student b)
  263. {
  264. return abs(a.datedsar.year) < abs(b.datedsar.year);
  265. }
  266.  
  267. bool sortvozrast(student a, student b)
  268. {
  269. if (a.datedsar.year != b.datedsar.year)
  270. return a.datedsar.year < b.datedsar.year;
  271. else if (a.datedsar.year == b.datedsar.year)
  272. {
  273. if (a.datedsar.mounth != b.datedsar.mounth)
  274. return a.datedsar.mounth < b.datedsar.mounth;
  275. else
  276. return a.datedsar.day < b.datedsar.day;
  277. };
  278. }
  279.  
  280.  
  281.  
  282. void searchhh(student *a, student b[], int n, int& chek)
  283. {
  284. student search, search2;
  285. cout << "Введите левую границу поиска(День,месяц,год — через пробел)" << endl;
  286. cin >> search.datedsar.day >> search.datedsar.mounth >> search.datedsar.year;
  287. cout << "Введите правую границу поиска(День,месяц,год — через пробел)" << endl;
  288. cin >> search2.datedsar.day >> search2.datedsar.mounth >> search2.datedsar.year;
  289. chek = 0;
  290. for (int i = 0; i < n; i++)
  291. {
  292.  
  293.  
  294. if ((a[i].datedsar.year >= search.datedsar.year ) and (a[i].datedsar.year <= search2.datedsar.year))
  295. {
  296. if ((search.datedsar.year == a[i].datedsar.year) or (a[i].datedsar.year == search2.datedsar.year))
  297. {
  298. if ((search2.datedsar.mounth >= a[i].datedsar.mounth))
  299. {
  300. b[chek] = a[i];
  301. chek++;
  302. }
  303. }
  304. else {
  305. b[chek] = a[i];
  306. chek++;
  307. }
  308. }
  309. }
  310.  
  311. }
  312.  
  313. bool sortalf(student a, student b)
  314. {
  315. if (a.name != b.name)
  316. return a.name < b.name;
  317. else
  318. return a.inits < b.inits;
  319. }
  320.  
  321. void addstudent(student a[], int& n)
  322. {
  323. int s;
  324. cout << "Введите имя, инициаллы и дату рождения студента(день,месяц,год)" << endl;
  325. student newstudent;
  326. cin >> newstudent.name >> newstudent.inits >> newstudent.datedsar.day >> newstudent.datedsar.mounth >> newstudent.datedsar.year;
  327. a[n] = newstudent;
  328. n++;
  329. }
  330.  
  331. void deletestudent(student a[], int& n)
  332. {
  333. int i, j;
  334. char ch;
  335. i = 0;
  336. cout << "Номер указанной строки";
  337. cin >> j;
  338. j--;
  339. if (j < 0 || j >= n)
  340. {
  341. cout << "Такой строки нет";
  342. system("pause");
  343. return;
  344. }
  345. else
  346. {
  347.  
  348. cout << a[j].name << " " << a[j].inits << " " << a[j].datedsar.day << "." << a[j].datedsar.mounth << "."<< a[j].datedsar.year << "." << endl;
  349. cout << "Удалить?" << endl;
  350. cout << "Yes/No" << endl;
  351. cin >> ch;
  352. if (ch == 'n')
  353. return;
  354. if (ch != 'y')
  355. {
  356. cout << "Некорректно";
  357. system("pause");
  358. return;
  359. }
  360. for (i = j + 1; i < n; i++)
  361. a[i - 1] = a[i];
  362. n--;
  363. cout << "Запись удалена \n";
  364.  
  365. }
  366. system("pause");
  367. }
  368.  
  369. void pere4en(student *a, int n, dayyear b[], int& c4k)
  370. {
  371. c4k = 0;
  372. for (int i = 0; i < n; i++)
  373. {
  374. b[i].nm = 0;
  375. b[i].dr.year = 0;
  376. b[i].dr.mounth = 0;
  377. b[i].dr.day = 0;
  378. }
  379. int fl = 0;
  380. for (int i = 0; i < n; i++)
  381. {
  382. fl = 0;
  383. for (int j = 0; j < n; j++)
  384. {
  385. if (a[i].datedsar.day == b[j].dr.day && a[i].datedsar.mounth == b[j].dr.mounth && a[i].datedsar.year == b[j].dr.year)
  386. {
  387. fl = 1;
  388. }
  389. }
  390. if (fl == 0)
  391. {
  392. b[c4k].dr = a[i].datedsar;
  393. c4k++;
  394. }
  395. }
  396. for (int i = 0; i < c4k; i++)
  397. {
  398. for (int j = 0; j < n; j++)
  399. if (b[i].dr.day == a[j].datedsar.day && b[i].dr.mounth == a[j].datedsar.mounth && b[i].dr.year == a[j].datedsar.year)
  400. {
  401. b[i].nm++;
  402. }
  403. }
  404. cout << "Перечень удачно сформирован!" << endl << endl;
  405. }
  406.  
  407. void vivodpere4n(dayyear *a, int n)
  408. {
  409. cout << "__________________________________" << endl;
  410. cout << "| № | День рождения | Количество |" << endl;
  411. cout << "__________________________________" << endl;
  412. for (int i = 0; i < n; i++)
  413. {
  414. cout << "|" << setw(3) << i + 1 << "|" << setw(7) << a[i].dr.day << "." << setw(2) << a[i].dr.mounth << "." << setw(4) << a[i].dr.year << "|" << setw(11) << a[i].nm << "|" << endl;
  415. }
  416. }
  417.  
  418. void zapisvfileper4(dayyear *a, int n)
  419. {
  420. int otvet;
  421. cout << "Сохранить в файл?(1-да/2-нет)" << endl; cin >> otvet;
  422. if (otvet == 1)
  423. {
  424. string filename, kone4niipyt;
  425. string pytrabstol = "C:/Users/Vadim/Desktop/";
  426. string txt = ".txt";
  427. cout << "Введите имя конечного файла" << endl; cin >> filename;
  428. kone4niipyt = pytrabstol + filename;
  429. kone4niipyt = kone4niipyt + txt;
  430. ofstream fout(kone4niipyt);
  431. if (!fout.is_open()) cout << "Ошибка";
  432. fout << "__________________________________" << endl;
  433. fout << "| № | День рождения | Количество |" << endl;
  434. fout << "__________________________________" << endl;
  435. for (int i = 0; i < n; i++)
  436. {
  437. fout << "|" << setw(3) << i + 1 << "|" << setw(7) << a[i].dr.day << "." << setw(2) << a[i].dr.mounth << "." << setw(4) << a[i].dr.year << "|" << setw(11) << a[i].nm << "|" << endl;
  438. }
  439. }
  440. else return;
  441.  
  442. }
  443.  
  444. bool sortpere41(dayyear a,dayyear b)
  445. {
  446. if (a.dr.year != b.dr.year)
  447. return a.dr.year < b.dr.year;
  448. else if (a.dr.year == b.dr.year)
  449. {
  450. if (a.dr.mounth != b.dr.mounth)
  451. return a.dr.mounth < b.dr.mounth;
  452. else
  453. return a.dr.day < b.dr.day;
  454. };
  455. }
  456.  
  457. bool sortpere42(dayyear a, dayyear b)
  458. {
  459. return a.nm > b.nm;
  460. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement