Untitled
unknown
c_cpp
3 years ago
6.5 kB
4
Indexable
Never
// [YOUR CODE WILL BE PLACED HERE] #include <iostream> #include <cmath> #include <string> #include <map> #include <vector> #include <algorithm> class Grade{ friend std::ostream& operator<<(std::ostream& os, const Grade& g); friend Grade Adjust(const Grade g, int added); public: Grade(); ~Grade(); Grade(const char* s); // for one character. e.g. "A" Grade(const std::string& s); // for a string. e.g. "A+" Grade(int i); std::string GetRank() const; int GetScores() const; void operator=(int i); bool operator>(const Grade& rhs) const; bool operator<(const Grade& rhs) const; bool operator==(const Grade& rhs) const; bool operator!=(const Grade& rhs) const; void Reset(); operator bool() const{ std::string x = "X"; if (this->rank == x){ return false; } else{ return true; } }; private: std::vector<std::string> ranks = {"A+", "A", "A-", "B+", "B", "B-", "C+", "C", "C-", "F", "X"}; std::string rank = ""; int score; bool isGraded = false; }; std::ostream& operator<<(std::ostream& os, const Grade& g); // constructers & destructers Grade::Grade(){ rank = "X"; } Grade::~Grade(){ } Grade::Grade(const char* s){ rank = s; if(s == ranks[0]) {score = 95;} else if(s == ranks[1]){score = 87; } else if(s == ranks[2]){score = 82;} else if(s == ranks[3]){score = 78;} else if(s == ranks[4]){score = 75;} else if(s == ranks[5]){score = 70;} else if(s == ranks[6]){score = 68;} else if(s == ranks[7]){score = 65;} else if(s == ranks[8]){score = 60;} else if(s == ranks[9]){score = 50;} else if(s == ranks[10]){score = 0;} } Grade::Grade(const std::string& s){ rank = s; if(s == ranks[0]) {score = 95;} else if(s == ranks[1]){score = 87;} else if(s == ranks[2]){score = 82;} else if(s == ranks[3]){score = 78;} else if(s == ranks[4]){score = 75;} else if(s == ranks[5]){score = 70;} else if(s == ranks[6]){score = 68;} else if(s == ranks[7]){score = 65;} else if(s == ranks[8]){score = 60;} else if(s == ranks[9]){score = 50;} else if(s == ranks[10]){score = 0;} } Grade::Grade(int i){ score = i; if(i>=90) {rank = ranks[0];} else if(85 <= i && i <= 89) {rank = ranks[1];} else if(80 <= i && i <= 84) {rank = ranks[2];} else if(77 <= i && i <= 79) {rank = ranks[3];} else if(73 <= i && i <= 76) {rank = ranks[4];} else if(70 <= i && i <= 72) {rank = ranks[5];} else if(67 <= i && i <= 69) {rank = ranks[6];} else if(63 <= i && i <= 66) {rank = ranks[7];} else if(60 <= i && i <= 62) {rank = ranks[8];} else if(0 <= i && i <= 59) {rank = ranks[9];} } void Grade::operator=(int i){ this->score = i; if(i>=90) {this->rank = ranks[0];} else if(85 <= i && i <= 89) {this->rank = ranks[1];} else if(80 <= i && i <= 84) {this->rank = ranks[2];} else if(77 <= i && i <= 79) {this->rank = ranks[3];} else if(73 <= i && i <= 76) {this->rank = ranks[4];} else if(70 <= i && i <= 72) {this->rank = ranks[5];} else if(67 <= i && i <= 69) {this->rank = ranks[6];} else if(63 <= i && i <= 66) {this->rank = ranks[7];} else if(60 <= i && i <= 62) {this->rank = ranks[8];} else if(0 <= i && i <= 59) {this->rank = ranks[9];} } std::string Grade::GetRank() const{ return this->rank; } int Grade::GetScores() const{ return this->score; } // operator overloading bool Grade::operator>(const Grade& rhs) const{ if(this->rank == this->ranks[9] && rhs.rank == this->ranks[10]){ // F, X return 0; } else if(this->rank == this->ranks[10] && rhs.rank == this->ranks[9]){ // X, F return 0; } else{ return std::find(this->ranks.begin(), this->ranks.end(), this->rank) < std::find(this->ranks.begin(), this->ranks.end(), rhs.rank); } } bool Grade::operator<(const Grade& rhs) const{ if(this->rank == this->ranks[9] && rhs.rank == this->ranks[10]){ // F, X return 0; } else if(this->rank == this->ranks[10] && rhs.rank == this->ranks[9]){ // X, F return 0; } else{ return std::find(this->ranks.begin(), this->ranks.end(), this->rank) > std::find(this->ranks.begin(), this->ranks.end(), rhs.rank); } } bool Grade::operator==(const Grade& rhs) const{ if(this->rank == this->ranks[9] && rhs.rank == this->ranks[10]){ // F, X return 1; } else if(this->rank == this->ranks[10] && rhs.rank == this->ranks[9]){ // X, F return 1; } else{ return this->rank == rhs.rank; } } bool Grade::operator!=(const Grade& rhs) const{ if(this->rank == this->ranks[9] && rhs.rank == this->ranks[10]){ // F, X return 0; } else if(this->rank == this->ranks[10] && rhs.rank == this->ranks[9]){ // X, F return 0; } else{ return this->rank != rhs.rank; } } // functions Grade Adjust(const Grade g, int added){ Grade new_g; if(g.GetRank() != g.ranks[10]){ // if orig rank is 'X', then no adjust new_g.score = g.score; new_g.score += added; if(new_g.score > 100){ new_g.score = 100; } else if (new_g.score < 0){ new_g.score = 0; } if(new_g.score>=90) {new_g.rank = g.ranks[0];} else if(85 <= new_g.score && new_g.score <= 89) {new_g.rank = g.ranks[1];} else if(80 <= new_g.score && new_g.score <= 84) {new_g.rank = g.ranks[2];} else if(77 <= new_g.score && new_g.score <= 79) {new_g.rank = g.ranks[3];} else if(73 <= new_g.score && new_g.score <= 76) {new_g.rank = g.ranks[4];} else if(70 <= new_g.score && new_g.score <= 72) {new_g.rank = g.ranks[5];} else if(67 <= new_g.score && new_g.score <= 69) {new_g.rank = g.ranks[6];} else if(63 <= new_g.score && new_g.score <= 66) {new_g.rank = g.ranks[7];} else if(60 <= new_g.score && new_g.score <= 62) {new_g.rank = g.ranks[8];} else if(0 <= new_g.score && new_g.score <= 59) {new_g.rank = g.ranks[9];} else if(new_g.score == 0) {new_g.rank = g.ranks[10];} } return new_g; } void Grade::Reset(){ std::string x = "X"; this->rank = x; this->score = -1; this->isGraded = false; } std::ostream& operator<<(std::ostream& os, const Grade& g){ return os << g.GetRank(); }