程序设计与算法(三)第五周测验(2018春季) 1:全面的MyString

题目网址:http://cxsjsxmooc.openjudge.cn/2018t3springw5/1/

题目描述

程序填空,输出指定结果

 1 #include <cstdlib>
 2 #include <iostream>
 3 using namespace std;
 4 int strlen(const char * s) 
 5 {    int i = 0;
 6     for(; s[i]; ++i);
 7     return i;
 8 }
 9 void strcpy(char * d,const char * s)
10 {
11     int i = 0;
12     for( i = 0; s[i]; ++i)
13         d[i] = s[i];
14     d[i] = 0;
15         
16 }
17 int strcmp(const char * s1,const char * s2)
18 {
19     for(int i = 0; s1[i] && s2[i] ; ++i) {
20         if( s1[i] < s2[i] )
21             return -1;
22         else if( s1[i] > s2[i])
23             return 1;
24     }
25     return 0;
26 }
27 void strcat(char * d,const char * s)
28 {
29     int len = strlen(d);
30     strcpy(d+len,s);
31 }
32 class MyString
33 {
34 // 在此处补充你的代码
35 };
36 
37 
38 int CompareString( const void * e1, const void * e2)
39 {
40     MyString * s1 = (MyString * ) e1;
41     MyString * s2 = (MyString * ) e2;
42     if( * s1 < *s2 )
43     return -1;
44     else if( *s1 == *s2)
45     return 0;
46     else if( *s1 > *s2 )
47     return 1;
48 }
49 int main()
50 {
51     MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
52     MyString SArray[4] = {"big","me","about","take"};
53     cout << "1. " << s1 << s2 << s3<< s4<< endl;
54     s4 = s3;
55     s3 = s1 + s3;
56     cout << "2. " << s1 << endl;
57     cout << "3. " << s2 << endl;
58     cout << "4. " << s3 << endl;
59     cout << "5. " << s4 << endl;
60     cout << "6. " << s1[2] << endl;
61     s2 = s1;
62     s1 = "ijkl-";
63     s1[2] = 'A' ;
64     cout << "7. " << s2 << endl;
65     cout << "8. " << s1 << endl;
66     s1 += "mnop";
67     cout << "9. " << s1 << endl;
68     s4 = "qrst-" + s2;
69     cout << "10. " << s4 << endl;
70     s1 = s2 + s4 + " uvw " + "xyz";
71     cout << "11. " << s1 << endl;
72     qsort(SArray,4,sizeof(MyString),CompareString);
73     for( int i = 0;i < 4;i ++ )
74     cout << SArray[i] << endl;
75     //s1的从下标0开始长度为4的子串
76     cout << s1(0,4) << endl;
77     //s1的从下标5开始长度为10的子串
78     cout << s1(5,10) << endl;
79     return 0;
80 }

 

输入

  无

输出

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 class MyString
 2 {
 3 private:
 4     char* p;
 5     int len;
 6 public:
 7     MyString(const char* a){
 8         len=strlen(a);
 9         p=new char[len+1];
10         strcpy(p,a);
11     }    
12     MyString(){
13         len=1;
14         p=new char[1];
15         strcpy(p,"");
16     }
17     MyString(const MyString& a){
18         len=strlen(a.p);
19         p=new char[len+1];
20         strcpy(p,a.p);
21     }
22     friend ostream& operator<<(ostream& o,const MyString& a){
23     o<<a.p;
24     return o;
25     }
26     MyString& operator=(const MyString& a){
27         if(p==a.p){
28             return *this;
29         }
30         if(p)
31             delete p;
32         len=strlen(a.p);
33         p=new char[len+1];
34         strcpy(p,a.p);
35         return *this;
36     }
37     char& operator[](int i){
38         return p[i];
39     }
40     friend MyString operator+(const MyString& a,const MyString& b){
41         int l=b.len+a.len;
42         char *c=new char[l+1];
43         strcpy(c,a.p);
44         int i ;
45         int j=0;
46     for( i = a.len; i<=l-1; ++i,++j)
47         c[i]=b.p[j];
48     c[i] = 0;
49     return MyString(c);
50     }
51     void operator +=(const char *a){
52         int i=len;
53         char *b=new char[len+1];
54         strcpy(b,p);
55         delete p;
56         len=len+strlen(a);
57         p=new char[len+1];
58         strcpy(p,b);
59         int j=0;
60         for(;i<=len-1;i++,j++){
61             p[i]=a[j];
62         }
63         p[i]=0;
64     }
65     friend bool operator<(const MyString& a,const MyString& b){
66         if(strcmp(b.p,a.p)==1){
67             return 1;
68         }
69         return 0;
70     }
71     friend bool operator>(const MyString& a,const MyString& b){
72         if(strcmp(a.p,b.p)==1){
73         return 1;
74         }
75         return 0;
76     }
77     friend bool operator==(const MyString& a,const MyString& b){
78         if(strcmp(a.p,b.p)==0){
79             return 1;
80         }
81         return 0;
82     }    
83     char* operator()(int a,int l){
84         char* c=new char[l+1];
85         int j=0;
86         for(int i=a;i<a+l;i++,j++){
87             c[j]=p[i];
88         }
89         c[j]=0;
90         return c;
91     }
92 };

 

posted @ 2018-04-19 23:18  咖啡君056  阅读(1484)  评论(0编辑  收藏  举报