第八周项目5-计数的模式匹配
来源:互联网 发布:深圳奥芯软件 编辑:程序博客网 时间:2024/05/19 03:20
sqstring.h:- #include <stdio.h>
- #define MaxSize 100
- typedef struct
- {
- char data[MaxSize];
- int length;
- } SqString;
- void StrAssign(SqString &s,char cstr[]);
- void StrCopy(SqString &s,SqString t);
- bool StrEqual(SqString s,SqString t);
- int StrLength(SqString s);
- SqString Concat(SqString s,SqString t);
- SqString SubStr(SqString s,int i,int j);
- SqString InsStr(SqString s1,int i,SqString s2);
- SqString DelStr(SqString s,int i,int j) ;
- SqString RepStr(SqString s,int i,int j,SqString t);
- void DispStr(SqString s);
sqstring.cpp:- #include <stdio.h>
- #include "sqstring.h"
- int index(SqString s,SqString t)
- {
- int i=0,j=0;
- int count=0;
- while(i<s.length && j<t.length)
- {
- if(s.data[i]==t.data[j])
- i++,j++;
- else
- {
- i=i-j+1;
- j=0;
- }
- if(j>=t.length)
- {
- count++;
- i=i-j+1;
- j=0;
- }
- }
- return count;
- }
- int main()
- {
- SqString s1,s2;
- char a[1000],b[1000];
- while(gets(a))
- {
- gets(b);
- StrAssign(s1,a);
- StrAssign(s2,b);
- printf("%d\n\n",index(s1,s2));
- }
- return 0;
- }
main.cpp:- #include <stdio.h>
- #include "sqstring.h"
- int index(SqString s,SqString t)
- {
- int i=0,j=0;
- int count=0;
- while(i<s.length && j<t.length)
- {
- if(s.data[i]==t.data[j])
- i++,j++;
- else
- {
- i=i-j+1;
- j=0;
- }
- if(j>=t.length)
- {
- count++;
- i=i-j+1;
- j=0;
- }
- }
- return count;
- }
- int main()
- {
- SqString s1,s2;
- char a[1000],b[1000];
- while(gets(a))
- {
- gets(b);
- StrAssign(s1,a);
- StrAssign(s2,b);
- printf("%d\n\n",index(s1,s2));
- }
- return 0;
- }
运行结果:
0 0