8周 顺序串算法

[cpp]  view plain  copy
  1. /*   
  2. Copyright (c)2016,烟台大学计算机与控制工程学院   
  3. All rights reserved.   
  4. 文件名称:项目7.cbp   
  5. 作    者:泮春宇 
  6. 完成日期:2016年11月3日   
  7. 版 本 号:v1.0    
  8. 问题描述:采用顺序存储方式存储串,实现下列算法并测试:   
  9.          (1)试编写算法实现将字符串S中所有值为c1的字符换成值为c2的字符:   
  10.           void Trans(SqString *&s, char c1, char c2);   
  11.          (2)试编写算法,实现将已知字符串所有字符倒过来重新排列。如ABCDEF改为FEDCBA。   
  12.           void Invert(SqString &s)   
  13.          (3)从串s中删除其值等于c的所有字符。如从message中删除’e’,得到的是mssag。   
  14.           void DellChar(SqString &s, char c)   
  15.          (4)有两个串s1和s2,设计一个算法求一个这样的串,该串中的字符是s1和s2中公共字符。  
  16.           所谓公共子串,是由在s1中有,且在s2中也有的字符构成的字符。例s1为”message”,s2为”agent”,得到的公共子串是”eage”。   
  17.           SqString CommChar(SqString s1,SqString s2);  
  18. 输入描述:无   
  19. 程序输出:测试数据   
  20. */   

头文件sqString.h代码详见顺序串算法库。

(1)试编写算法实现将字符串S中所有值为c1的字符换成值为c2的字符: 
          void Trans(SqString *&s, char c1, char c2); 

代码:

[cpp]  view plain  copy
  1. #include <stdio.h>    
  2. #include "sqString.h"    
  3. void Trans(SqString &s, char c1, char c2)    
  4. {    
  5.     int i;    
  6.     for(i=0;i<s.length;i++)    
  7.         if(s.data[i]==c1) //= 为赋值,if中==时判断两边是否相等    
  8.             s.data[i]=c2;    
  9. }    
  10. int main()    
  11. {    
  12.     SqString s;    
  13.     StrAssign(s, "messages");    
  14.     Trans(s, 'e''a');    
  15.     DispStr(s);    
  16.     return 0;    
  17. }    
[cpp]  view plain  copy
  1. <span style="color: rgb(85, 85, 85); font-family: "microsoft yahei"; font-size: 18px;">运行结果:</span>  
[cpp]  view plain  copy
  1. <span style="color: rgb(85, 85, 85); font-family: "microsoft yahei"; font-size: 18px;"><img src="https://img-blog.csdn.net/20161023173337083?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />  
  2. </span>  
[cpp]  view plain  copy
  1. (2)试编写算法,实现将已知字符串所有字符倒过来重新排列。如ABCDEF改为FEDCBA。   
  2.           void Invert(SqString &s) ;  
  3. 代码:  
[cpp]  view plain  copy
  1. #include <stdio.h>    
  2. #include "sqString.h"     
  3. //将字符串中的第一个元素与最后一个元素进行交换,    
  4. //第二个元素与倒数第二个元素进行交换,以此类推,将所有的字符进行交换,最后将字符串反序。    
  5.     
  6. void Invert(SqString &s)    
  7. {       
  8.     int i;    
  9.     char temp;    
  10.     for (i=0; i<s.length/2; i++)    
  11.     {    
  12.         temp = s.data[i];    
  13.         s.data[i]=s.data[s.length-i-1];    
  14.         s.data[s.length-i-1] = temp;    
  15.     }    
  16. }    
  17. int main()    
  18. {    
  19.     SqString s;    
  20.     StrAssign(s, "abcdefg");    
  21.     Invert(s);    
  22.     DispStr(s);    
  23.     return 0;    
  24. }    

运行结果:


(3)从串s中删除其值等于c的所有字符。如从message中删除’e’,得到的是mssag。 
          void DellChar(SqString &s, char c) ;

代码:

[cpp]  view plain  copy
  1. #include <stdio.h>    
  2. #include "sqString.h"     
  3. //从头到尾扫描s串,对于其值为c的元素采用移动的方式进行删除。    
  4. void DellChar(SqString &s, char c)    
  5. {    
  6.     int i=0,k=0;    
  7.     while(i<s.length)    
  8.     {    
  9.         if(s.data[i]==c)//k记录值等于c的字符个数    
  10.             k++;    
  11.         else    
  12.             s.data[i-k]=s.data[i];    
  13.         i++;    
  14.     }    
  15.     s.length-=k;    
  16. }    
  17. int main()    
  18. {    
  19.     SqString s;    
  20.     StrAssign(s, "message");    
  21.     DellChar(s, 'e');    
  22.     DispStr(s);    
  23.     return 0;    
  24. }    

运行结果:


(4)有两个串s1和s2,设计一个算法求一个这样的串,该串中的字符是s1和s2中公共字符。
          所谓公共子串,是由在s1中有,且在s2中也有的字符构成的字符。例s1为”message”,s2为”agent”,得到的公共子串是”eage”。 
          SqString CommChar(SqString s1,SqString s2);

代码:


[cpp]  view plain  copy
  1. #include <stdio.h>    
  2. #include "sqString.h"     
  3. //对于s1中的每一个字符,查看在s2中是否出现,如果出现,则加到结果字符串中。    
  4. SqString CommChar(SqString s1,SqString s2)    
  5. {    
  6.     SqString str;    
  7.     int i,j;    
  8.     int k=0;    
  9.     for(i=0;i<s1.length;i++)    
  10.     {    
  11.         for(j=0;j<s2.length;j++)    
  12.         {    
  13.             if(s1.data[i]==s2.data[j])    
  14.             {    
  15.                 str.data[k]=s1.data[i];    
  16.                 k++;    
  17.             }    
  18.         }    
  19.     }    
  20.     str.length=k;    
  21.     return str;    
  22. }    
  23. int main()    
  24. {    
  25.     SqString s1, s2, s;    
  26.     StrAssign(s1, "message");    
  27.     StrAssign(s2, "agent");    
  28.     s = CommChar(s1, s2);    
  29.     DispStr(s);    
  30.     return 0;    
  31. }    
运行结果:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值