1. 程式人生 > >5w5:第五週程式填空題1

5w5:第五週程式填空題1

描述

寫一個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 
版權宣告:本文為博主原創文章,轉載請附上博文連結!

  

這一個解釋的還算比較詳細,貼在上面供以後複習使用。