|
1 | | -//设计一个字符串类String,通过运算符重载实现字符串的输入、输出以及+=、==、!=、<、>、>=、[ ]等运算。 |
| 1 | +//设计一个字符串类String,通过运算符重载实现字符串的输入、输出以及+=、==、!=、<、>、>=、[ ]等运算。 |
2 | 2 | #include <iostream> |
3 | 3 | #include <cstring> |
4 | 4 | using namespace std; |
5 | | -class String { |
| 5 | +class String |
| 6 | +{ |
6 | 7 | private: |
7 | | -int length; //字符串长度 |
8 | | -char *sPtr; //存放字符串的指针 |
9 | | -void setString( const char *s2); |
10 | | -friend ostream &operator<<(ostream &os, const String &s); |
11 | | -friend istream &operator>>(istream &is, String &s); //重载输入运算符 |
| 8 | + int length; //字符串长度 |
| 9 | + char *sPtr; //存放字符串的指针 |
| 10 | + void setString(const char *s2); |
| 11 | + friend ostream &operator<<(ostream &os, const String &s) |
| 12 | + { |
| 13 | + return os << s.sPtr; |
| 14 | + }; |
| 15 | + friend istream &operator>>(istream &is, String &s) |
| 16 | + { |
| 17 | + return is >> s.sPtr; |
| 18 | + }; //重载输入运算符 |
12 | 19 | public: |
13 | | - String( const char * = "" ); |
14 | | - const String &operator=(const String &R); //重载赋值运算符 = |
15 | | - const String &operator+=(const String &R); //字符串的连接 += |
16 | | - bool operator==(const String &R); //字符串的相等比较 == |
17 | | - bool operator!=(const String &R); //字符串的不等比较 != |
18 | | - bool operator!() ; //判定字符串是否为空 |
19 | | - bool operator<(const String &R) const; //字符串的小于比较 < |
20 | | - bool operator>(const String &R); //字符串的大于比较 > |
21 | | - bool operator>=(const String &R); //字符串的大于等于比较 |
22 | | - char &operator[](int); //字符串的下标运算 |
23 | | - ~String(); |
| 20 | + String(const char * = ""); |
| 21 | + const String &operator=(const String &R) |
| 22 | + { |
| 23 | + length = R.length; |
| 24 | + strcpy(R.sPtr, sPtr); |
| 25 | + return *this; |
| 26 | + }; //重载赋值运算符 = |
| 27 | + const String &operator+=(const String &R); //字符串的连接 += |
| 28 | + bool operator==(const String &R); //字符串的相等比较 == |
| 29 | + bool operator!=(const String &R); //字符串的不等比较 != |
| 30 | + bool operator!(); //判定字符串是否为空 |
| 31 | + bool operator<(const String &R) const; //字符串的小于比较 < |
| 32 | + bool operator>(const String &R); //字符串的大于比较 > |
| 33 | + bool operator>=(const String &R); //字符串的大于等于比较 |
| 34 | + char &operator[](int); //字符串的下标运算 |
| 35 | + ~String(){}; |
24 | 36 | }; |
25 | | -const String &String::operator+=(const String &R) { |
26 | | - char *temp = sPtr; |
27 | | - length += R.length; |
28 | | - sPtr = new char[length+1]; |
29 | | - strcpy(sPtr,temp ); |
30 | | - strcat(sPtr,R.sPtr ); |
31 | | - delete [] temp; |
32 | | - return *this; |
| 37 | +const String &String::operator+=(const String &R) |
| 38 | +{ |
| 39 | + char *temp = sPtr; |
| 40 | + length += R.length; |
| 41 | + sPtr = new char[length + 1]; |
| 42 | + strcpy(sPtr, temp); |
| 43 | + strcat(sPtr, R.sPtr); |
| 44 | + delete[] temp; |
| 45 | + return *this; |
33 | 46 | } |
34 | | -bool String::operator==(const String &R){return strcmp(sPtr,R.sPtr)==0;} |
35 | | -bool String::operator!=(const String & R){return !(*this==R);} |
36 | | -bool String::operator!(){return length ==0;} |
37 | | -bool String::operator<(const String &R)const{return strcmp(sPtr,R.sPtr)<0;} |
38 | | -bool String::operator>(const String &R){return R<*this;} |
39 | | -bool String::operator>=(const String &R){return !(*this<R);} |
40 | | -char &String::operator[](int subscript){return sPtr[subscript];} |
41 | | -int main(){ |
42 | | - String s1("happy"),s2("new year"),s3; |
43 | | - cout << "s1 is " << s1 << "\ns2 is " << s2 << "\ns3 is " << s3 |
44 | | - << "\n比较s2和s1:" |
45 | | - << "\ns2 ==s1结果是 " << ( s2 == s1 ? "true" : "false") |
46 | | - << "\ns2 != s1结果是 " << ( s2 != s1 ? "true" : "false") |
47 | | - << "\ns2 > s1结果是 " << ( s2 > s1 ? "true" : "false") |
48 | | - << "\ns2 < s1结果是 " << ( s2 < s1 ? "true" : "false") |
49 | | - << "\ns2 >= s1结果是 " << ( s2 >= s1 ? "true" : "false"); |
50 | | - cout << "\n\n测试s3是否为空: "; |
51 | | - if (!s3){ |
52 | | - cout << "s3是空串"<<endl; //L3 |
53 | | - cout<<"把s1赋给s3的结果是:"; |
54 | | - s3 = s1; |
55 | | - cout << "s3=" << s3 << "\n"; //L5 |
| 47 | +String::String(const char *str) |
| 48 | +{ |
| 49 | + sPtr = new char[strlen(str) + 1]; |
| 50 | + strcpy(sPtr, str); |
| 51 | + length = strlen(str); |
| 52 | +}; |
| 53 | +bool String::operator==(const String &R) { return strcmp(sPtr, R.sPtr) == 0; } |
| 54 | +bool String::operator!=(const String &R) { return !(*this == R); } |
| 55 | +bool String::operator!() { return length == 0; } |
| 56 | +bool String::operator<(const String &R) const { return strcmp(sPtr, R.sPtr) < 0; } |
| 57 | +bool String::operator>(const String &R) { return R < *this; } |
| 58 | +bool String::operator>=(const String &R) { return !(*this < R); } |
| 59 | +char &String::operator[](int subscript) { return sPtr[subscript]; } |
| 60 | +int main() |
| 61 | +{ |
| 62 | + String s1("happy"), s2("new year"), s3; |
| 63 | + cout << "s1 is " << s1 << "\ns2 is " << s2 << "\ns3 is " << s3 |
| 64 | + << "\n比较s2和s1:" |
| 65 | + << "\ns2 ==s1结果是 " << (s2 == s1 ? "true" : "false") |
| 66 | + << "\ns2 != s1结果是 " << (s2 != s1 ? "true" : "false") |
| 67 | + << "\ns2 > s1结果是 " << (s2 > s1 ? "true" : "false") |
| 68 | + << "\ns2 < s1结果是 " << (s2 < s1 ? "true" : "false") |
| 69 | + << "\ns2 >= s1结果是 " << (s2 >= s1 ? "true" : "false"); |
| 70 | + cout << "\n\n测试s3是否为空: "; |
| 71 | + if (!s3) |
| 72 | + { |
| 73 | + cout << "s3是空串" << endl; //L3 |
| 74 | + cout << "把s1赋给s3的结果是:"; |
| 75 | + s3 = s1; |
| 76 | + cout << "s3=" << s3 << "\n"; //L5 |
56 | 77 | } |
57 | | - cout << "s1 += s2 的结果是:s1="; //L6 |
58 | | - s1 += s2; |
59 | | - cout << s1; //L7 |
60 | | - |
61 | | - cout << "\ns1 += to you 的结果是:"; //L8 |
62 | | - s1 += " to you"; |
63 | | - cout << "s1 = " << s1 <<endl; //L9 |
64 | | - s1[0] = 'H'; |
| 78 | + cout << "s1 += s2 的结果是:s1="; //L6 |
| 79 | + s1 += s2; |
| 80 | + cout << s1; //L7 |
| 81 | + |
| 82 | + cout << "\ns1 += to you 的结果是:"; //L8 |
| 83 | + s1 += " to you"; |
| 84 | + cout << "s1 = " << s1 << endl; //L9 |
| 85 | + s1[0] = 'H'; |
65 | 86 | s1[6] = 'N'; |
66 | 87 | s1[10] = 'Y'; |
67 | | - cout << "s1 = " << s1 << "\n"; //L10 |
| 88 | + cout << "s1 = " << s1 << "\n"; //L10 |
68 | 89 | system("pause"); |
69 | 90 | return 0; |
70 | 91 | } |
0 commit comments