uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

student.cpp (3451B)


      1 #include "student.hpp"
      2 
      3 Student::Student(const char *id, const std::string& name)
      4     :id(conv<char>(id, len(id))), name(name), semester(1),
      5     pcourses(0), sc(nullptr), nsc(0) {}
      6 
      7 Student::Student(const char *id, const std::string& name,
      8         const unsigned int semester)
      9     :id(conv<char>(id, len(id))), name(name), semester(semester),
     10     pcourses(0), sc(nullptr), nsc(0) {}
     11 
     12 Student::Student(const char *id, const std::string& name,
     13         const unsigned int semester,
     14         const unsigned int pcourses, const float *grades)
     15     :id(conv<char>(id, len(id))), name(name), semester(semester),
     16     pcourses(pcourses), grades(conv<float>(grades, pcourses)),
     17     sc(nullptr), nsc(0) {}
     18 
     19 Student::Student(const Student& s)
     20     :name(s.name), semester(s.semester), pcourses(s.pcourses)
     21 {
     22     this->id = conv<char>(s.id, len(s.id));
     23     this->grades = conv<float>(s.grades, s.pcourses);
     24     if (s.nsc <= 0)
     25     {
     26         nsc = 0;
     27         sc = nullptr;
     28     }
     29     else
     30     {
     31         this->sc = new Course *[s.nsc];
     32         std::memcpy(sc, s.sc, sizeof(s.sc) * s.nsc);
     33     }
     34 }
     35 
     36 Student::~Student()
     37 {
     38     if (this->id != nullptr) delete[] this->id;
     39     if (this->grades != nullptr) delete[] this->grades;
     40     if (this->sc != nullptr) delete[] this->sc;
     41 }
     42 
     43 void
     44 Student::operator+= (Course *c)
     45 {
     46     Course **tmp = resize<Course *>(sc, nsc);
     47     tmp[nsc] = c;
     48     sc = tmp;
     49     nsc++;
     50 }
     51 
     52 Student
     53 Student::operator= (const Student& s)
     54 {
     55     if (this == &s) return *this;
     56     this->id = conv<char>(s.id, len(s.id)); 
     57     this->name = s.name;
     58     this->semester = s.semester;
     59     this->pcourses = s.pcourses;
     60     if (s.grades != nullptr)
     61         this->grades = conv<float>(s.grades, s.pcourses);
     62     if (s.sc != nullptr)
     63     {
     64         this->nsc = s.nsc;
     65         this->sc = s.sc;
     66     }
     67     return *this;
     68 }
     69 
     70 const std::string&
     71 Student::get_name() const
     72 {
     73     return name;
     74 }
     75 
     76 void
     77 Student::set_id(const char *id)
     78 {
     79     if (this->id != nullptr) delete[] this->id;
     80     this->id = conv<char>(id, len(id));
     81 }
     82 
     83 void
     84 Student::set_name(const std::string& name)
     85 {
     86     this->name = name;
     87 }
     88 
     89 void
     90 Student::set_semester(const unsigned int semester)
     91 {
     92     this->semester = semester;
     93 }
     94 
     95 void
     96 Student::set_pcourses(const unsigned int pcourses)
     97 {
     98     this->pcourses = pcourses;
     99 }
    100 
    101 void
    102 Student::set_grades(const float *grades)
    103 {
    104     this->grades = conv<float>(grades, pcourses);
    105 }
    106 
    107 void
    108 Student::set_num_submitted_courses(const unsigned nsc)
    109 {
    110     this->nsc = nsc;
    111 }
    112 
    113 void
    114 Student::set_submitted_courses(Course **sc)
    115 {
    116     if (sc != nullptr)
    117     {
    118         if (this->sc != nullptr) delete[] this->sc;
    119         this->sc = new Course *[nsc];
    120         std::memcpy(this->sc, sc, sizeof(Course *) * nsc);
    121     }
    122 }
    123 
    124 void
    125 Student::add_grade(const float grade)
    126 {
    127     float *tmp = resize<float>(grades, pcourses);
    128     tmp[pcourses] = grade;
    129     grades = tmp;
    130     pcourses++;
    131 }
    132 
    133 void
    134 Student::detailed_print() const
    135 {
    136     if (grades != nullptr)
    137     {
    138         for (unsigned int i = 0; i < pcourses; i++)
    139         {
    140             std::cout << "Course " << i+1 << ": ";
    141             std::cout << grades[i] << std::endl;        
    142         }
    143         std::cout << "Average grade: " << std::setprecision(2) << avg() << std::endl;
    144     }
    145 }
    146 
    147 float
    148 Student::avg() const
    149 {
    150     if (grades != nullptr)
    151     {
    152         float sum = 0;
    153         for (unsigned int i = 0; i < pcourses; i++)
    154             sum += grades[i];
    155         float average = sum / pcourses;
    156         return average;
    157     }
    158     else return 0.0f;
    159 }