Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.34 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<iostream>
  3. #include<ctime>
  4. #include<fstream>
  5. #include<string>
  6. #include<cstdio>
  7. #include<Windows.h>
  8. using namespace std;
  9. //params
  10. struct Person
  11. {
  12. char FIO[100] = "";
  13. int yearofborn, groupnumber, phys, math, IT, chem;
  14. double averageball;
  15. }Student[100];
  16. typedef void(*Function)();
  17. string pathtofile = "stud_list.txt";
  18. ofstream fout;
  19. ifstream fin;
  20. FILE* F;
  21. //skeleton
  22. void CheckFile(Function);
  23. void ListView();//2
  24. void Personadd();//3
  25. void PersonChanges();
  26. void FileClean();
  27. int NumOfStudents();
  28. void TaskSolution();//4
  29. void FromFileToStruct();
  30. void menuUI();
  31. void menuPointsUI(Function);
  32. //program
  33. void main()
  34. {
  35. SetConsoleOutputCP(1251);
  36. SetConsoleCP(1251);
  37. setlocale(0, "");
  38. menuUI();
  39. }
  40. //works
  41. void menuUI()
  42. {
  43. bool exit = false;
  44. int choose;
  45. do
  46. {
  47. cout << "Press any key to escape or :\n1)Изменение содержимого файла.\n2)Просмотр.\n3)Индивидуальное задание." << endl;
  48. cin >> choose;
  49. switch (choose)
  50. {
  51. case 1://Содержимое
  52. {
  53. cout << "1)Добавить запись\n2)Отредактировать запись\n3)Очистка файла" << endl;
  54. int choose1;
  55. cin >> choose1;
  56. switch (choose1)
  57. {
  58. case 1:
  59. {
  60. CheckFile(Personadd);
  61. break;
  62. }
  63. case 2:
  64. {
  65. CheckFile(PersonChanges);
  66. break;
  67. }
  68. case 3:
  69. {
  70. CheckFile(FileClean);
  71. break;
  72. }
  73.  
  74. }
  75.  
  76. break; }
  77. case 2://Просмотр
  78. {
  79. system("cls");
  80. CheckFile(ListView);
  81. cout << "Для выхода в меню введите что-нибудь" << endl;
  82. cin.ignore(1000, '\n'); getchar();
  83. system("cls"); cin.ignore(1000, '\n');
  84. break; }
  85. case 3://Задание
  86. {
  87. system("cls");
  88. CheckFile(TaskSolution);
  89. cout << "Для выхода в меню введите что-нибудь" << endl;
  90. cin.ignore(1000, '\n'); getchar();
  91. system("cls"); cin.ignore(1000, '\n');
  92. break; }
  93. default:
  94. {
  95. cout << "Escape" << endl;
  96. exit = true;
  97.  
  98. }
  99. }
  100. } while (!exit);
  101. }
  102. //works
  103. void menuPointsUI(Function X)
  104. {
  105. bool exitfrompoint = false;
  106. do
  107. {
  108. X();
  109. cout << "Продолжить/Выйти в меню(y/any other key)" << endl;
  110. if (getchar() == 'y')
  111. {
  112. exitfrompoint = false;
  113. }
  114. else
  115. {
  116. getchar();
  117. exitfrompoint = true;
  118. break;
  119. }
  120. } while (!exitfrompoint);
  121. }
  122. //works
  123. void FromFileToStruct()
  124. {
  125. int numofstudents = NumOfStudents();
  126. for (int i = 0; i < numofstudents; i++)
  127. {
  128. int size = 0;
  129. char t;
  130. do
  131. {
  132. t = fgetc(F);
  133. size++;
  134. } while ((int)t >57 || (int)t <48);
  135. fseek(F, -size, SEEK_CUR);
  136. fgets(Student[i].FIO, size, F);
  137. fscanf(F, "%i %i %i %i %i %i %lf\n",&Student[i].yearofborn, &Student[i].groupnumber, &Student[i].phys, &Student[i].math, &Student[i].IT, &Student[i].chem, &Student[i].averageball);
  138. }
  139. }
  140. //works
  141. void Personadd()
  142. {
  143. FILE* F;
  144. F = fopen("stud_list.txt", "a+");
  145. for (int i = NumOfStudents();; i++)
  146. {
  147. cout << "Запись студента с номером:" << i + 1 << endl;
  148. cout << "ФИО" << endl; cin.ignore(500, '\n'); gets_s(Student[i].FIO);
  149. cout << "Год рождения" << endl; cin >> Student[i].yearofborn;
  150. cout << "Номер группы" << endl; cin >> Student[i].groupnumber;
  151. cout << "Физика" << endl; cin >> Student[i].phys;
  152. cout << "Математика" << endl; cin >> Student[i].math;
  153. cout << "ОАиП" << endl; cin >> Student[i].IT;
  154. cout << "Химия" << endl; cin >> Student[i].chem;
  155. Student[i].averageball = (Student[i].phys + Student[i].math + Student[i].IT + Student[i].chem) / 4.;
  156. fprintf(F, "%s %i %i %i %i %i %i %f\n", Student[i].FIO, Student[i].yearofborn, Student[i].groupnumber, Student[i].phys, Student[i].math, Student[i].IT, Student[i].chem, Student[i].averageball);
  157. cout << "Продолжить или выйти(y/any other key)" << endl;
  158. _flushall();
  159. if (getchar() == 'y')
  160. {
  161. continue;
  162. }
  163. else
  164. {
  165. getchar();
  166. system("cls");
  167. return;
  168. }
  169. }
  170. fclose(F);
  171. }
  172. //in progress
  173. void PersonChanges()
  174. {
  175. FromFileToStruct();
  176. for (int i = 0; i < NumOfStudents(); i++)
  177. {
  178. cout << "ID:" << i + 1 << "\t" << Student[i].FIO<<endl;
  179. }
  180. cout << endl;
  181. cout << "Укажите ID студента,данные которого необходимо изменить:" << endl; int ID; cin >> ID;
  182. --ID;
  183. cout << "Что хотите изменить?1)ФИО\n2)Год рождения\n3)Номер группы\n4)Оценку по физике\n5)Оценку по математике\n6)Оценку по программированию\n7)Оценку по химии" << endl;
  184. int choice; cin >> choice;
  185. long size = 0;
  186. char* buff = new char[1000]; string b;
  187. char* buff2 = new char[1000];
  188. char* requiredstring = new char[1000];
  189. switch (choice)
  190. {
  191. case 1:
  192. {
  193. ifstream fin;
  194. for (int i = 0; i < 8 * ID; i++)
  195. {
  196. getline(fin, b);
  197. }
  198. cout << b;
  199. break;
  200. }
  201. case 2:
  202. {
  203. break;
  204. }
  205. case 3:
  206. {
  207. break;
  208. }
  209. case 4:
  210. {
  211. break;
  212. }
  213. case 5:
  214. {
  215. break;
  216. }
  217. case 6:
  218. {
  219. break;
  220. }
  221. case 7:
  222. {
  223. break;
  224. }
  225. }
  226. delete[]buff;
  227. delete[]buff2;
  228. delete[]requiredstring;
  229. }
  230. //works
  231. void ListView()
  232. {
  233. int numofstudents = NumOfStudents();
  234. cout << "Кол-во студентов в файле:" << numofstudents << endl;
  235. int ch; cout << "Способ показа информации:\n1)В чистом виде(как в файле)\n2)Запись из файла в структуру и чтение из неё" << endl;
  236. cin >> ch;
  237. switch (ch)
  238. {
  239. case 1:
  240. {
  241. string tmp;
  242. ifstream fin;
  243. fin.open(pathtofile);
  244. while (getline(fin, tmp))
  245. {
  246. cout << tmp << endl;
  247. }
  248. tmp = "";
  249. break;
  250. }
  251. case 2:
  252. {
  253. FromFileToStruct();
  254. cout << "Вывести на экран\n1)Всех студентов.\n2)Определённое кол-во\n3)Студента под номером" << endl;
  255. int vibor;
  256. ifstream fin;
  257. cin >> vibor;
  258. switch (vibor)
  259. {
  260. case 1:
  261. {
  262. for (int i = 0; i < numofstudents; i++)
  263. {
  264. cout << "ФИО:" << Student[i].FIO << endl << "Год рождения:" << Student[i].yearofborn << endl << "Номер группы:" << Student[i].groupnumber << endl << "Математика:" << Student[i].math << endl << "Физика:" << Student[i].phys << endl << "ОАиП:" << Student[i].IT << endl << "Химия:" << Student[i].chem << endl << "Средний балл:" << Student[i].averageball << endl;
  265. cout << "-----------------------------------------------------------------------------------------------------" << endl;
  266. }
  267. break;
  268. }
  269. case 2:
  270. {
  271. int x = 0; int count; cout << "Укажите количество:"; cin >> count;
  272. for (int i = 0; i < count; i++)
  273. {
  274. cout << "ФИО:" << Student[i].FIO << endl << "Год рождения:" << Student[i].yearofborn << endl << "Номер группы:" << Student[i].groupnumber << endl << "Математика:" << Student[i].math << endl << "Физика:" << Student[i].phys << endl << "ОАиП:" << Student[i].IT << endl << "Химия:" << Student[i].chem << endl << "Средний балл:" << Student[i].averageball << endl;
  275. cout << "-----------------------------------------------------------------------------------------------------" << endl;
  276. }
  277. break;
  278. }
  279. case 3:
  280. {
  281. for (int i = 0; i < NumOfStudents(); i++)
  282. {
  283. cout <<"Number "<<i+1<<"\t"<<Student[i].FIO<<endl;
  284. }
  285. int number; cout << "Укажите номер:"; cin >> number;
  286. --number;
  287. cout << "ФИО:" << Student[number].FIO << endl << "Год рождения:" << Student[number].yearofborn << endl << "Номер группы:" << Student[number].groupnumber << endl << "Математика:" << Student[number].math << endl << "Физика:" << Student[number].phys << endl << "ОАиП:" << Student[number].IT << endl << "Химия:" << Student[number].chem << endl << "Средний балл:" << Student[number].averageball << endl;
  288. cout << "-----------------------------------------------------------------------------------------------------" << endl;
  289. break;
  290. }
  291. }
  292. break;
  293. }
  294. }
  295.  
  296.  
  297. }
  298. //works
  299. void TaskSolution()
  300. {
  301. FromFileToStruct();
  302. for (int i = 0; i < NumOfStudents(); i++)
  303. {
  304. if (Student[i].phys == 5 || Student[i].phys == 4 && Student[i].math > 8 && Student[i].IT > 8 && Student[i].chem > 8)
  305. {
  306. cout << "ФИО:" << Student[i].FIO << endl << "Год рождения:" << Student[i].yearofborn << endl << "Номер группы:" << Student[i].groupnumber << endl << "Математика:" << Student[i].math << endl << "Физика:" << Student[i].phys << endl << "ОАиП:" << Student[i].IT << endl << "Химия:" << Student[i].chem << endl << "Средний балл:" << Student[i].averageball << endl;
  307. }
  308. }
  309. }
  310. //works
  311. void CheckFile(Function X)
  312. {
  313. system("cls");
  314. if ((F = fopen("stud_list.txt", "a+") )== NULL)
  315. {
  316. cout << "Не удаётся открыть/использовать файл" << endl;
  317. }
  318. else
  319. {
  320. X(); fclose(F);
  321. }
  322. }
  323. //works
  324. void FileClean()
  325. {
  326. fout.open(pathtofile, ios::trunc);
  327. fout.close();
  328. }
  329. //works
  330. int NumOfStudents()
  331. {
  332. string tmp;
  333. ifstream fin("stud_list.txt");
  334. int num = 0;
  335. if (fin.is_open())
  336. {
  337. while (getline(fin, tmp))
  338. {
  339. num++;
  340. }
  341. }
  342. tmp = "";
  343. fin.close();
  344. return num;
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement