第八周项目二

来源:互联网 发布:centos 7 yum 安装svn 编辑:程序博客网 时间:2024/05/17 00:17
  1. /*  
  2. 烟台大学计算机学院  
  3.   
  4. 文件名称:xiangmu.cpp  
  5.   
  6. 作者:刘照京  
  7.   
  8. 完成日期:2017年11月2日  
  9.   
  10. 问题描述:采用顺序存储方式存储串,实现下列算法并测试 
  11.   
  12. 输入描述:无 
  13.   
  14. 输出描述:对串处理后的元素 
  15.   
  16. */   
  17.   
  18.   
  19. //1.试编写算法实现将字符串S中所有值为c1的字符换成值为c2的字符:   
  20. //void Trans(SqString *&s, char c1, char c2);   
  21.   
  22. #include <stdio.h>  
  23. #include "../sqstring.h"  
  24. void Trans(SqString &s, char c1, char c2)  
  25. {  
  26.     int i;  
  27.     for (i=0; i<s.length; i++)  
  28.         if (s.data[i]==c1)  
  29.             s.data[i]=c2;  
  30. }  
  31.   
  32. int main()  
  33. {  
  34.     SqString s;  
  35.     StrAssign(s, "messages");  
  36.     Trans(s, 'e''a');  
  37.     DispStr(s);  
  38.     return 0;  
  39. }  
  40.   
  41.   
  42. //2.试编写算法,实现将已知字符串所有字符倒过来重新排列。如ABCDEF改为FEDCBA。   
  43. //void Invert(SqString &s);  
  44.   
  45. #include <stdio.h>  
  46. #include "../sqstring.h"  
  47. void Invert(SqString &s)  
  48. {  
  49.     int i;  
  50.     char temp;  
  51.     for (i=0; i<s.length/2; i++)  
  52.     {  
  53.         temp = s.data[i];  
  54.         s.data[i]=s.data[s.length-i-1];  
  55.         s.data[s.length-i-1] = temp;  
  56.     }  
  57. }  
  58.   
  59. int main()  
  60. {  
  61.     SqString s;  
  62.     StrAssign(s, "abcdefg");  
  63.     Invert(s);  
  64.     DispStr(s);  
  65.     return 0;  
  66. }  
  67.   
  68.   
  69. //3.从串s中删除其值等于c的所有字符。如从message中删除’e’,得到的是mssag。   
  70. //void DellChar(SqString &s, char c);  
  71. #include <stdio.h>  
  72. #include "../sqstring.h"  
  73.   
  74. void DellChar(SqString &s, char c)  
  75. {  
  76.     int k=0, i=0;   //k记录值等于c的字符个数  
  77.     while(i<s.length)  
  78.     {  
  79.         if(s.data[i]==c)  
  80.             k++;  
  81.         else  
  82.             s.data[i-k]=s.data[i];  
  83.         i++;  
  84.     }  
  85.     s.length -= k;  
  86. }  
  87.   
  88. int main()  
  89. {  
  90.     SqString s;  
  91.     StrAssign(s, "message");  
  92.     DellChar(s, 'e');  
  93.     DispStr(s);  
  94.     return 0;  
  95. }  
  96.   
  97. //4.有两个串s1和s2,设计一个算法求一个这样的串,该串中的字符是s1和s2中公共字符。  
  98. //所谓公共子串,是由在s1中有,  
  99. //且在s2中也有的字符构成的字符。例s1为”message”,s2为”agent”,  
  100. //得到的公共子串是”eage”。   
  101. SqString CommChar(SqString s1,SqString s2);  
  102.   
  103.   
  104. #include <stdio.h>  
  105. #include "../sqstring.h"  
  106. SqString CommChar(SqString s1,SqString s2)  
  107. {  
  108.     SqString s3;  
  109.     int i,j,k=0;  
  110.     for (i=0; i<s1.length; i++)  
  111.     {  
  112.         for (j=0; j<s2.length; j++)  
  113.             if (s2.data[j]==s1.data[i])  
  114.                 break;  
  115.         if (j<s2.length)            //s1.data[i]是公共字符  
  116.         {  
  117.             s3.data[k]=s1.data[i];  
  118.             k++;  
  119.         }  
  120.     }  
  121.     s3.length=k;  
  122.     return s3;  
  123. }  
  124.   
  125. int main()  
  126. {  
  127.     SqString s1, s2, s;  
  128.     StrAssign(s1, "message");  
  129.     StrAssign(s2, "agent");  
  130.     s = CommChar(s1, s2);  
  131.     DispStr(s);  
  132.     return 0;  
  133. }  

运行结果:

学习心得:学会了运用顺序存储方式存储串。