第八周项目3-顺序串算法

来源:互联网 发布:mysql和oracle的区别 编辑:程序博客网 时间:2024/05/12 23:05

/*   

* 烟台大学计算机与控制工程学院   

* 作者:王雪松  

* 完成日期:2016年10月27日   

采用顺序存储方式存储串,实现下列算法并测试: 
(1)试编写算法实现将字符串S中所有值为c1的字符换成值为c2的字符: 
void Trans(SqString *&s, char c1, char c2); 
(2)试编写算法,实现将已知字符串所有字符倒过来重新排列。如ABCDEF改为FEDCBA。 
void Invert(SqString &s) 
(3)从串s中删除其值等于c的所有字符。如从message中删除’e’,得到的是mssag。 
void DellChar(SqString &s, char c) 
(4)有两个串s1和s2,设计一个算法求一个这样的串,该串中的字符是s1和s2中公共字符。所谓公共子串,是由在s1中有,且在s2中也有的字符构成的字符。例s1为”message”,s2为”agent”,得到的公共子串是”eage”。
SqString CommChar(SqString s1,SqString s2);

*/

(头文件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)  
  8.             s.data[i]=c2;  
  9. }  
  10.   
  11. int main()  
  12. {  
  13.     SqString s;  
  14.     StrAssign(s, "messages");  
  15.     Trans(s, 'e''a');  
  16.     DispStr(s);  
  17.     return 0;  
  18. }  

运行结果:

 

(2)试编写算法,实现将已知字符串所有字符倒过来重新排列。如ABCDEF改为FEDCBA。


      void Invert(SqString &s);
     将字符串中的第一个元素与最后一个元素进行交换,第二个元素与倒数第二个元素进行交换,以此类推,将所有的字符进行交换,最后将字符串反序。

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include "sqString.h"  
  3. void Invert(SqString &s)  
  4. {  
  5.     int i;  
  6.     char temp;  
  7.     for (i=0; i<s.length/2; i++)  
  8.     {  
  9.         temp = s.data[i];  
  10.         s.data[i]=s.data[s.length-i-1];  
  11.         s.data[s.length-i-1] = temp;  
  12.     }  
  13. }  
  14.   
  15. int main()  
  16. {  
  17.     SqString s;  
  18.     StrAssign(s, "abcdefg");  
  19.     Invert(s);  
  20.     DispStr(s);  
  21.     return 0;  
  22. }  

运行结果:

 

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

          参考:从头到尾扫描s串,对于其值为c的元素采用移动的方式进行删除。

[csharp] view plain copy
  1. #include <stdio.h>  
  2. #include "sqString.h"  
  3. void DellChar(SqString &s, char c)  
  4. {  
  5.     int k=0, i=0;   //k记录值等于c的字符个数  
  6.     while(i<s.length)  
  7.     {  
  8.         if(s.data[i]==c)  
  9.             k++;  
  10.         else  
  11.             s.data[i-k]=s.data[i];  
  12.         i++;  
  13.     }  
  14.     s.length -= k;  
  15. }  
  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);

参考:对于s1中的每一个字符,查看在s2中是否出现,如果出现,则加到结果字符串中。

[csharp] view plain copy
  1. #include <stdio.h>  
  2. #include "sqString.h"  
  3.   
  4. SqString CommChar(SqString s1,SqString s2)  
  5. {  
  6.     SqString s3;  
  7.     int i,j,k=0;  
  8.     for (i=0; i<s1.length; i++)  
  9.     {  
  10.         for (j=0; j<s2.length; j++)  
  11.             if (s2.data[j]==s1.data[i])  
  12.                 break;  
  13.         if (j<s2.length)            //s1.data[i]是公共字符  
  14.         {  
  15.             s3.data[k]=s1.data[i];  
  16.             k++;  
  17.         }  
  18.     }  
  19.     s3.length=k;  
  20.     return s3;  
  21. }  
  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