A:全面的MyString

描述

程序填空,输出指定结果

#include <cstdlib>
#include <iostream>
using namespace std;
int strlen(const char * s) 
{    int i = 0;
    for(; s[i]; ++i);
    return i;
}
void strcpy(char * d,const char * s)
{
    int i = 0;
    for( i = 0; s[i]; ++i)
        d[i] = s[i];
    d[i] = 0;
        
}
int strcmp(const char * s1,const char * s2)
{
    for(int i = 0; s1[i] && s2[i] ; ++i) {
        if( s1[i] < s2[i] )
            return -1;
        else if( s1[i] > s2[i])
            return 1;
    }
    return 0;
}
void strcat(char * d,const char * s)
{
    int len = strlen(d);
    strcpy(d+len,s);
}
class MyString
{
// 在此处补充你的代码
};


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-

来源Guo Wei

 1     char *p;
 2 public:
 3     //构造函数
 4     MyString(const char *a){
 5         //if(p)delete[]p;
 6         p=new char[strlen(a)+1];
 7         strcpy(p,a);
 8     }
 9     MyString(){
10         p=new char[1];
11         p[0]='\0';
12     }
13     //copy
14     MyString(const MyString&s){
15         p=new char[strlen(s.p)+1];
16         strcpy(p,s.p);
17     }
18     //destruct
19     ~MyString(){if(p)delete[]p;}
20     //cout
21     friend ostream&operator<<(ostream&os,MyString &a){
22         os<<a.p;
23         return os;
24     }
25     //class+class
26     friend MyString operator+(const MyString &a,const MyString &b){
27         char *res=new char[strlen(b.p)+strlen(a.p)+1];
28         strcpy(res,a.p);
29         strcat(res,b.p);
30         return MyString(res);
31     }
32     //[]
33     char&operator[](int x){
34         return p[x];
35     }
36     //class=class
37     MyString& operator=(const MyString&a){
38         if(p)delete[]p;
39         p=new char[strlen(a.p)+1];
40         strcpy(p,a.p);
41         return *this;
42     }
43     //class=char
44     MyString& operator=(char *s){
45         if(p)delete[]p;
46         p=new char[strlen(s)+1];
47         strcpy(p,s);
48         return *this;
49     }
50     //class+=char
51     MyString operator +=(const char *s){
52         *this=*this+MyString(s);
53         return *this;
54     }
55     //char+class
56     friend MyString operator +(char *s,MyString&a){
57         return MyString(s)+a;
58     }
59     //class+char
60     MyString operator +(char*s){
61         return *this+MyString(s);
62     }
63     //<
64     friend bool operator<(MyString&a,MyString&b){
65         return (strcmp(a.p,b.p)==-1);
66     }
67     //>
68     friend bool operator>(MyString&a,MyString&b){
69         return (strcmp(a.p,b.p)==1);
70     }
71     //=
72     friend bool operator==(MyString&a,MyString&b){
73         return (strcmp(a.p,b.p)==0);
74     }
75     //()
76     char*operator()(int x,int y){
77        char *ptr=new char[y+1];
78        for(int i=0;i<y;i++){
79            ptr[i]=p[i+x];
80        }
81        ptr[y]='\0';
82        return ptr;
83     }
View Code

相关文章:

  • 2022-02-08
  • 2022-12-23
  • 2022-12-23
  • 2021-11-14
  • 2021-11-26
  • 2021-12-20
  • 2021-10-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-08
  • 2021-08-25
  • 2021-08-26
  • 2021-07-09
  • 2021-11-16
相关资源
相似解决方案