描述

写一个MyString 类,使得下面程序的输出结果是:

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类必须是从C++的标准类string类派生而来。提示1:如果将程序中所有 "MyString" 用"string" 替换,那么题目的程序中除了最后两条语句编译无法通过外,其他语句都没有问题,而且输出和前面给的结果吻合。也就是说,MyString类对 string类的功能扩充只体现在最后两条语句上面。提示2: string类有一个成员函数 string substr(int start,int length); 能够求从 start位置开始,长度为length的子串

程序:

#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
// 在此处补充你的代码
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-样例输入

样例输出

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-

找了几个参考答案,但是提交的时候都失败了。

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class MyString : public string
{
public:
MyString() {};
//1.0继承类继承父类所有的成员变量和成员函数,但不继承构造函数和析构函数
//1.1继承类的无参构造函数,会隐式调用父类的无参构造函数
MyString(const char * st) :string(st) {};
//1.2继承类的有参构造函数,如果父类也有有参构造函数,则必须显示调用它
//2.0这里的参数根据reference有两种选择,此处必须用const char*,"xxx"的类型是const char*
MyString(const MyString& s):string(s){}
//1.3继承类的复制构造函数必须要显示的调用父类的复制构造函数,不然就会默认调用父类的无参构造函数
MyString operator +(MyString & op2)
{
string s1 = *this;
string s2 = op2;
string s = s1 + s2;
return *new MyString(s.c_str());
}
MyString & operator +(const char * cs2)
{
string str1 = *this;
string s = str1 + cs2;
return *new MyString(s.c_str());
} MyString & operator()(int s, int l)
{
string str = substr(s, l);
return *new MyString(str.c_str());
}
}; MyString operator+(const char * op1, MyString & op2)
{
string st2 = op2;
string s = op1 + st2;
return *new MyString(s.c_str());
} 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-");
MyString s4(s1);
MyString SArray[4] = { "big","me","about","take" };
//这里等号右边的赋值操作相当于调用了MyString的转换构造函数,其实就是单一非const classname&参数的构造函数可以直接接受参数类型的变量
cout << "1. " << s1 << s2 << s3 << s4 << endl;
s4 = s3;
//3.0 operator=可以直接用string类里面的
s3 = s1 + 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;
cout << s1(0, 4) << endl;
cout << s1(5, 10) << endl;
return 0;
}
---------------------
作者:qq_23908539
来源:CSDN
原文:https://blog.csdn.net/qq_23908539/article/details/51454521
版权声明:本文为博主原创文章,转载请附上博文链接!

  

这一个解释的还算比较详细,贴在上面供以后复习使用。

5w5:第五周程序填空题1的更多相关文章

  1. 程序设计实习MOOC / 继承和派生——编程作业 第五周程序填空题1

    描述 写一个MyString 类,使得下面程序的输出结果是: 1. abcd-efgh-abcd- 2. abcd- 3. 4. abcd-efgh- 5. efgh- 6. c 7. abcd- 8 ...

  2. 9w5:第九周程序填空题1

    描述 下面的程序输出结果是: 1 2 6 7 8 9 请填空: #include <iostream> #include <iterator> #include <set ...

  3. 6w6:第六周程序填空题3

    描述 下面的程序输出结果是: A::Fun A::Do A::Fun C::Do 请填空: #include <iostream> using namespace std; class A ...

  4. 6w4:第六周程序填空题1

    描述 下面程序的输出结果是: A::Fun C::Do 请填空: #include <iostream> using namespace std; class A { private: i ...

  5. 6w5:第六周程序填空题2

    描述 下面程序的输出结果是: destructor B destructor A 请完整写出 class A. 限制条件:不得为 class A 编写构造函数. #include <iostre ...

  6. 集训第五周 动态规划 B题LIS

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Des ...

  7. 集训第五周动态规划 F题 最大子矩阵和

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

  8. 集训第五周动态规划 D题 LCS

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  9. 计算机二级-C语言-程序填空题-190109记录-对二维字符串数组的处理

    //给定程序,函数fun的功能是:求出形参ss所指字符串数组中最长字符串的长度,将其余字符串右边用字符*补齐,使其与最长的字符串等长.ss所指字符串数组中共有M个字符串,且串长<N. //重难点 ...

随机推荐

  1. 分析java类的静态成员变量初始化先于非静态成员变量

    依上图中当class字节码文件被jvm虚拟机加载到内存中依次经过 连接 验证:对字节码进行验证 准备:给静态变量分配内存并赋予变量类型各自的默认值(注:基本类型为0或false,对象为null,sta ...

  2. 【linux】下载命令lftp\sftp\wget

    sftp sftp(安全文件传输协议)与ftp 有着几乎一样的语法和功能.FTP 为 SSH的一部份,是一种传输档案至 Blogger 伺服器的安全方式.它并不使用ftp守护进程(ftpd或wu-ft ...

  3. python's twenty-third day for me 面向对象进阶

    普通方法:对象和类绑定的过程. class A: def func1(self):pass def func2(self):pass def func3(self):pass def func4(se ...

  4. autoit 简单使用-- 操作windows窗口

    autoit 简单使用 autoit中文交流论坛: http://www.autoitx.com/index.php 1 下载 autoit 中文安装包 : https://yunpan.cn/OcP ...

  5. cin cout getline string

    1.C++ code, When we want to read a number whatever the type is int or double , just use cin >> ...

  6. libevent源码深度剖析一

    libevent源码深度剖析一 ——序幕 张亮 1 前言 Libevent是一个轻量级的开源高性能网络库,使用者众多,研究者更甚,相关文章也不少.写这一系列文章的用意在于,一则分享心得:二则对libe ...

  7. 12-在eclipse上安装lxml

    1.可用easy_install安装方式,也可以用pip的方式: pip install lxml 2.安装完毕:写代码导包时提示错误,这是需要配置一下eclipse,是因为它没有更新导入的包,所以需 ...

  8. 多线程编程为什么要sleep

    以前在分析程序的时候就遇到很多sleep,当时很觉得很奇怪?不直到为什么要调用这个sleep,现在明白了,这种情况一般是应用程序有多个线程,那怎么手动切换线程呢?调用sleep即可.

  9. Reddit指南

    一.简介 Reddit是网页新鲜事件和热点事件的源头,它也是互联网最有趣的地方和有高度组织的链接集中处.   二.指南 http://article.yeeyan.org/view/523415/45 ...

  10. 洛谷P2569 [SCOI2010]股票交易

    P2569 [SCOI2010]股票交易 题目描述 最近lxhgww又迷上了投资股票,通过一段时间的观察和学习,他总结出了股票行情的一些规律. 通过一段时间的观察,lxhgww预测到了未来T天内某只股 ...