Advertisement
Courbe_Impliquee

struct3.1

Nov 2nd, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.59 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <clocale>
  5. #include <fstream>
  6. #include <iomanip>
  7. #include <string>
  8. using namespace std;
  9.  
  10. struct FIO {
  11. char name[20];
  12. char surname[20];
  13. char patronymic[20];
  14. };
  15.  
  16. struct Student {
  17. FIO fio;
  18. int group;
  19. int exams;
  20. int mark[7];
  21. char disp[7][20];
  22. };
  23.  
  24. void read_file(Student a[], FILE* filetxt, int i, int* n) {
  25. while (filetxt) {
  26. fscanf_s(filetxt, "%s", a[i].fio.surname, _countof(a[i].fio.surname));
  27. if (strlen(a[i].fio.surname) < 2) {
  28. *n = i;
  29. break;
  30. }
  31. fscanf_s(filetxt, "%d", &a[i].exams);
  32. for (int j = 0; j < a[i].exams; j++) {
  33. fscanf_s(filetxt, "%s", a[i].disp[j], _countof(a[i].disp[j]));
  34. }
  35. fscanf_s(filetxt, "%s", a[i].fio.name, _countof(a[i].fio.name));
  36. fscanf_s(filetxt, "%s", a[i].fio.patronymic, _countof(a[i].fio.patronymic));
  37. fscanf_s(filetxt, "%d", &a[i].group);
  38. for (int j = 0; j < a[i].exams; j++) {
  39. fscanf_s(filetxt, "%d", &a[i].mark[j]);
  40. }
  41. i++;
  42. }
  43. }
  44.  
  45. void print_info(Student a[], int n) {
  46. for (int i = 0; i < n; i++) {
  47. cout << setw(25) << left << a[i].fio.surname << " " << setw(4) << left << a[i].exams;
  48. for (int j = 0; j < a[i].exams; j++) {
  49. cout << a[i].disp[j] << " ";
  50. }
  51. cout << endl;
  52. cout << setw(10) << left << a[i].fio.name << " "<< setw(15) << left << a[i].fio.patronymic << setw(4) << left << a[i].group << " ";
  53. for (int j = 0; j < a[i].exams; j++) {
  54. cout << setw(4) << a[i].mark[j] << " ";
  55. }
  56. cout << endl;
  57. cout << endl;
  58. }
  59. cout << endl;
  60. }
  61. void add_info(Student a[], int n) {
  62.  
  63. cout << "Введите ФИО студента: ";
  64. cin >> a[n].fio.surname;
  65. cin >> a[n].fio.name;
  66. cin >> a[n].fio.patronymic;
  67.  
  68. cout << "Введите число экзаменов: ";
  69. cin >> a[n].exams;
  70.  
  71. cout << "Введите номер группы: ";
  72. cin >> a[n].group;
  73.  
  74. cout << "Введите названия экзаменов: ";
  75. for (int j = 0; j < a[n].exams; j++) {
  76. cin >> a[n].disp[j];
  77. }
  78.  
  79. cout << "Введите оценки студента по этим предметам: ";
  80. for (int j = 0; j < a[n].exams; j++) {
  81. cin >> a[n].mark[j];
  82. }
  83. }
  84. void delete_info(Student a[], int n) {
  85. char key1[20];
  86. cout << "Введите ключ,по которому хотите удалить: ";
  87. cin >> key1;
  88. for (int i = 0; i < n; i++) {
  89. if ((strcmp(key1, a[i].fio.name) == 0) || (strcmp(key1, a[i].fio.surname) == 0) || (strcmp(key1, a[i].fio.patronymic) == 0)) {
  90. for (int s = i; i < n; i++) {
  91.  
  92. strcpy_s(a[i].fio.name, a[i + 1].fio.name);
  93.  
  94.  
  95. strcpy_s(a[i].fio.surname, a[i + 1].fio.surname);
  96.  
  97.  
  98. strcpy_s(a[i].fio.patronymic, a[i + 1].fio.patronymic);
  99.  
  100. a[i].group = a[i + 1].group;
  101.  
  102. a[i].exams = a[i + 1].exams;
  103.  
  104. for (int j = 0; j < a[i].exams; j++) {
  105. strcpy_s(a[i].disp[j], a[i + 1].disp[j]);
  106. }
  107.  
  108. for (int j = 0; j < a[i].exams; j++) {
  109. a[i].mark[j] = a[i + 1].mark[j];
  110. }
  111.  
  112. }
  113. }
  114. }
  115. }
  116. void add_info1(Student a[], int n) {
  117. int p;
  118. printf("Введите номер ячейки, перед которой хотите вставить данные вставить данные: ");
  119. cin >> p;
  120. char yep[20] = " ";
  121. int repl[7];
  122. int repl2;
  123. int repl3;
  124.  
  125. cout << "Введите ФИО студента: ";
  126. cin >> a[n].fio.surname;
  127. cin >> a[n].fio.name;
  128. cin >> a[n].fio.patronymic;
  129.  
  130. cout << "Введите число экзаменов: ";
  131. cin >> a[n].exams;
  132.  
  133. cout << "Введите номер группы: ";
  134. cin >> a[n].group;
  135.  
  136. cout << "Введите названия экзаменов: ";
  137. for (int j = 0; j < a[n].exams; j++) {
  138. cin >> a[n].disp[j];
  139. }
  140.  
  141. cout << "Введите оценки студента по этим предметам: ";
  142. for (int j = 0; j < a[n].exams; j++) {
  143. cin >> a[n].mark[j];
  144. }
  145.  
  146. for (int i = n; i > p; i--) {
  147.  
  148. strcpy_s(yep, a[i - 1].fio.name);
  149. strcpy_s(a[i - 1].fio.name, a[i].fio.name);
  150. strcpy_s(a[i].fio.name, yep);
  151.  
  152. strcpy_s(yep, a[i - 1].fio.surname);
  153. strcpy_s(a[i - 1].fio.surname, a[i].fio.surname);
  154. strcpy_s(a[i].fio.surname, yep);
  155.  
  156. strcpy_s(yep, a[i - 1].fio.patronymic);
  157. strcpy_s(a[i - 1].fio.patronymic, a[i].fio.patronymic);
  158. strcpy_s(a[i].fio.patronymic, yep);
  159.  
  160. repl2 = a[i - 1].exams;
  161. a[i - 1].exams = a[i].exams;
  162. a[i].exams = repl2;
  163.  
  164. repl3 = a[i - 1].group;
  165. a[i - 1].group = a[i].group;
  166. a[i].group = repl3;
  167.  
  168. for (int j = 0; j < a[i].exams; j++) {
  169. strcpy_s(yep, a[i - 1].disp[j]);
  170. strcpy_s(a[i - 1].disp[j], a[i].disp[j]);
  171. strcpy_s(a[i].disp[j], yep);
  172. }
  173.  
  174. for (int j = 0; j < a[i].exams; j++) {
  175. repl[j] = a[i - 1].mark[j];
  176. a[i - 1].mark[j] = a[i].mark[j];
  177. a[i].mark[j] = repl[j];
  178. }
  179. }
  180.  
  181. }
  182. void add_info2(Student a[], int n) {
  183. int p;
  184. printf("Введите номер ячейки, после которой хотите вставить данные вставить данные: ");
  185. cin >> p;
  186. char yep[20] = " ";
  187.  
  188. int repl[7];
  189. int repl2;
  190. int repl3;
  191. cout << "Введите ФИО студента: ";
  192. cin >> a[n].fio.surname;
  193. cin >> a[n].fio.name;
  194. cin >> a[n].fio.patronymic;
  195.  
  196. cout << "Введите число экзаменов: ";
  197. cin >> a[n].exams;
  198.  
  199. cout << "Введите номер группы: ";
  200. cin >> a[n].group;
  201.  
  202. cout << "Введите названия экзаменов: ";
  203. for (int j = 0; j < a[n].exams; j++) {
  204. cin >> a[n].disp[j];
  205. }
  206.  
  207. cout << "Введите оценки студента по этим предметам: ";
  208. for (int j = 0; j < a[n].exams; j++) {
  209. cin >> a[n].mark[j];
  210. }
  211.  
  212. for (int i = n; i > p + 1; i--) {
  213.  
  214. strcpy_s(yep, a[i - 1].fio.name);
  215. strcpy_s(a[i - 1].fio.name, a[i].fio.name);
  216. strcpy_s(a[i].fio.name, yep);
  217.  
  218. strcpy_s(yep, a[i - 1].fio.surname);
  219. strcpy_s(a[i - 1].fio.surname, a[i].fio.surname);
  220. strcpy_s(a[i].fio.surname, yep);
  221.  
  222. strcpy_s(yep, a[i - 1].fio.patronymic);
  223. strcpy_s(a[i - 1].fio.patronymic, a[i].fio.patronymic);
  224. strcpy_s(a[i].fio.patronymic, yep);
  225.  
  226. repl2 = a[i - 1].exams;
  227. a[i - 1].exams = a[i].exams;
  228. a[i].exams = repl2;
  229.  
  230. repl3 = a[i - 1].group;
  231. a[i - 1].group = a[i].group;
  232. a[i].group = repl3;
  233.  
  234. for (int j = 0; j < a[n].exams; j++) {
  235. strcpy_s(yep, a[i - 1].disp[j]);
  236. strcpy_s(a[i - 1].disp[j], a[i].disp[j]);
  237. strcpy_s(a[i].disp[j], yep);
  238. }
  239.  
  240. for (int j = 0; j < a[n].exams; j++) {
  241. repl[j] = a[i - 1].mark[j];
  242. a[i - 1].mark[j] = a[i].mark[j];
  243. a[i].mark[j] = repl[j];
  244. }
  245. }
  246. }
  247. void search_info(Student a[], int n) {
  248. char key[20];
  249. int f = 1;
  250. cout << "Введите слово для поиска: ";
  251. cin >> key;
  252. cout << endl;
  253. for (int i = 0; i < n; i++) {
  254.  
  255. if (strcmp(key, a[i].fio.surname) == 0 || strcmp(key, a[i].fio.name) == 0 || strcmp(key, a[i].fio.patronymic) == 0) {
  256.  
  257. cout << setw(25) << left << a[i].fio.surname << " " << setw(4) << left << a[i].exams;
  258. for (int j = 0; j < a[i].exams; j++) {
  259. cout << a[i].disp[j] << " ";
  260. }
  261. cout << endl;
  262. cout << setw(10) << left << a[i].fio.name << " " << setw(15) << left << a[i].fio.patronymic << setw(4) << left << a[i].group << " ";
  263. for (int j = 0; j < a[i].exams; j++) {
  264. cout << setw(4) << a[i].mark[j] << " ";
  265. }
  266. cout << endl;
  267. cout << endl;
  268. }
  269. cout << endl;
  270. }
  271.  
  272. }
  273. void sort(Student a[], int n) {
  274. char repl[20] = " ";
  275. int p[7];
  276. int p1;
  277. int p2;
  278. for (int i = 0; i < n - 1; i++) {
  279. for (int j = 0; j < n - i - 1; j++) {
  280. if (strcmp(a[j].fio.surname, a[j + 1].fio.surname) > 0) {
  281.  
  282. strcpy_s(repl, a[j].fio.name);
  283. strcpy_s(a[j].fio.name, a[j + 1].fio.name);
  284. strcpy_s(a[j + 1].fio.name, repl);
  285.  
  286. strcpy_s(repl, a[j].fio.surname);
  287. strcpy_s(a[j].fio.surname, a[j + 1].fio.surname);
  288. strcpy_s(a[j + 1].fio.surname, repl);
  289.  
  290. strcpy_s(repl, a[j].fio.patronymic);
  291. strcpy_s(a[j].fio.patronymic, a[j + 1].fio.patronymic);
  292. strcpy_s(a[j + 1].fio.patronymic, repl);
  293.  
  294. p2 = a[j].group;
  295. a[j].group = a[j + 1].group;
  296. a[j + 1].group = p2;
  297.  
  298. p1 = a[j].exams;
  299. a[j].exams = a[j + 1].exams;
  300. a[j + 1].exams = p1;
  301.  
  302. for (int r = 0; r < 7; r++) {
  303. strcpy_s(repl, a[j].disp[r]);
  304. strcpy_s(a[j].disp[r], a[j + 1].disp[r]);
  305. strcpy_s(a[j + 1].disp[r], repl);
  306. }
  307.  
  308. for (int r = 0; r < 7; r++) {
  309. p[r] = a[j].mark[r];
  310. a[j].mark[r] = a[j + 1].mark[r];
  311. a[j + 1].mark[r] = p[r];
  312. }
  313.  
  314.  
  315.  
  316. }
  317.  
  318. }
  319. }
  320. }
  321. void sort1(Student a[], int n) {
  322. char repl[20] = " ";
  323. int p[7];
  324. int p1;
  325. int p2;
  326. for (int i = 0; i < n - 1; i++) {
  327. for (int j = 0; j < n - i - 1; j++) {
  328. if (strcmp(a[j].fio.surname, a[j + 1].fio.surname) > 0) {
  329.  
  330. strcpy_s(repl, a[j].fio.name);
  331. strcpy_s(a[j].fio.name, a[j + 1].fio.name);
  332. strcpy_s(a[j + 1].fio.name, repl);
  333.  
  334. strcpy_s(repl, a[j].fio.surname);
  335. strcpy_s(a[j].fio.surname, a[j + 1].fio.surname);
  336. strcpy_s(a[j + 1].fio.surname, repl);
  337.  
  338. strcpy_s(repl, a[j].fio.patronymic);
  339. strcpy_s(a[j].fio.patronymic, a[j + 1].fio.patronymic);
  340. strcpy_s(a[j + 1].fio.patronymic, repl);
  341.  
  342. p2 = a[j].group;
  343. a[j].group = a[j + 1].group;
  344. a[j + 1].group = p2;
  345.  
  346. p1 = a[j].exams;
  347. a[j].exams = a[j + 1].exams;
  348. a[j + 1].exams = p1;
  349.  
  350. for (int r = 0; r < 7; r++) {
  351. strcpy_s(repl, a[j].disp[r]);
  352. strcpy_s(a[j].disp[r], a[j + 1].disp[r]);
  353. strcpy_s(a[j + 1].disp[r], repl);
  354. }
  355.  
  356. for (int r = 0; r < 7; r++) {
  357. p[r] = a[j].mark[r];
  358. a[j].mark[r] = a[j + 1].mark[r];
  359. a[j + 1].mark[r] = p[r];
  360. }
  361.  
  362.  
  363.  
  364. }
  365.  
  366. }
  367. }
  368. for (int i = 0; i < n - 1; i++) {
  369. for (int j = 0; j < n - i - 1; j++) {
  370. if (strcmp(a[j].fio.surname, a[j + 1].fio.surname) == 0){
  371. if (strcmp(a[j].fio.name, a[j + 1].fio.name) > 0) {
  372.  
  373. strcpy_s(repl, a[j].fio.name);
  374. strcpy_s(a[j].fio.name, a[j + 1].fio.name);
  375. strcpy_s(a[j + 1].fio.name, repl);
  376.  
  377. strcpy_s(repl, a[j].fio.surname);
  378. strcpy_s(a[j].fio.surname, a[j + 1].fio.surname);
  379. strcpy_s(a[j + 1].fio.surname, repl);
  380.  
  381. strcpy_s(repl, a[j].fio.patronymic);
  382. strcpy_s(a[j].fio.patronymic, a[j + 1].fio.patronymic);
  383. strcpy_s(a[j + 1].fio.patronymic, repl);
  384.  
  385. p2 = a[j].group;
  386. a[j].group = a[j + 1].group;
  387. a[j + 1].group = p2;
  388.  
  389. p1 = a[j].exams;
  390. a[j].exams = a[j + 1].exams;
  391. a[j + 1].exams = p1;
  392.  
  393. for (int r = 0; r < 7; r++) {
  394. strcpy_s(repl, a[j].disp[r]);
  395. strcpy_s(a[j].disp[r], a[j + 1].disp[r]);
  396. strcpy_s(a[j + 1].disp[r], repl);
  397. }
  398.  
  399. for (int r = 0; r < 7; r++) {
  400. p[r] = a[j].mark[r];
  401. a[j].mark[r] = a[j + 1].mark[r];
  402. a[j + 1].mark[r] = p[r];
  403. }
  404.  
  405.  
  406. }
  407. }
  408.  
  409. }
  410. }
  411. }
  412. Student a[20];
  413. int main() {
  414. setlocale(LC_ALL, "rus");
  415. FILE* filetxt;
  416. FILE* filetxt2;
  417. fopen_s(&filetxt, "students_info.txt", "a+");
  418. fopen_s(&filetxt2, "student_info2.txt", "w");
  419. int i = 0, n = 0;
  420. int k = -1;
  421. while (k != 10) {
  422. printf(" 1-формирование данных\n 2-добавить данные\n 3-поиск данных по ключу\n 4-удаление данных по ключу\n 5-вставка данных в массив перед i-ой записью\n 6-вставка данных в массив после i-ой записи\n 7-сортировка по фамилии\n 8-сортировка по имени и фамилии\n 9-сохранение данных в файл\n 0-вывод данных\n 10-выход\n");
  423. cin >> k;
  424. if (k == 1)
  425. read_file(a, filetxt, i, &n);
  426.  
  427. if (k == 2) {
  428. add_info(a, n);
  429. n++;
  430. }
  431.  
  432. if (k == 3)
  433. search_info(a, n);
  434.  
  435. if (k == 4) {
  436. delete_info(a, n);
  437. n--;
  438. }
  439.  
  440. if (k == 5) {
  441. add_info1(a, n);
  442. n++;
  443. }
  444. if (k == 6) {
  445.  
  446. add_info2(a, n);
  447. n++;
  448. }
  449.  
  450. if (k == 7) {
  451. sort(a, n);
  452. }
  453.  
  454. if (k == 8) {
  455. sort1(a, n);
  456. }
  457. if (k == 9) {
  458. for (int i = 0; i < n; i++) {
  459. fprintf(filetxt2,"%-25s", a[i].fio.surname); fprintf(filetxt2, " ");
  460. fprintf(filetxt2, "%-4d", a[i].exams);
  461. for (int j = 0; j < a[i].exams; j++) {
  462. fprintf(filetxt2,"%s", a[i].disp[j]);
  463. fprintf_s(filetxt2, " ");
  464. }
  465. fprintf(filetxt2, "\n");
  466. fprintf(filetxt2, "%-10s",a[i].fio.name); fprintf(filetxt2, " ");
  467. fprintf(filetxt2, "%-15s",a[i].fio.patronymic);
  468. fprintf(filetxt2, "%-4d", a[i].group); fprintf(filetxt2, " ");
  469. for (int j = 0; j < a[i].exams; j++) {
  470. fprintf_s(filetxt2, "%-4d", a[i].mark[j]);
  471. fprintf(filetxt2, " ");
  472. }
  473. fprintf(filetxt2, "\n");
  474. fprintf(filetxt2, "\n");
  475. }
  476. }
  477.  
  478. if (k == 0)
  479. print_info(a, n);
  480.  
  481. if (k == 10) {
  482. fclose(filetxt);
  483. fclose(filetxt2);
  484. }
  485. }
  486. system("pause");
  487. return 0;
  488. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement