Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.85 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <conio.h>
  5. #include <string.h>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <Windows.h>
  9. using namespace std;
  10. //void input MasStud( int i, student a[]);
  11. //void output MasStud(int i, student a[]);
  12. struct date
  13. {
  14. int d;
  15. int m;
  16. int y;
  17. };
  18. struct student
  19. {
  20. string name;
  21. string f;
  22. string o;
  23. date dr;
  24. };
  25. struct sezon
  26. {
  27. string sez;
  28. int col;
  29.  
  30. };
  31.  
  32. //#define MAX_LINE 25
  33. //student a[MAX_LINE];
  34. //sezon y[4];
  35.  
  36. //int i;
  37. const int L = 30;
  38.  
  39. void inputMasStudFile(int &n, student *&px)
  40. {
  41. char file[L];
  42.  
  43. // n = 0;
  44. setlocale(LC_ALL, "rus");
  45. ifstream fin;
  46. student t;
  47. cout << "Имя файла: ";
  48. cin >> file;
  49. strcat_s(file, ".txt");
  50. fin.open(file);
  51. if (fin.fail())
  52.  
  53. {
  54. cout << file << "Не открылся\n ";
  55. _getch();
  56. return;
  57. }
  58. n = 0;
  59. while (!fin.eof())
  60. {
  61. fin >> t.name >> t.f >> t.o >> t.dr.d >> t.dr.m >> t.dr.y;
  62. if (fin.fail()) break;
  63. n++;
  64. }
  65. fin.close();
  66. px = new student[n];
  67. if (px == NULL)
  68. {
  69. cout << "Нет памяти";
  70. _getch();
  71. n = 0;
  72. return;
  73. }
  74. fin.open(file);
  75. if (fin.fail())
  76. {
  77. cout << "Файл не открылся";
  78. _getch();
  79. delete[] px;
  80. px = NULL;
  81. n = 0;
  82. return;
  83. }
  84. for (int i=0; i < n; i++)
  85.  
  86. fin >> px[i].name >> px[i].f >> px[i].o >> px[i].dr.d >> px[i].dr.m >> px[i].dr.y;
  87.  
  88. fin.close();
  89. cout << "Файл введен";
  90. _getch();
  91.  
  92.  
  93. }
  94.  
  95.  
  96. void outputMasStudFile(int n, student px[])
  97.  
  98. {
  99. int j;
  100. cout << "_____________________________________________________________\n";
  101. cout << " | | \n";
  102. cout << "№ | Имя студента | Дата рождения \n";
  103. cout << " | |__________________________\n";
  104. cout << " | | День | Месяц | Год \n";
  105. cout << "__|___________________________________________________________\n";
  106. for (j = 0; j < n; j++)
  107. cout << setw(2) << j + 1 << "|" << setw(27) << px[j].name << " " << px[j].f << "." << px[j].o << " | " << setw(6) << px[j].dr.d << " | " << setw(7) << px[j].dr.m << " | " << setw(4) << px[j].dr.y << endl;
  108.  
  109. }
  110.  
  111.  
  112. void sortyear(int n, student px[]) //сортировка по дате в порядке возрастания
  113. {
  114.  
  115. student s;
  116. for (int i = 0; i<n; i++)
  117. for (int j = 0; j<n; j++)
  118. if (px[i].dr.y<px[j].dr.y) //сортировка по годам
  119. {
  120. s = px[i];
  121. px[i] = px[j];
  122. px[j] = s;
  123. }
  124.  
  125. }
  126.  
  127. void sortname(int n , student px[]) //по алфавиту
  128. {
  129. student t;
  130. for (int i = 0; i<n; i++)
  131. for (int j = 0; j<n; j++)
  132. if (px[i].name<px[j].name)
  133. {
  134. t = px[i];
  135. px[i] = px[j];
  136. px[j] = t;
  137. }
  138. for (int i = 0; i<n; i++)
  139. for (int j = 0; j<n; j++)
  140. if (px[i].name == px[j].name)
  141. {
  142. if (px[i].f < px[j].f)
  143. {
  144. t = px[i];
  145. px[i] = px[j];
  146. px[j] = t;
  147. }
  148. }
  149. for (int i = 0; i<n; i++)
  150. for (int j = 0; j<n; j++)
  151. if (px[i].name == px[j].name && px[i].f == px[j].f)
  152. {
  153. if (px[i].o < px[j].o)
  154. {
  155. t = px[i];
  156. px[i] = px[j];
  157. px[j] = t;
  158. }
  159. }
  160. }
  161.  
  162. void sortyearvixod(int n, student px[]) //сортировка по дате в порядке возрастания
  163. {
  164.  
  165. student s;
  166. //student y;
  167. for (int i = 0; i<n; i++)
  168. for (int j = 0; j<n; j++)
  169. if (px[i].dr.y>px[j].dr.y) //сортировка по годам
  170. {
  171. s = px[i];
  172. px[i] = px[j];
  173. px[j] = s;
  174. }
  175. for (int i = 0; i<n; i++)//сортировка по месяцам
  176. for (int j = 0; j<n; j++)
  177. if (px[i].dr.y == px[j].dr.y)
  178. {
  179. if (px[i].dr.m>px[j].dr.m)
  180. {
  181. s = px[i];
  182. px[i] = px[j];
  183. px[j] = s;
  184. }
  185. }
  186. for (int i = 0; i<n; i++)//сортировка по дням
  187. for (int j = 0; j<n; j++)
  188. if (px[i].dr.y == px[j].dr.y && px[i].dr.m == px[j].dr.m)
  189. {
  190. if (px[i].dr.d>px[j].dr.d)
  191. {
  192. s = px[i];
  193. px[i] = px[j];
  194. px[j] = s;
  195. }
  196. }
  197. //a[k] = y[l];
  198.  
  199. }
  200.  
  201.  
  202.  
  203. void sortnameyear(int n, student px[]) //совмещенная сортировка
  204. {
  205.  
  206. student s;
  207. for (int i = 0; i < n; i++)
  208. for (int j = 0; j < n; j++)
  209. if (px[i].dr.y < px[j].dr.y) //сортировка по годам
  210. {
  211. s = px[i];
  212. px[i] = px[j];
  213. px[j] = s;
  214. }
  215. for (int i = 0; i < n; i++)//сортировка по месяцам
  216. for (int j = 0; j < n; j++)
  217. if (px[i].dr.y == px[j].dr.y)
  218. {
  219. if (px[i].dr.m < px[j].dr.m)
  220. {
  221. s = px[i];
  222. px[i] = px[j];
  223. px[j] = s;
  224. }
  225. }
  226. for (int i = 0; i < n; i++)//сортировка по дням
  227. for (int j = 0; j < n; j++)
  228. if (px[i].dr.y == px[j].dr.y && px[i].dr.m == px[j].dr.m)
  229. {
  230. if (px[i].dr.d < px[j].dr.d)
  231. {
  232. s = px[i];
  233. px[i] = px[j];
  234. px[j] = s;
  235. }
  236. }
  237. for (int i = 0; i < n; i++)
  238. for (int j = 0; j < n; j++)
  239. if (px[i].dr.y == px[j].dr.y && px[i].dr.m == px[j].dr.m && px[i].dr.d == px[j].dr.d)
  240. {
  241. if (px[i].name < px[j].name)
  242. {
  243. s = px[i];
  244. px[i] = px[j];
  245. px[j] = s;
  246. }
  247. }
  248. for (int i = 0; i < n; i++)
  249. for (int j = 0; j < n; j++)
  250. if (px[i].name == px[j].name)
  251. {
  252. if (px[i].f < px[j].f)
  253. {
  254. s = px[i];
  255. px[i] = px[j];
  256. px[j] = s;
  257. }
  258. }
  259. for (int i = 0; i < n; i++)
  260. for (int j = 0; j < n; j++)
  261. if (px[i].name == px[j].name && px[i].f == px[j].f)
  262. {
  263. if (px[i].o < px[j].o)
  264. {
  265. s = px[i];
  266. px[i] = px[j];
  267. px[j] = s;
  268. }
  269. }
  270. }
  271.  
  272. void deleten(int &n, student *&px)
  273. {
  274. int i, j;
  275. student *p;
  276. char ch;
  277. outputMasStudFile(n, px);
  278. cout << "Номер указанной строки";
  279. cin >> j;
  280. j--;
  281. if (j < 0 || j >= n)
  282. {
  283. cout << "Такой строки нет";
  284. _getch();
  285. return;
  286. }
  287. //j--;
  288. cout << j + 1 << "-я строка";
  289. cout << px[j].name << px[j].f << px[j].o << px[j].o << px[j].dr.d << px[j].dr.m << px[j].dr.y << endl;
  290. cout << "Удалить?" << endl;
  291. cout << "Yes/No" << endl;
  292. cin >> ch;
  293. if (ch == 'n')
  294. return;
  295. if (ch != 'y')
  296. {
  297. cout << "Некорректно";
  298. _getch();
  299. return;
  300. }
  301. if (n == 1)
  302. {
  303. delete[]px;
  304. px == NULL;
  305. n = 0;
  306. }
  307. else
  308. {
  309. p = new student[n - 1];
  310. if (p == NULL)
  311. {
  312. cout << "Нет памяти";
  313. _getch;
  314. return;
  315. }
  316. for (i = 0; i < j; i++)
  317. p[i] = px[i];
  318. for (i = j + 1; i < n; i++)
  319. p[i - 1] = px[i];
  320. delete[]px;
  321. px = p;
  322. n--;
  323.  
  324. }
  325. cout << "Запись удалена \n";
  326. _getch();
  327.  
  328. }
  329.  
  330. void pluse(int &n, student *&px)
  331. {
  332. setlocale(LC_ALL, "rus");
  333. student t;
  334. if (n==0)
  335. {
  336. cout << "Массив существует";
  337. _getch();
  338. return;
  339.  
  340. }
  341. cout << "Фамилия"; cin >> t.name;
  342. cout << "Имя"; cin >> t.f;
  343. cout << "Отчество"; cin >> t.o;
  344. cout << "Дата рождения";
  345. cout << "День"; cin >> t.dr.d;
  346. cout << "Месяц"; cin >> t.dr.m;
  347. cout << "Год"; cin >> t.dr.y;
  348. student*promMas = new student[n + 1];
  349. if (promMas == NULL)
  350. {
  351. cout << "Нет памяти";
  352. _getch();
  353. return;
  354. }
  355. for (int i = 0; i < n; i++)
  356. {
  357. promMas[i] = px[i];
  358. }
  359. promMas[n] = t;
  360. delete[]px;
  361. n++;
  362. px = new student[n];
  363. if (px == NULL)
  364. {
  365. cout << "Нет памяти";
  366. _getch();
  367. return;
  368. }
  369. for (int i = 0; i < n; i++)
  370. {
  371. px[i] = promMas[i];
  372. }
  373. cout << "Запись добавлена";
  374. delete[] promMas;
  375. _getch();
  376.  
  377.  
  378.  
  379. }
  380.  
  381. void outputMasStudFilet(int n, student px[])
  382.  
  383. {
  384. ofstream fout("filelab3.txt");
  385. if (!fout.is_open()) cout << "Ошибка";
  386. int j;
  387. fout << "_____________________________________________________________\n";
  388. fout << " | | \n";
  389. fout << "№ | Имя студента | Дата рождения \n";
  390. fout << " | |__________________________\n";
  391. fout << " | | День | Месяц | Год \n";
  392. fout << "__|___________________________________________________________\n";
  393. for (j = 0; j < n; j++)
  394. fout << setw(2) << j + 1 << "|" << setw(27) << px[j].name << " " << px[j].f << "." << px[j].o << " | " << setw(6) << px[j].dr.d << " | " << setw(7) << px[j].dr.m << " | " << setw(4) << px[j].dr.y << endl;
  395.  
  396. }
  397.  
  398. void outputMasStudFiletvixod(int n, student px[])
  399.  
  400. {
  401. ofstream fout("filelab4.txt");
  402. if (!fout.is_open()) cout << "Ошибка";
  403. int j;
  404. fout << "_____________________________________________________________\n";
  405. fout << " | | \n";
  406. fout << "№ | Имя студента | Дата рождения \n";
  407. fout << " | |__________________________\n";
  408. fout << " | | День | Месяц | Год \n";
  409. fout << "__|___________________________________________________________\n";
  410. for (j = 0; j < n; j++)
  411. fout << setw(2) << j + 1 << "|" << setw(27) << px[j].name << " " << px[j].f << "." << px[j].o << " | " << setw(6) << px[j].dr.d << " | " << setw(7) << px[j].dr.m << " | " << setw(4) << px[j].dr.y << endl;
  412.  
  413. }
  414.  
  415. void Perechen(int n, student px[], int &k, sezon *&py)
  416. {
  417.  
  418. int i, j;
  419. int l = 4;
  420. k = 4;
  421. sezon *y;
  422. y = new sezon[n];
  423. if (y==NULL)
  424.  
  425. {
  426. cout << "Перечень пуст\n";
  427. _getch();
  428. return;
  429. }
  430.  
  431.  
  432.  
  433. y[0].col = 0;
  434. y[1].col = 0;
  435. y[2].col = 0;
  436. y[3].col = 0;
  437.  
  438. for (i = 0; i < n; i++)
  439. {
  440. if ((px[i].dr.m == 12) || (px[i].dr.m == 1) || (px[i].dr.m == 2))
  441. {
  442. y[0].col++;
  443.  
  444. }
  445. if ((px[i].dr.m == 3) || (px[i].dr.m == 4) || (px[i].dr.m == 5))
  446. {
  447. y[1].col++;
  448. }
  449. if ((px[i].dr.m == 6) || (px[i].dr.m == 7) || (px[i].dr.m == 8))
  450. {
  451. y[2].col++;
  452. }
  453.  
  454. if ((px[i].dr.m == 9) || (px[i].dr.m == 10) || (px[i].dr.m == 11))
  455. {
  456. y[3].col++;
  457. }
  458. }
  459.  
  460.  
  461. y[0].sez = "зима";
  462. y[1].sez = "весна";
  463. y[2].sez = "лето";
  464. y[3].sez = "осень";
  465. if (py != NULL) delete[]py;
  466. py = new sezon[k];
  467. if (py == NULL)
  468. {
  469. cout << "Нет памяти";
  470. _getch();
  471. delete[]py;
  472. return;
  473. }
  474.  
  475. for (j = 0; j < l; j++)
  476. {
  477. py[j] = y[j];
  478.  
  479. }
  480. delete[]y;
  481.  
  482. cout << "Перечень составлен \n ";
  483. _getch();
  484.  
  485. }
  486.  
  487.  
  488. void outputPerechen(int k, sezon py[])
  489.  
  490. {
  491.  
  492. int j;
  493. cout << "______________________________________________________________\n";
  494. cout << " | Перечень \n";
  495. cout << "№ |___________________________________________________________\n";
  496. cout << " | Сезон | Колличество годов \n";
  497. cout << "__|___________________________________________________________\n";
  498. for (j = 0; j < k; j++)
  499. cout << setw(2) << j + 1 << "|" << setw(33) << py[j].sez << " | " << setw(21) << py[j].col << " | " << endl;
  500.  
  501. }
  502.  
  503.  
  504. void sortPerechenyb(int m, sezon py[])
  505. {
  506. sezon s;
  507. for (int i = 0; i < m; i++)
  508. for (int j = 0; j < m; j++)
  509. {
  510. if (py[i].col > py[j].col)
  511. {
  512. s = py[i];
  513. py[i] = py[j];
  514. py[j] = s;
  515. }
  516.  
  517. }
  518.  
  519. }
  520.  
  521. void outputPerechenFile(int m, sezon py[])
  522.  
  523. {
  524. ofstream fout("filelab5.txt");
  525. if (!fout.is_open()) cout << "Ошибка";
  526. int j;
  527. fout << "______________________________________________________________\n";
  528. fout << " | Перечень \n";
  529. fout << "№ |__________________________________________________________ \n";
  530. fout << " | Сезон | Колличество годов \n";
  531. fout << "__|___________________________________________________________\n";
  532. for (j = 0; j < m; j++)
  533. fout << setw(2) << j + 1 << "|" << setw(33) << py[j].sez << " | " << setw(21) << py[j].col << " | " << endl;
  534.  
  535. }
  536.  
  537.  
  538.  
  539. int main()
  540. {
  541.  
  542.  
  543. student *px(NULL), *pb(NULL);
  544. sezon *py(NULL);
  545. int n(0),m(0), k(0);
  546. //int k = 4;
  547.  
  548. {
  549.  
  550.  
  551. SetConsoleCP(1251);
  552. SetConsoleOutputCP(1251);
  553. }
  554.  
  555.  
  556. setlocale(LC_ALL, "rus");
  557. while (1)
  558. {
  559. system("cls");
  560.  
  561. cout << "1-ввод данных из файла" << endl;
  562. cout << "2-вывод данных на экран" << endl;
  563. cout << "3-сортировка входного массива по году в порядке возростания" << endl;
  564. cout << "4-сортировка входного массива по алфавиту" << endl;
  565. cout << "5-сортировка входного массива по алфавиту и по дате" << endl;
  566.  
  567. cout << "6-удаление строки" << endl;
  568. cout << "7-добавление строки" << endl;
  569.  
  570. cout << "8- вывод в файл" << endl;
  571. cout << "9-в порядке возрастания даты рождения выходной" << endl;
  572. cout << "10- вывод в файл выходной" << endl;
  573. cout << "11- перечень" << endl;
  574. cout << "12- вывод перечня на экран" << endl;
  575. cout << "13- сортировка перечня в порядке убывания" << endl;
  576. cout << "14-вывод перечня в файл" << endl;
  577.  
  578. int ch;
  579. cin >> ch;
  580. switch (ch)
  581. {
  582. case 1: inputMasStudFile(n, px);
  583. case 2: outputMasStudFile(n, px); break;
  584. case 3: sortyear(n, px); break;
  585. case 4: sortname(n, px); break;
  586. case 5: sortnameyear(n, px); break;
  587.  
  588. case 6: deleten(n, px); break;
  589. case 7: pluse(n, px); break;
  590. case 8: outputMasStudFilet(n, px); break;
  591. case 9: sortyearvixod(n, px); break;
  592. case 10: outputMasStudFiletvixod(n, px); break;
  593. case 11: Perechen(n, px, k, py); break;
  594. case 12:outputPerechen(k, py); break;
  595. case 13: sortPerechenyb(k, py); break;
  596. case 14: outputPerechenFile(k, py); break;
  597.  
  598. }
  599. _getch();
  600. }
  601. _getch();
  602. return 0;
  603.  
  604. _getch();
  605. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement