Advertisement
Courbe_Impliquee

struct3.2

Nov 2nd, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.62 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& n) {
  25. n = 0;
  26. while (filetxt) {
  27. fscanf_s(filetxt, "%s", a[n].fio.surname, _countof(a[n].fio.surname));
  28. if (strlen(a[n].fio.surname) < 2) {
  29. break;
  30. }
  31. fscanf_s(filetxt, "%d", &a[n].exams);
  32. for (int j = 0; j < a[n].exams; j++) {
  33. fscanf_s(filetxt, "%s", a[n].disp[j], _countof(a[n].disp[j]));
  34. }
  35. fscanf_s(filetxt, "%s", a[n].fio.name, _countof(a[n].fio.name));
  36. fscanf_s(filetxt, "%s", a[n].fio.patronymic, _countof(a[n].fio.patronymic));
  37. fscanf_s(filetxt, "%d", &a[n].group);
  38. for (int j = 0; j < a[n].exams; j++) {
  39. fscanf_s(filetxt, "%d", &a[n].mark[j]);
  40. }
  41. n++;
  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. n++;
  84. }
  85. void delete_info(Student a[], int& n) {
  86. char key1[20];
  87. cout << "Введите ключ,по которому хотите удалить: ";
  88. cin >> key1;
  89. for (int i = 0; i < n; i++) {
  90. if ((strcmp(key1, a[i].fio.name) == 0) || (strcmp(key1, a[i].fio.surname) == 0) || (strcmp(key1, a[i].fio.patronymic) == 0)) {
  91. for (int s = i; s < n-1; s++) {
  92. strcpy_s(a[s].fio.name, a[s + 1].fio.name);
  93. strcpy_s(a[s].fio.surname, a[s + 1].fio.surname);
  94. strcpy_s(a[s].fio.patronymic, a[s + 1].fio.patronymic);
  95. a[s].group = a[s + 1].group;
  96. a[s].exams = a[s + 1].exams;
  97. for (int j = 0; j < a[s].exams; j++) {
  98. strcpy_s(a[s].disp[j], a[s + 1].disp[j]);
  99. }
  100. for (int j = 0; j < a[s].exams; j++) {
  101. a[s].mark[j] = a[s + 1].mark[j];
  102. }
  103. }
  104. i--;
  105. n--;
  106. }
  107. }
  108. }
  109. void add_info1(Student a[], int& n) {
  110. int p;
  111. printf("Введите номер ячейки, перед которой хотите вставить данные вставить данные (нумерация с 0): ");
  112. cin >> p;
  113. if (p>n) p = n;
  114. if (p < 0) p = 0;
  115. for (int i = n; i > p; i--) {
  116. strcpy_s(a[i].fio.name, a[i - 1].fio.name);
  117. strcpy_s(a[i].fio.surname, a[i - 1].fio.surname);
  118. strcpy_s(a[i].fio.patronymic, a[i - 1].fio.patronymic);
  119. a[i].exams = a[i - 1].exams;
  120. a[i].group = a[i - 1].group;
  121. for (int j = 0; j < a[i].exams; j++) {
  122. strcpy_s(a[i].disp[j], a[i - 1].disp[j]);
  123. }
  124.  
  125. for (int j = 0; j < a[i].exams; j++) {
  126. a[i].mark[j] = a[i - 1].mark[j];
  127. }
  128. }
  129.  
  130. cout << "Введите ФИО студента: ";
  131. cin >> a[p].fio.surname;
  132. cin >> a[p].fio.name;
  133. cin >> a[p].fio.patronymic;
  134.  
  135. cout << "Введите число экзаменов: ";
  136. cin >> a[p].exams;
  137.  
  138. cout << "Введите номер группы: ";
  139. cin >> a[p].group;
  140.  
  141. cout << "Введите названия экзаменов: ";
  142. for (int j = 0; j < a[p].exams; j++) {
  143. cin >> a[p].disp[j];
  144. }
  145.  
  146. cout << "Введите оценки студента по этим предметам: ";
  147. for (int j = 0; j < a[p].exams; j++) {
  148. cin >> a[p].mark[j];
  149. }
  150. n++;
  151. }
  152. void add_info2(Student a[], int& n) {
  153. int p;
  154. printf("Введите номер ячейки, после которой хотите вставить данные вставить данные (нумерация с 0): ");
  155. cin >> p;
  156. if (p>n) p = n;
  157. if (p < 0) p = 0;
  158. p ++;
  159. for (int i = n; i > p; i--) {
  160. strcpy_s(a[i].fio.name, a[i - 1].fio.name);
  161. strcpy_s(a[i].fio.surname, a[i - 1].fio.surname);
  162. strcpy_s(a[i].fio.patronymic, a[i - 1].fio.patronymic);
  163. a[i].exams = a[i - 1].exams;
  164. a[i].group = a[i - 1].group;
  165. for (int j = 0; j < a[n].exams; j++) {
  166. strcpy_s(a[i].disp[j], a[i - 1].disp[j]);
  167. }
  168. for (int j = 0; j < a[n].exams; j++) {
  169. a[i].mark[j] = a[i - 1].mark[j];
  170. }
  171. }
  172.  
  173. cout << "Введите ФИО студента: ";
  174. cin >> a[p].fio.surname;
  175. cin >> a[p].fio.name;
  176. cin >> a[p].fio.patronymic;
  177.  
  178. cout << "Введите число экзаменов: ";
  179. cin >> a[p].exams;
  180.  
  181. cout << "Введите номер группы: ";
  182. cin >> a[p].group;
  183.  
  184. cout << "Введите названия экзаменов: ";
  185. for (int j = 0; j < a[p].exams; j++) {
  186. cin >> a[p].disp[j];
  187. }
  188.  
  189. cout << "Введите оценки студента по этим предметам: ";
  190. for (int j = 0; j < a[p].exams; j++) {
  191. cin >> a[p].mark[j];
  192. }
  193. n++;
  194. }
  195. void search_info(Student a[], int n) {
  196. char key[20];
  197. int f = 1;
  198. cout << "Введите слово для поиска: ";
  199. cin >> key;
  200. cout << endl;
  201. for (int i = 0; i < n; i++) {
  202.  
  203. if (strcmp(key, a[i].fio.surname) == 0 || strcmp(key, a[i].fio.name) == 0 || strcmp(key, a[i].fio.patronymic) == 0) {
  204.  
  205. cout << setw(25) << left << a[i].fio.surname << " " << setw(4) << left << a[i].exams;
  206. for (int j = 0; j < a[i].exams; j++) {
  207. cout << a[i].disp[j] << " ";
  208. }
  209. cout << endl;
  210. cout << setw(10) << left << a[i].fio.name << " " << setw(15) << left << a[i].fio.patronymic << setw(4) << left << a[i].group << " ";
  211. for (int j = 0; j < a[i].exams; j++) {
  212. cout << setw(4) << a[i].mark[j] << " ";
  213. }
  214. cout << endl;
  215. cout << endl;
  216. }
  217. cout << endl;
  218. }
  219.  
  220. }
  221. void sort(Student a[], int n) {
  222. char repl[20] = " ";
  223. int p[7];
  224. int p1;
  225. int p2;
  226. for (int i = 0; i < n - 1; i++) {
  227. for (int j = 0; j < n - i - 1; j++) {
  228. if (strcmp(a[j].fio.surname, a[j + 1].fio.surname) > 0) {
  229.  
  230. strcpy_s(repl, a[j].fio.name);
  231. strcpy_s(a[j].fio.name, a[j + 1].fio.name);
  232. strcpy_s(a[j + 1].fio.name, repl);
  233.  
  234. strcpy_s(repl, a[j].fio.surname);
  235. strcpy_s(a[j].fio.surname, a[j + 1].fio.surname);
  236. strcpy_s(a[j + 1].fio.surname, repl);
  237.  
  238. strcpy_s(repl, a[j].fio.patronymic);
  239. strcpy_s(a[j].fio.patronymic, a[j + 1].fio.patronymic);
  240. strcpy_s(a[j + 1].fio.patronymic, repl);
  241.  
  242. p2 = a[j].group;
  243. a[j].group = a[j + 1].group;
  244. a[j + 1].group = p2;
  245.  
  246. p1 = a[j].exams;
  247. a[j].exams = a[j + 1].exams;
  248. a[j + 1].exams = p1;
  249.  
  250. for (int r = 0; r < 7; r++) {
  251. strcpy_s(repl, a[j].disp[r]);
  252. strcpy_s(a[j].disp[r], a[j + 1].disp[r]);
  253. strcpy_s(a[j + 1].disp[r], repl);
  254. }
  255.  
  256. for (int r = 0; r < 7; r++) {
  257. p[r] = a[j].mark[r];
  258. a[j].mark[r] = a[j + 1].mark[r];
  259. a[j + 1].mark[r] = p[r];
  260. }
  261. }
  262. }
  263. }
  264. }
  265. void sort1(Student a[], int n) {
  266. char repl[20] = " ";
  267. int p[7];
  268. int p1;
  269. int p2;
  270. for (int i = 0; i < n - 1; i++) {
  271. for (int j = 0; j < n - i - 1; j++) {
  272. if (strcmp(a[j].fio.surname, a[j + 1].fio.surname) > 0 ||
  273. strcmp(a[j].fio.surname, a[j + 1].fio.surname) == 0 && strcmp(a[j].fio.name, a[j + 1].fio.name) > 0) {
  274.  
  275. strcpy_s(repl, a[j].fio.name);
  276. strcpy_s(a[j].fio.name, a[j + 1].fio.name);
  277. strcpy_s(a[j + 1].fio.name, repl);
  278.  
  279. strcpy_s(repl, a[j].fio.surname);
  280. strcpy_s(a[j].fio.surname, a[j + 1].fio.surname);
  281. strcpy_s(a[j + 1].fio.surname, repl);
  282.  
  283. strcpy_s(repl, a[j].fio.patronymic);
  284. strcpy_s(a[j].fio.patronymic, a[j + 1].fio.patronymic);
  285. strcpy_s(a[j + 1].fio.patronymic, repl);
  286.  
  287. p2 = a[j].group;
  288. a[j].group = a[j + 1].group;
  289. a[j + 1].group = p2;
  290.  
  291. p1 = a[j].exams;
  292. a[j].exams = a[j + 1].exams;
  293. a[j + 1].exams = p1;
  294.  
  295. for (int r = 0; r < 7; r++) {
  296. strcpy_s(repl, a[j].disp[r]);
  297. strcpy_s(a[j].disp[r], a[j + 1].disp[r]);
  298. strcpy_s(a[j + 1].disp[r], repl);
  299. }
  300.  
  301. for (int r = 0; r < 7; r++) {
  302. p[r] = a[j].mark[r];
  303. a[j].mark[r] = a[j + 1].mark[r];
  304. a[j + 1].mark[r] = p[r];
  305. }
  306. }
  307.  
  308. }
  309. }
  310.  
  311. }
  312. Student a[20];
  313. int main() {
  314. setlocale(LC_ALL, "rus");
  315. FILE* filetxt;
  316. FILE* filetxt2;
  317. fopen_s(&filetxt, "students_info.txt", "a+");
  318. fopen_s(&filetxt2, "student_info2.txt", "w");
  319. int i = 0, n = 0;
  320. int k = -1;
  321. while (k != 10) {
  322. 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");
  323. cin >> k;
  324. if (k == 1)
  325. read_file(a, filetxt, n);
  326.  
  327. if (k == 2) {
  328. add_info(a, n);
  329. }
  330.  
  331. if (k == 3)
  332. search_info(a, n);
  333.  
  334. if (k == 4) {
  335. delete_info(a, n);
  336. }
  337.  
  338. if (k == 5) {
  339. add_info1(a, n);
  340. }
  341. if (k == 6) {
  342.  
  343. add_info2(a, n);
  344. }
  345.  
  346. if (k == 7) {
  347. sort(a, n);
  348. }
  349.  
  350. if (k == 8) {
  351. sort1(a, n);
  352. }
  353. if (k == 9) {
  354. for (int i = 0; i < n; i++) {
  355. fprintf(filetxt2,"%-25s", a[i].fio.surname); fprintf(filetxt2, " ");
  356. fprintf(filetxt2, "%-4d", a[i].exams);
  357. for (int j = 0; j < a[i].exams; j++) {
  358. fprintf(filetxt2,"%s", a[i].disp[j]);
  359. fprintf_s(filetxt2, " ");
  360. }
  361. fprintf(filetxt2, "\n");
  362. fprintf(filetxt2, "%-10s",a[i].fio.name); fprintf(filetxt2, " ");
  363. fprintf(filetxt2, "%-15s",a[i].fio.patronymic);
  364. fprintf(filetxt2, "%-4d", a[i].group); fprintf(filetxt2, " ");
  365. for (int j = 0; j < a[i].exams; j++) {
  366. fprintf_s(filetxt2, "%-4d", a[i].mark[j]);
  367. fprintf(filetxt2, " ");
  368. }
  369. fprintf(filetxt2, "\n");
  370. fprintf(filetxt2, "\n");
  371. }
  372. }
  373.  
  374. if (k == 0)
  375. print_info(a, n);
  376.  
  377. if (k == 10) {
  378. fclose(filetxt);
  379. fclose(filetxt2);
  380. }
  381. }
  382. system("pause");
  383. return 0;
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement