Guest User

Untitled

a guest
Mar 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. else {
  2. RecordBook *next, *prev = listOfStudents;
  3. current = listOfStudents;
  4.  
  5. while (current->next && current->index != pos) {
  6. prev = current;
  7. current = current->next;
  8. }
  9.  
  10. next = current->next;
  11. prev->next = next;
  12.  
  13. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  14. ***//Вот здесь срабатывает ошибка***
  15. delete current;
  16. current = NULL;
  17. }
  18.  
  19. #include <iostream>
  20. #include <stdio.h>
  21. #include <string>
  22.  
  23. using namespace std;
  24.  
  25.  
  26. class RecordBook {
  27. public:
  28. void setName(string);
  29. string getName();
  30. void setLastName(string);
  31. string getLastName();
  32. void setScores(int, int, int, bool, bool, bool);
  33. void showScores();
  34.  
  35. private:
  36. string name;
  37. string lastName;
  38.  
  39. int Math;
  40. int MathAnalysis;
  41. int Programming;
  42.  
  43. bool English;
  44. bool PhysEducation;
  45. bool History;
  46.  
  47. //Navigation
  48. size_t index = 0;
  49. RecordBook *next = nullptr;
  50.  
  51. friend class Group;
  52.  
  53.  
  54. };
  55.  
  56. class Group {
  57. public:
  58. //Добавить студента
  59. void addStudent(string, string, int=5, int=5, int=5, bool=true, bool=true, bool=true);
  60. //Найти студента и вывести оценки
  61. void findStudent(string);
  62. //Вставить зачетную книжку в заданный индекс
  63. void insert(RecordBook &, size_t = 0);
  64. //Удалить зачетную книжку по заданному индексу
  65. void erase(size_t = 0);
  66.  
  67. private:
  68. //Динамический список студентов, который указывает на последнего студента
  69. RecordBook *listOfStudents;
  70. //Количество студентов
  71. size_t countOfElements = 0;
  72.  
  73. //Создать новый узел для студента в динамическом списке
  74. void createOfNode(RecordBook &);
  75. //Обновить индексы динамического списка
  76. void updateOfIndex();
  77.  
  78. } mx101;
  79.  
  80. int main() {
  81. setlocale(0, "");
  82.  
  83. mx101.addStudent("Al", "As");
  84. mx101.addStudent("Petya", "Petrov");
  85.  
  86. RecordBook student;
  87. student.setName("vasya");
  88. student.setLastName("XXX");
  89. student.setScores(2, 3, 4, 1, 1, 0);
  90.  
  91. mx101.insert(student, 1);
  92. mx101.erase(1);
  93.  
  94. return 0;
  95. }
  96.  
  97. void RecordBook::setName(string name1) {
  98. name = name1;
  99. }
  100.  
  101. string RecordBook::getName() {
  102. return name;
  103. }
  104.  
  105. void RecordBook::setLastName(string lastName1) {
  106. lastName = lastName1;
  107. }
  108.  
  109. string RecordBook::getLastName() {
  110. return lastName;
  111. }
  112.  
  113. void RecordBook::setScores(int M, int MA, int pr, bool En, bool Ph, bool Hi) {
  114. Math = M;
  115. MathAnalysis = MA;
  116. Programming = pr;
  117.  
  118. English = En;
  119. PhysEducation = Ph;
  120. History = Hi;
  121. }
  122.  
  123. void RecordBook::showScores() {
  124. printf("Name: %s, Last Name: %sn", name.c_str(), lastName.c_str());
  125. printf("Math: %d, Math analysis: %d, Programming: %dn", Math, MathAnalysis, Programming);
  126. printf(English ? "English: passedn" : "English: didn't passedn");
  127. printf(PhysEducation ? "PhysEducation: passedn" : "PhysEducation didn't passedn");
  128. printf(History ? "History: passedn" : "History: didn't passedn");
  129. cout << endl;
  130. }
  131.  
  132. void Group::addStudent(string name, string lastName, int a, int b, int c, bool d, bool e, bool f) {
  133. RecordBook student;
  134. student.setName(name);
  135. student.setLastName(lastName);
  136. student.setScores(a, b, c, d, e, f);
  137.  
  138. createOfNode(student);
  139. }
  140.  
  141. void Group::createOfNode(RecordBook &elem) {
  142. RecordBook *current = new RecordBook;
  143.  
  144. if (countOfElements == 0) {
  145. *current = elem;
  146. current->next = nullptr;
  147. current->index = countOfElements++;
  148.  
  149. listOfStudents = current;
  150. }
  151. else {
  152. *current = elem;
  153. current->index = countOfElements++;
  154. current->next = listOfStudents;
  155.  
  156. listOfStudents = current;
  157. }
  158. }
  159.  
  160. void Group::findStudent(string str) {
  161. RecordBook *current = listOfStudents;
  162.  
  163. do {
  164. if (!current->getName().compare(str) || !current->getLastName().compare(str)) {
  165. current->showScores();
  166.  
  167. return;
  168. }
  169. else {
  170. current = current->next;
  171. }
  172. } while (current);
  173.  
  174. cout << "Ничего не найдено" << endl;
  175. }
  176.  
  177. void Group::insert(RecordBook &elem, size_t pos) {
  178. if (pos < 0 || pos > countOfElements) cout << "Неверный индекс" << endl;
  179. else {
  180. RecordBook *current = NULL;
  181.  
  182. if (pos == 0) {
  183. current = listOfStudents;
  184.  
  185. while (current->next) {
  186. current = current->next;
  187. }
  188.  
  189. current->next = &elem;
  190. }
  191. else if (pos == countOfElements) {
  192. current = new RecordBook;
  193.  
  194. current = &elem;
  195. current->next = listOfStudents;
  196. listOfStudents = current;
  197. }
  198. else {
  199. RecordBook *next = NULL;
  200. current = listOfStudents;
  201.  
  202. while (current->next && current->index != pos) {
  203. current = current->next;
  204. }
  205.  
  206. RecordBook *item = new RecordBook;
  207. next = current->next;
  208.  
  209. item = &elem;
  210. item->next = next;
  211.  
  212. current->next = item;
  213. }
  214.  
  215. countOfElements++;
  216. updateOfIndex();
  217. }
  218. }
  219.  
  220. void Group::erase(size_t pos) {
  221. if (pos < 0 || pos > countOfElements - 1) cout << "Неверный индекс" << endl;
  222. else {
  223. RecordBook *current;
  224.  
  225. if (pos == 0) {
  226. current = listOfStudents;
  227.  
  228. while (current->next->next) {
  229. current = current->next;
  230. }
  231.  
  232. delete current->next;
  233. current->next = NULL;
  234. }
  235. else if (pos == countOfElements - 1) {
  236. current = listOfStudents;
  237. listOfStudents = listOfStudents->next;
  238.  
  239. delete current;
  240. current = NULL;
  241. }
  242. else {
  243. RecordBook *next, *prev = listOfStudents;
  244. current = listOfStudents;
  245.  
  246. while (current->next && current->index != pos) {
  247. prev = current;
  248. current = current->next;
  249. }
  250.  
  251. next = current->next;
  252. prev->next = next;
  253.  
  254. //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  255. ***//Вот здесь срабатывает ошибка***
  256. delete current;
  257. current = NULL;
  258. }
  259.  
  260.  
  261. }
  262. }
  263.  
  264. void Group::updateOfIndex() {
  265. RecordBook *current = listOfStudents;
  266. size_t acc = countOfElements - 1;
  267.  
  268. while (current->next) {
  269. current->index = acc--;
  270. current = current->next;
  271. }
  272. }
Add Comment
Please, Sign In to add comment