程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> string-字符串的代碼錯誤好奇怪啊,哪位大神指點一下,如果把第11行改成int sz = 128就沒問題

string-字符串的代碼錯誤好奇怪啊,哪位大神指點一下,如果把第11行改成int sz = 128就沒問題

編輯:編程綜合問答
字符串的代碼錯誤好奇怪啊,哪位大神指點一下,如果把第11行改成int sz = 128就沒問題
  1. # #ifndef ASTRING
  2. # #define ASTRING
  3. # #include
  4. # #include
  5. # using namespace std;
  6. # #define defaultSize = 128
  7. #
  8. # class AString
  9. # {
  10. # public:
  11. # AString(int sz = defaultSize );
  12. # AString(const char *init);
  13. # AString(const AString& ob);
  14. # AString operator() (int pos, int len);
  15. # int operator == (AString& ob)const { int temp; temp = strcmp(ch,ob.ch)==0; return temp;}
  16. # int operator != (AString& ob)const {return strcmp(ch,ob.ch) != 0;}
  17. # int operator !()const {return curLength == 0;}
  18. # AString& operator = (AString& ob);
  19. # char& operator ;
  20. # int Find(AString& pat, int k)const;
  21. # void getNext(int next[]);
  22. # int fastFind(AString& pat, int k, int next[])const;
  23. # bool judge(char *str);
  24. # void replace(AString& s, AString& t, AString& v);
  25. # void frequency(AString& s);
  26. # private:
  27. # char *ch;
  28. # int curLength;
  29. # int maxSize;
  30. # };
  31. #
  32. # AString::AString(int sz)
  33. # {
  34. # maxSize = sz;
  35. # ch = new char[maxSize+1];
  36. # if (ch == NULL)
  37. # {cout<<"error"<<endl; exit(1);}
  38. #
  39. # curLength = 0; ch[0] = '\0';
  40. # };
  41. #
  42. # AString::AString(const char *init)
  43. # {
  44. # int len = strlen(init);
  45. # maxSize = (len>128)? len: 128;
  46. # ch = new char[maxSize+1];
  47. # if(ch == NULL)
  48. # {cout<<"error"<<endl; exit(1);}
  49. # curLength = len;
  50. # strcpy(ch,init);
  51. # };
  52. #
  53. # AString::AString(const AString& ob)
  54. # {
  55. # maxSize = ob.maxSize;
  56. # ch = new char(maxSize+1);
  57. # if(ch == NULL){cout<<"error"<<endl; exit(1);}
  58. # curLength = ob.curLength;
  59. # strcpy(ch, ob.ch);
  60. # };
  61. #
  62. # AString AString::operator()(int pos, int len)
  63. # {
  64. # AString temp;
  65. # if (pos= maxSize || len < 0)
  66. # { temp.curLength = 0; temp.ch[0] = '\0';}
  67. # else
  68. # { if (pos+len-1 >= curLength) len = curLength - pos;
  69. # temp.curLength = len;
  70. # for (int i=0,j=pos; i<len; i++,j++) temp.ch[i] = ch[j];
  71. # temp.ch[len] = '\0';
  72. # }
  73. # return temp;
  74. # }
  75. #
  76. # char& AString::operator
  77. # {
  78. # if (i=curLength) {cout<<"error"<<endl; exit(1);}
  79. # return ch[i];
  80. # }
  81. #
  82. # int AString::Find(AString& pat, int k)const
  83. # {
  84. # int i, j;
  85. # for(i=k; i<=curLength - pat.curLength; i++)
  86. # {
  87. # for(j=0; j<pat.curLength; j++)
  88. # if(ch[i+j] != pat.ch[j])
  89. # break;
  90. #
  91. # if(j == pat.curLength)
  92. # return i;
  93. # }
  94. # return -1;
  95. # };
  96. #
  97. # void AString::getNext(int next[])
  98. # {
  99. # int j = 0, k = -1, lengthP = curLength;
  100. # next[0] = -1;
  101. # while (j<lengthP)
  102. # if (k == -1 || ch[j] == ch[k])
  103. # {
  104. # j++; k++;
  105. # next[j] = k;
  106. # }
  107. # else k = next[k];
  108. # }
  109. #
  110. # int AString::fastFind(AString& pat, int k, int next[])const
  111. # {
  112. # int posP = 0, posT = k;
  113. # int lengthP = pat.curLength;
  114. # int lengthT = curLength;
  115. # while (posP < lengthP && posT < lengthT)
  116. # if (posP == -1 || pat.ch[posP] == ch[posT])
  117. # { posP++;
  118. # posT++;
  119. # }
  120. # else posP = next[posP];
  121. # if (posP < lengthP) return -1;
  122. # else return posT-lengthP;
  123. # };
  124. # bool AString::judge(char *str) //如果是回文字,則返回true,否則為false
  125. # {
  126. # int length,i;
  127. # char *tmp;
  128. # char a;
  129. # stack s;
  130. # length = strlen(str);
  131. # tmp = new char(length);
  132. # for(i = 0; i < length; i++)
  133. # s.push(str[i]);
  134. # for(i = 0; i < length; i++)
  135. # { a=s.top();
  136. # tmp[i] = a;
  137. # s.pop();
  138. # }
  139. # tmp[i] = '\0';
  140. # if (strcmp(str, tmp) == 0)
  141. # return true;
  142. # return false;
  143. # };
  144. # void AString::replace(AString &s, AString &t, AString &v)
  145. # {
  146. # int lens = s.curLength, lent = t.curLength, lenv = v.curLength;
  147. # int *next = new int(10);
  148. # int value;
  149. # t.getNext(next);
  150. # int post = 0, postp;
  151. # value = s.fastFind(t, 0, next);
  152. # while( value != -1)
  153. # {
  154. # char *tmp = new char[100];
  155. # strcpy(tmp, s.ch+value+lent);
  156. # strcpy(s.ch+value, v.ch);
  157. # strcat(s.ch,tmp);
  158. # s.curLength = strlen(s.ch);
  159. # value = s.fastFind(t, value + lenv,next);
  160. # }
  161. # }
  162. # void AString::frequency(AString& s)
  163. # {
  164. # int i = 0, j , k;
  165. # int a,b,c,d;
  166. # a = b = c = d = 0;
  167. # for(i = 0; i < s.curLength; i++)
  168. # {
  169. # switch(s.ch[i])
  170. # {
  171. # case 'a': a++; break;
  172. # case 'b': b++; break;
  173. # case 'c': c++; break;
  174. # case 'd': d++; break;
  175. # }
  176. # }
  177. # }
  178. # #endif
  179. # #include "string.h"
  180. #
  181. # int main()
  182. # {
  183. # AString str1;
  184. # return 0;
  185. #
  186. # }

最佳回答:


#define defaultSize = 128 改為 const defaultSize = 128

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved