编程题#1

来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)

注意: 总时间限制: 1000ms 内存限制: 65536kB

描述

写一个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 <cstring>
#include <cstdlib>
using namespace std;
// 在此处补充你的代码
class MyString:public string {
public:
MyString():string(){};
MyString(const char *s):string(s) {};
MyString(const string &s):string(s) {};
MyString operator()(int i, int j) {
return this->substr(i,j);
}
};
int CompareString( const void * e1, const void * e2) {
MyString * s1 = (MyString * ) e1;
MyString * s2 = (MyString * ) e2;
if( *s1 < *s2 ) return -;
else if( *s1 == *s2 ) return ;
else if( *s1 > *s2 ) return ;
}
int main() {
MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
MyString SArray[] = {"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[] << endl;
s2 = s1; s1 = "ijkl-";
s1[] = '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,,sizeof(MyString), CompareString);
for( int i = ;i < ;++i )
cout << SArray[i] << endl;
//输出s1从下标0开始长度为4的子串
cout << s1(,) << endl;
//输出s1从下标为5开始长度为10的子串
cout << s1(,) << endl;
return ;
}

POJ C++程序设计 编程题#1 编程作业—继承与派生的更多相关文章

  1. C++第四次作业--继承与派生

    C++ 继承 面向对象程序设计中最重要的一个概念是继承.继承允许我们依据另一个类来定义一个类,这使得创建和维护一个应用程序变得更容易.这样做,也达到了重用代码功能和提高执行效率的效果. 当创建一个类时 ...

  2. 04737_C++程序设计_第6章_继承和派生

    例6.1 使用默认内联函数实现单一继承. #include<iostream> using namespace std; class Point { private: int x, y; ...

  3. POJ C++程序设计 编程题#1 编程作业—STL1

    编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的程序输出结 ...

  4. POJ C++程序设计 编程题#3 编程作业—文件操作与模板

    编程题#3: 整数的输出格式 来源: POJ(Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 1000kB 描述 利 ...

  5. POJ C++程序设计 编程题#2 编程作业—文件操作与模板

    编程题#2: 实数的输出格式 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 1000kB 描述 ...

  6. POJ C++程序设计 编程题#1 编程作业—文件操作与模板

    编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 实现一个三维数组 ...

  7. POJ C++程序设计 编程题#3 编程作业—多态与虚函数

    编程题 #3 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的程序输出 ...

  8. POJ C++程序设计 编程题#2 编程作业—多态与虚函数

    编程题#2 来源: POJ(Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面程序的输出结果 ...

  9. POJ C++程序设计 编程题#1 编程作业—多态与虚函数

    编程题 #1 来源: POJ(Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 下面程序的输出结果是: ...

随机推荐

  1. jenkins参数化构建过程

    http://www.cnblogs.com/meitian/p/5492457.html 1,首先增加Jenkisn插件 https://wiki.jenkins-ci.org/display/JE ...

  2. struts (三)

    1. <action name="test" class="com.gc.Test"> <result name="success& ...

  3. Java中的Comparable<T>和Comparator<T>接口

    有的时候在面试时会被问到Comparable<T>和Comparator<T>的区别(或者Java中两种排序功能的实现区别). 1) 在使用普通数组的时候,如果想对数据进行排序 ...

  4. VisualVM 的 OQL 的一些例子

    Visual VM的OQL语言是对HeapDump进行查询,类似于SQL的查询语言,它的基本语法如下: select <JavaScript expression to select> [ ...

  5. Highcharts 对数组的要求

    function Reflush(phaid,proid) { $.post('GetProjectSummer.ashx', { proid: proid, phaid: phaid }, func ...

  6. 图标的使用————JAVA——Swing

    public class MyImageIcon extends JFrame{    public MyImageIcon()    {    JFrame jf=new JFrame();     ...

  7. 【转】windows和linux间共享互传文件

    原文:http://blog.guorunmin.cn/2015/09/16/windows%E5%92%8Clinux%E9%97%B4%E5%85%B1%E4%BA%AB%E4%BA%92%E4% ...

  8. Redis数据持久化之AOF持久化

    一.RDB持久化的缺点创建RDB文件需要将服务器所有的数据库的数据都保存起来,这是一个非常耗费资源和时间的操作,所以服务器需要隔一段时间才能创建一个新的RDB文件,就也是说创建RDB文件的操作不能执行 ...

  9. [工具] 如何利用Notepad++去除重复行

    问题: 需要去除重复数据, 例如: 解决方案: 1. 打开notepad++: 2. 如果没有找到"TextFx" 选项, 需要先安装该插件. 依次打开"插件" ...

  10. javascript各种兼容性问题,不断更新

    ie6-ie8 不支持textContent支持innerTextchrome  支持textContent  innerTextfireFox    仅支持textContent不支持innerTe ...