019:全面的MyString

it2025-05-22  20

019:全面的MyString

题面

描述

程序填空,输出指定结果

#include <cstdlib> #include <iostream> using namespace std; int strlen(const char * s) { int i = 0; for(; s[i]; ++i); return i; } void strcpy(char * d,const char * s) { int i = 0; for( i = 0; s[i]; ++i) d[i] = s[i]; d[i] = 0; } int strcmp(const char * s1,const char * s2) { for(int i = 0; s1[i] && s2[i] ; ++i) { if( s1[i] < s2[i] ) return -1; else if( s1[i] > s2[i]) return 1; } return 0; } void strcat(char * d,const char * s) { int len = strlen(d); strcpy(d+len,s); } class MyString { // 在此处补充你的代码 }; int CompareString( const void * e1, const void * e2) { MyString * s1 = (MyString * ) e1; MyString * s2 = (MyString * ) e2; if( * s1 < *s2 ) return -1; else if( *s1 == *s2) return 0; else if( *s1 > *s2 ) return 1; } int main() { MyString s1("abcd-"),s2,s3("efgh-"),s4(s1); MyString SArray[4] = {"big","me","about","take"}; cout << "1. " << s1 << s2 << s3<< s4<< endl; s4 = s3; s3 = s1 + s3; cout << "2. " << s1 << endl; cout << "3. " << s2 << endl; cout << "4. " << s3 << endl; cout << "5. " << s4 << endl; cout << "6. " << s1[2] << endl; s2 = s1; s1 = "ijkl-"; s1[2] = 'A' ; cout << "7. " << s2 << endl; cout << "8. " << s1 << endl; s1 += "mnop"; cout << "9. " << s1 << endl; s4 = "qrst-" + s2; cout << "10. " << s4 << endl; s1 = s2 + s4 + " uvw " + "xyz"; cout << "11. " << s1 << endl; qsort(SArray,4,sizeof(MyString),CompareString); for( int i = 0;i < 4;i ++ ) cout << SArray[i] << endl; //s1的从下标0开始长度为4的子串 cout << s1(0,4) << endl; //s1的从下标5开始长度为10的子串 cout << s1(5,10) << endl; return 0; }

输入

输出

\1. abcd-efgh-abcd- \2. abcd- 3. \4. abcd-efgh- \5. efgh- \6. c \7. abcd- \8. ijAl- \9. ijAl-mnop \10. qrst-abcd- \11. abcd-qrst-abcd- uvw xyz about big me take abcd qrst-abcd

思路

MyString s1("abcd-"), s2, s3("efgh-"), s4(s1);

可以看到需要构造函数,一种是char *,一种是复制构造

在构造函数和析构函数时候需要考虑 c h a r ∗ char * char的动态分配和删除

MyString(const char* s = NULL) { if (s) { str = new char[strlen(s) + 1]; strcpy(str, s); } else { str = new char[1]; str[0] = '\0'; } } ~MyString() { if (str) delete[] str; } MyString(const MyString& s) { if (s.str) { str = new char[strlen(s.str) + 1]; strcpy(str, s.str); } else { str = new char[1]; str[0] = '\0'; } }

MyString SArray[4] = { "big","me","about","take" };

s4 = s3;和s1 = "ijkl-";重载=

需要考虑的是如果是s3=s3的情况需要直接返回,如果不是的话再动态赋值

MyString& operator= (const char* s) { if (str == s) { return *this; } delete[] str; if (s) { str = new char[strlen(s) + 1]; strcpy(str, s); } else { str = new char[1]; str[0] = '\0'; } return *this; } MyString& operator=(const MyString& s) { if (str == s.str) { return *this; } delete[] str; if (s.str) { str = new char[strlen(s.str) + 1]; strcpy(str, s.str); } else { str = new char[1]; str[0] = '\0'; } return *this; }

s3 = s1 + s3;

s4 = "qrst-" + s2;

s1 = s2 + s4 + " uvw " + "xyz";需要重载+

friend MyString operator+ (const MyString& a,const MyString& b) { char* tmp = new char[strlen(a.str) + strlen(b.str) + 1]; strcpy(tmp, a.str); strcat(tmp, b.str); return MyString(tmp); } friend MyString operator+ (const char* p, const MyString& s) { char* tmp = new char[strlen(p) + strlen(s.str) + 1]; strcpy(tmp, p); strcat(tmp, s.str); return MyString(tmp); } friend MyString operator+ (const MyString& s, const char* p) { char* tmp = new char[strlen(s.str) + strlen(p) + 1]; strcpy(tmp, s.str); strcat(tmp, p); return MyString(tmp); }

s1[2]

s1[2] = 'A' ;

需要重载[],返回引用是保证第二个赋值的实现

