• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 183,  comments - 10,  trackbacks - 0
            設計一個字符串類 String,實現
            ·+= 連接兩個字符串
            ·+   連接兩個字符串
            ·== 判斷兩個字符串是否相等
            ·<   判斷兩個字符串大小關系

            類的聲明:
            class String
            {
            private:
                
            char* mychar;
                
            int   len;
            public:
                
            };

            代碼:
              1 #include <iostream>
              2 #include <cstring>
              3 using namespace std;
              4 
              5 class String
              6 {
              7 private:
              8     char* mychar;
              9     int len;
             10 private:
             11     void clear()
             12     {
             13         delete[] mychar;
             14         mychar = 0;
             15         len = 0;
             16     }
             17 public:
             18     String(char* s = "")
             19     {
             20         int l = strlen(s);
             21         mychar = new char[l + 1];
             22         if (mychar != 0)
             23         {
             24             strcpy(mychar, s);
             25             len = l;
             26         }
             27     }
             28     String(const String& s)
             29     {
             30         mychar = new char[s.len + 1];
             31         if (mychar != 0)
             32         {
             33             strcpy(mychar, s.mychar);
             34             len = s.len;
             35         }
             36     }
             37     String& operator=(const String& s)
             38     {
             39         if (this != &s)
             40         {
             41             clear();
             42             mychar = new char[s.len + 1];
             43             if (mychar != 0)
             44             {
             45                 strcpy(mychar, s.mychar);
             46                 len = s.len;
             47             }
             48         }
             49         return *this;
             50     }
             51     ~String()
             52     {
             53         clear();
             54     }
             55     String& operator +=(const String& s)
             56     {
             57         int l = len + s.len + 1;
             58         char* t = new char[l];
             59         if (t != 0)
             60         {
             61             // 初始化內存
             62             memset(t, 0sizeof (char* l);
             63             strcat(t, mychar);
             64             strcat(t, s.mychar);
             65         }
             66         delete [] mychar;
             67         mychar = t;
             68         len = l;
             69         return *this;
             70     }
             71 
             72     friend String operator +(const String& lhs, const String& rhs);
             73     friend bool operator ==(const String& lhs, const String& rhs);
             74     friend bool operator < (const String& lhs, const String& rhs);
             75     friend istream& operator >>(istream& in, String& s);
             76     friend ostream& operator <<(ostream& outconst String& s);
             77 };
             78 
             79 String operator +(const String& lhs, const String& rhs)
             80 {
             81     String t(lhs);
             82     return t += rhs;
             83 }
             84 
             85 bool operator==(const String& lhs, const String& rhs)
             86 {
             87     return strcmp(lhs.mychar, rhs.mychar) == 0;
             88 }
             89 
             90 bool operator<(const String& lhs, const String& rhs)
             91 {
             92     return strcmp(lhs.mychar, rhs.mychar) < 0;
             93 }
             94 
             95 bool operator >(const String& lhs, const String& rhs)
             96 {
             97     return !(lhs < rhs) && !(lhs == rhs);
             98 }
             99 
            100 bool operator <=(const String& lhs, const String& rhs)
            101 {
            102     return lhs < rhs || lhs == rhs;
            103 }
            104 
            105 bool operator >=(const String& lhs, const String& rhs)
            106 {
            107     return !(lhs < rhs);
            108 }
            109 
            110 istream& operator >>(istream& in, String& s)
            111 {
            112     s.clear();
            113     char t[100];
            114     in >> t;
            115     if (!in)
            116     {
            117         cerr << "Input error!" << endl;
            118         exit(1);
            119     }
            120     int l = strlen(t);
            121     s.mychar = new char[l + 1];
            122     if (s.mychar != 0)
            123     {
            124         strcpy(s.mychar, t);
            125         s.len = l;
            126     }
            127     return in;
            128 }
            129 
            130 ostream& operator <<(ostream& outconst String& s)
            131 {
            132     out << s.mychar;
            133     return out;
            134 }
            135 
            136 int main()
            137 {
            138     String s1, s2;
            139     while (cin >> s1 >> s2)
            140     {
            141         cout << s1 << endl;
            142         cout << s2 << endl;
            143         cout << (s1 == s2) << endl;
            144         cout << (s1 < s2)  << endl;
            145         cout << (s1 > s2)  << endl;
            146         cout << (s1 <= s2) << endl;
            147         cout << (s1 >= s2) << endl;
            148 
            149         cout << s1 + s2 << endl;
            150         s1 += s2;
            151         cout << s1 << endl;
            152         cout << s2 << endl;
            153         s2 += s1;
            154         cout << s1 << endl;
            155         cout << s2 << endl;
            156     }
            157     return 0;
            158 }
            posted on 2011-04-22 13:12 unixfy 閱讀(173) 評論(0)  編輯 收藏 引用
            久久亚洲日韩精品一区二区三区| a级毛片无码兔费真人久久| 国产免费久久精品99re丫y| 久久精品国产2020| 久久久女人与动物群交毛片| 欧美亚洲国产精品久久蜜芽| 久久综合一区二区无码| 精品久久8x国产免费观看| 精品一久久香蕉国产线看播放| 色综合久久夜色精品国产| 久久国产精品99国产精| 人妻精品久久久久中文字幕| 久久久久久毛片免费播放| 亚洲v国产v天堂a无码久久| 久久99精品久久久久久hb无码| 国产精品内射久久久久欢欢| 亚洲αv久久久噜噜噜噜噜| 久久精品视屏| 99久久精品国产一区二区| 精品久久久无码人妻中文字幕| 91精品国产综合久久四虎久久无码一级| 亚洲欧美国产精品专区久久 | 久久综合偷偷噜噜噜色| 国产亚洲精久久久久久无码| 久久青青草原精品国产不卡| 久久精品国产精品亚洲精品| 久久精品国产99久久久古代| 青青热久久国产久精品| 国产亚洲美女精品久久久| 久久综合综合久久97色| 国产∨亚洲V天堂无码久久久| 成人综合久久精品色婷婷| 日本久久中文字幕| 久久午夜无码鲁丝片午夜精品| 国内精品久久久久| 久久se精品一区二区| 国产麻豆精品久久一二三| 99久久99这里只有免费的精品| 日韩精品久久无码人妻中文字幕 | 久久亚洲国产欧洲精品一| 精品久久久噜噜噜久久久|