【数据结构】 串的基本操作

前端之家收集整理的这篇文章主要介绍了【数据结构】 串的基本操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. /*
  2. ==========================================================================================
  3. 串的基本操作
  4. By~fanxingzju 2014.04.23
  5. 1.StrAssign(&T,chars)
  6. 初始条件:chars是字符串常量
  7. 操作结果:生成一个其值等于chars的串T
  8. 2.StrCopy(&T,S)
  9. 初始条件:串S存在
  10. 操作结果:由串S复制的串T
  11. 3.StrEmpty(S)
  12. 初始条件:串S存在
  13. 操作结果:若S为空串,则返回true,否则返回false
  14. 4.StrCompare(S,T)
  15. 初始条件:串S和T存在
  16. 操作结果:若S > T,则返回值 > 0;若S = T,则返回值 = 0;若S < T,则返回值 < 0
  17. 5.StrLength(S)
  18. 初始条件:串S存在
  19. 操作结果:返回S的元素个数,称为串的长度
  20. 6.ClearString(&S)
  21. 初始条件:串S存在
  22. 操作结果:将S清为空串
  23. 7.Concat(&T,S1,S2)
  24. 初始条件:串S1和S2存在
  25. 操作结果:用T返回由S1和S2联接而成的新串
  26. 8.SubString(&Sub,S,pos,len)
  27. 初始条件:串S存在,1≦pos≦Strlength(S)且0≦len≦Strlength(S)-pos+1
  28. 操作结果:用Sub返回串S的第pos个字符起长度为len的字串
  29. 9.Index(S,T,pos)
  30. 初始条件:串S和T存在,T为非空串,1≦pos≦Strlength(S)
  31. 操作结果:若主串中存在和串T值相同的字串,则返回它在字串S中第pos个字符之后第一次出现的位置;否则函数值为0
  32. 10.Replace(&S,V)
  33. 初始条件:串S,T和V存在,T是非空串
  34. 操作结果:用V替换主串S中出现的所有与T相等的不重叠的字串
  35. 11.StrInsert(&S,T)
  36. 初始条件:串S和T存在,1≦pos≦Strlength(S)+1
  37. 操作结果:在串S的第pos个位置字符之前传入串T
  38. 12.StrDelete(&S,len)
  39. 初始条件:串S存在,1≦pos≦Strlength(S)-len+1
  40. 操作结果:从串S中删除第pos个字符起长度为len的字串
  41. 13.DestroyString(&S)
  42. 初始条件:串S存在
  43. 操作结果:串S被销毁
  44. 14.PrintString(T)
  45. 初始条件:串S存在
  46. 操作结果:将S打印在屏幕上
  47. 15.InitString(&T)
  48. 初始条件:串T已经定义
  49. 操作结果:将串S初始化
  50. ==========================================================================================
  51. 备注:
  52. 1.这里将空串定义为:T.ch = new char[1]; T.length = 0; *T.ch = '\0';
  53. 2.这里将不存在的串视为:T.ch = NULL;
  54. 3.字符串全部以'\0'结束
  55. 4.尽量保持各个函数的独立性,只有在Replace()函数调用了Index()函数
  56. 5.函数中的打印信息可以根据需要选择
  57. 6.由于函数比较多,此处不再提供详细的测试代码,可以根据需要自行设计测试特定函数
  58. */
  59.  
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62.  
  63. typedef struct
  64. {
  65. char *ch;
  66. int length;
  67. }HString;
  68.  
  69. //1.StrAssign(&T,chars)
  70. bool StrAssign(HString &T,char *chars)
  71. {
  72. if (T.ch)
  73. {
  74. delete[] T.ch;
  75. }
  76.  
  77. char *ctemp = chars;
  78. T.length = 0;
  79.  
  80. while(*ctemp)
  81. {
  82. ++T.length;
  83. ++ctemp;
  84. }
  85.  
  86. T.ch = new char[T.length + 1];
  87. if (!T.ch)
  88. {
  89. printf("StrAssign()函数执行,内存分配失败,程序即将退出\n");
  90. system("pause");
  91. exit(-1);
  92. }
  93.  
  94. char *tmp = T.ch;
  95. while(*chars)
  96. {
  97. *tmp++ = *chars++;
  98. }
  99. *tmp = '\0';
  100.  
  101. printf("StrAssign()函数执行,串T生成成功\n");
  102. return true;
  103. }
  104.  
  105. //2.StrCopy(&T,S)
  106. bool StrCopy(HString &T,HString Str)
  107. {
  108. if (!Str.ch)
  109. {
  110. printf("StrCopy()函数执行,被拷贝串不存在,程序即将退出\n");
  111. system("pause");
  112. exit(0);
  113. }
  114. if (T.ch)
  115. {
  116. delete[] T.ch;
  117. }
  118. T.length = Str.length;
  119.  
  120. T.ch = new char[T.length + 1];
  121. if (!T.ch)
  122. {
  123. printf("StrCopy()函数执行,内存分配失败,程序即将退出\n");
  124. system("pause");
  125. exit(-1);
  126. }
  127. char *tmp = T.ch;
  128. while(*Str.ch)
  129. {
  130. *tmp++ = *Str.ch++;
  131. }
  132. *tmp = '\0';
  133.  
  134. printf("StrCopy函数执行,串拷贝成功\n");
  135. return true;
  136. }
  137.  
  138. //3.StrEmpty(S)
  139. bool StrEmpty(HString Str)
  140. {
  141. if (!Str.ch)
  142. {
  143. printf("StrEmpty()函数执行,串不存在,程序即将退出\n");
  144. system("pause");
  145. exit(0);
  146. }
  147. else
  148. {
  149. if (!Str.length)
  150. {
  151. printf("StrEmpty()函数执行,串为空\n");
  152. return true;
  153. }
  154. else
  155. {
  156. printf("StrEmpty()函数执行,串非空\n");
  157. return false;
  158. }
  159. }
  160. }
  161.  
  162. //4.StrCompare(S,T)
  163. int StrCompare(HString Str,HString T)
  164. {
  165. if ((!T.ch)||(!Str.ch))
  166. {
  167. printf("StrCompare()函数执行,程序即将退出\n");
  168. system("pause");
  169. exit(0);
  170. }
  171. int flag = 0;
  172. for (int i = 0; (i < Str.length)&&(i < T.length); ++i)
  173. {
  174. if (*(T.ch + i) != *(Str.ch + i))
  175. {
  176. flag = *(Str.ch + i) - *(T.ch + i);
  177. break;
  178. }
  179. }
  180. if (0 == flag)
  181. {
  182. flag = Str.length - T.length;
  183. }
  184. printf("StrCompare()函数执行,比较的返回值为: %d \n",flag);
  185. return flag;
  186. }
  187.  
  188. //5.StrLength(S)
  189. int Strlength(HString Str)
  190. {
  191. if (!Str.ch)
  192. {
  193. printf("Strlength()函数执行,串不存在,程序即将退出\n");
  194. system("pause");
  195. exit(0);
  196. }
  197. printf("Strlength()函数执行,返回的串的长度为: %d \n",Str.length);
  198. return Str.length;
  199. }
  200.  
  201. //6.ClearString(&S)
  202. bool ClearString(HString &Str)
  203. {
  204. if (Str.ch)
  205. {
  206. delete[] Str.ch;
  207. }
  208. else
  209. {
  210. printf("ClearString()函数执行,串不存在,程序即将退出\n");
  211. system("pause");
  212. exit(0);
  213. }
  214. Str.ch = new char[1];
  215. *Str.ch = '\0';
  216. Str.length = 0;
  217. printf("ClearString()函数执行,串已清空\n");
  218. return true;
  219. }
  220.  
  221. //7.Concat(&T,S2)
  222. bool Concat(HString &T,HString S1,HString S2)
  223. {
  224. if (T.ch)
  225. {
  226. delete[] T.ch;
  227. }
  228. T.length = S1.length + S2.length;
  229. T.ch = new char[T.length + 1];
  230. if (!T.ch)
  231. {
  232. printf("Concat()函数执行,内存分配失败,程序即将退出\n");
  233. system("pause");
  234. exit(-1);
  235. }
  236. char *tmp = T.ch;
  237. while(*S1.ch)
  238. {
  239. *tmp++ = *S1.ch++;
  240. }
  241. while(*S2.ch)
  242. {
  243. *tmp++ = *S2.ch++;
  244. }
  245. *tmp = '\0';
  246. printf("Concat()函数执行,串链接成功\n");
  247. return true;
  248. }
  249.  
  250. //8.SubString(&Sub,len)
  251. bool SubString(HString &Sub,HString Str,int pos,int len)
  252. {
  253. if (Sub.ch)
  254. {
  255. delete[] Sub.ch;
  256. }
  257. if (!Str.ch)
  258. {
  259. printf("SubString()函数执行,串为空,程序即将退出\n");
  260. system("pause");
  261. exit(0);
  262. }
  263. if ((pos < 1)||(pos > Str.length)||(len < 0)||(len > Str.length - pos +1))
  264. {
  265. printf("SubString()函数执行,参数pos和len有错误获取字串失败\n");
  266. Sub.ch = new char[1];
  267. *Sub.ch = '\0';
  268. Sub.length = 0;
  269. return false;
  270. }
  271. Sub.ch = new char[len + 1];
  272. char *tmp = Sub.ch;
  273. for(int i = 1; i != pos; ++i)
  274. {
  275. Str.ch++;
  276. }
  277. for(int i = 0; i != len; ++i)
  278. {
  279. *tmp++ = *Str.ch++;
  280. }
  281. *tmp = '\0';
  282. printf("SubString()函数执行,字串获取成功\n");
  283. return true;
  284. }
  285.  
  286. //9.Index(S,pos)
  287. int Index(HString Str,HString T,int pos)
  288. {
  289. if (!Str.ch||!T.ch||(0 == T.length))
  290. {
  291. printf("Index()函数执行,串不存在或为空串,程序即将退出\n");
  292. system("pause");
  293. exit(0);
  294. }
  295. if ((pos < 1)||(pos > Str.length))
  296. {
  297. printf("Index()函数执行,pos参数错误\n");
  298. return 0;
  299. }
  300. for(int i = 1; i != pos; ++i)
  301. {
  302. ++Str.ch;
  303. }
  304. for (int i = pos; i != Str.length - T.length + 2; ++i)
  305. {
  306. if (*Str.ch == *T.ch)
  307. {
  308. bool flag = true;
  309. char *Stmp = Str.ch;
  310. char *Ttmp = T.ch;
  311. while(*Ttmp)
  312. {
  313. if (*Ttmp++ != *Stmp++)
  314. {
  315. flag = false;
  316. break;
  317. }
  318. }
  319. if (flag)
  320. {
  321. printf("Index()函数执行,主串S第%d个字符之后第一次出现与串T相同的字串的位置为:%d\n",i);
  322. return i;
  323. }
  324. }
  325. Str.ch++;
  326. }
  327. printf("Index()函数执行,主串第%d个字符之后未找到与串T相同的字串\n",pos);
  328. return 0;
  329. }
  330.  
  331. //10.Replace(&S,V)
  332. bool Replace(HString &Str,HString V)
  333. {
  334. if ((!Str.ch)||(!T.ch)||(!V.ch)||(0 == T.length))
  335. {
  336. printf("Replace()函数执行,串不存在或为空,程序即将退出\n");
  337. system("pause");
  338. exit(0);
  339. }
  340. int pos = Index(Str,1);
  341. while(pos)
  342. {
  343. int nlength = Str.length - T.length + V.length;
  344. char *ctemp = new char[nlength + 1];
  345. if (!ctemp)
  346. {
  347. printf("Replace()函数执行,内存分配失败,程序即将退出\n");
  348. system("pause");
  349. exit(-1);
  350. }
  351. char *tmp = ctemp;
  352. char *stmp = Str.ch;
  353. char *vtmp = V.ch;
  354. for (int i = 1; i != pos; ++i)
  355. {
  356. *tmp++ = *stmp++;
  357. }
  358. for (int i = 0; i != T.length; ++i)
  359. {
  360. ++stmp;
  361. }
  362. for (int i = 0; i != V.length; ++i)
  363. {
  364. *tmp++ = *vtmp++;
  365. }
  366. while(*stmp)
  367. {
  368. *tmp++ = *stmp++;
  369. }
  370. *tmp = '\0';
  371. delete Str.ch;
  372. Str.length = nlength;
  373. Str.ch = ctemp;
  374. pos = Index(Str,pos + V.length);
  375. }
  376. printf("Replace()函数执行,子串替代成功\n");
  377. return true;
  378. }
  379.  
  380. //11.StrInsert(&S,T)
  381. bool StrInsert(HString &Str,HString T)
  382. {
  383. if ((pos < 1)||(pos > Str.length + 1)||(NULL == T.ch))
  384. {
  385. printf("StrInsert()函数执行,pos参数错误或串不存在\n");
  386. return false;
  387. }
  388. int nlength = Str.length + T.length;
  389. char *ctemp = new char[nlength + 1];
  390. if (!ctemp)
  391. {
  392. printf("StrInsert()函数执行,内存分配失败,程序即将退出\n");
  393. system("pause");
  394. exit(-1);
  395. }
  396. char *tmp = ctemp;
  397. char *stmp = Str.ch;
  398. for (int i = 1; i != pos; ++i)
  399. {
  400. *tmp++ = *stmp++;
  401. }
  402. while(*T.ch)
  403. {
  404. *tmp++ = *T.ch++;
  405. }
  406. while(*stmp)
  407. {
  408. *tmp++ = *stmp++;
  409. }
  410. *tmp = '\0';
  411. delete[] Str.ch;
  412. Str.ch = ctemp;
  413. Str.length = nlength;
  414. printf("StrInsert()函数执行,在主串第 %d 个字符前插入字串成功\n",pos);
  415. return true;
  416. }
  417.  
  418. //12.StrDelete(&S,len)
  419. bool StrDelete(HString &Str,int len)
  420. {
  421. if ((pos < 1)||(pos > Str.length - len + 1))
  422. {
  423. printf("StrDelete()函数执行,输入参数错误\n");
  424. return false;
  425. }
  426. int nlength = Str.length - len;
  427. char *ctemp = new char[nlength + 1];
  428. if (!ctemp)
  429. {
  430. printf("StrDelete()函数执行,内存分配失败,程序即将退出\n");
  431. system("pause");
  432. exit(-1);
  433. }
  434. char *tmp = ctemp;
  435. char *stmp = Str.ch;
  436. for (int i = 1; i != pos; ++i)
  437. {
  438. *tmp++ = *stmp++;
  439. }
  440. for (int i = 0; i != len; ++i)
  441. {
  442. ++stmp;
  443. }
  444. while(*stmp)
  445. {
  446. *tmp++ = *stmp++;
  447. }
  448. *tmp = '\0';
  449. delete[] Str.ch;
  450. Str.ch = ctemp;
  451. Str.length = nlength;
  452. printf("StrDelete()函数执行,指定位置 %d 和长度 %d 的子串删除成功\n",len);
  453. return true;
  454. }
  455. //13.DestroyString(&S)
  456. bool DestoryString(HString &Str)
  457. {
  458. if (Str.ch)
  459. {
  460. delete[] Str.ch;
  461. }
  462. Str.ch = NULL;
  463. Str.length = 0;
  464. printf("DestoryString()函数执行,串销毁成功\n");
  465. return true;
  466. }
  467.  
  468. //14.PrintString(T)
  469. bool PrintString(HString T)
  470. {
  471. if (!T.ch)
  472. {
  473. printf("PrintString()函数执行,串不存在\n");
  474. return false;
  475. }
  476. else
  477. {
  478. printf("PrintString()函数执行,串的长度为 %d ,打印结果如下:",T.length);
  479. while(*T.ch)
  480. {
  481. printf("%c",*T.ch++);
  482. }
  483. printf("\n");
  484. return true;
  485. }
  486. }
  487.  
  488. //15.InitString(&T)
  489. bool InitString(HString &T)
  490. {
  491. T.ch = NULL;
  492. T.length = 0;
  493. return true;
  494. }
  495.  
  496. int main()
  497. {
  498. HString T,T1,T2;
  499. InitString(T);
  500. InitString(T1);
  501. InitString(T2);
  502.  
  503. char *test = "abc abc abc abc a";
  504. char *test1 = "a";
  505. char *test2 = "/Here/";
  506.  
  507. StrAssign(T,test);
  508. PrintString(T);
  509.  
  510. StrAssign(T1,test1);
  511. PrintString(T1);
  512.  
  513. StrAssign(T2,test2);
  514. PrintString(T2);
  515.  
  516. //TODO:
  517.  
  518. DestoryString(T);
  519. DestoryString(T1);
  520. DestoryString(T2);
  521.  
  522. system("pause");
  523. return 0;
  524. }

猜你在找的数据结构相关文章