程序设计实习——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 }

 

转载于:https://www.cnblogs.com/baoaolei/p/5458529.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值