OJ: OpenJudge
019:全面的MyString
这个题也是有很多的成员函数,我们来从主函数分析一下:
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;
此处有7个函数要写,再看CompareString分析:
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;
此处还有3个函数要写,一共是10个:
- 无参构造
- 有参构造,参数为字符数组
- 重载<<
- 重载+
- 重载[]
- 重载+=
- 重载()
- 重载<
- 重载>
- 重载==
下面我们来慢慢分析一下:
首先,开头四个函数先了解一下:
strlen:求字符数组长度
strcpy:字符串拷贝
strcmp:字符串比较
strcat:字符串拼接
无参构造 、有参构造,参数为字符数组
private:
char str[30] = { 0 };
public:
MyString() {
}
MyString(const char* s)
{
strcpy(str, s);
}
成员变量字符数组,用\0初始化,含参构造调用字符串拷贝就行
重载<<
friend ostream& operator<<(ostream& o, MyString& mystr)
{
cout << mystr.str;
return o;
}
正常重载,比较容易,输出成员变量即可
重载+
friend MyString operator+(MyString mystr1,MyString mystr2)
{
MyString mystr3(mystr1);
strcat(mystr3.str, mystr2.str);
return mystr3;
}
此处重载+注意新建了一个对象来存结果,因为相加不改变两个对象的值,如果不这样做,调用字符串拼接函数就会修改前一个对象里面所存的结果(拼接其实是把第二个拼到第一个参数末尾,会修改第一个参数存的东西),然后将数组结果返回
重载[]
char& operator [](int i)
{
return str[i];
}
很容易,返回数组的位置就行,这样返回的值依然可以遵循char类型变量进行相关操作
重载+=
MyString& operator +=(const char* mystr)
{
strcat(str, mystr);
return *this;
}
这个跟+不一样,这个需要修改,所以直接拼接到该对象变量即可。
重载()
char* operator()(int begin, int leng)
{
char *c=new char[30]; int i;
for ( i = 0; i <leng; i++)
{
c[i] = str[begin + i];
}
c[i + 1] = '\0';
return c;
}
这里我采用新建一个数组来存结果,运用for就可以完成,注意尾部用\0结尾
重载< 、 重载> 、重载==
bool operator<(MyString mystr)
{
if (strcmp(str, mystr.str) == -1)
return 1;
return 0;
}
bool operator==(MyString mystr)
{
if (strcmp(str, mystr.str) == 0)
return 1;
return 0;
}
bool operator>(MyString mystr)
{
if (strcmp(str, mystr.str) == 1)
return 1;
return 0;
}
这三个函数其实大同小异,调用字符串比较函数就行,根据不同返回值来判断
-1是小于,0是等于,1是大于,再字符串比较函数里都可以看出。
小结
这个题我没有写复制构造函数,因为可以不写,此处不涉及深浅拷贝不一样的情况。
还有对于+,主函数有对象+字符串操作,这里编译器会调用带参构造函数将字符串转换成对象,所以很多行自然而然能实现了。
完整代码如下:
#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[30] = { 0 };
public:
MyString() {
}
MyString(const char* s)
{
strcpy(str, s);
}
friend ostream& operator<<(ostream& o, MyString& mystr)
{
cout << mystr.str;
return o;
}
friend MyString operator+(MyString mystr1,MyString mystr2)
{
MyString mystr3(mystr1);
strcat(mystr3.str, mystr2.str);
return mystr3;
}
char& operator [](int i)
{
return str[i];
}
MyString& operator +=(const char* mystr)
{
strcat(str, mystr);
return *this;
}
char* operator()(int begin, int leng)
{
char *c=new char[30]; int i;
for ( i = 0; i <leng; i++)
{
c[i] = str[begin + i];
}
c[i + 1] = '\0';
return c;
}
bool operator<(MyString mystr)
{
if (strcmp(str, mystr.str) == -1)
return 1;
return 0;
}
bool operator==(MyString mystr)
{
if (strcmp(str, mystr.str) == 0)
return 1;
return 0;
}
bool operator>(MyString mystr)
{
if (strcmp(str, mystr.str) == 1)
return 1;
return 0;
}
};
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;
}
020:继承自string的MyString
这个题很有意思,由于是继承,string类里实现的函数很多,不用我们写了,调用时相当于调用string类里的方法。
主函数跟上一题一样,但是此处经分析只需要写3个构造函数,一个重载()即可
构造函数
MyString() {
}
MyString(const char* s):string(s)
{
}
MyString(string mystr):string(mystr)
{
}
一个无参,一个参数为char,一个为string,只要使用初始化列表,让他们去给string类的变量赋值即可,MyString也能当string用
重载()
string operator()(int begin, int leng)
{
string c = substr(begin, leng);
return c;
}
这个重载也很容易,用string 库里的substr即可,返回string可以直接输出。
如果返回MyString也可以,MyString就是当string用的。
完整代码如下:
#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class MyString :public string
{
// 在此处补充你的代码
private:
//string str;
public:
MyString() {
}
MyString(const char* s):string(s)
{
}
MyString(string mystr):string(mystr)
{
}
MyString operator()(int begin, int leng)
{
MyString c = substr(begin, leng);
return c;
}
};
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;
sort(SArray, SArray + 4);
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;
}
标签:return,string,int,C++,char,poj,MyString,重载,程序设计
From: https://blog.csdn.net/qq_74924951/article/details/139576069