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

来源:互联网 发布:淘宝官方客服电话400 编辑:程序博客网 时间:2024/04/30 07:51

1.     /*        

2.    Copyright (c)2016,烟台大学计算机与控制工程学院        

3.     All rights reserved.        

4.    文件名称:第8周项目3-顺序串算法.cpp        

5.         者:朱建豪        

6.    完成日期:20161020        

7.       号:v1.0        

8.            

9.     问题描述:采用顺序存储方式存储串,实现下列算法并测试:   

10.      1)试编写算法实现将字符串S中所有值为c1的字符换成值为c2的字符:   

11.            void Trans(SqString *&s, char c1, char c2);   

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

13.            void Invert(SqString &s)   

14.      3)从串s中删除其值等于c的所有字符。如从message中删除’e’,得到的是mssag   

15.            void DellChar(SqString &s, char c)   

16.      4)有两个串s1s2,设计一个算法求一个这样的串,该串中的字符是s1s2中公共字符。所谓公共子串,是由在s1中有,且在s2中也有的字符构成的字符。  

17.            s1”message”s2”agent”,得到的公共子串是”eage”   

18.           SqString CommChar(SqString s1,SqString s2);   

19.  输入描述:各种串的输入。  

20. 程序输出:各操作后的输出。  

21.  */    


头文件及功能函数详见【顺序串算法库】

1)试编写算法实现将字符串S中所有值为c1的字符换成值为c2的字符

1)的实现函数:

[cpp] view plain copy

1.     void Trans(SqString &s, char c1, char c2)    

2.    {    

3.         int i;    

4.        for (i=0; i<s.length; i++)    

5.             if (s.data[i]==c1)    

6.                s.data[i]=c2;    

7.     }    

1)的main函数:

[cpp] view plain copy

1.     #include <stdio.h>    

2.    #include "sqString.h"    

3.     int main()    

4.    {    

5.         SqString s;    

6.        StrAssign(s, "messages");    

7.         Trans(s, 'e''a');    

8.        DispStr(s);    

9.         return 0;    

10. }    



1)的运行结果:

原来:StrAssign(s, "messages"); 


2)的实现函数:

[cpp] view plain copy

1.     void Invert(SqString &s)    

2.    {    

3.         int i;    

4.        char temp;    

5.         for (i=0; i<s.length/2; i++)    

6.        {    

7.             temp = s.data[i];    

8.            s.data[i]=s.data[s.length-i-1];    

9.             s.data[s.length-i-1] = temp;    

10.     }    

11.  }    


2)的main函数:

[cpp] view plain copy

1.     #include <stdio.h>    

2.    #include "sqString.h"    

3.     int main()    

4.    {    

5.         SqString s;    

6.        StrAssign(s, "abcdefg");    

7.         Invert(s);    

8.        DispStr(s);    

9.         return 0;    

10. }    

2)的运行结果:

原来:StrAssign(s, "abcdefg");


3)的实现函数:

[cpp] view plain copy

1.     void DellChar(SqString &s, char c)    

2.    {    

3.         int k=0, i=0;   //k记录值等于c的字符个数    

4.        while(i<s.length)    

5.         {    

6.            if(s.data[i]==c)    

7.                 k++;    

8.            else    

9.                 s.data[i-k]=s.data[i];    

10.         i++;    

11.      }    

12.     s.length -= k;    

13.  }    


3)的main函数:

[cpp] view plain copy

1.     #include <stdio.h>    

2.    #include "sqString.h"    

3.     int main()    

4.    {    

5.         SqString s;    

6.        StrAssign(s, "message");    

7.         DellChar(s, 'e');    

8.        DispStr(s);    

9.         return 0;    

10. }    



3)的运行结果:

原来:StrAssign(s, "message"); 

 

4)的实现函数:

[cpp] view plain copy

1.     SqString CommChar(SqString s1,SqString s2)    

2.    {    

3.         SqString s3;    

4.        int i,j,k=0;    

5.         for (i=0; i<s1.length; i++)    

6.        {    

7.             for (j=0; j<s2.length; j++)    

8.                if (s2.data[j]==s1.data[i])    

9.                     break;    

10.         if (j<s2.length)            //s1.data[i]是公共字符    

11.          {    

12.             s3.data[k]=s1.data[i];    

13.              k++;    

14.         }    

15.      }    

16.     s3.length=k;    

17.      return s3;    

18. }    

4)的main函数:

[cpp] view plain copy

1.     #include <stdio.h>    

2.    #include "sqString.h"    

3.     int main()    

4.    {    

5.         SqString s1, s2, s;    

6.        StrAssign(s1, "message");    

7.         StrAssign(s2, "agent");    

8.        s = CommChar(s1, s2);    

9.         DispStr(s);    

10.     return 0;    

11.  }    

4)的运行结果:



知识点总结:

串的算法的应用。

 

0 0
原创粉丝点击