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

问题及代码:

  1. /*  
  2. * Copyright (c)2016,烟台大学计算机与控制工程学院  
  3. * All rights reserved.  
  4. * 文件名称:项目3.cpp  
  5. * 作    者:陈哲  
  6. * 完成日期:2016年10月20日  
  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和源文件SqString.cpp详见第八周项目1--建立顺序串的算法库

注:在头文件中加上相应的函数声明。

        并且在源文件中加上相应的函数实现。

(1)函数实现:

  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函数:

  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)运行结果:

原来:

运行结果:



(2)函数实现:

  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函数:

  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)运行结果:
原来:

运行后:



(3)函数实现:

  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函数:

  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)运行结果:
原来:


运行后:


(4)的main函数:

[cpp]  view plain  copy
 print ?
  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值