Victoria_Listing2

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
3.6 kB
4
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 -выпускник

};

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

void printArray(Student*, int);
void printByGroup(Student*, int, const char*);
void up(Student*, int);

int main()
{   const int size = 6;
    Student arr[size] = {  Student(), 
                        Student("Ivanov", "DKP"),
                        Student("Petrova", "DKP"),
                        Student("Gribas", "DMM-1", 4),
                        Student("Rudina", "DMM-2", 5),
    };
    printArray(arr, size);
    std::cout<<"DMM:\n";
    printByGroup(arr, size, "DMM");
    
    std::cout<<"\nenter data for 1st student:";
    std::cin>>arr[0];
    printArray(arr, size);
    
    std::cout<<"\nAfter 1st up:\n";
    up(arr, size);
    printArray(arr, size);
    std::cout<<"\nAfter 2nd up:\n";    
    up(arr, size);
    printArray(arr, size);
    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<<" ";
    
    if(year == 0)
        s<<"-";
    else if(year == -1)
        s<<"graduate";
    else
        s<< 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;
}

void printArray(Student* a, int n)
{
    for(int i=0; i<n; i++)
        std::cout<<a[i]<<std::endl;    
    
}

void printByGroup(Student* a, int n, const char* group)
{
    for(int i=0; i<n; i++)
        if(strstr(a[i].getGroup(), group)!=0) std::cout<<a[i]<<"; ";
    std::cout<<std::endl;
    
}

void up(Student* a, int n)
{
  for(int i=0; i<n; i++)
    a[i].addYear();
}