?? bf.cpp
字號:
//字符串匹配算法
//BF(Brute Force)算法,區分大小寫
#include<stdio.h>
#include<stdlib.h>
#define pat_length 34 //模式字符串長度
int compare(unsigned char *t, unsigned char *p);
unsigned char pat[pat_length+1]={"consideration in Utah and Michigan"}; //存儲模式數據,最后一位存儲字符串終止符
unsigned char test[pat_length+1]={0}; //存儲與模式等長的文本數據
void main()
{
int i;
int match; //字符比較的返回值
unsigned char *t,*p; //分別指向test數組和pat數組
FILE *fp;
if((fp=fopen("e:/c projects/bf_algorithm/test.txt","r"))==NULL) //打開文本文件
{
printf("Cannot open test file!");
exit(1);
}
fread(test,sizeof(unsigned char),pat_length,fp);
do
{
t=test;
p=pat;
match=compare(t,p);
//如果字符串匹配則結束循環
if(match)
{
printf("Stream match!!!\n");
break;
}
else //否則向右滑行一個字符繼續比較
{
for(i=0;i<pat_length-1;i++)
test[i]=test[i+1];
fread(&test[pat_length-1],sizeof(unsigned char),1,fp);
if(test[pat_length-1]=='\n') //如果最后一位是換行符,則用空格符代替換行符
test[pat_length-1]=' ';
}
}while(!feof(fp));
if(!match)
printf("Stream not match......!!!\n");
fclose(fp);
return ;
}
//字符串比較函數,不匹配返回0,匹配返回1
int compare(unsigned char *t, unsigned char *p)
{
int i;
int match;
unsigned char a,d;
for(i=0;i<pat_length;i++)
{
a=*t;
d=*p;
// printf("d=%c\n",d);
// printf("a=%c\n",a);
if(d==a)
{
t++;
p++;
}
else
{
match=0;
break;
}
}
if(i==pat_length)
match=1;
return match;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -