阅读:1430回复:0
麻烦大家帮看看这个search_q函数啊?
我要实现在一个数组里面对不同的字符串进行搜索,找到匹配的字符串口返回数组行号(字符串编号)。程序为如下所示,这是怎么回事情啊?用不了!估计写错了,麻烦指点指点,谢谢!
#include <windows.h> #include <iostream.h> #include <string.h> #include <stdio.h> #define MAX 2 void strcopyG(unsigned char *destr,unsigned char *soustr, int g) { for(int e=0; e<g; e++) destr[e] = soustr[e]; destr[e] = '\0'; } bool search( char * string, const char *substring) { int i,j,k,count = 0; bool m = false; int pp = strlen(string) - strlen(substring); for(i=0; i<pp; i++) { for(j=i,k=0; string[j]==substring[k]; j++) { if(!substring[k+1]) { count++; k = -1; if(count) m = true; } k++; } } if(m) return true; else return false; } int search_q( unsigned char *string,const unsigned char *qr[]) { int l = 0; bool find = false; unsigned char a[20]; unsigned char *b = a; unsigned char strbuf[30]; strcopyG(strbuf,string,30); for(l=0; l<MAX; l++) { strcpy((char*)b,(const char*)qr[l+0]); find = search((char*)strbuf,(const char *)b); if(find) break; else l++; } if(find) return l; else return 0; } void main(void) { static unsigned char aa[6] = {0xaa,0xe0,0x45,0x01,0x76,0x00}; static unsigned char bb[6] = {0xaa,0xe0,0x45,0x01,0x76,0x03}; unsigned char cc[2][3] = {{0xaa,0xe0,0x45},{0x03}}; unsigned char *b = bb; unsigned char *a = aa; unsigned char *qqr[2] ; qqr[0] = cc[0]; qqr[1] = cc[1]; bool ba = false; int sq; ba = search((char*)b,(const char*)a); if(ba) cout<<" true! " <<endl; else cout<<" false!" <<endl; sq = search_q(bb,qqr[0]); cout<< sq <<endl; } |
|