• <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
            設(shè)計(jì)一個(gè)字符串類(lèi) String,實(shí)現(xiàn)
            ·+= 連接兩個(gè)字符串
            ·+   連接兩個(gè)字符串
            ·== 判斷兩個(gè)字符串是否相等
            ·<   判斷兩個(gè)字符串大小關(guān)系

            類(lèi)的聲明:
            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             // 初始化內(nèi)存
             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 閱讀(180) 評(píng)論(0)  編輯 收藏 引用

            只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            精品久久久久久无码专区| 欧美精品福利视频一区二区三区久久久精品 | 囯产精品久久久久久久久蜜桃| 久久天天躁狠狠躁夜夜avapp| 国产精品久久久久久久久软件 | 久久九九青青国产精品| 亚洲国产精品综合久久网络| 久久久久久毛片免费播放| 国产AⅤ精品一区二区三区久久| 伊人久久亚洲综合影院| 久久精品国产免费| 国产成人久久精品一区二区三区 | 久久久久se色偷偷亚洲精品av| 国产午夜福利精品久久2021 | 99国产精品久久久久久久成人热| 青青青伊人色综合久久| 国内高清久久久久久| 99久久伊人精品综合观看| 久久九九兔免费精品6| 99久久国产亚洲高清观看2024 | 久久精品无码一区二区无码| 精品欧美一区二区三区久久久| 久久精品国产亚洲77777| 久久久网中文字幕| 狠狠人妻久久久久久综合| 99热热久久这里只有精品68| 丁香狠狠色婷婷久久综合| 日韩人妻无码精品久久久不卡| 久久人妻少妇嫩草AV蜜桃| 色欲综合久久躁天天躁| 婷婷久久综合九色综合九七| 国内精品免费久久影院| 久久婷婷五月综合97色直播| 久久久精品国产Sm最大网站| 国产一区二区三精品久久久无广告| 99精品国产在热久久无毒不卡| 久久精品欧美日韩精品| 狠狠色丁香婷婷久久综合不卡| 伊人久久综合热线大杳蕉下载| 色偷偷888欧美精品久久久| 国产—久久香蕉国产线看观看 |