From 3bc65bb35aecbcadc35078ba80fa142acbf547a8 Mon Sep 17 00:00:00 2001 From: unlockable Date: Tue, 14 Mar 2023 19:17:55 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E5=91=A8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- OOP/04/Exercise01.cpp | 42 ++++++++++++++ OOP/04/Exercise02.cpp | 119 ++++++++++++++++++++++++++++++++++++++++ OOP/FiveInARow/main.cpp | 5 +- 3 files changed, 164 insertions(+), 2 deletions(-) create mode 100644 OOP/04/Exercise01.cpp create mode 100644 OOP/04/Exercise02.cpp diff --git a/OOP/04/Exercise01.cpp b/OOP/04/Exercise01.cpp new file mode 100644 index 0000000..61f639b --- /dev/null +++ b/OOP/04/Exercise01.cpp @@ -0,0 +1,42 @@ +#include + +class Date { +private: + int d, m, y; + +public: + Date(int dd = 0, int mm = 0, int yy = 0); + void addDay(); + friend Date operator ++(Date& D); + friend Date operator ++(Date& D, int); + void Print(); +}; + +Date::Date(int dd, int mm, int yy): d(dd), m(mm), y(yy){}; + +Date operator ++(Date& D) { + D.addDay(); + return D; +} +Date operator ++(Date& D, int) { + Date tmp = Date(D); + D.addDay(); + return tmp; +} + +void Date::addDay() { + this->d++; +} + +void Date::Print() { + std::cout << this->d << "," << this->m << "," << this->y << std::endl; +} + +int main() { + Date aDay = Date(12,3,2023); + aDay++.Print(); + aDay.Print(); + (++aDay).Print(); + aDay.Print(); + return 0; +} \ No newline at end of file diff --git a/OOP/04/Exercise02.cpp b/OOP/04/Exercise02.cpp new file mode 100644 index 0000000..d1664f6 --- /dev/null +++ b/OOP/04/Exercise02.cpp @@ -0,0 +1,119 @@ +#include + +class String { +private: + char *str; + +public: + String(); + String(String &otherStr); + String(char *otherStr); + ~String(); + + bool operator==(String &otherStr); + bool operator>(String &otherStr); + bool operator<(String &otherStr); + String& operator=(String& otherStr); + + char *getStrPtr(); + void printOut(); +}; + +String::String() { + this->str = NULL; +} + +String::String(String &otherStr) { + this->str = new char[strlen(otherStr.getStrPtr()) + 1]; + strcpy(this->str, otherStr.getStrPtr()); +} + +String::String(char *otherStr) { + this->str = new char[strlen(otherStr) + 1]; + strcpy(this->str, otherStr); +} + +String::~String() { + if (this->str != NULL) { + delete[] this->str; + } +} + +bool String::operator==(String &otherString) { + if (this->str == NULL) { + if (otherString.getStrPtr() == NULL) { + return true; + } + else { + return false; + } + } + + return (strcmp(this->str, otherString.getStrPtr()) == 0); +} + +bool String::operator>(String &otherString) { + return strcmp(this->str, otherString.getStrPtr()) > 0; +} + +bool String::operator<(String &otherString) { + return strcmp(this->str, otherString.getStrPtr()) < 0; +} + +String operator+(String &lhs, String &rhs) { + String tmp(new char[strlen(lhs.getStrPtr()) + strlen(rhs.getStrPtr()) + 1]); + strcpy(tmp.getStrPtr(), lhs.getStrPtr()); + strcat(tmp.getStrPtr(), rhs.getStrPtr()); + return tmp; +} + +String& String::operator=(String &otherString) { + if (this->str != NULL) { + delete[] this->str; + } + this->str = new char[strlen(otherString.getStrPtr()) + 1]; + strcpy(this->str, otherString.getStrPtr()); + return *this; +} + +char *String::getStrPtr() { + return this->str; +} + +void String::printOut() { + std::cout << this->str << std::endl; +} + +int main() { + char *str1 = new char[10]; + char *str2 = new char[10]; + std::cout << "String 1: "; + std::cin >> str1; + std::cout << "String 2: "; + std::cin >> str2; + String string1(str1); + String string2(str2); + String string3 = string2; + string1.printOut(); + string2.printOut(); + string3.printOut(); + string3 = string1; + string3.printOut(); + (string1 + string2).printOut(); + if (!(string1 > string2)) { + std::cout <<"String1 NOT > String2" << std::endl; + } + + if (string1 < string2) { + std::cout <<"String1 < String2" << std::endl; + } + + if (string1 == string3) { + std::cout << "String1 = String3" << std::endl; + } + + if (!(string2 == string3)) { + std::cout << "String2 != String3" << std::endl; + } + return 0; +} \ No newline at end of file diff --git a/OOP/FiveInARow/main.cpp b/OOP/FiveInARow/main.cpp index 336e4fe..e16aa08 100644 --- a/OOP/FiveInARow/main.cpp +++ b/OOP/FiveInARow/main.cpp @@ -37,7 +37,8 @@ int main() { aBoard->show(); putChessPieceResult result; - bool nowMovePlayerIndex = false; + bool nowMovePlayerIndex = + players[0]->getName() > players[1]->getName() ? BLACK : WHITE; // 只有两种状态,bool足够 do { @@ -104,7 +105,7 @@ int main() { '\n'); } while (input != 'y' && input != 'Y' && input != 'n' && input != 'N'); - + if (input == 'y' || input == 'Y') { aBoard->reset(); aBoard->show();