Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- else {
- RecordBook *next, *prev = listOfStudents;
- current = listOfStudents;
- while (current->next && current->index != pos) {
- prev = current;
- current = current->next;
- }
- next = current->next;
- prev->next = next;
- //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- ***//Вот здесь срабатывает ошибка***
- delete current;
- current = NULL;
- }
- #include <iostream>
- #include <stdio.h>
- #include <string>
- using namespace std;
- class RecordBook {
- public:
- void setName(string);
- string getName();
- void setLastName(string);
- string getLastName();
- void setScores(int, int, int, bool, bool, bool);
- void showScores();
- private:
- string name;
- string lastName;
- int Math;
- int MathAnalysis;
- int Programming;
- bool English;
- bool PhysEducation;
- bool History;
- //Navigation
- size_t index = 0;
- RecordBook *next = nullptr;
- friend class Group;
- };
- class Group {
- public:
- //Добавить студента
- void addStudent(string, string, int=5, int=5, int=5, bool=true, bool=true, bool=true);
- //Найти студента и вывести оценки
- void findStudent(string);
- //Вставить зачетную книжку в заданный индекс
- void insert(RecordBook &, size_t = 0);
- //Удалить зачетную книжку по заданному индексу
- void erase(size_t = 0);
- private:
- //Динамический список студентов, который указывает на последнего студента
- RecordBook *listOfStudents;
- //Количество студентов
- size_t countOfElements = 0;
- //Создать новый узел для студента в динамическом списке
- void createOfNode(RecordBook &);
- //Обновить индексы динамического списка
- void updateOfIndex();
- } mx101;
- int main() {
- setlocale(0, "");
- mx101.addStudent("Al", "As");
- mx101.addStudent("Petya", "Petrov");
- RecordBook student;
- student.setName("vasya");
- student.setLastName("XXX");
- student.setScores(2, 3, 4, 1, 1, 0);
- mx101.insert(student, 1);
- mx101.erase(1);
- return 0;
- }
- void RecordBook::setName(string name1) {
- name = name1;
- }
- string RecordBook::getName() {
- return name;
- }
- void RecordBook::setLastName(string lastName1) {
- lastName = lastName1;
- }
- string RecordBook::getLastName() {
- return lastName;
- }
- void RecordBook::setScores(int M, int MA, int pr, bool En, bool Ph, bool Hi) {
- Math = M;
- MathAnalysis = MA;
- Programming = pr;
- English = En;
- PhysEducation = Ph;
- History = Hi;
- }
- void RecordBook::showScores() {
- printf("Name: %s, Last Name: %sn", name.c_str(), lastName.c_str());
- printf("Math: %d, Math analysis: %d, Programming: %dn", Math, MathAnalysis, Programming);
- printf(English ? "English: passedn" : "English: didn't passedn");
- printf(PhysEducation ? "PhysEducation: passedn" : "PhysEducation didn't passedn");
- printf(History ? "History: passedn" : "History: didn't passedn");
- cout << endl;
- }
- void Group::addStudent(string name, string lastName, int a, int b, int c, bool d, bool e, bool f) {
- RecordBook student;
- student.setName(name);
- student.setLastName(lastName);
- student.setScores(a, b, c, d, e, f);
- createOfNode(student);
- }
- void Group::createOfNode(RecordBook &elem) {
- RecordBook *current = new RecordBook;
- if (countOfElements == 0) {
- *current = elem;
- current->next = nullptr;
- current->index = countOfElements++;
- listOfStudents = current;
- }
- else {
- *current = elem;
- current->index = countOfElements++;
- current->next = listOfStudents;
- listOfStudents = current;
- }
- }
- void Group::findStudent(string str) {
- RecordBook *current = listOfStudents;
- do {
- if (!current->getName().compare(str) || !current->getLastName().compare(str)) {
- current->showScores();
- return;
- }
- else {
- current = current->next;
- }
- } while (current);
- cout << "Ничего не найдено" << endl;
- }
- void Group::insert(RecordBook &elem, size_t pos) {
- if (pos < 0 || pos > countOfElements) cout << "Неверный индекс" << endl;
- else {
- RecordBook *current = NULL;
- if (pos == 0) {
- current = listOfStudents;
- while (current->next) {
- current = current->next;
- }
- current->next = &elem;
- }
- else if (pos == countOfElements) {
- current = new RecordBook;
- current = &elem;
- current->next = listOfStudents;
- listOfStudents = current;
- }
- else {
- RecordBook *next = NULL;
- current = listOfStudents;
- while (current->next && current->index != pos) {
- current = current->next;
- }
- RecordBook *item = new RecordBook;
- next = current->next;
- item = &elem;
- item->next = next;
- current->next = item;
- }
- countOfElements++;
- updateOfIndex();
- }
- }
- void Group::erase(size_t pos) {
- if (pos < 0 || pos > countOfElements - 1) cout << "Неверный индекс" << endl;
- else {
- RecordBook *current;
- if (pos == 0) {
- current = listOfStudents;
- while (current->next->next) {
- current = current->next;
- }
- delete current->next;
- current->next = NULL;
- }
- else if (pos == countOfElements - 1) {
- current = listOfStudents;
- listOfStudents = listOfStudents->next;
- delete current;
- current = NULL;
- }
- else {
- RecordBook *next, *prev = listOfStudents;
- current = listOfStudents;
- while (current->next && current->index != pos) {
- prev = current;
- current = current->next;
- }
- next = current->next;
- prev->next = next;
- //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- ***//Вот здесь срабатывает ошибка***
- delete current;
- current = NULL;
- }
- }
- }
- void Group::updateOfIndex() {
- RecordBook *current = listOfStudents;
- size_t acc = countOfElements - 1;
- while (current->next) {
- current->index = acc--;
- current = current->next;
- }
- }
Add Comment
Please, Sign In to add comment