• POJ C++程序设计 编程题#1 编程作业—继承与派生


    编程题#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-

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdlib>
     4 using namespace std;
     5 // 在此处补充你的代码
     6 class MyString:public string {
     7 public:
     8     MyString():string(){};
     9     MyString(const char *s):string(s) {};
    10     MyString(const string &s):string(s) {};
    11     MyString operator()(int i, int j) {
    12         return this->substr(i,j);
    13     }
    14 };
    15 int CompareString( const void * e1, const void * e2) {
    16     MyString * s1 = (MyString * ) e1;
    17     MyString * s2 = (MyString * ) e2;
    18     if( *s1 < *s2 ) return -1;
    19     else if( *s1 == *s2 ) return 0;
    20     else if( *s1 > *s2 ) return 1;
    21 }
    22 int main() {
    23     MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
    24     MyString SArray[4] = {"big","me","about","take"};
    25     cout << "1. " << s1 << s2 << s3<< s4<< endl;
    26     s4 = s3; s3 = s1 + s3;
    27     cout << "2. " << s1 << endl;
    28     cout << "3. " << s2 << endl;
    29     cout << "4. " << s3 << endl;
    30     cout << "5. " << s4 << endl;
    31     cout << "6. " << s1[2] << endl;
    32     s2 = s1; s1 = "ijkl-";
    33     s1[2] = 'A' ;
    34     cout << "7. " << s2 << endl;
    35     cout << "8. " << s1 << endl;
    36     s1 += "mnop";
    37     cout << "9. " << s1 << endl;
    38     s4 = "qrst-" + s2;
    39     cout << "10. " << s4 << endl;
    40     s1 = s2 + s4 + " uvw " + "xyz";
    41     cout << "11. " << s1 << endl;
    42     qsort(SArray,4,sizeof(MyString), CompareString);
    43     for( int i = 0;i < 4;++i )
    44         cout << SArray[i] << endl;
    45     //输出s1从下标0开始长度为4的子串
    46     cout << s1(0,4) << endl;
    47     //输出s1从下标为5开始长度为10的子串
    48     cout << s1(5,10) << endl;
    49     return 0;
    50 }
  • 相关阅读:
    phxpaxos状态机的管理
    redis基础
    phxpaxos的checkpoint从哪个server同步
    结合phxpaxos简单看下paxos
    levelDB实现相关概念及基础
    在变参模版出现之前,functional如何实现bind功能
    IntelliJ IDEA自动部署项目至远程服务器与传统部署项目至远程服务器的区别
    Python实现王者荣耀小助手(二)
    Python实现王者荣耀小助手(一)
    高性能网络通信框架 HP-Socket
  • 原文地址:https://www.cnblogs.com/dagon/p/4759552.html
Copyright © 2020-2023  润新知