第四周。
This commit is contained in:
42
OOP/04/Exercise01.cpp
Normal file
42
OOP/04/Exercise01.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
119
OOP/04/Exercise02.cpp
Normal file
119
OOP/04/Exercise02.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user