Advertisement
Guest User

Untitled

a guest
May 20th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string>
  3. #include <stdlib.h>
  4. #include <fstream>
  5. #include <io.h>
  6. #include <vector>
  7. #include <iostream>
  8.  
  9. struct Man {
  10. char fam[20];
  11. char name[20];
  12. char patronymic[20];
  13. int year;
  14. };
  15. struct Student {
  16. int group;
  17. int marks[4];
  18. double sr_ball;
  19. Man ob;
  20. };
  21. //Функция ввода студентов в массив, где первый
  22. //параметр - это имя заполняемого массива,
  23. //а два следующих параметра - это номера
  24. //элементов массива, т.е. с какого по какой.
  25. void vvod_stud(Student*, int, int, std::ofstream &file);
  26.  
  27. //Функция вывода тех студентов, у кого средний балл выше 4.0
  28. void vivod_max_srball(const Student*, int);
  29.  
  30. //Функция сортировки по полю фамилия
  31. void sort_fam(Student*, int, std::ofstream &f);
  32.  
  33. void read_file(std::ifstream &file, std::vector <std::string> &vec, std::string str);
  34.  
  35. //Функция ввода одного студента.
  36. void vvod(Student*, std::ofstream &file);
  37.  
  38. //Функция вывода одного судента.
  39. void vivod(const Student*);
  40.  
  41. //Функция вывода всех студентов
  42. void vivod_stud(const Student*, int);
  43.  
  44. int kol_udal_stud(Student*, int);
  45.  
  46. //функция проверки файла на существование
  47. bool FileExists(const char *fname);
  48.  
  49. //вывод студентов у которых ср. балл больше 4
  50. void file_max_ball(std::string n_, std::vector <std::string> v, int ecx, int z);
  51.  
  52. //вывод сортирочки с файла
  53. void file_sort(int ecx, int z, std::vector<std::string> v);
  54.  
  55. //вывод инфо с файла
  56. void vivod_file(bool count, int ecx, int z, std::string str, std::string n_, std::vector<std::string> v, std::vector<std::string> s);
  57. void main(void) {
  58. std::vector <std::string> v,s;
  59. std::string str;
  60. std::ofstream _out;
  61. if (FileExists("Students_List.txt") == true) {
  62. std::ifstream in("Students_List.txt");
  63. std::ofstream _out;
  64. bool count = false;
  65. char str1[20];
  66. read_file(in, v, str);
  67. std::string n_ = v[2];
  68. int z = atoi(n_.c_str());
  69. z *= 4;
  70. int n, k, n1, ecx(0);
  71. while (1)
  72. {
  73. puts("\nEnter:\n1 - vivod\n2 - max_sr_ball");
  74. puts("3-sort\n4-delete\n5-add\n0 exit");
  75. scanf_s("%d", &k);
  76. if (!k) break;
  77. switch (k)
  78. {
  79. case 1:
  80. {
  81. printf_s("%-20s%-20s%-20s%s\n", "Fam", "Name", "Group", "Sr_ball");
  82. vivod_file(count,ecx,z,str,n_,v,s);
  83. /*if (count == false) {
  84. if (FileExists("Students_List_New.txt") == true) {
  85. ecx = 0;
  86. std::ifstream _in("Students_List_New.txt");
  87. for (int i = 3; i <= z + 2; i++) {
  88. if (ecx % 4 != 0)
  89. printf_s("%-20s ", v[i].c_str());
  90. else {
  91. printf_s("\n");
  92. printf_s("%-20s ", v[i].c_str());
  93. }
  94. ecx++;
  95. }
  96. s.clear();
  97. read_file(_in, s, str);
  98. std::string j_ = s[0];
  99. int z1 = atoi(j_.c_str());
  100. z1 *= 4;
  101. ecx = 0;
  102. for (int i = 1; i <= z1; i++) {
  103. if (ecx % 4 != 0)
  104. printf_s("%-20s ", s[i].c_str());
  105. else {
  106. printf_s("\n");
  107. printf_s("%-20s ", s[i].c_str());
  108. }
  109. ecx++;
  110. }
  111. }
  112. else {
  113. ecx = 0;
  114. for (int i = 3; i <= z + 2; i++) {
  115. if (ecx % 4 != 0)
  116. printf_s("%-20s ", v[i].c_str());
  117. else {
  118. printf_s("\n");
  119. printf_s("%-20s ", v[i].c_str());
  120. }
  121. ecx++;
  122. }
  123. }
  124. }
  125. else {
  126. if (atoi(n_.c_str()) == 1) {
  127. for (int i = z + 3; i <= z + 4 * 2; i++)
  128. printf_s("%-20s", v[i].c_str());
  129. }
  130.  
  131. ecx = 0;
  132. for (int i = z + 6; i < z*atoi(n_.c_str()); i += 4) {
  133. if (atoi(v[i].c_str()) >= 4) {
  134. for (int j = 3; j >= 0; j--)
  135. printf_s("%-20s ", v[i - j].c_str());
  136. printf_s("\n");
  137. }
  138. ecx++;
  139. if (ecx >= atoi(n_.c_str()))
  140. break;
  141. }
  142. }*/
  143. break;
  144. }
  145. case 2:
  146. {
  147. printf_s("%-20s%-20s%-20s%s\n", "Fam", "Name", "Group", "Sr_ball");
  148. file_max_ball(n_,v,ecx,z);
  149. /*if (atoi(n_.c_str()) == 1) {
  150. for (int i = z + 3; i <= z + 4*2; i++)
  151. printf_s("%-20s", v[i].c_str());
  152. }
  153.  
  154. ecx = 0;
  155. for (int i = z + 6; i < z*atoi(n_.c_str()); i+=4) {
  156. if (atoi(v[i].c_str()) >= 4) {
  157. for (int j = 3; j >= 0; j--)
  158. printf_s("%-20s ", v[i-j].c_str());
  159. printf_s("\n");
  160. }
  161. ecx++;
  162. if (ecx >= atoi(n_.c_str()))
  163. break;
  164. }*/
  165. break;
  166. }
  167. case 3:
  168. {
  169. printf_s("%-20s%-20s%-15s%s\n", "Fam", "Name", "Group", "Sr_ball");
  170. file_sort(ecx, z, v);
  171. /*ecx = 0;
  172. printf_s("%-20s%-20s%-15s%s\n", "Fam", "Name", "Group", "Sr_ball");
  173. for (int i = z + 3; i < z*2+3; i++) {
  174. if (ecx % 4 == 0) {
  175. printf_s("\n");
  176. printf_s("%-20s", v[i].c_str());
  177. }
  178. else
  179. printf_s("%-20s", v[i].c_str());
  180. ecx++;
  181. }*/
  182. break;
  183. }
  184. case 4:
  185. {
  186. count = true;
  187. break;
  188. }
  189. case 5:
  190. {
  191. if (FileExists("Students_List_New.txt") == false) {
  192. printf_s("Vvedite kol dobavl: ");
  193. scanf_s("%d", &n1);
  194. _out.open("Students_List_New.txt");
  195. _out << n1 << '\n';
  196. ecx = n1;
  197. double arr[4];
  198. double sr_ball;
  199. for (int i = 0; i < ecx; i++) {
  200. printf_s("Fam:\t");
  201. scanf_s("%s", str1, 20);
  202. _out << str1 << ' ';
  203. printf_s("Name:\t");
  204. scanf_s("%s", str1, 20);
  205. _out << str1 << ' ';
  206. printf_s("Patronymic:\t");
  207. scanf_s("%s", str1, 20);
  208. printf_s("Year:\t");
  209. scanf_s("%d", &n1);
  210. printf_s("Group:\t");
  211. scanf_s("%d", &n1);
  212. _out << n1 << ' ';
  213. printf_s("Marks:\nMat:\t");
  214. std::cin >> arr[0];
  215. printf_s("Info:\t");
  216. std::cin >> arr[1];
  217. printf_s("Fizika:\t");
  218. std::cin >> arr[2];
  219. printf_s("Ekonom:\t");
  220. std::cin >> arr[3];
  221. sr_ball = (arr[0] + arr[1] + arr[2] + arr[3]) / 4;
  222. _out << sr_ball << '\n';
  223. }
  224. _out.close();
  225. }
  226. else {
  227. std::ifstream _in("Students_List_New.txt");
  228. read_file(_in, s, str);
  229. int a(0), n(0), lim(0), dec(0), cnt(0);
  230. lim = atoi(s[0].c_str()) * 4;
  231. a = atoi(s[0].c_str());
  232. printf_s("Vvedite kol dobav: ");
  233. scanf_s("%d", &n);
  234. a += n;
  235. s[0] = std::to_string(a);
  236. std::string j_ = s[0];
  237. int z1 = atoi(j_.c_str());
  238. z1 *= 4;
  239. _out.open("Students_List_New.txt");
  240. _out << s[0].c_str() << '\n';
  241. for (int i = 0; i <= lim; i++) {
  242. if (cnt != 0 && cnt % 4 != 0) {
  243. _out << s[i].c_str() << ' ';
  244. }
  245. else if (cnt != 0 && cnt % 4 == 0)
  246. _out << s[i].c_str() << '\n';
  247. cnt++;
  248. }
  249. double arr[4];
  250. double sr_ball;
  251. for (int i = 0; i < n; i++) {
  252. printf_s("Fam:\t");
  253. scanf_s("%s", str1, 20);
  254. _out << str1 << ' ';
  255. printf_s("Name:\t");
  256. scanf_s("%s", str1, 20);
  257. _out << str1 << ' ';
  258. printf_s("Patronymic:\t");
  259. scanf_s("%s", str1, 20);
  260. printf_s("Year:\t");
  261. scanf_s("%d", &n1);
  262. printf_s("Group:\t");
  263. scanf_s("%d", &n1);
  264. _out << n1 << ' ';
  265. printf_s("Marks:\nMat:\t");
  266. std::cin >> arr[0];
  267. printf_s("Info:\t");
  268. std::cin >> arr[1];
  269. printf_s("Fizika:\t");
  270. std::cin >> arr[2];
  271. printf_s("Ekonom:\t");
  272. std::cin >> arr[3];
  273. sr_ball = (arr[0] + arr[1] + arr[2] + arr[3]) / 4;
  274. _out << sr_ball << '\n';
  275. }
  276. _out.close();
  277. }
  278. }
  279. break;
  280. }
  281. }
  282. }
  283. else {
  284. std::ofstream out("Students_List.txt");
  285. std::ifstream in("Students_List.txt");
  286. int n, k, n1;
  287. Student *ob, *ob1;
  288. printf_s("Vvedite kol studentov: ");
  289. scanf_s("%d", &n);
  290. if (n <= 0) {
  291. puts("Informacia vvedena neverno"); return;
  292. }
  293. ob = (Student*)malloc(n * sizeof(Student));
  294. if (!ob) { printf_s("Net mesta!\n"); return; }
  295. out << "kol studentov: " << n << '\n';
  296. vvod_stud(ob, 0, n, out);
  297. while (1) {
  298. puts("Enter:\n1 - vivod\n2 - max_sr_ball");
  299. puts("3-sort\n4-delete\n5-add\n0 exit");
  300. scanf_s("%d", &k);
  301. if (!k) break;
  302. switch (k) {
  303. case 1:
  304. if (FileExists("Students_List_New.txt") == true) {
  305. vivod_stud(ob, n);
  306. std::ifstream _in("Students_List_New.txt");
  307. s.clear();
  308. read_file(_in, s, str);
  309. std::string j_ = s[0];
  310. int z1 = atoi(j_.c_str());
  311. z1 *= 4;
  312. int ecx = 0;
  313. for (int i = 1; i <= z1; i++) {
  314. if (ecx % 4 != 0)
  315. printf_s("%-20s", s[i].c_str());
  316. else {
  317. printf_s("\n");
  318. printf_s("%-20s", s[i].c_str());
  319. }
  320. ecx++;
  321. }
  322. printf_s("\n");
  323. }
  324. else
  325. vivod_stud(ob, n);
  326. break;
  327. case 2:
  328. vivod_max_srball(ob, n);
  329. break;
  330. case 3:
  331. sort_fam(ob, n, out);
  332. break;
  333. case 4:
  334. {
  335. n1 = kol_udal_stud(ob, n);
  336. if (!n1) {
  337. printf_s("Net takih studentov\n");
  338. break;
  339. }
  340. ob1 = (Student*)realloc(ob, (n - n1) * sizeof(Student));
  341. if (ob1)
  342. {
  343. ob = ob1;
  344. n = n - n1;
  345. }
  346. else printf_s("Net mesta\n");
  347. break;
  348. }
  349. case 5:
  350. {
  351. char str1[20];
  352. if (FileExists("Students_List_New.txt") == false) {
  353. printf_s("Vvedite kol dobavl: ");
  354. scanf_s("%d", &n1);
  355. _out.open("Students_List_New.txt");
  356. _out << n1 << '\n';
  357. int ecx = n1;
  358. double arr[4];
  359. double sr_ball;
  360. for (int i = 0; i < ecx; i++) {
  361. printf_s("Fam:\t");
  362. scanf_s("%s", str1, 20);
  363. _out << str1 << ' ';
  364. printf_s("Name:\t");
  365. scanf_s("%s", str1, 20);
  366. _out << str1 << ' ';
  367. printf_s("Patronymic:\t");
  368. scanf_s("%s", str1, 20);
  369. printf_s("Year:\t");
  370. scanf_s("%d", &n1);
  371. printf_s("Group:\t");
  372. scanf_s("%d", &n1);
  373. _out << n1 << ' ';
  374. printf_s("Marks:\nMat:\t");
  375. std::cin >> arr[0];
  376. printf_s("Info:\t");
  377. std::cin >> arr[1];
  378. printf_s("Fizika:\t");
  379. std::cin >> arr[2];
  380. printf_s("Ekonom:\t");
  381. std::cin >> arr[3];
  382. sr_ball = (arr[0] + arr[1] + arr[2] + arr[3]) / 4;
  383. _out << sr_ball << '\n';
  384. }
  385. _out.close();
  386. }
  387. else {
  388. std::ifstream _in("Students_List_New.txt");
  389. read_file(_in, s, str);
  390. int a(0), n(0), lim(0), dec(0), cnt(0);
  391. lim = atoi(s[0].c_str()) * 4;
  392. a = atoi(s[0].c_str());
  393. printf_s("Vvedite kol dobav: ");
  394. scanf_s("%d", &n);
  395. a += n;
  396. s[0] = std::to_string(a);
  397. std::string j_ = s[0];
  398. int z1 = atoi(j_.c_str());
  399. z1 *= 4;
  400. _out.open("Students_List_New.txt");
  401. _out << s[0].c_str() << '\n';
  402. for (int i = 0; i <= lim; i++) {
  403. if (cnt != 0 && cnt % 4 != 0) {
  404. _out << s[i].c_str() << ' ';
  405. }
  406. else if (cnt != 0 && cnt % 4 == 0)
  407. _out << s[i].c_str() << '\n';
  408. cnt++;
  409. }
  410. double arr[4];
  411. double sr_ball;
  412. for (int i = 0; i < n; i++) {
  413. printf_s("Fam:\t");
  414. scanf_s("%s", str1, 20);
  415. _out << str1 << ' ';
  416. printf_s("Name:\t");
  417. scanf_s("%s", str1, 20);
  418. _out << str1 << ' ';
  419. printf_s("Patronymic:\t");
  420. scanf_s("%s", str1, 20);
  421. printf_s("Year:\t");
  422. scanf_s("%d", &n1);
  423. printf_s("Group:\t");
  424. scanf_s("%d", &n1);
  425. _out << n1 << ' ';
  426. printf_s("Marks:\nMat:\t");
  427. std::cin >> arr[0];
  428. printf_s("Info:\t");
  429. std::cin >> arr[1];
  430. printf_s("Fizika:\t");
  431. std::cin >> arr[2];
  432. printf_s("Ekonom:\t");
  433. std::cin >> arr[3];
  434. sr_ball = (arr[0] + arr[1] + arr[2] + arr[3]) / 4;
  435. _out << sr_ball << '\n';
  436. }
  437. _out.close();
  438. }
  439. break;
  440. }
  441. }
  442. }
  443. }
  444. }
  445.  
  446. //ф-ция вывода информации с файла
  447. void vivod_file(bool count, int ecx, int z, std::string str, std::string n_, std::vector<std::string> v, std::vector<std::string> s) {
  448. if (count == false) {
  449. if (FileExists("Students_List_New.txt") == true) {
  450. ecx = 0;
  451. std::ifstream _in("Students_List_New.txt");
  452. for (int i = 3; i <= z + 2; i++) {
  453. if (ecx % 4 != 0)
  454. printf_s("%-20s ", v[i].c_str());
  455. else {
  456. printf_s("\n");
  457. printf_s("%-20s ", v[i].c_str());
  458. }
  459. ecx++;
  460. }
  461. s.clear();
  462. read_file(_in, s, str);
  463. std::string j_ = s[0];
  464. int z1 = atoi(j_.c_str());
  465. z1 *= 4;
  466. ecx = 0;
  467. for (int i = 1; i <= z1; i++) {
  468. if (ecx % 4 != 0)
  469. printf_s("%-20s ", s[i].c_str());
  470. else {
  471. printf_s("\n");
  472. printf_s("%-20s ", s[i].c_str());
  473. }
  474. ecx++;
  475. }
  476. }
  477. else {
  478. ecx = 0;
  479. for (int i = 3; i <= z + 2; i++) {
  480. if (ecx % 4 != 0)
  481. printf_s("%-20s ", v[i].c_str());
  482. else {
  483. printf_s("\n");
  484. printf_s("%-20s ", v[i].c_str());
  485. }
  486. ecx++;
  487. }
  488. }
  489. }
  490. else {
  491. if (atoi(n_.c_str()) == 1) {
  492. for (int i = z + 3; i <= z + 4 * 2; i++)
  493. printf_s("%-20s", v[i].c_str());
  494. }
  495.  
  496. ecx = 0;
  497. for (int i = z + 6; i < z*atoi(n_.c_str()); i += 4) {
  498. if (atoi(v[i].c_str()) >= 4) {
  499. for (int j = 3; j >= 0; j--)
  500. printf_s("%-20s ", v[i - j].c_str());
  501. printf_s("\n");
  502. }
  503. ecx++;
  504. if (ecx >= atoi(n_.c_str()))
  505. break;
  506. }
  507. }
  508. }
  509.  
  510. //функция для чтения с файла и вывод тех студентов у которых средний балл больше 4
  511. void file_max_ball(std::string n_, std::vector <std::string> v, int ecx, int z) {
  512. if (atoi(n_.c_str()) == 1) {
  513. for (int i = z + 3; i <= z + 4 * 2; i++)
  514. printf_s("%-20s", v[i].c_str());
  515. }
  516.  
  517. ecx = 0;
  518. for (int i = z + 6; i <= z*atoi(n_.c_str()); i += 4) {
  519. if (atoi(v[i].c_str()) >= 4) {
  520. for (int j = 3; j >= 0; j--)
  521. printf_s("%-20s ", v[i - j].c_str());
  522. printf_s("\n");
  523. }
  524. ecx++;
  525. if (ecx >= atoi(n_.c_str()))
  526. break;
  527. }
  528. }
  529.  
  530. //Ввод одного студента
  531. void vvod(Student*p, std::ofstream &file)
  532. {
  533. printf_s("Fam:\t");
  534. scanf_s("%s", p->ob.fam, 20);
  535. file << p->ob.fam << ' ';
  536. printf_s("Name:\t");
  537. scanf_s("%s", p->ob.name, 20);
  538. file << p->ob.name << ' ';
  539. printf_s("Patronymic:\t");
  540. scanf_s("%s", p->ob.patronymic, 20);
  541. printf_s("Year:\t");
  542. scanf_s("%d", &p->ob.year);
  543. printf_s("Group:\t");
  544. scanf_s("%d", &p->group);
  545. file << p->group << ' ';
  546. printf_s("Marks:\nMat:\t");
  547. scanf_s("%d", &p->marks[0]);
  548. printf_s("Info:\t");
  549. scanf_s("%d", &p->marks[1]);
  550. printf_s("Fizika:\t");
  551. scanf_s("%d", &p->marks[2]);
  552. printf_s("Ekonom:\t");
  553. scanf_s("%d", &p->marks[3]);
  554. p->sr_ball = (p->marks[0] + (double)p->marks[1] + p->marks[2] + p->marks[3]) / 4;
  555. file << p->sr_ball << '\n';
  556. }
  557.  
  558. //чтение сортировки с файла
  559. void file_sort(int ecx, int z, std::vector<std::string> v) {
  560. ecx = 0;
  561. for (int i = z + 3; i < z * 2 + 3; i++) {
  562. if (ecx % 4 == 0) {
  563. printf_s("\n");
  564. printf_s("%-20s", v[i].c_str());
  565. }
  566. else
  567. printf_s("%-20s", v[i].c_str());
  568. ecx++;
  569. }
  570. }
  571.  
  572. //чтение с файла и запись его содержимого в вектор
  573. void read_file(std::ifstream &file, std::vector <std::string> &vec, std::string str) {
  574. if (!file.is_open())
  575. {
  576. printf_s("cannot open file");
  577. }
  578. while (file >> str)
  579. vec.push_back(str);
  580. }
  581.  
  582. //Ввод массива студентов
  583. void vvod_stud(Student*ob, int k1, int k2, std::ofstream &file) {
  584. for (int i = k1; i<k2; i++) vvod(&ob[i], file);
  585. }
  586.  
  587. bool FileExists(const char *fname)
  588. {
  589. return _access(fname, 0) != -1;
  590. }
  591.  
  592. //Вывод одного студента
  593. void vivod(const Student*ob) {
  594. printf_s("%-20s%-20s%-15d%.2lf\n", ob->ob.fam, ob->ob.name, ob->group, ob->sr_ball);
  595. }
  596.  
  597. //Вывод массива студентов
  598. void vivod_stud(const Student*ob, int n)
  599. {
  600. printf_s("%-20s%-20s%-15s%s\n", "Fam", "Name", "Group", "Sr_ball");
  601. for (int i = 0; i<n; i++) vivod(&ob[i]);
  602. }
  603.  
  604. //Функция вывода тех студентов у кого средний балл выше 4.0
  605. void vivod_max_srball(const Student*ob, int n)
  606. { int k = 0;
  607. for (int i = 0; i<n; i++)
  608. {
  609. if (ob[i].sr_ball >= 4.0) {
  610. k++;
  611. vivod(&ob[i]);
  612. }
  613. }
  614. if (k == 0) printf_s("Net takih studentov\n");
  615. }
  616.  
  617. //Функция сортировки по полю фамилия
  618. void sort_fam(Student*ob, int n, std::ofstream &f)
  619. {
  620. int i, j, k;
  621. Student buf;
  622. for (i = 0; i<n; i++)
  623. {
  624. k = i;
  625. for (j = i + 1; j<n; j++)
  626. {
  627. if (ob[k].group > ob[j].group) {
  628. k = j;
  629. buf = ob[i];
  630. ob[i] = ob[k];
  631. ob[k] = buf;
  632. }
  633. }
  634. }
  635. for (i = 0; i<n; i++)
  636. {
  637. k = i;
  638. for (j = i + 1; j<n; j++)
  639. {
  640. if ((ob[k].group == ob[j].group) && (ob[k].sr_ball < ob[j].sr_ball)) {
  641. k = j;
  642. buf = ob[i];
  643. ob[i] = ob[k];
  644. ob[k] = buf;
  645. }
  646. }
  647. }
  648. for (i = 0; i < n; i++)
  649. f << (&ob[i])->ob.fam << ' ' << (&ob[i])->ob.name << ' ' << (&ob[i])->group << ' ' << (&ob[i])->sr_ball << '\n';
  650. f.close();
  651. }
  652.  
  653. //Функция считает тех студентов, у кого средний балл ниже 4.0 и смещает их.
  654. int kol_udal_stud(Student*ob, int n)
  655. {
  656. int k = 0;
  657. for (int i = 0; i<n - k; i++)
  658. {
  659. if (ob[i].sr_ball<4.0)
  660. {
  661. k++;
  662. for (int j = i; j<n - k; j++) ob[j] = ob[j + 1];
  663. i--;
  664. }
  665. }
  666. return k;
  667. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement