diff --git a/practical_exercises/10_day_practice/day7/综合例题/重要.cpp b/practical_exercises/10_day_practice/day7/综合例题/重要.cpp index 10ec7ed..a7e5e42 100644 --- a/practical_exercises/10_day_practice/day7/综合例题/重要.cpp +++ b/practical_exercises/10_day_practice/day7/综合例题/重要.cpp @@ -1,26 +1,37 @@ -//һַStringͨʵַ롢Լ+===!=<>>=[ ]㡣 +//设计一个字符串类String,通过运算符重载实现字符串的输入、输出以及+=、==、!=、<、>、>=、[ ]等运算。 #include #include using namespace std; class String { private: -int length; //ַ -char *sPtr; //ַָ +int length; //字符串长度 +char *sPtr; //存放字符串的指针 void setString( const char *s2); -friend ostream &operator<<(ostream &os, const String &s); -friend istream &operator>>(istream &is, String &s); // +friend ostream &operator<<(ostream &os, const String &s) +{ + return os << s.sPtr; +}; +friend istream &operator>>(istream &is, String &s) +{ + return is >> s; +}; //重载输入运算符 public: String( const char * = "" ); - const String &operator=(const String &R); //ظֵ = - const String &operator+=(const String &R); //ַ += - bool operator==(const String &R); //ַȱȽ == - bool operator!=(const String &R); //ַIJȱȽ != - bool operator!() ; //жַǷΪ - bool operator<(const String &R) const; //ַСڱȽ < - bool operator>(const String &R); //ַĴڱȽ > - bool operator>=(const String &R); //ַĴڵڱȽ - char &operator[](int); //ַ± - ~String(); + const String &operator=(const String &R) + { + length = R.length; + strcpy(R.sPtr, sPtr); + return *this; + } ; //重载赋值运算符 = + const String &operator+=(const String &R); //字符串的连接 += + bool operator==(const String &R); //字符串的相等比较 == + bool operator!=(const String &R); //字符串的不等比较 != + bool operator!() ; //判定字符串是否为空 + bool operator<(const String &R) const; //字符串的小于比较 < + bool operator>(const String &R); //字符串的大于比较 > + bool operator>=(const String &R); //字符串的大于等于比较 + char &operator[](int); //字符串的下标运算 + ~String(){}; }; const String &String::operator+=(const String &R) { char *temp = sPtr; @@ -31,6 +42,12 @@ const String &String::operator+=(const String &R) { delete [] temp; return *this; } +String::String(const char *str) +{ + sPtr = new char[strlen(str) + 1]; + strcpy(sPtr, str); + length = strlen(str); +}; bool String::operator==(const String &R){return strcmp(sPtr,R.sPtr)==0;} bool String::operator!=(const String & R){return !(*this==R);} bool String::operator!(){return length ==0;} @@ -41,24 +58,24 @@ char &String::operator[](int subscript){return sPtr[subscript];} int main(){ String s1("happy"),s2("new year"),s3; cout << "s1 is " << s1 << "\ns2 is " << s2 << "\ns3 is " << s3 - << "\nȽs2s1:" - << "\ns2 ==s1 " << ( s2 == s1 ? "true" : "false") - << "\ns2 != s1 " << ( s2 != s1 ? "true" : "false") - << "\ns2 > s1 " << ( s2 > s1 ? "true" : "false") - << "\ns2 < s1 " << ( s2 < s1 ? "true" : "false") - << "\ns2 >= s1 " << ( s2 >= s1 ? "true" : "false"); - cout << "\n\ns3ǷΪ: "; + << "\n比较s2和s1:" + << "\ns2 ==s1结果是 " << ( s2 == s1 ? "true" : "false") + << "\ns2 != s1结果是 " << ( s2 != s1 ? "true" : "false") + << "\ns2 > s1结果是 " << ( s2 > s1 ? "true" : "false") + << "\ns2 < s1结果是 " << ( s2 < s1 ? "true" : "false") + << "\ns2 >= s1结果是 " << ( s2 >= s1 ? "true" : "false"); + cout << "\n\n测试s3是否为空: "; if (!s3){ - cout << "s3ǿմ"<