From ae0a2175fd4bf58bac29add439b2cb48b2c29336 Mon Sep 17 00:00:00 2001 From: unlockable Date: Sat, 17 Jun 2023 00:56:41 +0800 Subject: [PATCH] =?UTF-8?q?Record=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 大作业/Date.cpp | 2 ++ 大作业/Makefile | 2 +- 大作业/Record.cpp | 50 +++++++++++++++++++++------------------ 大作业/Record.hpp | 5 ++-- 大作业/Tools.cpp | 59 +++++++++++++++++++++++++++++++++++++++++------ 大作业/Tools.hpp | 12 ++++++++-- 大作业/main.cpp | 12 +++++++++- 7 files changed, 106 insertions(+), 36 deletions(-) diff --git a/大作业/Date.cpp b/大作业/Date.cpp index 4b520b7..1abe538 100644 --- a/大作业/Date.cpp +++ b/大作业/Date.cpp @@ -1,4 +1,5 @@ #include "Date.hpp" +#include Date::Date() : time(0), isAny(true){}; @@ -30,6 +31,7 @@ time_t Date::getTime() const { std::string Date::toString() const { char buffer[20]; strftime(buffer, 20, "%F", localtime(&this->time)); + return std::string(buffer); } bool Date::operator<(const Date &otherDate) const { diff --git a/大作业/Makefile b/大作业/Makefile index cde84a7..8cc67f7 100644 --- a/大作业/Makefile +++ b/大作业/Makefile @@ -1,6 +1,6 @@ generalArguments = -std=c++11 -main.out: main.o Tools.o Exceptions.o +main.out: main.o Tools.o Exceptions.o Record.o Date.o clang++ $(generalArguments) -g $^ -o main.out main.o: main.cpp diff --git a/大作业/Record.cpp b/大作业/Record.cpp index b3be83d..289d6f6 100644 --- a/大作业/Record.cpp +++ b/大作业/Record.cpp @@ -3,14 +3,14 @@ #include #include -unsigned BaseRecord::nextRecordID = 0; +unsigned BaseRecord::nextRecordID = 1; std::string BaseRecord::cutToLength(std::string str, int length) const { if (str.length() <= length) { return str; } else { - return str.substr(length) + "+"; + return str.substr(0, length) + "+"; } } @@ -45,7 +45,7 @@ const std::string BaseRecord::getStudentName() const { return this->studentName; } -void BaseRecord::promptForNewDate(bool showOriginal = true) { +void BaseRecord::promptForNewDate(bool showOriginal) { time_t originTime = this->date.getTime(); tm *curDate = localtime(&originTime); int tempInput; @@ -86,17 +86,19 @@ void BaseRecord::promptForNewDate(bool showOriginal = true) { this->date = mktime(curDate); } -void BaseRecord::promptForNewCourseName(bool showOriginal = true) { +void BaseRecord::promptForNewCourseName(bool showOriginal) { if (showOriginal) { std::cout << setoutputcolor(green) << "Original course name: " << resetOutputColor - << this->courseName << std::endl; + << this->courseName << "\n" + << setoutputcolor(green) + << "Enter 0 to retain original value." << std::endl; } std::cout << "Course name: " << std::flush; std::cin >> this->courseName; } -void BaseRecord::promptForNewStudentInfo(bool showOriginal = true) { +void BaseRecord::promptForNewStudentInfo(bool showOriginal) { if (showOriginal) { int tempStuID; std::string tempStuName; @@ -131,17 +133,18 @@ void BaseRecord::promptForNewStudentInfo(bool showOriginal = true) { void BaseRecord::display() const { std::cout << std::setfill('0') << std::setw(8) << this->recordID; - std::cout << " "; - std::cout << std::setfill(' ') << std::setw(8) - << this->cutToLength(this->date.toString(), 8); - std::cout << " "; + std::cout << " | "; + std::cout << std::setfill(' ') << std::setw(12) + << this->cutToLength(this->date.toString(), 12); + std::cout << " | "; std::cout << std::setfill(' ') << std::setw(15) << this->cutToLength(this->courseName, 15); - std::cout << " "; - std::cout << std::setfill(' ') << std::setw(8) << this->studentNumber; - std::cout << " "; + std::cout << " | "; + std::cout << std::setfill(' ') << std::setw(10) << this->studentNumber; + std::cout << " | "; std::cout << std::setfill(' ') << std::setw(15) << this->cutToLength(this->studentName, 15); + std::cout << " | "; std::cout << std::setfill(' ') << std::setw(10) << this->getRecordType(); std::cout << std::endl; } @@ -198,19 +201,20 @@ SaveRecord::SaveRecord(const unsigned _recordID, const time_t _date, studentNumber(_studentNumber), studentNameLength(studentName.length()), recordType(_recordType){}; -BaseRecord &&SaveRecord::convertToRecord(const char *_courseName, - const char *_name) const { +BaseRecord *SaveRecord::convertToRecord(const char *_courseName, + const char *_name) const { switch (this->recordType) { case Late: - return LateRecord(this->recordID, this->date, std::string(_courseName), - this->studentNumber, std::string(_name)); + return new LateRecord(this->recordID, this->date, + std::string(_courseName), this->studentNumber, + std::string(_name)); case Absent: - return AbsentRecord(this->recordID, this->date, - std::string(_courseName), this->studentNumber, - std::string(_name)); + return new AbsentRecord(this->recordID, this->date, + std::string(_courseName), this->studentNumber, + std::string(_name)); case PersonalLeave: - return PersonalLeaveRecord(this->recordID, this->date, - std::string(_courseName), - this->studentNumber, std::string(_name)); + return new PersonalLeaveRecord(this->recordID, this->date, + std::string(_courseName), + this->studentNumber, std::string(_name)); } } \ No newline at end of file diff --git a/大作业/Record.hpp b/大作业/Record.hpp index 96ab1c7..7702539 100644 --- a/大作业/Record.hpp +++ b/大作业/Record.hpp @@ -47,6 +47,7 @@ public: void promptForNewCourseName(bool showOriginal = true); void promptForNewStudentInfo(bool showOriginal = true); void display() const; + void displayComplete() const; const char *getCourseNameChar() const; const char *getStudentNameChar() const; virtual std::string getRecordType() const = 0; @@ -97,6 +98,6 @@ public: SaveRecord(const unsigned _recordID, const time_t _date, const std::string &courseName, const int _studentNumber, const std::string &studentName, RecordType _recordType); - BaseRecord &&convertToRecord(const char *_courseName, - const char *_name) const; + BaseRecord *convertToRecord(const char *_courseName, + const char *_name) const; }; \ No newline at end of file diff --git a/大作业/Tools.cpp b/大作业/Tools.cpp index 7363118..cfca06b 100644 --- a/大作业/Tools.cpp +++ b/大作业/Tools.cpp @@ -1,11 +1,12 @@ #include "Tools.hpp" -setoutputcolor::setoutputcolor() : color(clear), bold(false) {}; -setoutputcolor::setoutputcolor(ConsoleColors _color) : color(_color), bold(false) {}; +setoutputcolor::setoutputcolor(ConsoleColors _color) + : color(_color), bold(false){}; -setoutputcolor::setoutputcolor(bool _bold) : color(clear), bold(_bold) {}; +setoutputcolor::setoutputcolor(bool _bold) : color(clear), bold(_bold){}; -setoutputcolor::setoutputcolor(ConsoleColors _color, bool _bold) : color(_color), bold(_bold) {}; +setoutputcolor::setoutputcolor(ConsoleColors _color, bool _bold) + : color(_color), bold(_bold){}; std::ostream &operator<<(std::ostream &out, setoutputcolor setOutput) { out << "\033["; @@ -20,6 +21,9 @@ std::ostream &operator<<(std::ostream &out, setoutputcolor setOutput) { out << "0"; } switch (setOutput.color) { + case black: + out << ";30m"; + break; case red: out << ";31m"; break; @@ -29,14 +33,55 @@ std::ostream &operator<<(std::ostream &out, setoutputcolor setOutput) { case blue: out << ";34m"; break; - default: - out << ";0m"; + case magenta: + out << ";35m"; break; + case cyan: + out << ";36m"; + break; + case lightGray: + out << ";37m"; + break; + default: + out << "m\033[0m"; } return out; } +std::ostream &operator<<(std::ostream &out, setbgcolor setbg) { + switch (setbg.color) { + case black: + out << "\033[7;30m"; + break; + case red: + out << "\033[7;31m"; + break; + case green: + out << "\033[7;32m"; + break; + case blue: + out << "\033[7;34m"; + break; + case magenta: + out << "\033[7;35m"; + break; + case cyan: + out << "\033[7;36m"; + break; + case lightGray: + out << "\033[7;37m"; + break; + default: + // clang-format off + {;} + // clang-format on + } + return out; +} + +setbgcolor::setbgcolor(ConsoleColors _color) : color(_color){}; + std::ostream &resetOutputColor(std::ostream &out) { - out << setoutputcolor(); + out << setoutputcolor(clear); return out; } \ No newline at end of file diff --git a/大作业/Tools.hpp b/大作业/Tools.hpp index cd927eb..12ffadd 100644 --- a/大作业/Tools.hpp +++ b/大作业/Tools.hpp @@ -3,7 +3,7 @@ // Reference: https://cplusplus.com/forum/unices/36461/ -enum ConsoleColors { red, green, blue, clear }; +enum ConsoleColors { black, red, green, blue, magenta, cyan, lightGray, clear }; class setoutputcolor { private: @@ -13,10 +13,18 @@ private: setoutputcolor setOutput); public: - setoutputcolor(); setoutputcolor(ConsoleColors _color); setoutputcolor(bool bold); setoutputcolor(ConsoleColors _color, bool bold); }; +class setbgcolor { +private: + ConsoleColors color; + friend std::ostream &operator<<(std::ostream &out, setbgcolor setbg); + +public: + setbgcolor(ConsoleColors _color); +}; + std::ostream &resetOutputColor(std::ostream &out); \ No newline at end of file diff --git a/大作业/main.cpp b/大作业/main.cpp index 7564f16..82c644f 100644 --- a/大作业/main.cpp +++ b/大作业/main.cpp @@ -1,5 +1,6 @@ #include "ListE.hpp" #include "Tools.hpp" +#include "Record.hpp" #include #include @@ -39,6 +40,15 @@ int main() { bList.filter([=](const int &a) { return a > 6; }); display(bList); - std::cout << "hhh" << setoutputcolor(blue, true) << "aaa" << setoutputcolor(blue, false) << "bbb" << setoutputcolor() << std::endl; + std::cout << "hhh" << setoutputcolor(blue, true) << "aaa" << setoutputcolor(blue, false) << "bbb" << resetOutputColor << std::endl; + LateRecord la(125534456, "SomeCourse", 10821398, "SomeStu"); + AbsentRecord lb(2874238743, "SomeotherCourse", 10239944, "SomeotherStu"); + la.display(); + std::cout << setbgcolor(lightGray); + lb.display(); + std::cout << resetOutputColor; + + la.promptForNewStudentInfo(); + la.display(); return 0; } \ No newline at end of file