Record基本完成。
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "Date.hpp"
|
||||
#include <string>
|
||||
|
||||
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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -3,14 +3,14 @@
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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);
|
||||
12
大作业/main.cpp
12
大作业/main.cpp
@@ -1,5 +1,6 @@
|
||||
#include "ListE.hpp"
|
||||
#include "Tools.hpp"
|
||||
#include "Record.hpp"
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user