Advertisement
qberik

Untitled

Dec 20th, 2021
32
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <iomanip>
  4. #include <cstring>
  5. #include <fstream>
  6. #include <Windows.h>
  7. using namespace std;
  8.  
  9. const int Max_students_count{ 100 }, Max_length_of_word{ 20 };
  10.  
  11. enum Menu_commands {
  12. Input_from_keyboard = 1,
  13. Input_from_text_file = 2,
  14. Input_from_binary_file = 3,
  15. Display_entering = 4,
  16. Output_data_to_file = 5,
  17. Request = 6,
  18. Convert_to_text_file = 7,
  19. Convert_to_binary_file = 8,
  20. Add_note = 9,
  21. Change_note = 10,
  22. Delete_note = 11,
  23. Sort_database = 12,
  24. Exit = 0
  25. };
  26.  
  27. enum Sort_commands {
  28. Name_sorting = 1,
  29. Surname_sorting = 2,
  30. Year_of_admission_sorting = 3,
  31. Course_sorting = 4,
  32. Math_sorting = 5,
  33. Rus_sorting = 6,
  34. Eng_sorting = 7,
  35. };
  36.  
  37. enum Sort_by_datatype {
  38. Int = 1,
  39. Char = 2
  40. };
  41.  
  42. enum Type_of_sorting {
  43. Ascending = 1,
  44. Descending = 2
  45. };
  46.  
  47. struct Student {
  48. char name[Max_length_of_word]{ "No name" };
  49. char surname[Max_length_of_word]{ "No surname" };
  50. int year_of_admission{};
  51. int course{};
  52. int marks[3]{};
  53.  
  54. int year_of_born{};
  55. char group[Max_length_of_word]{ "No group" };
  56. };
  57.  
  58. Student students[Max_students_count]{};
  59.  
  60. /* Функции */
  61.  
  62. int Check_int(int min_num, int max_num); //проверка на числа
  63.  
  64. void Check_char(char ret_val[], int maxCountOfSymbols); //проверка на слова
  65.  
  66. void Menu_items(); //вывод меню
  67.  
  68. int Input_students_count(); //вывод количества студентов
  69.  
  70. void Keyboard_input(int count_students, int maxCountOfSymbols, Student students[]); //ввод данных
  71.  
  72. void Input_from_text_file_fun(int maxCountOfSymbols, int& countOfStudents, Student students[]);
  73.  
  74. void Display_database(int countOfStudents, Student* students);
  75.  
  76. void Output_database_to_file(int countOfStudents, Student* students);
  77.  
  78. void Query(int countOfStudents, Student* students);
  79.  
  80. void Add_a_note(int& countOfStudents, int maxCountOfSymbols, Student* students);
  81.  
  82. void Change_a_note(int& countOfStudents, int maxCountOfSymbols, Student* students);
  83.  
  84. void Delete_a_note(int& countOfStudents, Student* students);
  85.  
  86. void Database_sorting(int maxCountOfSymbols, int countOfStudents, Student* students);
  87.  
  88. void Display_sorting_menu();
  89.  
  90. int Type_of_sorting();
  91.  
  92. void Sorting_int(int maxCountOfSymbols, int countOfStudents, int valueSortingType, int* sort_charact,
  93. Student* students);
  94.  
  95. void Sorting_char(int maxCountOfSymbols, int countOfStudents, int valueSortingType, char** sort_charact,
  96. Student* students);
  97.  
  98. void strCopy(char* dynamicSymbolsArray, char* staticSymbolsArray);
  99.  
  100. void swapInt(int& number1, int& number2);
  101.  
  102. void convertToTextFile(int countOfStudents, Student students[]);
  103.  
  104. void Convert_to_binary(int maxCountOfSymbols);
  105.  
  106. void Input_from_binary(int& countOfStudents, Student* students);
  107.  
  108. /* конец */
  109.  
  110. int main() {
  111. SetConsoleCP(1251);
  112. SetConsoleOutputCP(1251);
  113.  
  114. int countOfStudents{}, menu_item = -1;
  115. int* pointerCountOfStudents = &countOfStudents;
  116. Student students[Max_students_count]{};
  117.  
  118. while (menu_item != 0) {
  119. Menu_items();
  120.  
  121. menu_item = Check_int(0, 12);
  122. system("cls");
  123. switch (menu_item) {
  124. case Exit:
  125. break;
  126. case Input_from_keyboard:
  127. countOfStudents = 2;
  128. system("cls");
  129. Keyboard_input(countOfStudents, Max_length_of_word, students);
  130. break;
  131. case Input_from_text_file:
  132. Input_from_text_file_fun(Max_length_of_word, countOfStudents, students);
  133. break;
  134. case Input_from_binary_file:
  135. Input_from_binary(countOfStudents, students);
  136. break;
  137. case Display_entering:
  138. Display_database(countOfStudents, students);
  139. break;
  140. case Output_data_to_file:
  141. Output_database_to_file(countOfStudents, students);
  142. break;
  143. case Request:
  144. Query(countOfStudents, students);
  145. break;
  146. case Convert_to_text_file:
  147. convertToTextFile(countOfStudents, students);
  148. break;
  149. case Convert_to_binary_file:
  150. Convert_to_binary(Max_length_of_word);
  151. break;
  152. case Add_note:
  153. Add_a_note(countOfStudents, Max_length_of_word, students);
  154. break;
  155. case Change_note:
  156. Change_a_note(countOfStudents, Max_length_of_word, students);
  157. break;
  158. case Delete_note:
  159. Delete_a_note(countOfStudents, students);
  160. break;
  161. case Sort_database:
  162. Database_sorting(Max_length_of_word, countOfStudents, students);
  163. break;
  164. }
  165.  
  166. system("pause");
  167. system("cls");
  168. }
  169.  
  170. return 0;
  171. }
  172.  
  173. int Check_int(int min_num, int max_num) {
  174. int number = 0;
  175. bool correct_num = false;
  176. cin >> number;
  177.  
  178. while (!(correct_num)) {
  179. if (!((number >= min_num) && (number <= max_num))) {
  180. cout << endl;
  181. cout << "Вы ввели некорректное число!" << endl << " Введите число в диапазоне от " << min_num << " до "
  182. << max_num << " включительно: " << endl;
  183. cin >> number;
  184. }
  185. else {
  186. correct_num = true;
  187. }
  188. }
  189.  
  190. cin.ignore(1, '\n');
  191. return number;
  192. }
  193.  
  194. void Check_char(char ret_val[], int maxCountOfSymbols) {
  195. const int Edge = 1000;
  196. char* value = new char[Edge] {};
  197. bool correct_val = false;
  198. cin.getline(value, Edge);
  199.  
  200. while (!(correct_val)) {
  201. if ((strlen(value) < maxCountOfSymbols) && (strcmp(value, ""))) {
  202. strCopy(ret_val, value);
  203. correct_val = true;
  204. }
  205. else {
  206. cout << endl;
  207. cout
  208. << "Вы ввели неправильное значение!" << endl << "(либо поле пустое)" << endl <<
  209. "Введите значение,не превышающее " << maxCountOfSymbols << " символов: " << endl;
  210. cin.getline(value, Edge);
  211. }
  212. }
  213.  
  214. delete[] value;
  215. }
  216.  
  217. void Menu_items() {
  218. cout << "***Работа с базой данных*** " << endl;
  219. cout << "Выберите пункт меню: " << endl;
  220. cout << endl;
  221.  
  222. cout << " 1) Ввод информации с клавиатуры" << endl;
  223. cout << " 2) Ввод информации из уже созданного текстового файла" << endl;
  224. cout << " 3) Ввод информации из уже созданного бинарного файла" << endl;
  225. cout << " 4) Вывод данных на экран" << endl;
  226. cout << " 5) Вывод данных в файл" << endl;
  227. cout << " 6) Вывод распечатки информации данных по запросу" << endl;
  228. cout << " 7) Перевод базы данных в текстовый файл" << endl;
  229. cout << " 8) Перевод содержимого текстового файла в бинарный файл" << endl;
  230. cout << " 9) Добавить запись" << endl;
  231. cout << "10) Изменить запись" << endl;
  232. cout << "11) Удалить запись" << endl;
  233. cout << "12) Сортировать базу данных" << endl;
  234. cout << " 0) Выход" << endl;
  235. cout << endl;
  236. }
  237.  
  238. int Input_students_count() {
  239. int countOfStudents{};
  240. cout << "В базу данных можно ввести не больше, чем " << Max_students_count << " студентов" << endl
  241. << "Информацию о каком количестве студентов вы хотите ввести?" << endl << "Количество студентов: " << endl;
  242.  
  243. countOfStudents = Check_int(1, 100);
  244.  
  245. return countOfStudents;
  246. }
  247.  
  248. void Keyboard_input(int countOfStudents, int maxCountOfSymbols, Student students[]) {
  249. cout << "Введите информацию о студентах, количество которых равно " << countOfStudents << endl << endl;
  250. for (int i = 0; i < countOfStudents; i++) {
  251. cout << "Имя " << i + 1 << "-го студента: " << endl;
  252. Check_char(students[i].name, maxCountOfSymbols);
  253. cout << "Фамилия " << i + 1 << "-го студента: " << endl;
  254. Check_char(students[i].surname, maxCountOfSymbols);
  255. cout << "Год поступления " << i + 1 << "-го студента: " << endl;
  256. students[i].year_of_admission = Check_int(1900, 2021);
  257. cout << "Курс " << i + 1 << "-го студента: " << endl;
  258. students[i].course = Check_int(1, 5);
  259. cout << "Оценка " << i + 1 << "-го студента по Математике: " << endl;
  260. students[i].marks[0] = Check_int(2, 5);
  261. cout << "Оценка " << i + 1 << "-го студента по Русскому: " << endl;
  262. students[i].marks[1] = Check_int(2, 5);
  263. cout << "Оценка " << i + 1 << "-го студента по Английскому: " << endl;
  264. students[i].marks[2] = Check_int(2, 5);
  265. system("cls");
  266. }
  267. }
  268.  
  269.  
  270. void Input_from_binary(int& countOfStudents, Student* students) {
  271. ifstream fin;
  272. fin.open("database.bin", ios::binary);
  273.  
  274. if (fin.is_open()) {
  275. fin.read((char*)&countOfStudents, sizeof(countOfStudents));
  276.  
  277. for (int i = 0; i < countOfStudents; i++) {
  278. fin.read((char*)&students[i], sizeof(students[i]));
  279. }
  280.  
  281. }
  282. else {
  283. system("cls");
  284. cout << "***Ошибка открытия файла***";
  285. system("pause");
  286. }
  287.  
  288. fin.close();
  289. }
  290.  
  291.  
  292. void Display_database(int countOfStudents, Student* students) {
  293.  
  294. cout << "+------+----------------+--------------------+-------------------+--------+------------+------------+------------+"
  295. << endl;
  296. cout << "|" << setw(6) << "№" << "|" << setw(16) << "Имя" << "|" << setw(20) << "Фамилия" << "|" << setw(19)
  297. << "Год поступления" << "|" << setw(8) << "Курс" << "|" << setw(12) << "Математика" << "|" << setw(12)
  298. << "Русский" << "|" << setw(12) << "Английский" << "|" << endl;
  299.  
  300.  
  301. for (int i = 0; i < countOfStudents; i++) {
  302. cout << "+------+----------------+--------------------+-------------------+--------+------------+------------+------------+"
  303. << endl;
  304. cout << "|" << left << setw(6) << i + 1;
  305. cout << "|" << left << setw(16) << students[i].name;
  306. cout << "|" << left << setw(20) << students[i].surname;
  307. cout << "|" << left << setw(19) << students[i].year_of_admission;
  308. cout << "|" << left << setw(8) << students[i].course;
  309. cout << "|" << left << setw(12) << students[i].marks[0];
  310. cout << "|" << left << setw(12) << students[i].marks[1];
  311. cout << "|" << left << setw(12) << students[i].marks[2] << "|";
  312. cout << endl;
  313.  
  314. }
  315. cout << "+------+----------------+--------------------+-------------------+--------+------------+------------+------------+" << endl;
  316. cout << endl;
  317. }
  318.  
  319. void Output_database_to_file(int countOfStudents, Student* students) {
  320. ofstream fout;
  321.  
  322. fout.open("stud_dbase.txt");
  323.  
  324. if (fout.is_open()) {
  325. fout << "+------+----------------+--------------------+-------------------+--------+------------+------------+------------+"
  326. << endl;
  327. fout << "|" << setw(6) << "№" << "|" << setw(16) << "Имя" << "|" << setw(20) << "Фамилия" << "|" << setw(19)
  328. << "Год поступления" << "|" << setw(8) << "Курс" << "|" << setw(12) << "Математика" << "|" << setw(12)
  329. << "Русский" << "|" << setw(12) << "Английский" << "|" << endl;
  330. for (int i = 0; i < countOfStudents; i++) {
  331. fout << "+------+----------------+--------------------+-------------------+--------+------------+------------+------------+"
  332. << endl;
  333. fout << "|" << left << setw(6) << i + 1;
  334. fout << "|" << left << setw(16) << students[i].name;
  335. fout << "|" << left << setw(20) << students[i].surname;
  336. fout << "|" << left << setw(19) << students[i].year_of_admission;
  337. fout << "|" << left << setw(8) << students[i].course;
  338. fout << "|" << left << setw(12) << students[i].marks[0];
  339. fout << "|" << left << setw(12) << students[i].marks[1];
  340. fout << "|" << left << setw(12) << students[i].marks[2] << "|";
  341. fout << endl;
  342. }
  343. fout << "+------+----------------+--------------------+-------------------+--------+------------+------------+------------+" << endl;
  344. fout << endl;
  345. }
  346. else {
  347. system("cls");
  348. cout << "Ошибка открытия файла.";
  349. system("pause");
  350. }
  351.  
  352. fout.close();
  353. }
  354.  
  355. void Query(int countOfStudents, Student* students) {
  356.  
  357. int num = 1000; int min_i = -1;
  358. for (int i = 0; i < countOfStudents; i++) {
  359. int sum = 0;
  360. sum += (students)[i].marks[0];
  361. sum += (students)[i].marks[1];
  362. sum += (students)[i].marks[2];
  363. if (sum < num) {
  364. num = sum;
  365. min_i = i;
  366. }
  367. }
  368. cout << "+------+----------------+--------------------+-------------------+--------+------------+------------+------------+"
  369. << endl;
  370. cout << "|" << setw(6) << "№" << "|" << setw(16) << "Имя" << "|" << setw(20) << "Фамилия" << "|" << setw(19)
  371. << "Год поступления" << "|" << setw(8) << "Курс" << "|" << setw(12) << "Математика" << "|" << setw(12)
  372. << "Русский" << "|" << setw(12) << "Английский" << "|" << endl;
  373. cout << "Студент с минимальным средним баллом "<< endl;
  374. cout << "+------+----------------+--------------------+-------------------+--------+------------+------------+------------+"
  375. << endl;
  376. cout << "|" << left << setw(6) << min_i + 1;
  377. cout << "|" << left << setw(16) << students[min_i].name;
  378. cout << "|" << left << setw(20) << students[min_i].surname;
  379. cout << "|" << left << setw(19) << students[min_i].year_of_admission;
  380. cout << "|" << left << setw(8) << students[min_i].course;
  381. cout << "|" << left << setw(12) << students[min_i].marks[0];
  382. cout << "|" << left << setw(12) << students[min_i].marks[1];
  383. cout << "|" << left << setw(12) << students[min_i].marks[2] << "|";
  384. cout << endl;
  385. cout << "+------+----------------+--------------------+-------------------+--------+------------+------------+------------+" << endl;
  386.  
  387. }
  388.  
  389. void convertToTextFile(int countOfStudents, Student students[]) {
  390. ofstream fout;
  391. fout.open("inp_dbase.txt");
  392.  
  393. if (fout.is_open()) {
  394. fout << countOfStudents << endl;
  395. for (int i = 0; i < countOfStudents; i++) {
  396. fout << students[i].name << endl;
  397. fout << students[i].surname << endl;
  398. fout << students[i].year_of_admission << endl;
  399. fout << students[i].course << endl;
  400. fout << students[i].marks[0] << endl;
  401. fout << students[i].marks[1] << endl;
  402. fout << students[i].marks[2] << endl;
  403. }
  404. }
  405. else {
  406. system("cls");
  407. cout << "Ошибка открытия файла.";
  408. system("pause");
  409. }
  410.  
  411. fout.close();
  412. }
  413.  
  414. void Convert_to_binary(int maxCountOfSymbols) {
  415. Student tempStudent{};
  416. int Students_count{};
  417.  
  418. ifstream fin;
  419. ofstream fout;
  420. fin.open("inp_dbase.txt");
  421. fout.open("database.bin", ios::binary);
  422.  
  423. if (fin.is_open() && fout.is_open()) {
  424. fin >> Students_count;
  425. fout.write((char*)&Students_count, sizeof(Students_count));
  426. fin.ignore(1, '\n');
  427.  
  428. for (int i = 0; i < Students_count; i++) {
  429. fin.getline(tempStudent.name, maxCountOfSymbols);
  430. fin.getline(tempStudent.surname, maxCountOfSymbols);
  431. fin >> tempStudent.year_of_admission;
  432. fin >> tempStudent.course;
  433. fin >> tempStudent.marks[0];
  434. fin >> tempStudent.marks[1];
  435. fin >> tempStudent.marks[2];
  436. char tmp[100];
  437. fin.getline(tmp, 100);
  438. fout.write((char*)&tempStudent, sizeof(tempStudent));
  439. }
  440. }
  441. else {
  442. system("cls");
  443. cout << "Ошибка открытия файла.";
  444. system("pause");
  445. }
  446. }
  447. void Add_a_note(int& countOfStudents, int maxCountOfSymbols, Student* students) {
  448. cout << "Введите информацию о студентах, количество которых равно " << countOfStudents + 1 << endl << endl;
  449. cout << "Имя студента номер " << countOfStudents + 1 << ": " << endl;
  450. Check_char(students[countOfStudents].name, maxCountOfSymbols);
  451. cout << "Фамилия студента номер " << countOfStudents + 1 << ": " << endl;
  452. Check_char(students[countOfStudents].surname, maxCountOfSymbols);
  453. cout << "Год поступления студента номер " << countOfStudents + 1 << ": " << endl;
  454. students[countOfStudents].year_of_admission = Check_int(1900, 2021);
  455. cout << "Курс студента номер " << countOfStudents + 1 << ": " << endl;
  456. students[countOfStudents].course = Check_int(1, 6);
  457. cout << "Оценка по Математике номер " << countOfStudents + 1 << ": " << endl;
  458. students[countOfStudents].marks[0] = Check_int(2, 5);
  459. cout << "Оценка по Русскому номер " << countOfStudents + 1 << ": " << endl;
  460. students[countOfStudents].marks[1] = Check_int(2, 5);
  461. cout << "Оценка по Английскому номер " << countOfStudents + 1 << ": " << endl;
  462. students[countOfStudents].marks[2] = Check_int(2, 5);
  463.  
  464.  
  465.  
  466. countOfStudents = countOfStudents + 1;
  467.  
  468. system("cls");
  469. }
  470.  
  471. void Change_a_note(int& countOfStudents, int maxCountOfSymbols, Student* students) {
  472. int indexOfItemBeingModified{};
  473.  
  474. cout << "Введите номер студента: " << endl;
  475. indexOfItemBeingModified = Check_int(1, countOfStudents);
  476. indexOfItemBeingModified--;
  477.  
  478. cout << "Введите обновленную информацию о студенте" << endl;
  479. cout << endl;
  480. cout << "Имя студента: " << endl;
  481. Check_char(students[indexOfItemBeingModified].name, maxCountOfSymbols);
  482. cout << "Фамилия студента: " << endl;
  483. Check_char(students[indexOfItemBeingModified].surname, maxCountOfSymbols);
  484. cout << "Год поступления студента: " << endl;
  485. students[indexOfItemBeingModified].year_of_admission = Check_int(1900, 2021);
  486. cout << "Курс студента: " << endl;
  487. students[indexOfItemBeingModified].course = Check_int(1, 6);
  488. cout << "Оценка по Математике студента: " << endl;
  489. students[indexOfItemBeingModified].marks[0] = Check_int(2, 5);
  490. cout << "Оценка по Русскому студента: " << endl;
  491. students[indexOfItemBeingModified].marks[0] = Check_int(2, 5);
  492. cout << "Оценка по Английскому студента: " << endl;
  493. students[indexOfItemBeingModified].marks[0] = Check_int(2, 5);
  494.  
  495.  
  496.  
  497. system("cls");
  498. }
  499.  
  500. void Delete_a_note(int& countOfStudents, Student* students) {
  501. int indexOfItemToRemove{};
  502.  
  503. cout << "Введите номер студента: " << endl;
  504. indexOfItemToRemove = Check_int(1, countOfStudents);
  505. indexOfItemToRemove--;
  506.  
  507. for (int i = indexOfItemToRemove; i < countOfStudents - 1; i++) {
  508. strcpy_s(students[i].name, students[i + 1].name);
  509. strcpy_s(students[i].surname, students[i + 1].surname);
  510. students[i].year_of_admission = students[i + 1].year_of_admission;
  511. students[i].course = students[i + 1].course;
  512. students[i].marks[0] = students[i + 1].marks[0];
  513. students[i].marks[1] = students[i + 1].marks[1];
  514. students[i].marks[2] = students[i + 1].marks[2];
  515.  
  516. }
  517.  
  518. memset(students[countOfStudents - 1].name, 0, Max_length_of_word);
  519. memset(students[countOfStudents - 1].surname, 0, Max_length_of_word);
  520. students[countOfStudents - 1].year_of_admission = 0;
  521. students[countOfStudents - 1].course = 0;
  522. students[countOfStudents - 1].marks[0] = 0;
  523. students[countOfStudents - 1].marks[1] = 0;
  524. students[countOfStudents - 1].marks[2] = 0;
  525.  
  526. countOfStudents = countOfStudents - 1;
  527.  
  528. system("cls");
  529. }
  530.  
  531. void Database_sorting(int maxCountOfSymbols, int countOfStudents, Student* students) {
  532. int itemNumber{ -1 }, valueSortingType{}, dataTypeToSort{};
  533. int* sort_charact_int = new int[countOfStudents] {};
  534. char** sort_charact_char = new char* [countOfStudents] {nullptr};
  535. for (int i = 0; i < countOfStudents; i++) {
  536. sort_charact_char[i] = new char[maxCountOfSymbols];
  537. }
  538.  
  539. Display_sorting_menu();
  540. itemNumber = Check_int(1, 4);
  541. system("cls");
  542.  
  543. valueSortingType = Type_of_sorting();
  544. switch (itemNumber) {
  545. case Name_sorting:
  546. for (int i = 0; i < countOfStudents; i++) {
  547. sort_charact_char[i] = students[i].name;
  548. }
  549. dataTypeToSort = Char;
  550. break;
  551. case Surname_sorting:
  552. for (int i = 0; i < countOfStudents; i++) {
  553. sort_charact_char[i] = students[i].surname;
  554. }
  555. dataTypeToSort = Char;
  556. break;
  557. case Year_of_admission_sorting:
  558. for (int i = 0; i < countOfStudents; i++) {
  559. sort_charact_int[i] = students[i].year_of_admission;
  560. }
  561. dataTypeToSort = Int;
  562. break;
  563. case Course_sorting:
  564. for (int i = 0; i < countOfStudents; i++) {
  565. sort_charact_int[i] = students[i].course;
  566. }
  567. dataTypeToSort = Int;
  568. break;
  569.  
  570. }
  571.  
  572.  
  573. if (dataTypeToSort == Int) {
  574. Sorting_int(maxCountOfSymbols, countOfStudents, valueSortingType, sort_charact_int, students);
  575. }
  576. else if (dataTypeToSort == Char) {
  577. Sorting_char(maxCountOfSymbols, countOfStudents, valueSortingType, sort_charact_char, students);
  578. }
  579.  
  580. delete[] sort_charact_int;
  581. delete[] sort_charact_char;
  582. }
  583.  
  584. void Display_sorting_menu() {
  585. cout << "1) Сортировать по имени" << endl;
  586. cout << "2) Сортировать по фамилии" << endl;
  587. cout << "3) Сортировать по году поступления" << endl;
  588. cout << "4) Сортировать по курсу" << endl;
  589.  
  590. cout << endl;
  591.  
  592. cout << "Введите номер пункта: " << endl;
  593. }
  594.  
  595. int Type_of_sorting() {
  596. int itemNumber{};
  597.  
  598. cout << "1) Сортировать по возрастанию" << endl;
  599. cout << "2) Сортировать по убыванию" << endl;
  600. cout << endl;
  601.  
  602. cout << "Введите номер пункта: " << endl;
  603. itemNumber = Check_int(1, 2);
  604. system("cls");
  605.  
  606. return itemNumber;
  607. }
  608.  
  609. void Sorting_int(int maxCountOfSymbols, int countOfStudents, int valueSortingType, int* sort_charact,
  610. Student* students) {
  611. char* tempName = new char[maxCountOfSymbols] {};
  612. char* tempSurname = new char[maxCountOfSymbols] {};
  613. char* tempGroup = new char[maxCountOfSymbols] {};
  614.  
  615. if (valueSortingType == Ascending) {
  616. for (int i = 1; i < countOfStudents; i++) {
  617. for (int j = 0; j < countOfStudents - i; j++) {
  618. if (sort_charact[j] > sort_charact[j + 1]) {
  619. swapInt(sort_charact[j], sort_charact[j + 1]);
  620. swapInt(students[j].year_of_admission, students[j + 1].year_of_admission);
  621. swapInt(students[j].course, students[j + 1].course);
  622. swapInt(students[j].marks[0], students[j + 1].marks[0]);
  623. swapInt(students[j].marks[1], students[j + 1].marks[1]);
  624. swapInt(students[j].marks[2], students[j + 1].marks[2]);
  625.  
  626. strCopy(tempName, students[j].name);
  627. strCopy(tempSurname, students[j].surname);
  628.  
  629. strcpy_s(students[j].name, students[j + 1].name);
  630. strcpy_s(students[j].surname, students[j + 1].surname);
  631. strcpy_s(students[j + 1].name, tempName);
  632. strcpy_s(students[j + 1].surname, tempSurname);
  633. }
  634. }
  635. }
  636. }
  637. else if (valueSortingType == Descending) {
  638. for (int i = 1; i < countOfStudents; i++) {
  639. for (int j = 0; j < countOfStudents - i; j++) {
  640. if (sort_charact[j] < sort_charact[j + 1]) {
  641. swapInt(sort_charact[j], sort_charact[j + 1]);
  642. swapInt(students[j].year_of_admission, students[j + 1].year_of_admission);
  643. swapInt(students[j].course, students[j + 1].course);
  644. swapInt(students[j].marks[0], students[j + 1].marks[0]);
  645. swapInt(students[j].marks[1], students[j + 1].marks[1]);
  646. swapInt(students[j].marks[2], students[j + 1].marks[2]);
  647.  
  648. strCopy(tempName, students[j].name);
  649. strCopy(tempSurname, students[j].surname);
  650.  
  651. strcpy_s(students[j].name, students[j + 1].name);
  652. strcpy_s(students[j].surname, students[j + 1].surname);
  653. strcpy_s(students[j + 1].name, tempName);
  654. strcpy_s(students[j + 1].surname, tempSurname);
  655. }
  656. }
  657. }
  658. }
  659.  
  660. delete[] tempName;
  661. delete[] tempSurname;
  662. delete[] tempGroup;
  663. }
  664.  
  665. void Sorting_char(int maxCountOfSymbols, int countOfStudents, int valueSortingType, char** sort_charact,
  666. Student* students) {
  667. char* tempName = new char[maxCountOfSymbols] {};
  668. char* tempSurname = new char[maxCountOfSymbols] {};
  669. char* tempGroup = new char[maxCountOfSymbols] {};
  670. char* temp_sort_charact = new char[maxCountOfSymbols] {};
  671.  
  672. if (valueSortingType == Ascending) {
  673. for (int i = 1; i < countOfStudents; i++) {
  674. for (int j = 0; j < countOfStudents - i; j++) {
  675. if (sort_charact[j][0] > sort_charact[j + 1][0]) {
  676. swapInt(students[j].year_of_admission, students[j + 1].year_of_admission);
  677. swapInt(students[j].course, students[j + 1].course);
  678. swapInt(students[j].marks[0], students[j + 1].marks[0]);
  679. swapInt(students[j].marks[1], students[j + 1].marks[1]);
  680. swapInt(students[j].marks[2], students[j + 1].marks[2]);
  681.  
  682. strCopy(temp_sort_charact, sort_charact[j]);
  683. strCopy(tempName, students[j].name);
  684. strCopy(tempSurname, students[j].surname);
  685.  
  686. strCopy(sort_charact[j], sort_charact[j + 1]);
  687. strcpy_s(students[j].name, students[j + 1].name);
  688. strcpy_s(students[j].surname, students[j + 1].surname);
  689.  
  690. strCopy(sort_charact[j + 1], temp_sort_charact);
  691. strcpy_s(students[j + 1].name, tempName);
  692. strcpy_s(students[j + 1].surname, tempSurname);
  693.  
  694.  
  695. }
  696. }
  697. }
  698. }
  699. else if (valueSortingType == Descending) {
  700. for (int i = 1; i < countOfStudents; i++) {
  701. for (int j = 0; j < countOfStudents - i; j++) {
  702. if (sort_charact[j][0] < sort_charact[j + 1][0]) {
  703. swapInt(students[j].year_of_admission, students[j + 1].year_of_admission);
  704. swapInt(students[j].course, students[j + 1].course);
  705. swapInt(students[j].marks[0], students[j + 1].marks[0]);
  706. swapInt(students[j].marks[1], students[j + 1].marks[1]);
  707. swapInt(students[j].marks[2], students[j + 1].marks[2]);
  708.  
  709. strCopy(temp_sort_charact, sort_charact[j]);
  710. strCopy(tempName, students[j].name);
  711. strCopy(tempSurname, students[j].surname);
  712.  
  713. strCopy(sort_charact[j], sort_charact[j + 1]);
  714. strcpy_s(students[j].name, students[j + 1].name);
  715. strcpy_s(students[j].surname, students[j + 1].surname);
  716.  
  717. strCopy(sort_charact[j + 1], temp_sort_charact);
  718. strcpy_s(students[j + 1].name, tempName);
  719. strcpy_s(students[j + 1].surname, tempSurname);
  720. }
  721. }
  722. }
  723. }
  724.  
  725. delete[] tempName;
  726. delete[] tempSurname;
  727. delete[] tempGroup;
  728. delete[] temp_sort_charact;
  729. }
  730.  
  731. void strCopy(char* symbolsArray1, char* symbolsArray2) {
  732. for (unsigned int i = 0; i < strlen(symbolsArray1); i++) {
  733. symbolsArray1[i] = ' ';
  734. }
  735.  
  736. for (unsigned int i = 0; i < strlen(symbolsArray2); i++) {
  737. symbolsArray1[i] = symbolsArray2[i];
  738. }
  739. }
  740.  
  741. void swapInt(int& number1, int& number2) {
  742. int tempNumber{};
  743.  
  744. tempNumber = number1;
  745. number1 = number2;
  746. number2 = tempNumber;
  747. }
  748.  
  749. void swapStrings(char* symbolsArray1, char* symbolsArray2, int maxCountOfSymbols) {
  750. char* tempSymbolsArray = new char[maxCountOfSymbols] {};
  751. }
  752.  
  753. void Input_from_text_file_fun(int maxCountOfSymbols, int& countOfStudents, Student students[]) {
  754. ifstream fin;
  755. fin.open("inp_dbase.txt");
  756.  
  757. if (fin.is_open()) {
  758. fin >> countOfStudents;
  759. fin.ignore(1, '\n');
  760. for (int i = 0; i < countOfStudents; i++) {
  761. fin.getline(students[i].name, maxCountOfSymbols);
  762. fin.getline(students[i].surname, maxCountOfSymbols);
  763. fin >> students[i].year_of_admission;
  764. fin >> students[i].course;
  765. fin >> students[i].marks[0];
  766. fin >> students[i].marks[1];
  767. fin >> students[i].marks[2];
  768. char tmp[100];
  769. fin.getline(tmp, 100);
  770. }
  771. }
  772. else {
  773. system("cls");
  774. cout << "Ошибка открытия файла.";
  775. system("pause");
  776. }
  777.  
  778. fin.close();
  779. }
  780.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement