天天看点

第八周项目三

/*        
 * Copyright (c) 2017,烟台大学计算机学院        
 * All right reserved.        
 * 文件名称:main.cpp       
 * 作者:于嵩        
 * 完成日期:2017年10月26日        
 * 版本号:v1.0        
 *        
 * 问题描述:顺序串算法    
 * 输入描述:标准函数输入        
 * 程序输出:标准函数输出    
*/         
           

采用顺序存储方式存储串,实现下列算法并测试: 

(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);

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

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

[cpp] view plain copy print ?

  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. int main()  
  11. {  
  12.     SqString s;  
  13.     StrAssign(s, "messages");  
  14.     Trans(s, 'e', 'a');  
  15.     DispStr(s);  
  16.     return 0;  
  17. }  
#include <stdio.h>
#include "sqString.h"
void Trans(SqString &s, char c1, char c2)
{
    int i;
    for (i=0; i<s.length; i++)
        if (s.data[i]==c1)
            s.data[i]=c2;
}

int main()
{
    SqString s;
    StrAssign(s, "messages");
    Trans(s, 'e', 'a');
    DispStr(s);
    return 0;
}
           

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

void Invert(SqString &s)

[cpp] view plain copy print ?

  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. int main()  
  15. {  
  16.     SqString s;  
  17.     StrAssign(s, "abcdefg");  
  18.     Invert(s);  
  19.     DispStr(s);  
  20.     return 0;  
  21. }  
#include <stdio.h>
#include "sqString.h"
void Invert(SqString &s)
{
    int i;
    char temp;
    for (i=0; i<s.length/2; i++)
    {
        temp = s.data[i];
        s.data[i]=s.data[s.length-i-1];
        s.data[s.length-i-1] = temp;
    }
}

int main()
{
    SqString s;
    StrAssign(s, "abcdefg");
    Invert(s);
    DispStr(s);
    return 0;
}
           

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

void DellChar(SqString &s, char c)

[cpp] view plain copy print ?

  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. int main()  
  17. {  
  18.     SqString s;  
  19.     StrAssign(s, "message");  
  20.     DellChar(s, 'e');  
  21.     DispStr(s);  
  22.     return 0;  
  23. }  
#include <stdio.h>
#include "sqString.h"
void DellChar(SqString &s, char c)
{
    int k=0, i=0;   //k记录值等于c的字符个数
    while(i<s.length)
    {
        if(s.data[i]==c)
            k++;
        else
            s.data[i-k]=s.data[i];
        i++;
    }
    s.length -= k;
}

int main()
{
    SqString s;
    StrAssign(s, "message");
    DellChar(s, 'e');
    DispStr(s);
    return 0;
}
           

(4)有两个串s1和s2,设计一个算法求一个这样的串,该串中的字符是s1和s2中公共字符。所谓公共子串,是由在s1中有,且在s2中也有的字符构成的字符。例s1为”message”,s2为”agent”,得到的公共子串是”eage”。 

SqString CommChar(SqString s1,SqString s2);

[cpp] view plain copy print ?

  1. #include <stdio.h>   
  2. #include "sqString.h"   
  3. SqString CommChar(SqString s1,SqString s2)  
  4. {  
  5.     SqString s3;  
  6.     int i,j,k=0;  
  7.     for (i=0; i<s1.length; i++)  
  8.     {  
  9.         for (j=0; j<s2.length; j++)  
  10.             if (s2.data[j]==s1.data[i])  
  11.                 break;  
  12.         if (j<s2.length)            //s1.data[i]是公共字符   
  13.         {  
  14.             s3.data[k]=s1.data[i];  
  15.             k++;  
  16.         }  
  17.     }  
  18.     s3.length=k;  
  19.     return s3;  
  20. }  
  21. int main()  
  22. {  
  23.     SqString s1, s2, s;  
  24.     StrAssign(s1, "message");  
  25.     StrAssign(s2, "agent");  
  26.     s = CommChar(s1, s2);  
  27.     DispStr(s);  
  28.     return 0;  
  29. }  
#include <stdio.h>
#include "sqString.h"

SqString CommChar(SqString s1,SqString s2)
{
    SqString s3;
    int i,j,k=0;
    for (i=0; i<s1.length; i++)
    {
        for (j=0; j<s2.length; j++)
            if (s2.data[j]==s1.data[i])
                break;
        if (j<s2.length)            //s1.data[i]是公共字符
        {
            s3.data[k]=s1.data[i];
            k++;
        }
    }
    s3.length=k;
    return s3;
}

int main()
{
    SqString s1, s2, s;
    StrAssign(s1, "message");
    StrAssign(s2, "agent");
    s = CommChar(s1, s2);
    DispStr(s);
    return 0;
}
           

运行结果:

第八周项目三
第八周项目三
第八周项目三
第八周项目三