char& operator[] (int i) { return str[i]; } s1 += "mnop";需要重载+= MyString& operator+= (const char* s) { char* tmp = new char[strlen(str) + 1]; strcpy(tmp, str); delete[] str; str = new char[strlen(tmp) + strlen(s) + 1]; strcpy(str, tmp); strcat(str, s); return *this; } s1(5, 10)需要重载() char* operator() (int start, int length) { char* tmp = new char[length + 1]; for (int i = start; i < start + length; ++i) { tmp[i - start] = str[i]; } tmp[length] = '\0'; return tmp; } 重载流运算符<< friend ostream& operator<< (ostream& o,const MyString& s) { o << s.str; return o; } qsort(SArray,4,sizeof(MyString),CompareString);

其中注意到CompareString里需要重载各个比较运算符

#include <cstdlib> #include <iostream> using namespace std; int strlen(const char* s) { int i = 0; for (; s[i]; ++i); return i; } void strcpy(char* d, const char* s) { int i = 0; for (i = 0; s[i]; ++i) d[i] = s[i]; d[i] = 0; } int strcmp(const char* s1, const char* s2) { for (int i = 0; s1[i] && s2[i]; ++i) { if (s1[i] < s2[i]) return -1; else if (s1[i] > s2[i]) return 1; } return 0; } void strcat(char* d, const char* s) { int len = strlen(d); strcpy(d + len, s); } class MyString { private: char* str; public: MyString(const char* s = NULL) { if (s) { str = new char[strlen(s) + 1]; strcpy(str, s); } else { str = new char[1]; str[0] = '\0'; } } ~MyString() { if (str) delete[] str; } MyString(const MyString& s) { if (s.str) { str = new char[strlen(s.str) + 1]; strcpy(str, s.str); } else { str = new char[1]; str[0] = '\0'; } } MyString& operator= (const char* s) { if (str == s) { return *this; } delete[] str; if (s) { str = new char[strlen(s) + 1]; strcpy(str, s); } else { str = new char[1]; str[0] = '\0'; } return *this; } MyString& operator=(const MyString& s) { if (str == s.str) { return *this; } delete[] str; if (s.str) { str = new char[strlen(s.str) + 1]; strcpy(str, s.str); } else { str = new char[1]; str[0] = '\0'; } return *this; } friend MyString operator+ (const MyString& a,const MyString& b) { char* tmp = new char[strlen(a.str) + strlen(b.str) + 1]; strcpy(tmp, a.str); strcat(tmp, b.str); return MyString(tmp); } friend ostream& operator<< (ostream& o,const MyString& s) { o << s.str; return o; } char& operator[] (int i) { return str[i]; } MyString& operator+= (const char* s) { char* tmp = new char[strlen(str) + 1]; strcpy(tmp, str); delete[] str; str = new char[strlen(tmp) + strlen(s) + 1]; strcpy(str, tmp); strcat(str, s); return *this; } friend MyString operator+ (const char* p, const MyString& s) { char* tmp = new char[strlen(p) + strlen(s.str) + 1]; strcpy(tmp, p); strcat(tmp, s.str); return MyString(tmp); } friend MyString operator+ (const MyString& s, const char* p) { char* tmp = new char[strlen(s.str) + strlen(p) + 1]; strcpy(tmp, s.str); strcat(tmp, p); return MyString(tmp); } friend bool operator< (const MyString& s1,const MyString& s2) { if (strcmp(s1.str, s2.str) < 0) { return true; } else { return false; } } friend bool operator> (const MyString& s1,const MyString& s2) { if (strcmp(s1.str, s2.str) > 0) return true; else return false; } friend bool operator== (const MyString& s1,const MyString& s2) { if (strcmp(s1.str, s2.str) == 0) return true; else return false; } char* operator() (int start, int length) { char* tmp = new char[length + 1]; for (int i = start; i < start + length; ++i) { tmp[i - start] = str[i]; } tmp[length] = '\0'; return tmp; } }; int CompareString(const void* e1, const void* e2) { MyString* s1 = (MyString*)e1; MyString* s2 = (MyString*)e2; if (*s1 < *s2) return -1; else if (*s1 == *s2) return 0; else if (*s1 > * s2) return 1; } int main() { MyString s1("abcd-"), s2, s3("efgh-"), s4(s1); MyString SArray[4] = { "big","me","about","take" }; cout << "1. " << s1 << s2 << s3 << s4 << endl; s4 = s3; s3 = s1 + s3; cout << "2. " << s1 << endl; cout << "3. " << s2 << endl; cout << "4. " << s3 << endl; cout << "5. " << s4 << endl; cout << "6. " << s1[2] << endl; s2 = s1; s1 = "ijkl-"; s1[2] = 'A'; cout << "7. " << s2 << endl; cout << "8. " << s1 << endl; s1 += "mnop"; cout << "9. " << s1 << endl; s4 = "qrst-" + s2; cout << "10. " << s4 << endl; s1 = s2 + s4 + " uvw " + "xyz"; cout << "11. " << s1 << endl; qsort(SArray, 4, sizeof(MyString), CompareString); for (int i = 0; i < 4; i++) cout << SArray[i] << endl; //s1的从下标0开始长度为4的子串 cout << s1(0, 4) << endl; //s1的从下标5开始长度为10的子串 cout << s1(5, 10) << endl; return 0; }
最新回复(0)