Advertisement
Courbe_Impliquee

vector.struct

Nov 8th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.61 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. #include <vector>
  9. using namespace std;
  10.  
  11. struct FIO {
  12. char name[20];
  13. char surname[20];
  14. char patronymic[20];
  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(FILE* filetxt,vector<Student> v) {
  25. Student a;
  26. while (filetxt) {
  27. fscanf_s(filetxt, "%s", a.fio.surname, _countof(a.fio.surname));
  28. if (strlen(a.fio.surname) < 2) {
  29. break;
  30. }
  31. fscanf_s(filetxt, "%d", &a.exams);
  32. for (int j = 0; j < a.exams; j++) {
  33. fscanf_s(filetxt, "%s", a.disp[j], _countof(a.disp[j]));
  34. }
  35. fscanf_s(filetxt, "%s", a.fio.name, _countof(a.fio.name));
  36. fscanf_s(filetxt, "%s", a.fio.patronymic, _countof(a.fio.patronymic));
  37. fscanf_s(filetxt, "%d", &a.group);
  38. for (int j = 0; j < a.exams; j++) {
  39. fscanf_s(filetxt, "%d", &a.mark[j]);
  40. }
  41. v.push_back(a);
  42. }
  43. }
  44. void print_info(Student a[]) {
  45. for (int i = 0; i < v.size(); i++) {
  46. cout << setw(25) << left << a[i].fio.surname << " " << setw(4) << left << a[i].exams;
  47. for (int j = 0; j < a[i].exams; j++) {
  48. cout << a[i].disp[j] << " ";
  49. }
  50. cout << endl;
  51. cout << setw(10) << left << a[i].fio.name << " " << setw(15) << left << a[i].fio.patronymic << setw(4) << left << a[i].group << " ";
  52. for (int j = 0; j < a[i].exams; j++) {
  53. cout << setw(4) << a[i].mark[j] << " ";
  54. }
  55. cout << endl;
  56. cout << endl;
  57. }
  58. cout << endl;
  59. }
  60. void add_info(vector<Student> v) {
  61. Student a;
  62. cout << "Введите ФИО студента: ";
  63. cin >> a.fio.surname;
  64. cin >> a.fio.name;
  65. cin >> a.fio.patronymic;
  66.  
  67. cout << "Введите число экзаменов: ";
  68. cin >> a.exams;
  69.  
  70. cout << "Введите номер группы: ";
  71. cin >> a.group;
  72.  
  73. cout << "Введите названия экзаменов: ";
  74. for (int j = 0; j < a.exams; j++) {
  75. cin >> a.disp[j];
  76. }
  77.  
  78. cout << "Введите оценки студента по этим предметам: ";
  79. for (int j = 0; j < a.exams; j++) {
  80. cin >> a.mark[j];
  81. }
  82. v.push_back(a);
  83.  
  84. }
  85. void delete_info(vector<Student> v) {
  86. char key1[20];
  87. cout << "Введите ключ,по которому хотите удалить: ";
  88. cin >> key1;
  89. for (int i = 0; i < v.size(); 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.  
  106. }
  107. }
  108. }
  109. void add_info1(vector<Student> v) {
  110. int p;
  111. printf("Введите номер ячейки, перед которой хотите вставить данные вставить данные (нумерация с 0): ");
  112. cin >> p;
  113. if (p>v.size()) p = v.size();
  114. if (p < 0) p = 0;
  115. for (int i = v.size(); 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. }
  151. void add_info2(vector<Student> v) {
  152. int p;
  153. printf("Введите номер ячейки, после которой хотите вставить данные вставить данные (нумерация с 0): ");
  154. cin >> p;
  155. if (p>n) p = n;
  156. if (p < 0) p = 0;
  157. p++;
  158. for (int i = n; i > p; i--) {
  159. strcpy_s(a[i].fio.name, a[i - 1].fio.name);
  160. strcpy_s(a[i].fio.surname, a[i - 1].fio.surname);
  161. strcpy_s(a[i].fio.patronymic, a[i - 1].fio.patronymic);
  162. a[i].exams = a[i - 1].exams;
  163. a[i].group = a[i - 1].group;
  164. for (int j = 0; j < a[n].exams; j++) {
  165. strcpy_s(a[i].disp[j], a[i - 1].disp[j]);
  166. }
  167. for (int j = 0; j < a[n].exams; j++) {
  168. a[i].mark[j] = a[i - 1].mark[j];
  169. }
  170. }
  171.  
  172. cout << "Введите ФИО студента: ";
  173. cin >> a[p].fio.surname;
  174. cin >> a[p].fio.name;
  175. cin >> a[p].fio.patronymic;
  176.  
  177. cout << "Введите число экзаменов: ";
  178. cin >> a[p].exams;
  179.  
  180. cout << "Введите номер группы: ";
  181. cin >> a[p].group;
  182.  
  183. cout << "Введите названия экзаменов: ";
  184. for (int j = 0; j < a[p].exams; j++) {
  185. cin >> a[p].disp[j];
  186. }
  187.  
  188. cout << "Введите оценки студента по этим предметам: ";
  189. for (int j = 0; j < a[p].exams; j++) {
  190. cin >> a[p].mark[j];
  191. }
  192. }
  193. void search_info(vector<Student> v) {
  194. char key[20];
  195. int f = 1;
  196. cout << "Введите слово для поиска: ";
  197. cin >> key;
  198. cout << endl;
  199. for (int i = 0; i < v.size(); i++) {
  200.  
  201. if (strcmp(key, a[i].fio.surname) == 0 || strcmp(key, a[i].fio.name) == 0 || strcmp(key, a[i].fio.patronymic) == 0) {
  202.  
  203. cout << setw(25) << left << a[i].fio.surname << " " << setw(4) << left << a[i].exams;
  204. for (int j = 0; j < a[i].exams; j++) {
  205. cout << a[i].disp[j] << " ";
  206. }
  207. cout << endl;
  208. cout << setw(10) << left << a[i].fio.name << " " << setw(15) << left << a[i].fio.patronymic << setw(4) << left << a[i].group << " ";
  209. for (int j = 0; j < a[i].exams; j++) {
  210. cout << setw(4) << a[i].mark[j] << " ";
  211. }
  212. cout << endl;
  213. cout << endl;
  214. }
  215. cout << endl;
  216. }
  217.  
  218. }
  219. void sort(vector<Student> v) {
  220. char repl[20] = " ";
  221. int p[7];
  222. int p1;
  223. int p2;
  224. for (int i = 0; i < v.size() - 1; i++) {
  225. for (int j = 0; j < v.size() - i - 1; j++) {
  226. if (strcmp(a[j].fio.surname, a[j + 1].fio.surname) > 0) {
  227.  
  228. strcpy_s(repl, a[j].fio.name);
  229. strcpy_s(a[j].fio.name, a[j + 1].fio.name);
  230. strcpy_s(a[j + 1].fio.name, repl);
  231.  
  232. strcpy_s(repl, a[j].fio.surname);
  233. strcpy_s(a[j].fio.surname, a[j + 1].fio.surname);
  234. strcpy_s(a[j + 1].fio.surname, repl);
  235.  
  236. strcpy_s(repl, a[j].fio.patronymic);
  237. strcpy_s(a[j].fio.patronymic, a[j + 1].fio.patronymic);
  238. strcpy_s(a[j + 1].fio.patronymic, repl);
  239.  
  240. p2 = a[j].group;
  241. a[j].group = a[j + 1].group;
  242. a[j + 1].group = p2;
  243.  
  244. p1 = a[j].exams;
  245. a[j].exams = a[j + 1].exams;
  246. a[j + 1].exams = p1;
  247.  
  248. for (int r = 0; r < 7; r++) {
  249. strcpy_s(repl, a[j].disp[r]);
  250. strcpy_s(a[j].disp[r], a[j + 1].disp[r]);
  251. strcpy_s(a[j + 1].disp[r], repl);
  252. }
  253.  
  254. for (int r = 0; r < 7; r++) {
  255. p[r] = a[j].mark[r];
  256. a[j].mark[r] = a[j + 1].mark[r];
  257. a[j + 1].mark[r] = p[r];
  258. }
  259. }
  260. }
  261. }
  262. }
  263. void sort1(vector<Student> v) {
  264. char repl[20] = " ";
  265. int p[7];
  266. int p1;
  267. int p2;
  268. for (int i = 0; i < v.size() - 1; i++) {
  269. for (int j = 0; j < v.size() - i - 1; j++) {
  270. if (strcmp(a[j].fio.surname, a[j + 1].fio.surname) > 0 ||
  271. strcmp(a[j].fio.surname, a[j + 1].fio.surname) == 0 && strcmp(a[j].fio.name, a[j + 1].fio.name) > 0) {
  272.  
  273. strcpy_s(repl, a[j].fio.name);
  274. strcpy_s(a[j].fio.name, a[j + 1].fio.name);
  275. strcpy_s(a[j + 1].fio.name, repl);
  276.  
  277. strcpy_s(repl, a[j].fio.surname);
  278. strcpy_s(a[j].fio.surname, a[j + 1].fio.surname);
  279. strcpy_s(a[j + 1].fio.surname, repl);
  280.  
  281. strcpy_s(repl, a[j].fio.patronymic);
  282. strcpy_s(a[j].fio.patronymic, a[j + 1].fio.patronymic);
  283. strcpy_s(a[j + 1].fio.patronymic, repl);
  284.  
  285. p2 = a[j].group;
  286. a[j].group = a[j + 1].group;
  287. a[j + 1].group = p2;
  288.  
  289. p1 = a[j].exams;
  290. a[j].exams = a[j + 1].exams;
  291. a[j + 1].exams = p1;
  292.  
  293. for (int r = 0; r < 7; r++) {
  294. strcpy_s(repl, a[j].disp[r]);
  295. strcpy_s(a[j].disp[r], a[j + 1].disp[r]);
  296. strcpy_s(a[j + 1].disp[r], repl);
  297. }
  298.  
  299. for (int r = 0; r < 7; r++) {
  300. p[r] = a[j].mark[r];
  301. a[j].mark[r] = a[j + 1].mark[r];
  302. a[j + 1].mark[r] = p[r];
  303. }
  304. }
  305.  
  306. }
  307. }
  308.  
  309. }
  310. Student a[20];
  311. int main() {
  312. setlocale(LC_ALL, "rus");
  313. FILE* filetxt;
  314. FILE* filetxt2;
  315. fopen_s(&filetxt, "students_info.txt", "a+");
  316. fopen_s(&filetxt2, "student_info2.txt", "w");
  317.  
  318. int i = 0, n = 0;
  319. int k = -1;
  320. while (k != 10) {
  321. 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");
  322. cin >> k;
  323. if (k == 1)
  324. read_file(filetxt);
  325.  
  326. if (k == 2) {
  327. add_info(a);
  328. }
  329.  
  330. if (k == 3)
  331. search_info(a);
  332.  
  333. if (k == 4) {
  334. delete_info(a);
  335. }
  336.  
  337. if (k == 5) {
  338. add_info1(a);
  339. }
  340. if (k == 6) {
  341.  
  342. add_info2(a);
  343. }
  344.  
  345. if (k == 7) {
  346. sort(a);
  347. }
  348.  
  349. if (k == 8) {
  350. sort1(a);
  351. }
  352. if (k == 9) {
  353. for (int i = 0; i < n; i++) {
  354. fprintf(filetxt2, "%-25s", a[i].fio.surname); fprintf(filetxt2, " ");
  355. fprintf(filetxt2, "%-4d", a[i].exams);
  356. for (int j = 0; j < a[i].exams; j++) {
  357. fprintf(filetxt2, "%s", a[i].disp[j]);
  358. fprintf_s(filetxt2, " ");
  359. }
  360. fprintf(filetxt2, "\n");
  361. fprintf(filetxt2, "%-10s", a[i].fio.name); fprintf(filetxt2, " ");
  362. fprintf(filetxt2, "%-15s", a[i].fio.patronymic);
  363. fprintf(filetxt2, "%-4d", a[i].group); fprintf(filetxt2, " ");
  364. for (int j = 0; j < a[i].exams; j++) {
  365. fprintf_s(filetxt2, "%-4d", a[i].mark[j]);
  366. fprintf(filetxt2, " ");
  367. }
  368. fprintf(filetxt2, "\n");
  369. fprintf(filetxt2, "\n");
  370. }
  371. }
  372.  
  373. if (k == 0)
  374. print_info(a);
  375.  
  376. if (k == 10) {
  377. fclose(filetxt);
  378. fclose(filetxt2);
  379. }
  380. }
  381. system("pause");
  382. return 0;
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement