字符串类HString实现

HString.h
 1 #ifndef HSTRING_H
 2 #define HSTRING_H
 3 
 4 #include <iostream>
 5 using namespace std;
 6 
 7 const int MAX_SIZE = 128;
 8 
 9 class HString
10 {
11 public:
12     HString();
13     HString(const HString &str);
14     HString(const char *str);
15     ~HString(){ delete [] ch; }
16 
17     bool empty() constreturn len == 0 ? true : false; }
18     int length() const { return len; }
19     void strClear(HString &str){ len = 0; ch[0= '\0'; }
20     char *toArray() { return ch; }
21 
22     char &operator[](int i) const;
23     bool operator!(){ return empty(); }
24     HString &operator=(const HString &str);
25     HString &operator+(const HString &str);
26     HString &operator+=(const HString &str);
27 
28     HString &copy(const HString &str);
29     int find(HString &pat);
30     int find(char *pa);
31 
32     void print();
33 
34 
35 
36 private:
37     char *ch;
38     int len;
39 
40 };
41 
42 #endif

HString.cpp
  1 #include "HString.h"
  2 
  3 HString::HString()
  4 {
  5     ch = new char[MAX_SIZE + 1];//tail is '\0'
  6     if(ch == NULL)
  7     {
  8         cerr << "Allocation failed! \n";
  9         exit(-1);
 10     }
 11 
 12     len = 0;
 13     ch[0= '\0';
 14 }
 15 
 16 HString::HString(const HString &str)
 17 {
 18     ch = new char[MAX_SIZE + 1];
 19     if(ch == NULL)
 20     {
 21         cerr << "Allocation failed! \n";
 22         exit(-1);
 23     }
 24 
 25     int i;
 26     len = str.length();
 27     for(i = 0; i < len; i++)
 28     {
 29         ch[i] = str[i];
 30     }
 31     ch[len] = '\0';
 32 }
 33 
 34 HString::HString(const char *str)
 35 {
 36     ch = new char[MAX_SIZE + 1];
 37     if(ch == NULL)
 38     {
 39         cerr << "Allocation failed! \n";
 40         exit(-1);
 41     }
 42     
 43     int i = 0;
 44     while(str[i] != '\0')
 45     {
 46         i++;
 47     }
 48 
 49 
 50     len = i;
 51 
 52     for(i = 0; i < len; i++)
 53     {
 54         ch[i] = str[i];
 55     }
 56     ch[len] = '\0';
 57 }
 58 
 59 void HString::print()
 60 {
 61     cout << len << " elements: ";
 62     for(int i = 0; i < len; i++)
 63     {
 64         cout << ch[i] << " ";
 65     }
 66     
 67     cout << endl;
 68 }
 69 
 70 
 71 char &HString::operator[](int i) const
 72 {
 73     if(i < 0 || i > len)
 74     {
 75         cerr << "Invailed index! \n";
 76         exit(-1);
 77     }
 78 
 79     return ch[i];
 80 }
 81 
 82 int HString::find(HString &pat)
 83 {
 84     char *pa = pat.toArray();
 85     
 86     if(*pa == '\0' || empty())
 87     {
 88         cerr << "Src or pattern string NULL \n";
 89         exit(-1);
 90     }
 91 
 92     int i = 0, j = 0;
 93     
 94     while(i < len && j < pat.length())
 95     {
 96         if(ch[i] == pa[j])
 97         {
 98             i++;
 99             j++;
100         }
101         else
102         {
103             i = i - j + 1;
104             j = 0;
105         }
106         //cout << i << ": " << ch[i] << " " << j << " " << pa[j] << endl;
107 
108     }
109 
110     if(j >= pat.length())
111         return i - j;
112     else
113         return -1;
114 }
115 
116 
117 int HString::find(char *pa)
118 {
119     HString pat(pa);
120     return find(pat);
121     
122 }
123 
124 HString &HString::operator=(const HString &str)
125 {
126     return this->copy(str);
127 }
128 
129 
130 HString &HString::operator+=(const HString &str)
131 {
132     if(str.empty())
133         return *this;
134     
135     if(len + str.length() > MAX_SIZE)
136     {
137         cerr << "cancat string too long \n";
138         exit(-1);
139     }
140 
141     for(int i = 0; i < str.length(); i++)
142     {
143         ch[len + i] = str[i];
144     }
145     ch[len + str.length()] = '\0';
146 
147     len = len + str.length();
148 
149     return *this;
150 }
151 
152 HString &HString::operator+(const HString &str)
153 {
154     HString *newStr = new HString(*this);
155     (*newStr) += str;
156 
157     return *newStr;
158 }
159 
160 HString &HString::copy(const HString &str)
161 {
162     if(&str == this)
163         return *this;
164 
165     delete [] ch;
166     ch = new char[MAX_SIZE + 1];
167     if(ch == NULL)
168     {
169         cerr << "Allocation failed \n";
170         exit(-1);
171     }
172 
173     len = str.length();
174     for(int i = 0; i < len; i++)
175     {
176         ch[i] = str[i];
177     }
178 
179     return *this;
180 }
181 
182 

main.cpp
 1 #include <iostream>
 2 #include "HString.h"
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     HString str("abcdefe");
 8     str.print();
 9 
10     HString pat("de");
11 
12     pat.print();
13     cout << str.find(pat) << endl;
14     cout << str.find("cd"<< endl;
15 
16 //    str = pat;
17 //    str.print();
18 
19     str += pat;
20     str.print();
21 
22     HString ns;
23     ns = str + pat;
24     ns.print();
25 
26 
27     return 0;
28 
29 }
posted @ 2007-06-10 00:25  中土  阅读(1203)  评论(0编辑  收藏  举报
©2005-2008 Suprasoft Inc., All right reserved.