Advertisement
lashrone1

bd

Apr 23rd, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.94 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include<iostream>
  3. #include<string>
  4. #include<windows.h>
  5. #include<stdlib.h>
  6. #include<string.h>
  7. #include<fstream>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. string adress = "БД студентов.txt";
  13.  
  14. ifstream readFile;
  15. ofstream writeFile;
  16.  
  17. struct Data {
  18. int days;
  19. int mounth;
  20. int year;
  21. };
  22.  
  23. struct BD {
  24. Data birt;//обьединение
  25. string name, surname, midname;
  26. int avr_mark;
  27. };
  28.  
  29. void sort(BD *student, int &N) {//сортировка по фамилии
  30. string temp;
  31. if (N == 1) {
  32. }
  33. else {
  34. for (int i = 0; i < N - 1; i++) {
  35. for (int j = 0; j < N - i - 1; j++) {
  36. if (student[j].surname > student[j + 1].surname) {
  37.  
  38. swap(student[j].surname, student[j + 1].surname);
  39. swap(student[j].name, student[j + 1].name);
  40. swap(student[j].midname, student[j + 1].midname);
  41. swap(student[j].avr_mark, student[j + 1].avr_mark);
  42. swap(student[j].birt.days, student[j + 1].birt.days);
  43. swap(student[j].birt.mounth, student[j + 1].birt.mounth);
  44. swap(student[j].birt.year, student[j + 1].birt.year);
  45.  
  46. }
  47. }
  48. }
  49. }
  50. }
  51.  
  52. void search_data(BD *student, int &N) {//поиск по дате рождения
  53. int buf_day;
  54. int buf_mounth;
  55. int q = 0;//счётчик студентов на корректность
  56. cout << "Введите дату рождения: " << endl;
  57. cout << "Введите день: ";
  58. cin >> buf_day;
  59. cout << "Введите месяц: ";
  60. cin >> buf_mounth;
  61.  
  62. system("cls");
  63.  
  64. for (int i = 0; i < N; i++) {
  65. if (buf_day == student[i].birt.days && buf_mounth == student[i].birt.mounth) {
  66. cout << "Номер в списке: ";
  67. cout << i + 1 << endl;
  68. cout << "Имя: ";
  69. cout << student[i].name << endl;
  70. cout << "Фамилия: ";
  71. cout << student[i].surname << endl;
  72. cout << "Отчество: ";
  73. cout << student[i].midname << endl;
  74. cout << "Средний балл: ";
  75. cout << student[i].avr_mark << endl;
  76. cout << "Дата рождения: ";
  77. cout << student[i].birt.days << ".";
  78. cout << student[i].birt.mounth << ".";
  79. cout << student[i].birt.year << endl;
  80. cout << "////////////////////////////////////////////////" << endl;
  81. q++;
  82. }
  83. }
  84. if (q == 0) {
  85. cout << "Таких студентов нет!" << endl;
  86. }
  87. }
  88.  
  89. void serach_avrg(BD *student, int &N) {//поиск по среднему баллу
  90. int buf_avrg_min;
  91. int buf_avrg_max;
  92. int q = 0;//счётчик студентов на корректность
  93. cout << "Введите диапазон(нижний): ";
  94. cin >> buf_avrg_min;
  95. cout << "Введите диапазон(вверхний): ";
  96. cin >> buf_avrg_max;
  97.  
  98. system("cls");
  99.  
  100. for (int i = 0; i < N; i++) {
  101. if (buf_avrg_min < student[i].avr_mark && buf_avrg_max > student[i].avr_mark) {
  102. cout << "Номер в списке: ";
  103. cout << i + 1 << endl;
  104. cout << "Имя: ";
  105. cout << student[i].name << endl;
  106. cout << "Фамилия: ";
  107. cout << student[i].surname << endl;
  108. cout << "Отчество: ";
  109. cout << student[i].midname << endl;
  110. cout << "Средний балл: ";
  111. cout << student[i].avr_mark << endl;
  112. cout << "Дата рождения: ";
  113. cout << student[i].birt.days << ".";
  114. cout << student[i].birt.mounth << ".";
  115. cout << student[i].birt.year << endl;
  116. cout << "////////////////////////////////////////////////" << endl;
  117. q++;
  118. }
  119. }
  120. if (q == 0) {
  121. cout << "Таких студентов нет!" << endl;
  122. }
  123. }
  124.  
  125. void search_surname(BD *student, int &N) {//поиск по фамилии
  126. string buf_surname;
  127. int q = 0;//счётчик студентов на корректность
  128. cout << "Введите фамилию студента: ";
  129. cin >> buf_surname;
  130.  
  131. system("cls");
  132. for (int i = 0; i < N; i++) {
  133. if (buf_surname == student[i].surname) {
  134. cout << "Номер в списке: ";
  135. cout << i + 1 << endl;
  136. cout << "Имя: ";
  137. cout << student[i].name << endl;
  138. cout << "Фамилия: ";
  139. cout << student[i].surname << endl;
  140. cout << "Отчество: ";
  141. cout << student[i].midname << endl;
  142. cout << "Средний балл: ";
  143. cout << student[i].avr_mark << endl;
  144. cout << "Дата рождения: ";
  145. cout << student[i].birt.days << ".";
  146. cout << student[i].birt.mounth << ".";
  147. cout << student[i].birt.year << endl;
  148. cout << "////////////////////////////////////////////////" << endl;
  149. q++;
  150. }
  151. }
  152. if (q == 0) {
  153. cout << "Таких студентов нет!" << endl;
  154. }
  155. }
  156.  
  157. BD *delete_stud(int &N, BD *student) {
  158.  
  159. int num;
  160. if (N == 0) {
  161. cout << "Некого удалять!!!" << endl;
  162. return 0;
  163. }
  164.  
  165. cout << "Введите номер студента которого хотите удалить " << endl;
  166. cin >> num;
  167. if (N < num) {
  168. cout << "Такого студента нет!" << endl;
  169. return student;
  170. }
  171. BD *buf1 = new BD[N - 1];
  172.  
  173. if (num == 1) {
  174. for (int i = 0; i < N - 1; i++) {
  175. buf1[i].name = student[i + 1].name;
  176. buf1[i].surname = student[i + 1].surname;
  177. buf1[i].midname = student[i + 1].midname;
  178. buf1[i].avr_mark = student[i + 1].avr_mark;
  179. buf1[i].birt.days = student[i + 1].birt.days;
  180. buf1[i].birt.mounth = student[i + 1].birt.mounth;
  181. buf1[i].birt.year = student[i + 1].birt.year;
  182. }
  183. cout << "Студент успешно отчислен!" << endl;
  184. N--;
  185. return buf1;
  186. }
  187.  
  188. for (int i = 0; i < num - 1; i++) {
  189. buf1[i].name = student[i].name;
  190. buf1[i].surname = student[i].surname;
  191. buf1[i].midname = student[i].midname;
  192. buf1[i].avr_mark = student[i].avr_mark;
  193. buf1[i].birt.days = student[i].birt.days;
  194. buf1[i].birt.mounth = student[i].birt.mounth;
  195. buf1[i].birt.year = student[i].birt.year;
  196. }
  197.  
  198. for (int i = num; i < N; i++) {
  199. buf1[i - 1].name = student[i].name;
  200. buf1[i - 1].surname = student[i].surname;
  201. buf1[i - 1].midname = student[i].midname;
  202. buf1[i - 1].avr_mark = student[i].avr_mark;
  203. buf1[i - 1].birt.days = student[i].birt.days;
  204. buf1[i - 1].birt.mounth = student[i].birt.mounth;
  205. buf1[i - 1].birt.year = student[i].birt.year;
  206. }
  207.  
  208. cout << "Студент успешно отчислен!" << endl;
  209. N--;
  210. return buf1;
  211. }
  212.  
  213. BD *push_back(int &N, BD *student) {//увеличение массива на 1
  214. int buf_var;//проверка корректности данных
  215.  
  216. BD *buf = new BD[N + 1];
  217.  
  218. for (int i = 0; i < N; i++) {
  219. buf[i].name = student[i].name;
  220. buf[i].surname = student[i].surname;
  221. buf[i].midname = student[i].midname;
  222. buf[i].avr_mark = student[i].avr_mark;
  223. buf[i].birt.days = student[i].birt.days;
  224. buf[i].birt.mounth = student[i].birt.mounth;
  225. buf[i].birt.year = student[i].birt.year;
  226. }
  227. cout << "Имя: ";
  228. cin >> buf[N].name;
  229. cout << "Фамилия: ";
  230. cin >> buf[N].surname;
  231. cout << "Отчество: ";
  232. cin >> buf[N].midname;
  233.  
  234. while (1) {
  235. cout << "Средний балл(0-100): ";
  236. cin >> buf_var;
  237. if (buf_var >= 0 && buf_var <= 100) {
  238. buf[N].avr_mark = buf_var;
  239. break;
  240. }
  241. else {
  242. cout << "Введите количестов баллов корректно" << endl;
  243. continue;
  244. }
  245. }
  246. cout << "Дата рождения: " << endl;
  247.  
  248. while (1) {
  249. cout << "День: ";
  250. cin >> buf_var;
  251. if (buf_var <= 31 && buf_var != 0) {
  252. buf[N].birt.days = buf_var;
  253. break;
  254. }
  255. else {
  256. cout << "Введите дату корректно" << endl;
  257. continue;
  258. }
  259. }
  260.  
  261. while (1) {
  262. cout << "Месяц: ";
  263. cin >> buf_var;
  264. if (buf_var <= 12 && buf_var != 0) {
  265. buf[N].birt.mounth = buf_var;
  266. break;
  267. }
  268. else {
  269. cout << "Введите дату корректно" << endl;
  270. continue;
  271. }
  272. }
  273.  
  274. while (1) {
  275. cout << "Год: ";
  276. cin >> buf_var;
  277. if (buf_var <= 2019) {
  278. buf[N].birt.year = buf_var;
  279. break;
  280. }
  281. else {
  282. cout << "Введите дату корректно" << endl;
  283. continue;
  284. }
  285. }
  286. N++;
  287. return buf;
  288. }
  289. void recap(int &N, BD *student) {//вывод в консоль
  290. if (N == 0) {
  291. cout << "Никого нет в списке!!!" << endl;
  292. }
  293. for (int i = 0; i < N; i++) {
  294. cout << "Номер в списке: ";
  295. cout << i + 1 << endl;
  296. cout << "Имя: ";
  297. cout << student[i].name << endl;
  298. cout << "Фамилия: ";
  299. cout << student[i].surname << endl;
  300. cout << "Отчество: ";
  301. cout << student[i].midname << endl;
  302. cout << "Средний балл: ";
  303. cout << student[i].avr_mark << endl;
  304. cout << "Дата рождения: ";
  305. cout << student[i].birt.days << ".";
  306. cout << student[i].birt.mounth << ".";
  307. cout << student[i].birt.year << endl;
  308. cout << "////////////////////////////////////////////////" << endl;
  309. }
  310.  
  311. }
  312. BD *read_from_file(int &N, BD *student) {//считываем инфу из файла и записываем в массив
  313.  
  314. readFile.open(adress);
  315. string none;//пропуск слов
  316.  
  317. readFile >> none;
  318.  
  319. for (int i = 0; i < N; i++) {
  320. readFile >> student[i].name;
  321. readFile >> student[i].surname;
  322. readFile >> student[i].midname;
  323. readFile >> student[i].avr_mark;
  324. readFile >> student[i].birt.days;
  325. readFile >> student[i].birt.mounth;
  326. readFile >> student[i].birt.year;
  327. }
  328.  
  329. readFile.close();
  330.  
  331. return student;
  332. }
  333.  
  334. void add_to_file(int &N, BD *student) {//переписываем файл добавля нового тсудента
  335.  
  336. writeFile.open(adress);
  337. writeFile << N << endl;
  338. for (int i = 0; i < N; i++) {
  339.  
  340. writeFile << student[i].name << endl;
  341. writeFile << student[i].surname << endl;
  342. writeFile << student[i].midname << endl;
  343. writeFile << student[i].avr_mark << endl;
  344. writeFile << student[i].birt.days << endl;
  345. writeFile << student[i].birt.mounth << endl;
  346. writeFile << student[i].birt.year << endl;
  347. }
  348.  
  349. writeFile.close();
  350.  
  351. }
  352. void check_file_empty(int &N) {
  353.  
  354. int file_size;//размер файла
  355.  
  356. readFile.open(adress, ios::app);//узнаём сколько студентов в списке
  357.  
  358. readFile.seekg(0, ios::beg);//указатель на начало файла // куда поставить указатель
  359. file_size = readFile.tellg();// считывает место указателя и возвращает эго//считывает количестов байтов слево от него
  360.  
  361. if (file_size == 0) {
  362. writeFile << 0;
  363. }
  364.  
  365. readFile >> N;
  366.  
  367. readFile.close();
  368.  
  369. }
  370.  
  371. int main() {
  372.  
  373. setlocale(LC_ALL, "RU");
  374. SetConsoleCP(1251);
  375. SetConsoleOutputCP(1251);
  376.  
  377. int N = 0;//количество студентов
  378. char switch_on;//меню
  379. char switch_on1;//под меню
  380.  
  381. /*readFile.open(adress, ios::app);//узнаём сколько студентов в списке
  382.  
  383. readFile >> N;
  384.  
  385. readFile.close();*/
  386.  
  387. check_file_empty(N);
  388.  
  389. BD *student = new BD[N];//конструктор
  390.  
  391. student = read_from_file(N, student);
  392.  
  393. while (1) {
  394. cout << "1. Новый студент" << endl;
  395. cout << "2. Вывод всех студентов" << endl;
  396. cout << "3. Поиск студента" << endl;
  397. cout << "4. Отчисление студента" << endl;
  398. cout << "0. Выход" << endl;
  399.  
  400. cin >> switch_on;
  401. switch (switch_on)
  402. {
  403. case '0': {
  404. return 0;
  405. }
  406. case '1': {
  407. system("cls");
  408. student = push_back(N, student);
  409. sort(student, N);
  410. add_to_file(N, student);
  411. break;
  412. }case '2': {
  413. system("cls");
  414. recap(N, student);
  415. break;
  416. }case '3': {
  417. cout << "1. Поиск по фамилии" << endl;
  418. cout << "2. Поиск по среднему баллу" << endl;
  419. cout << "3. Поиск по дате рождения" << endl;
  420. cout << "4. Выйти в первоначальное меню" << endl;
  421. cin >> switch_on1;
  422. switch (switch_on1)
  423. {
  424. case '1': {
  425.  
  426. search_surname(student, N);
  427. break;
  428. }
  429. case '2': {
  430.  
  431. serach_avrg(student, N);
  432. break;
  433. }
  434. case '3': {
  435.  
  436. search_data(student, N);
  437. break;
  438. }
  439. case '4': {
  440. system("cls");
  441. break;
  442. }
  443. default: {
  444. cout << "ERROR" << endl;
  445. break;
  446. }
  447. }
  448. break;
  449. }case '4': {
  450. system("cls");
  451. student = delete_stud(N, student);
  452. add_to_file(N, student);
  453. break;
  454. }
  455. default:
  456. cout << "ERROR" << endl;
  457. break;
  458. }
  459. }
  460. system("pause");
  461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement