- 字符串的代碼錯誤好奇怪啊,哪位大神指點一下,如果把第11行改成int sz = 128就沒問題
-
- # #ifndef ASTRING
- # #define ASTRING
- # #include
- # #include
- # using namespace std;
- # #define defaultSize = 128
- #
- # class AString
- # {
- # public:
- # AString(int sz = defaultSize );
- # AString(const char *init);
- # AString(const AString& ob);
- # AString operator() (int pos, int len);
- # int operator == (AString& ob)const { int temp; temp = strcmp(ch,ob.ch)==0; return temp;}
- # int operator != (AString& ob)const {return strcmp(ch,ob.ch) != 0;}
- # int operator !()const {return curLength == 0;}
- # AString& operator = (AString& ob);
- # char& operator ;
- # int Find(AString& pat, int k)const;
- # void getNext(int next[]);
- # int fastFind(AString& pat, int k, int next[])const;
- # bool judge(char *str);
- # void replace(AString& s, AString& t, AString& v);
- # void frequency(AString& s);
- # private:
- # char *ch;
- # int curLength;
- # int maxSize;
- # };
- #
- # AString::AString(int sz)
- # {
- # maxSize = sz;
- # ch = new char[maxSize+1];
- # if (ch == NULL)
- # {cout<<"error"<<endl; exit(1);}
- #
- # curLength = 0; ch[0] = '\0';
- # };
- #
- # AString::AString(const char *init)
- # {
- # int len = strlen(init);
- # maxSize = (len>128)? len: 128;
- # ch = new char[maxSize+1];
- # if(ch == NULL)
- # {cout<<"error"<<endl; exit(1);}
- # curLength = len;
- # strcpy(ch,init);
- # };
- #
- # AString::AString(const AString& ob)
- # {
- # maxSize = ob.maxSize;
- # ch = new char(maxSize+1);
- # if(ch == NULL){cout<<"error"<<endl; exit(1);}
- # curLength = ob.curLength;
- # strcpy(ch, ob.ch);
- # };
- #
- # AString AString::operator()(int pos, int len)
- # {
- # AString temp;
- # if (pos= maxSize || len < 0)
- # { temp.curLength = 0; temp.ch[0] = '\0';}
- # else
- # { if (pos+len-1 >= curLength) len = curLength - pos;
- # temp.curLength = len;
- # for (int i=0,j=pos; i<len; i++,j++) temp.ch[i] = ch[j];
- # temp.ch[len] = '\0';
- # }
- # return temp;
- # }
- #
- # char& AString::operator
- # {
- # if (i=curLength) {cout<<"error"<<endl; exit(1);}
- # return ch[i];
- # }
- #
- # int AString::Find(AString& pat, int k)const
- # {
- # int i, j;
- # for(i=k; i<=curLength - pat.curLength; i++)
- # {
- # for(j=0; j<pat.curLength; j++)
- # if(ch[i+j] != pat.ch[j])
- # break;
- #
- # if(j == pat.curLength)
- # return i;
- # }
- # return -1;
- # };
- #
- # void AString::getNext(int next[])
- # {
- # int j = 0, k = -1, lengthP = curLength;
- # next[0] = -1;
- # while (j<lengthP)
- # if (k == -1 || ch[j] == ch[k])
- # {
- # j++; k++;
- # next[j] = k;
- # }
- # else k = next[k];
- # }
- #
- # int AString::fastFind(AString& pat, int k, int next[])const
- # {
- # int posP = 0, posT = k;
- # int lengthP = pat.curLength;
- # int lengthT = curLength;
- # while (posP < lengthP && posT < lengthT)
- # if (posP == -1 || pat.ch[posP] == ch[posT])
- # { posP++;
- # posT++;
- # }
- # else posP = next[posP];
- # if (posP < lengthP) return -1;
- # else return posT-lengthP;
- # };
- # bool AString::judge(char *str) //如果是回文字,則返回true,否則為false
- # {
- # int length,i;
- # char *tmp;
- # char a;
- # stack s;
- # length = strlen(str);
- # tmp = new char(length);
- # for(i = 0; i < length; i++)
- # s.push(str[i]);
- # for(i = 0; i < length; i++)
- # { a=s.top();
- # tmp[i] = a;
- # s.pop();
- # }
- # tmp[i] = '\0';
- # if (strcmp(str, tmp) == 0)
- # return true;
- # return false;
- # };
- # void AString::replace(AString &s, AString &t, AString &v)
- # {
- # int lens = s.curLength, lent = t.curLength, lenv = v.curLength;
- # int *next = new int(10);
- # int value;
- # t.getNext(next);
- # int post = 0, postp;
- # value = s.fastFind(t, 0, next);
- # while( value != -1)
- # {
- # char *tmp = new char[100];
- # strcpy(tmp, s.ch+value+lent);
- # strcpy(s.ch+value, v.ch);
- # strcat(s.ch,tmp);
- # s.curLength = strlen(s.ch);
- # value = s.fastFind(t, value + lenv,next);
- # }
- # }
- # void AString::frequency(AString& s)
- # {
- # int i = 0, j , k;
- # int a,b,c,d;
- # a = b = c = d = 0;
- # for(i = 0; i < s.curLength; i++)
- # {
- # switch(s.ch[i])
- # {
- # case 'a': a++; break;
- # case 'b': b++; break;
- # case 'c': c++; break;
- # case 'd': d++; break;
- # }
- # }
- # }
- # #endif
- # #include "string.h"
- #
- # int main()
- # {
- # AString str1;
- # return 0;
- #
- # }
最佳回答:
#define defaultSize = 128 改為 const defaultSize = 128