Advertisement
Guest User

main.cpp

a guest
Nov 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <iomanip>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. const int N = 50;
  9.  
  10. struct Student
  11. {
  12. char nameSt[30];
  13. char group[10];
  14. int day;
  15. int month;
  16. int year;
  17. };
  18.  
  19. void readSourceDateFromFile(Student a[], int &size);
  20. void outputDateToScreen(Student a[], int size);
  21. void sortSourceStudYear(Student a[], int size);
  22. void sortSourceStudAge(Student a[], int size);
  23. void addStudent(Student a[], int &size);
  24. void deleteStudent(Student a[], int &size);
  25. void searchStudents(Student a[], int sourceSize, Student b[], int &resultSize);
  26. void sortSearchStudent(Student a[], int size);
  27. void insertDateToFile(Student a[], int size);
  28.  
  29.  
  30. int main()
  31. {
  32. bool exit = true;
  33. int choice;
  34.  
  35. Student sourceSt[N];
  36. Student searchSt[N];
  37. int realSourceSize, realSearchSize;
  38.  
  39. while (exit)
  40. {
  41. system("clear");//system("CLS");
  42.  
  43. cout << "1.\tВвести исходный массив из файла.\n";
  44. cout << "2.\tВывести исходный массив на экран.\n";
  45. cout << "3.\tСортировка исходного массива по дате рождения в порядке убывания возраста.\n";
  46. cout << "4.\tСортировка исходного массива по году рождения в порядку убывания.\n";
  47. cout << "5.\tДобавить студента в исходный массив студентов.\n";
  48. cout << "6.\tУдалить студента в исходный массив студентов.\n";
  49. cout << "7.\tЗаписать исходный массив в файл.\n";
  50. cout << "8.\tНайти всех студентов, родившихся весной (выходной массив).\n";
  51. cout << "9.\tВывести выходной массив на экран.\n";
  52. cout << "10.\tСортировка выходного массива по фамилии студентов в алфавитном порядке.\n";
  53. cout << "11.\tЗаписать выходной массив в файл.\n";
  54. cout << "0.\tВывод из программы.\n";
  55.  
  56. cout << "Введите пункт меню: ";
  57. cin >> choice;
  58. switch (choice)
  59. {
  60. case 0:
  61. exit = false;
  62. break;
  63. case 1:
  64. readSourceDateFromFile(sourceSt, realSourceSize);
  65. break;
  66. case 2:
  67. outputDateToScreen(sourceSt, realSourceSize);
  68. break;
  69. case 3:
  70. sortSourceStudAge(sourceSt, realSourceSize);
  71. break;
  72. case 4:
  73. sortSourceStudYear(sourceSt, realSourceSize);
  74. break;
  75. case 5:
  76. addStudent(sourceSt, realSourceSize);
  77. break;
  78. case 6:
  79. deleteStudent(sourceSt, realSourceSize);
  80. break;
  81. case 7:
  82. insertDateToFile(sourceSt, realSourceSize);
  83. break;
  84. case 8:
  85. searchStudents(sourceSt, realSourceSize, searchSt, realSearchSize);
  86. break;
  87. case 9:
  88. outputDateToScreen(searchSt, realSearchSize);
  89. break;
  90. case 10:
  91. sortSearchStudent(searchSt, realSearchSize);
  92. break;
  93. case 11:
  94. insertDateToFile(searchSt, realSearchSize);
  95. break;
  96. }
  97.  
  98. system("read -p 'Нажмите Enter для продолжения...' var");//system("pause");
  99. }
  100. }
  101. //1
  102. void readSourceDateFromFile(Student a[], int &size)
  103. {
  104. size = 0;
  105. char fileName[30];
  106. char iniz[10];
  107.  
  108. cout << "\nВведите имя файла: ";
  109. cin >> fileName;
  110.  
  111. //strcat(fileName, ".txt");//strcat_s(fileName, ".txt");
  112.  
  113. ifstream fin(fileName);
  114.  
  115. if (fin.is_open())
  116. cout << "\n\tФайл успешно открыт!\n";
  117. else
  118. {
  119. cout << "\n\tОшибка! Файл закрыт!\n";
  120. return;
  121. }
  122.  
  123. while (!fin.eof())
  124. {
  125. fin >> a[size].nameSt >> iniz >> a[size].group >> a[size].day >> a[size].month >> a[size].year;
  126. strcat(a[size].nameSt, " ");//strcat_s(fileName, ".txt");
  127. strcat(a[size].nameSt, iniz);//strcat_s(fileName, ".txt");
  128. size++;
  129. }
  130.  
  131. size--;
  132.  
  133. cout << "\t\tЗапись завершена.\n";
  134.  
  135. fin.close();
  136.  
  137. if ( !fin.is_open() )
  138. cout << "\tФайл успешно закрыт!\n\n";
  139. else
  140. {
  141. cout << "\tОшибка! Файл открыт!\n\n";
  142. return;
  143. }
  144. }
  145. //2,9
  146. void outputDateToScreen(Student a[], int size)
  147. {
  148. if (size == 0)
  149. {
  150. cout << "\n\tМассив пуст.";
  151. return;
  152. }
  153.  
  154. cout << "-------------------------------------------------------------\n";
  155. cout << "| № | Фамилия И. О. | Группа |Дата рождения|\n";
  156. cout << "-------------------------------------------------------------\n";
  157.  
  158. for (int i = 0; i < size; i++)
  159. {
  160. int b = ( strlen(a[i].nameSt) ) / 2;
  161. int c = ( strlen(a[i].group) ) / 2;
  162.  
  163. cout << "|" << setw(3) << i+1;
  164. cout << "|" << setw(30 + b) << a[i].nameSt;
  165. cout << "|" << setw(10 + c) << a[i].group;
  166. cout << "|" << setw(3) << a[i].day << "|" << setw(3) << a[i].month << "|" << setw(5) << a[i].year;
  167. cout << "|\n";
  168. }
  169.  
  170. cout << "-------------------------------------------------------------\n\n";
  171. }
  172. //3
  173. void sortSourceStudYear(Student a[], int size)
  174. {
  175. if (size == 0)
  176. {
  177. cout << "\n\tИсходный массив пуст.";
  178. return;
  179. }
  180.  
  181. for (int i = 0; i < size; i++)
  182. for (int j = 0; j < size; j++)
  183. {
  184. if (a[i].year >= a[j].year)
  185. {
  186. Student c = a[i];
  187. a[i] = a[j];
  188. a[j] = c;
  189. }
  190. }
  191.  
  192. cout << "\n\tСортировка исходного массива по году рождения в порядке убывания завершена.\n\n";
  193. }
  194. //4
  195. void sortSourceStudAge(Student a[], int size)
  196. {
  197. if (size == 0)
  198. {
  199. cout << "\n\tИсходный массив пуст.";
  200. return;
  201. }
  202.  
  203. for (int i = 0; i < size; i++)
  204. for (int j = 0; j < size; j++)
  205. {
  206. if (a[i].year > a[j].year)
  207. {
  208. Student c = a[i];
  209. a[i] = a[j];
  210. a[j] = c;
  211. }
  212. else if (a[i].year == a[j].year)
  213. {
  214. if (a[i].month > a[j].month)
  215. {
  216. Student c = a[i];
  217. a[i] = a[j];
  218. a[j] = c;
  219. }
  220. else if (a[i].month == a[j].month)
  221. {
  222. if (a[i].day > a[j].day)
  223. {
  224. Student c = a[i];
  225. a[i] = a[j];
  226. a[j] = c;
  227. }
  228. }
  229. }
  230. }
  231.  
  232. cout << "\n\tСортировка исходного массива по дате рождения в порядке убывания возраста завершена.\n\n";
  233. }
  234. //5
  235. void addStudent(Student a[], int &size)
  236. {
  237. if (size == N)
  238. {
  239. cout << "\n\tИсходный массив заполнен.\n";
  240. return;
  241. }
  242.  
  243. Student newStudent;
  244. char iniz[10];
  245.  
  246. cout << "\nВведите данные о новом студенте:\n";
  247. cout << "\tФамилия: ";
  248. cin >> newStudent.nameSt;
  249. cout << "\tИнициалы: ";
  250. cin >> iniz;
  251.  
  252. strcat(newStudent.nameSt, " ");//strcat_s(fileName, ".txt");
  253. strcat(newStudent.nameSt, iniz);//strcat_s(fileName, ".txt");
  254.  
  255. cout << "\tДата рождения:\n";
  256. cout << "\t\tдень: ";
  257. cin >> newStudent.day;
  258. cout << "\t\tмесяц: ";
  259. cin >> newStudent.month;
  260. cout << "\t\tгод: ";
  261. cin >> newStudent.year;
  262.  
  263. a[size++] = newStudent;
  264.  
  265. cout << "\n\tЗапись добавлена.\n\n";
  266. }
  267. //6
  268. void deleteStudent(Student a[], int &size)
  269. {
  270. if (size == 0)
  271. {
  272. cout << "\n\tИсходный массив пуст.\n";
  273. return;
  274. }
  275.  
  276. int number;
  277. char choice;
  278. bool exit = true;
  279.  
  280. outputDateToScreen(a, size);
  281.  
  282. while (true)
  283. {
  284. cout << "\nВведите номер удаляемой строки: ";
  285. cin >> number;
  286. number--;
  287.  
  288. if ((number < 0) || (number >= size))
  289. {
  290. cout << "\n\t!!! Нет такого номера!\n";
  291. system("read -p 'Нажмите Enter для продолжения...' var");//system("pause");
  292. }
  293. else
  294. break;
  295. }
  296.  
  297. cout << number+1 << " строка: " << a[number].nameSt << ' ' << a[number].group << ' ' << a[number].day << '.' << a[number].month << '.' << a[number].year << '\n';
  298.  
  299. while ( exit )
  300. {
  301. cout << "Удалить данный элемент? (Y,y,Д,д - да; N,n,Н,н - нет) -> ";
  302. cin >> choice;
  303.  
  304. switch (choice)
  305. {
  306. case 'n':
  307. case 'N':
  308. //case 'н'
  309. //case 'Н'
  310. cout << "\n\tЗапись не удалена.\n\n";
  311. exit = false;
  312. break;
  313. case 'y':
  314. case 'Y':
  315. //case 'д'
  316. //case 'Д'
  317. for (int i = number + 1; i < size; i++)
  318. a[i-1]=a[i];
  319.  
  320. size--;
  321.  
  322. cout << "\n\tЗапись удалена.\n\n";
  323.  
  324. exit = false;
  325.  
  326. break;
  327. default:
  328. cout << "Пожалуйста, введите 'Y' или 'N'\n";
  329.  
  330. system("read -p 'Нажмите Enter для повторного ввода...' var");
  331. }
  332. }
  333. }
  334. //7, 11
  335. void insertDateToFile(Student a[], int size)
  336. {
  337. if (size == 0)
  338. {
  339. cout << "\n\tМассив пуст.";
  340. return;
  341. }
  342.  
  343. char fileName[30];
  344. char iniz[10];
  345.  
  346. cout << "\nВведите имя файла: ";
  347. cin >> fileName;
  348.  
  349. //strcat(fileName, ".txt");//strcat_s(fileName, ".txt");
  350.  
  351. ofstream fout(fileName);
  352.  
  353. if (fout.is_open())
  354. cout << "\n\tФайл успешно открыт!\n";
  355. else
  356. {
  357. cout << "\n\tОшибка! Файл закрыт!\n";
  358. return;
  359. }
  360.  
  361. fout << "-------------------------------------------------------------\n";
  362. fout << "| № | Фамилия И. О. | Группа |Дата рождения|\n";
  363. fout << "-------------------------------------------------------------\n";
  364.  
  365. for (int i = 0; i < size; i++)
  366. {
  367. int b = ( strlen(a[i].nameSt) ) / 2;
  368. int c = ( strlen(a[i].group) ) / 2;
  369.  
  370. fout << "|" << setw(3) << i+1;
  371. fout << "|" << setw(30 + b) << a[i].nameSt;
  372. fout << "|" << setw(10 + c) << a[i].group;
  373. fout << "|" << setw(3) << a[i].day << "|" << setw(3) << a[i].month << "|" << setw(5) << a[i].year;
  374. fout << "|\n";
  375. }
  376.  
  377. fout << "-------------------------------------------------------------\n";
  378.  
  379. cout << "\t\tЗапись массива в файл " << fileName << " завершена.\n";
  380.  
  381. fout.close();
  382.  
  383. if ( !fout.is_open() )
  384. cout << "\tФайл успешно закрыт!\n\n";
  385. else
  386. {
  387. cout << "\tОшибка! Файл открыт!\n\n";
  388. return;
  389. }
  390. }
  391. //8
  392. void searchStudents(Student a[], int sourceSize, Student b[], int &resultSize)
  393. {
  394. if (sourceSize == 0)
  395. {
  396. cout << "\n\tИсходный массив пуст.";
  397. return;
  398. }
  399.  
  400. resultSize = 0;
  401.  
  402. for (int i = 0; i < sourceSize; ++i)
  403. if ((a[i].month <= 5) && (a[i].month >= 3))
  404. b[resultSize++] = a[i];
  405.  
  406. if (resultSize == 0)
  407. cout << "Студеты не найдены.\n";
  408. else
  409. cout << "\n\tПоиск завершен.\n\n";
  410. }
  411. //9
  412. void sortSearchStudent(Student a[], int size)
  413. {
  414. if (size == 0)
  415. {
  416. cout << "\n\tВыходной массив пуст.";
  417. return;
  418. }
  419. for (int i = 0; i < size; i++)
  420. for (int j = 0; j < size; j++)
  421. {
  422. if (strcmp(a[i].nameSt, a[j].nameSt) <= 0)
  423. {
  424. Student c = a[i];
  425. a[i] = a[j];
  426. a[j] = c;
  427. }
  428. }
  429.  
  430. cout << "\n\tСортировка выходного массива по фамилии студентов в алфавитном порядке завершена.\n\n";
  431. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement