1、D01:全面的MyString
总时间限制: 1000ms 内存限制: 65536kB
描述
程序填空,输出指定结果
#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 *pstr;
public:
MyString()
{
pstr = new char[1];
*pstr = '\0';
}
MyString(const char *str)
{
int len = strlen(str);
pstr = new char[len + 1];
strcpy(pstr,str);
}
MyString(const MyString &rhs)
{
int len = strlen(rhs.pstr);
pstr = new char[len + 1];
strcpy(pstr,rhs.pstr);
}
MyString & operator=(const MyString & rhs)
{
if(this == & rhs)
return *this;
delete []pstr;
int len = strlen(rhs.pstr);
pstr = new char[len + 1];
strcpy(pstr,rhs.pstr);
return *this;
}
MyString & operator +=(const MyString &rhs)
{
MyString temp(*this);
return *this = temp + rhs;
}
friend MyString operator+(const MyString & x,const MyString & y)
{
char * temp = new char[strlen(x.pstr) + strlen(y.pstr) + 1];
strcpy(temp,x.pstr);
strcat(temp,y.pstr);
MyString re(temp);
delete []temp;
return re ;
}
friend ostream & operator <<(ostream & os,const MyString & rhs)
{
os << rhs.pstr;
return os;
}
friend bool operator<(const MyString &x,const MyString & y)
{
return strcmp(x.pstr,y.pstr) < 0;
}
friend bool operator==(const MyString &x,const MyString & y)
{
return strcmp(x.pstr,y.pstr) == 0;
}
friend bool operator>(const MyString &x,const MyString & y)
{
return strcmp(x.pstr,y.pstr) > 0;
}
char& operator[](int n)
{
return *(pstr + n);
}
MyString operator()(int b,int len)
{
char * temp = new char[strlen(pstr)+1];
strcpy(temp,pstr+b);
*(temp+len) = '\0';
MyString re(temp);
delete []temp;
return re;
}
~MyString(){delete[]pstr;}
// 在此处结束你补充的代码
};
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"};
//根据main函数的执行,需要重载的运算符依次有
// << = + [] += < == > (),其中+ = += 的参数可能不止一种
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;
//没有编写参数为char*的赋值运算符重载函数,实际是用ijkl-构造了一个临时对象
//然后call赋值运算符重载函数
s1 = "ijkl-";
s1[2] = 'A' ;
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
//operator+(const MyString &,const MyString &)为友元函数的必要性
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-
样例输入
无
样例输出
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-
来源
Guo Wei
2、D02:继承自string的MyString
总时间限制: 1000ms 内存限制: 65536kB
描述
程序填空,输出指定结果
#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
//要求通过继承的方式,使用基类的相同功能,体现代码复用
class MyString:public string
{
// 在此处开始补充你的代码
public:
MyString() :string() {}
MyString(const char *pstr):string(pstr){}
//这里有两种可用的形式
//MyString(const MyString & rhs) :string(rhs.string::c_str()) {}
//如果不清楚基类对象与派生类对象的赋值兼容规则确定应该选取保险的上一个形式
//提示指出需要基类对象作为函数参数时,可以用派生类对象取而代之,故有了第二种形式
MyString(const MyString & rhs) :string(rhs) {}
MyString & operator=(const MyString & rhs)
{
string::assign(rhs.string::c_str());
return *this;
}
friend MyString operator+(const MyString & x, const MyString & y)
{
MyString temp(x);
temp.string::append(y.string::c_str());
return temp;
}
friend MyString operator+(const MyString & x, const char * pstr)
{
MyString temp(pstr);
return x + temp;
}
friend MyString operator+(const char * x, const MyString & y)
{
MyString temp(x);
return temp + y;
}
friend ostream & operator<<(ostream & os, const MyString &rhs)
{
os << rhs.string::c_str();
return os;
}
MyString operator()(int b, int len)
{
string s(string::c_str());
s = s.substr(b, len);
return MyString(s.c_str());
}
// 在此处结束你补充的代码
};
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;
}
输入
无
输出
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-
样例输入
无
样例输出
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-
提示
提示 1:如果将程序中所有 “MyString” 用 “string” 替换,那么除
了最后两条红色的语句编译无法通过外,其他语句都没有问题,而且输出和前
面给的结果吻合。也就是说,MyString 类对 string 类的功能扩充只体现在最
后两条语句上面。
提示 2: string 类有一个成员函数 string substr(int start,int
length); 能够求从 start 位置开始,长度为 length 的子串
提示 3: C++中,派生类的对象可以赋值给基类对象,因为,一个派生
类对象,也可看作是一个基类对象(大学生是学生)。反过来则不行(学生未
必是大学生) 同样,调用需要基类对象作参数的函数时,以派生类对象作为实参,也是没有问题的
来源
Guo Wei