AString字符串数组
又是字符串数组。
主函数
#include "AString.h"
#include <iostream>
using namespace std;
int main(){
cout << "Please input a string : ";
AString str;
cin >> str;
cout << "The length of the string is: " << str.Length() << endl;
cout << "\n1. Test the substr(int pos, int len): \n";
int pos, len;
cout << "Input the pos and the len: ";
cin >> pos >> len;
AString sub = str.substr(pos, len);
cout << "The substring is: " << sub << endl;
cout << "\n2. Test the operator()(int pos, int len): \n";
cout << "Input the pos and the len: ";
cin >> pos >> len;
sub = str(pos, len);
cout << "The substring is: " << sub << endl;
cout << "\n3. Test the operator[](int i): \n";
cout << "Input the positon i:";
int i;
cin >> i;
cout << "The char in position " << i << " is: " << str[i] << endl;
AString str2;
cout << "Input a new string:\n";
cin >> str2;
cout << "\n4. Test the operator ==\n";
cout << "The two strings " << "\"" << str << "\" and \"" << str2 << "\"";
if (str == str2) cout << " is Equal!\n";
else cout << " is not Equal!\n";
cout << "\n5. Test the operator !=\n";
cout << "The two strings " << "\"" << str << "\" and \"" << str2 << "\"";
if (str != str2) cout << " is not Equal!\n";
else cout << " is Equal!\n";
cout << "\n6. Test the operator +\n";
AString tmp = str + str2;
cout << "\"" << str << "\" + \"" << str2
<< "\" = \"" << tmp << "\"\n";
cout << "\n7. Test the operator +=\n";
cout << "\"" << str << "\" += \"" << str2
<< "\", then the result is: \n";
str += str2;
cout << str << endl;
cout << "\n8. Test int fastFind(AString& pat, int k, int *next):\n";
cout << "Input a string and a substring: " << endl;
cin >> str >> str2;
int *next = new int[str2.Length()+1];//ûÓÐ+1ʹÓÃÖгö½ç£¬Îö¹¹Ê±ÓÐÎÊÌâ
str2.getNext(next);
int f = str.fastFind(str2, 0, next);
delete [] next;
if(f == -1) cout << "Not Find!" << endl;
else cout << "Find,the position is : " << f << endl;
cout << "\n9. Test the replace(AString &t, AString &v):\n";
AString s("aabbabcbaabaaacbab");
AString t("bab");
AString v("abdc");
cout << "The initial string is: " << s << endl;
cout << "The string t is: " << t << endl;
cout << "The string v is: " << v << endl;
s.replace(t, v);
cout << "After s.replace(t, v), the string is: " << s << endl;
return 0;
}
AString类定义
const int defaultSize=128;
class AString{
private:
char *ch;
int curLength;
int maxSize;
public:
AString(int sz = defaultSize);
AString(const char *init );
AString(const AString &ob);
~AString(){
delete []ch;
}
int Length()const{
return curLength;
}
AString substr(int pos, int len);
AString operator()(int pos, int len); //取子串
bool operator == (AString &ob)const {
return strcmp(ch, ob.ch) == 0;
}
bool operator != (AString &ob)const{
return strcmp (ch, ob.ch) != 0;
}
bool operator !()const{
//判断串是否为空
return curLength == 0;
}
AString& operator = (const AString &ob);
AString& operator += (const AString &ob);
AString operator + (const AString &ob);
char& operator <