alexsendar 2015-05-01 10:25 采纳率: 100%
浏览 1985
已采纳

字符串的代码错误好奇怪啊,哪位大神指点一下,如果把第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. # }
  • 写回答

2条回答 默认 最新

  • qq_20134897 2015-05-01 12:06
    关注

    #define defaultSize = 128 改为 const defaultSize = 128

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退