diff --git "a/practical_exercises/10_day_practice/day7/\347\273\274\345\220\210\344\276\213\351\242\230/\351\207\215\350\246\201.cpp" "b/practical_exercises/10_day_practice/day7/\347\273\274\345\220\210\344\276\213\351\242\230/\351\207\215\350\246\201.cpp" index 10ec7edc..83545ee6 100644 --- "a/practical_exercises/10_day_practice/day7/\347\273\274\345\220\210\344\276\213\351\242\230/\351\207\215\350\246\201.cpp" +++ "b/practical_exercises/10_day_practice/day7/\347\273\274\345\220\210\344\276\213\351\242\230/\351\207\215\350\246\201.cpp" @@ -1,70 +1,91 @@ -//设计一个字符串类String,通过运算符重载实现字符串的输入、输出以及+=、==、!=、<、>、>=、[ ]等运算。 +//璁捐涓涓瓧绗︿覆绫籗tring锛岄氳繃杩愮畻绗﹂噸杞藉疄鐜板瓧绗︿覆鐨勮緭鍏ャ佽緭鍑轰互鍙+=銆==銆!=銆<銆>銆>=銆乕 ]绛夎繍绠椼 #include #include using namespace std; -class String { +class String +{ private: -int length; //字符串长度 -char *sPtr; //存放字符串的指针 -void setString( const char *s2); -friend ostream &operator<<(ostream &os, const String &s); -friend istream &operator>>(istream &is, String &s); //重载输入运算符 + int length; //瀛楃涓查暱搴 + char *sPtr; //瀛樻斁瀛楃涓茬殑鎸囬拡 + void setString(const char *s2); + friend ostream &operator<<(ostream &os, const String &s) + { + return os << s.sPtr; + }; + friend istream &operator>>(istream &is, String &s) + { + return is >> s.sPtr; + }; //閲嶈浇杈撳叆杩愮畻绗 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); //字符串的不等比较 != - bool operator!() ; //判定字符串是否为空 - bool operator<(const String &R) const; //字符串的小于比较 < - bool operator>(const String &R); //字符串的大于比较 > - bool operator>=(const String &R); //字符串的大于等于比较 - char &operator[](int); //字符串的下标运算 - ~String(); + String(const char * = ""); + 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; - length += R.length; - sPtr = new char[length+1]; - strcpy(sPtr,temp ); - strcat(sPtr,R.sPtr ); - delete [] temp; - return *this; +const String &String::operator+=(const String &R) +{ + char *temp = sPtr; + length += R.length; + sPtr = new char[length + 1]; + strcpy(sPtr, temp); + strcat(sPtr, R.sPtr); + delete[] temp; + return *this; } -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;} -bool String::operator<(const String &R)const{return strcmp(sPtr,R.sPtr)<0;} -bool String::operator>(const String &R){return R<*this;} -bool String::operator>=(const String &R){return !(*this 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是空串"<(const String &R) { return R < *this; } +bool String::operator>=(const String &R) { return !(*this < R); } +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姣旇緝s2鍜宻1:" + << "\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鏄┖涓" << endl; //L3 + cout << "鎶妔1璧嬬粰s3鐨勭粨鏋滄槸锛"; + s3 = s1; + cout << "s3=" << s3 << "\n"; //L5 } - cout << "s1 += s2 的结果是:s1="; //L6 - s1 += s2; - cout << s1; //L7 - - cout << "\ns1 += to you 的结果是:"; //L8 - s1 += " to you"; - cout << "s1 = " << s1 <