Advertisement
Courbe_Impliquee

struct3

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