第八周项目5-计数的模式匹配

来源:互联网 发布:深圳奥芯软件 编辑:程序博客网 时间:2024/05/19 03:20
  1. /* 
  2.  
  3. copyright (t) 2016,烟台大学计算机学院 
  4.  
  5. *All rights reserved. 
  6.  
  7. *文件名称:1.cpp 
  8.  
  9. *作者:车金阳
  10.  
  11. *完成日期:2016年12月15日 
  12.  
  13. *版本号:v1.0 
  14.  
  15. *问题描述:采用顺序结构存储串,编写一个算法计算指定子串在一个字符串中出现的次数,如果该子串不出现则为0。 
  16.  
  17. *输入描述:字符串(设为多组输入) 
  18.  
  19. *程序输出:指定子串在一个字符串中出现的次数 
  20.  
  21. */  

sqstring.h:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #define MaxSize 100  
  3. typedef struct                  //定义顺序串类型  
  4. {  
  5.     char data[MaxSize];         //存放字符  
  6.     int length;                 //记录串长度  
  7. } SqString;  
  8. void StrAssign(SqString &s,char cstr[]);                //字符串常量cstr赋给串s  
  9. void StrCopy(SqString &s,SqString t);                   //串t复制给串s  
  10. bool StrEqual(SqString s,SqString t);                   //判串相等  
  11. int StrLength(SqString s);                              //求串长  
  12. SqString Concat(SqString s,SqString t);                 //串连接  
  13. SqString SubStr(SqString s,int i,int j);                //求子串  
  14. SqString InsStr(SqString s1,int i,SqString s2);         //串插入  
  15. SqString DelStr(SqString s,int i,int j) ;               //串删去  
  16. SqString RepStr(SqString s,int i,int j,SqString t);     //串替换  
  17. void DispStr(SqString s);                               //输出串  

sqstring.cpp:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include "sqstring.h"  
  3. int index(SqString s,SqString t)  
  4. {  
  5.     int i=0,j=0;  
  6.     int count=0;  
  7.     while(i<s.length && j<t.length)  
  8.     {  
  9.         if(s.data[i]==t.data[j])  
  10.             i++,j++;  
  11.         else  
  12.         {  
  13.             i=i-j+1;  
  14.             j=0;  
  15.         }  
  16.         if(j>=t.length)                         //完成一次匹配后,次数+1  
  17.         {  
  18.             count++;  
  19.             i=i-j+1;                            //主串从下一位置开始匹配,子串从头开始匹配  
  20.             j=0;  
  21.         }  
  22.     }  
  23.     return count;  
  24. }  
  25. int main()  
  26. {  
  27.     SqString s1,s2;                             //s1为主串,s2为模式串  
  28.     char a[1000],b[1000];  
  29.     while(gets(a))  
  30.     {  
  31.         gets(b);  
  32.         StrAssign(s1,a);  
  33.         StrAssign(s2,b);  
  34.         printf("%d\n\n",index(s1,s2));  
  35.     }  
  36.     return 0;  
  37. }  

main.cpp:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <stdio.h>  
  2. #include "sqstring.h"  
  3. int index(SqString s,SqString t)  
  4. {  
  5.     int i=0,j=0;  
  6.     int count=0;  
  7.     while(i<s.length && j<t.length)  
  8.     {  
  9.         if(s.data[i]==t.data[j])  
  10.             i++,j++;  
  11.         else  
  12.         {  
  13.             i=i-j+1;  
  14.             j=0;  
  15.         }  
  16.         if(j>=t.length)  
  17.         {  
  18.             count++;  
  19.             i=i-j+1;  
  20.             j=0;  
  21.         }  
  22.     }  
  23.     return count;  
  24. }  
  25. int main()  
  26. {  
  27.     SqString s1,s2;                             //s1为主串,s2为模式串  
  28.     char a[1000],b[1000];  
  29.     while(gets(a))  
  30.     {  
  31.         gets(b);  
  32.         StrAssign(s1,a);  
  33.         StrAssign(s2,b);  
  34.         printf("%d\n\n",index(s1,s2));  
  35.     }  
  36.     return 0;  
  37. }  

运行结果:


0 0