Victoria_Listing1

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
4.1 kB
5
Indexable
Never
//организовать односвязный список студентов
//в качестве односвязного списка использовать очередь
#include <iostream>
#include <cstring>
#include <sstream>

class Student
{
public:
    Student();
    Student(const char*, const char*);
    Student(const char*, const char*, int);

    void setName(const char*);
    void setGroup(const char*);
    void setYear(int);
    
    const char* getName() const;
    const char* getGroup() const;
    int   getYear() const;
    
    std::string toStr() const; 
    void addYear();
    
private:

    char name[15];
    char group[6];
    int year;//1, 2, 3, 4 - курсы, 0 - не инициализировано, -1 -выпускник

};

struct QNode {
	Student data;
	QNode* next;
	QNode(const Student& d)
	{
		data = d;
		next = NULL;
	}
};

struct Queue
{
    QNode *front, *rear;
    void enQueue(const Student&);
    void deQueue();
    void display() const;
    Queue();
    ~Queue();
};


std::ostream& operator<<(std::ostream&, const Student&);
std::istream& operator>>(std::istream&, Student&);

int main()
{   
    Queue studentsList;
    
    studentsList.display();
    
    studentsList.enQueue(Student("Ivanov", "DKP"));
    studentsList.enQueue(Student("Petrova", "DKP"));
    studentsList.enQueue(Student("Petrov", "DKP", 3));
    studentsList.enQueue(Student("Ivanov", "DKP", 2));
    studentsList.enQueue(Student("Rudina", "DMM-2", 5));
    
    studentsList.display();
    
    studentsList.deQueue();
    studentsList.display();
    
    return 0;
}

//конструкторы
Student::Student()
{   strcpy(name, "Anonimus");
    strcpy(group, "unknown");
    year = 0;
}

Student::Student(const char* nameStud, const char* groupStud)
{   
    strcpy(name, nameStud);
    strcpy(group, groupStud);
    year = 1;
}

Student::Student(const char* nameStud, const char* groupStud, int y)
{   
    strcpy(name, nameStud);
    strcpy(group, groupStud);
    year = (y>0 && y<=4)? y:1;
}

//интерфейсные функции

//функции типа set:
void Student::setName(const char* newName)
{
    strcpy(name, newName);
}

void Student::setGroup(const char* newGroup)
{
    strcpy(group, newGroup);
}

void Student::setYear(int newYear)
{
    if (newYear>=1 && newYear<=4 || newYear==-1) year = newYear;
    else if (newYear==5) year = -1;
}

//функции типа get:
const char* Student::getName() const
{
    return name;
}

const char* Student::getGroup() const
{
    return group;
}

int   Student::getYear() const
{
    return year;
}

//прочие открытые функции
std::string Student::toStr() const
{
    std::stringstream s; 
    s<<name<<" "<<group<<" "<<year<<" course";
    return s.str();
}

void Student::addYear()
{
    setYear(year+1);
}

std::ostream& operator<<(std::ostream& out, const Student& person)
{
    out<<person.toStr();
    return out;
}

std::istream& operator>>(std::istream& in, Student& person)
{
    char tempN[15], tempG[6];
    int tempYear;
    std::cout<<"\nEnter name, group, grade: ";
    in>>tempN>>tempG>>tempYear;
    person.setName(tempN);
    person.setGroup(tempG);
    person.setYear(tempYear);
    return in;
}
Queue::Queue() { front = rear = NULL; }

Queue::~Queue() 
{
    QNode * temp;
    while(front != NULL){
        temp = front;
        front = front->next;
        delete temp;
    }
    rear = NULL;
}

void Queue::enQueue(const Student& student)
{
	QNode* temp = new QNode(student);

	if (rear == NULL) {
		front = rear = temp;
		return;
	}
	rear->next = temp;
	rear = temp;
}
void Queue::deQueue()
{
	if (front == NULL)
		return;

	QNode* temp = front;
	front = front->next;

	if (front == NULL)
		rear = NULL;

	delete (temp);
}
void Queue::display() const
{
    if(front == NULL) {std::cout<<"Queue is empty\n";
                        return;
                       }
    QNode *current = front;
    while (current != NULL)
    {
        std::cout<<current->data<<std::endl;
        current = current->next;
    }
    std::cout<<std::endl;
}