第八周项目二

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

运行结果: