程序設計實習——MOOC 全面的MyString


編程題#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-

 

解答:

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 using namespace std;
  5 
  6 class MyString
  7 {
  8 public:
  9     MyString() { sValue = ""; };
 10     MyString(const char *s) { 
 11         int n = strlen(s);
 12         sValue = new char[n + 1];
 13         int i;
 14         for (i = 0; i < n; i++)
 15             sValue[i] = s[i];
 16         sValue[i] = '\0';
 17     };
 18     MyString(const string &s) { 
 19         sValue = new char[strlen(s.c_str()) + 1];
 20         strcpy(sValue, s.c_str()); 
 21     };
 22 
 23     MyString operator()(int i, int j) {
 24         MyString res;
 25         res.sValue = new char[j];
 26         int start=i;
 27         int idx = 0;
 28         for (idx=0; idx < j; idx++)
 29             res.sValue[idx] = sValue[start++];
 30         res.sValue[idx] = '\0';
 31         return res;
 32     }
 33     char& operator[](int i) { return sValue[i]; }
 34     friend ostream& operator<<(ostream &os,const MyString &s)
 35     {
 36         if (s.sValue == NULL)
 37             return os;
 38         else
 39         {
 40             os <<s.sValue;
 41             return os;
 42         }
 43     }
 44     friend MyString operator+(const char*str, const MyString &rhs)
 45     {
 46         MyString res(str);
 47         res = res + rhs;
 48         return res;
 49     }
 50     MyString operator+(const MyString &rhs)
 51     {
 52         MyString res;
 53         int n = strlen(sValue) + strlen(rhs.sValue);
 54         res.sValue = new char[n];
 55         strcpy(res.sValue, sValue);
 56         strcat(res.sValue, rhs.sValue);
 57         return res;
 58     }
 59     MyString operator+(const char *rhs)
 60     {
 61         MyString res;
 62         MyString r(rhs);
 63         res = *this + r;
 64         return res;
 65     }
 66     MyString& operator+=(const char *rhs) {
 67         MyString res;
 68         MyString r(rhs);
 69         res = *this + r;
 70         *this = res;
 71         return *this;
 72     }
 73     bool operator < (const MyString & rhs) {
 74         int flag=strcmp(sValue, rhs.sValue);
 75         if (flag == -1)
 76             return true;
 77         else
 78         {
 79             return false;
 80         }
 81     }
 82     bool operator > (const MyString & rhs) {
 83         int flag = strcmp(sValue, rhs.sValue);
 84         if (flag == 1)
 85             return true;
 86         else
 87         {
 88             return false;
 89         }
 90     }
 91     bool operator ==(const MyString & rhs) {
 92         int flag = strcmp(sValue, rhs.sValue);
 93         if (flag == 0)
 94             return true;
 95         else
 96         {
 97             return false;
 98         }
 99     }
100 private:
101     char* sValue;
102 };
103 
104 
105 int compareString(const void * e1, const void * e2)
106 {
107     MyString *s1 = (MyString *)e1;
108     MyString *s2 = (MyString *)e2;
109     if (*s1 < *s2)
110         return -1;
111     else if (*s1 == *s2)
112         return 0;
113     else if (*s1>*s2)
114         return 1;
115 }
116 
117 int main()
118 {
119     MyString s1("abcd-"), s2, s3("efgh-"), s4(s1);
120     MyString SArray[4] = { "big","me","about","take" };
121     cout << "1. " << s1 << s2 << s3 << s4 << endl;
122     s4 = s3; s3 = s1 + s3;
123     cout << "2. " << s1 << endl;
124     cout << "3. " << s2 << endl;
125     cout << "4. " << s3 << endl;
126     cout << "5. " << s4 << endl;
127     cout << "6. " << s1[2] << endl;
128     s2 = s1; s1 = "ijkl-";
129     s1[2] = 'A';
130     cout << "7. " << s2 << endl;
131     cout << "8. " << s1 << endl;
132     s1 += "mnop";
133     cout << "9. " << s1 << endl;
134     s4 = "qrst-" + s2;
135     cout << "10. " << s4 << endl;
136     s1 = s2 + s4 + " uvw " + "xyz";
137     cout << "11. " << s1 << endl;
138     qsort(SArray, 4, sizeof(MyString), compareString);
139     for (int i = 0; i < 4; ++i)
140         cout << SArray[i] << endl;
141     //輸出s1從下標0開始長度為4的子串
142     cout << s1(0, 4) << endl;
143     //輸出s1從下標為5開始長度為10的子串
144     cout << s1(5, 10) << endl;
145     system("pause");
146     return 0;
147 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM