c语言实现搜索
‘壹’ c语言,如何实现搜索内存数据
一般的讲,内存里边虽然说有*G的空间,但有些地方只是挂名存在,实际上是不存在的,所以访问了就会出错,所以就要判断内存是不是为有效地址,
就要用到VirtualQuery获取指定内存属性, 根据属性来判断能不能进行读取,
如果能读取就从调用VirtualQuery中得到的内存信息minfo中获取当前内存地址的有效区域的大小,然后再进行读取. 你可以用VC调试来看看,不能访问的内存就用?号来表示.由于搜所内存是一种运算量庞大的工作,所以,在对比处理要作速度优化处理. 如果数据大于4字节,请用 long 的数据格式来作对比运算, long 是 char 的处理速度的三倍以上,(个人测试的) 用long处理前端数据,再用 char 作收尾工作. 这是对比处理了.流程就有以下:
判断地址的有效性->定好搜所范围->进行对比->输出结果.
StartAdd 开始地址
EndAdd 结束地址
Data 查找的数据
DataSize 数据大小
void *FindMemory(DWORD StartAdd,DWORD EndAdd,void *Data,DWORD DataSize)
{
MEMORY_BASIC_INFORMATION minfo;
DWORD rt;
while(StartAdd<EndAdd)
{
::VirtualQuery((void*)StartAdd,&minfo,sizeof(MEMORY_BASIC_INFORMATION));
if(minfo.AllocationProtect)
if(minfo.State==MEM_COMMIT||minfo.State==MEM_FREE)
{
char *s=(char*)StartAdd,*e=s+minfo.RegionSize;
for(;s<e&&s+DataSize<=e;s++)
if(memcmp(s,Data,DataSize)==0)
return s;
}
StartAdd=(DWORD)minfo.BaseAddress+minfo.RegionSize;
}
return 0;
}
‘贰’ A*搜寻算法的代码实现(C语言实现)
用C语言实现A*最短路径搜索算法,作者 Tittup frog(跳跳蛙)。 #include<stdio.h>#include<math.h>#defineMaxLength100 //用于优先队列(Open表)的数组#defineHeight15 //地图高度#defineWidth20 //地图宽度#defineReachable0 //可以到达的结点#defineBar1 //障碍物#definePass2 //需要走的步数#defineSource3 //起点#defineDestination4 //终点#defineSequential0 //顺序遍历#defineNoSolution2 //无解决方案#defineInfinity0xfffffff#defineEast(1<<0)#defineSouth_East(1<<1)#defineSouth(1<<2)#defineSouth_West(1<<3)#defineWest(1<<4)#defineNorth_West(1<<5)#defineNorth(1<<6)#defineNorth_East(1<<7)typedefstruct{ signedcharx,y;}Point;constPointdir[8]={ {0,1},//East {1,1},//South_East {1,0},//South {1,-1},//South_West {0,-1},//West {-1,-1},//North_West {-1,0},//North {-1,1}//North_East};unsignedcharwithin(intx,inty){ return(x>=0&&y>=0 &&x<Height&&y<Width);}typedefstruct{ intx,y; unsignedcharreachable,sur,value;}MapNode;typedefstructClose{ MapNode*cur; charvis; structClose*from; floatF,G; intH;}Close;typedefstruct//优先队列(Open表){ intlength; //当前队列的长度 Close*Array[MaxLength]; //评价结点的指针}Open;staticMapNodegraph[Height][Width];staticintsrcX,srcY,dstX,dstY; //起始点、终点staticCloseclose[Height][Width];//优先队列基本操作voidinitOpen(Open*q) //优先队列初始化{ q->length=0; //队内元素数初始为0}voidpush(Open*q,Closecls[Height][Width],intx,inty,floatg){ //向优先队列(Open表)中添加元素 Close*t; inti,mintag; cls[x][y].G=g; //所添加节点的坐标 cls[x][y].F=cls[x][y].G+cls[x][y].H; q->Array[q->length++]=&(cls[x][y]); mintag=q->length-1; for(i=0;i<q->length-1;i++) { if(q->Array[i]->F<q->Array[mintag]->F) { mintag=i; } } t=q->Array[q->length-1]; q->Array[q->length-1]=q->Array[mintag]; q->Array[mintag]=t; //将评价函数值最小节点置于队头}Close*shift(Open*q){ returnq->Array[--q->length];}//地图初始化操作voidinitClose(Closecls[Height][Width],intsx,intsy,intdx,intdy){ //地图Close表初始化配置 inti,j; for(i=0;i<Height;i++) { for(j=0;j<Width;j++) { cls[i][j].cur=&graph[i][j]; //Close表所指节点 cls[i][j].vis=!graph[i][j].reachable; //是否被访问 cls[i][j].from=NULL; //所来节点 cls[i][j].G=cls[i][j].F=0; cls[i][j].H=abs(dx-i)+abs(dy-j); //评价函数值 } } cls[sx][sy].F=cls[sx][sy].H; //起始点评价初始值 // cls[sy][sy].G=0; //移步花费代价值 cls[dx][dy].G=Infinity;}voidinitGraph(constintmap[Height][Width],intsx,intsy,intdx,intdy){ //地图发生变化时重新构造地 inti,j; srcX=sx; //起点X坐标 srcY=sy; //起点Y坐标 dstX=dx; //终点X坐标 dstY=dy; //终点Y坐标 for(i=0;i<Height;i++) { for(j=0;j<Width;j++) { graph[i][j].x=i;//地图坐标X graph[i][j].y=j;//地图坐标Y graph[i][j].value=map[i][j]; graph[i][j].reachable=(graph[i][j].value==Reachable); //节点可到达性 graph[i][j].sur=0;//邻接节点个数 if(!graph[i][j].reachable) { continue; } if(j>0) { if(graph[i][j-1].reachable) //left节点可以到达 { graph[i][j].sur|=West; graph[i][j-1].sur|=East; } if(i>0) { if(graph[i-1][j-1].reachable &&graph[i-1][j].reachable &&graph[i][j-1].reachable) //up-left节点可以到达 { graph[i][j].sur|=North_West; graph[i-1][j-1].sur|=South_East; } } } if(i>0) { if(graph[i-1][j].reachable) //up节点可以到达 { graph[i][j].sur|=North; graph[i-1][j].sur|=South; } if(j<Width-1) { if(graph[i-1][j+1].reachable &&graph[i-1][j].reachable &&map[i][j+1]==Reachable)//up-right节点可以到达 { graph[i][j].sur|=North_East; graph[i-1][j+1].sur|=South_West; } } } } }}intbfs(){ inttimes=0; inti,curX,curY,surX,surY; unsignedcharf=0,r=1; Close*p; Close*q[MaxLength]={&close[srcX][srcY]}; initClose(close,srcX,srcY,dstX,dstY); close[srcX][srcY].vis=1; while(r!=f) { p=q[f]; f=(f+1)%MaxLength; curX=p->cur->x; curY=p->cur->y; for(i=0;i<8;i++) { if(!(p->cur->sur&(1<<i))) { continue; } surX=curX+dir[i].x; surY=curY+dir[i].y; if(!close[surX][surY].vis) { close[surX][surY].from=p; close[surX][surY].vis=1; close[surX][surY].G=p->G+1; q[r]=&close[surX][surY]; r=(r+1)%MaxLength; } } times++; } returntimes;}intastar(){ //A*算法遍历 //inttimes=0; inti,curX,curY,surX,surY; floatsurG; Openq;//Open表 Close*p; initOpen(&q); initClose(close,srcX,srcY,dstX,dstY); close[srcX][srcY].vis=1; push(&q,close,srcX,srcY,0); while(q.length) { //times++; p=shift(&q); curX=p->cur->x; curY=p->cur->y; if(!p->H) { returnSequential; } for(i=0;i<8;i++) { if(!(p->cur->sur&(1<<i))) { continue; } surX=curX+dir[i].x; surY=curY+dir[i].y; if(!close[surX][surY].vis) { close[surX][surY].vis=1; close[surX][surY].from=p; surG=p->G+sqrt((curX-surX)*(curX-surX)+(curY-surY)*(curY-surY)); push(&q,close,surX,surY,surG); } } } //printf("times:%d ",times); returnNoSolution;//无结果}constintmap[Height][Width]={ {0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1,1}, {0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1}, {0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,1}, {0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,1}, {0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0}, {0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0}, {0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,1,0,0,0,1}, {0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0}};constcharSymbol[5][3]={"□","▓","▽","☆","◎"};voidprintMap(){ inti,j; for(i=0;i<Height;i++) { for(j=0;j<Width;j++) { printf("%s",Symbol[graph[i][j].value]); } puts(""); } puts("");}Close*getShortest(){ //获取最短路径 intresult=astar(); Close*p,*t,*q=NULL; switch(result) { caseSequential: //顺序最近 p=&(close[dstX][dstY]); while(p) //转置路径 { t=p->from; p->from=q; q=p; p=t; } close[srcX][srcY].from=q->from; return&(close[srcX][srcY]); caseNoSolution: returnNULL; } returnNULL;}staticClose*start;staticintshortestep;intprintShortest(){ Close*p; intstep=0; p=getShortest(); start=p; if(!p) { return0; } else { while(p->from) { graph[p->cur->x][p->cur->y].value=Pass; printf("(%d,%d)→ ",p->cur->x,p->cur->y); p=p->from; step++; } printf("(%d,%d) ",p->cur->x,p->cur->y); graph[srcX][srcY].value=Source; graph[dstX][dstY].value=Destination; returnstep; }}voidclearMap(){ //ClearMapMarksofSteps Close*p=start; while(p) { graph[p->cur->x][p->cur->y].value=Reachable; p=p->from; } graph[srcX][srcY].value=map[srcX][srcY]; graph[dstX][dstY].value=map[dstX][dstY];}voidprintDepth(){ inti,j; for(i=0;i<Height;i++) { for(j=0;j<Width;j++) { if(map[i][j]) { printf("%s",Symbol[graph[i][j].value]); } else { printf("%2.0lf",close[i][j].G); } } puts(""); } puts("");}voidprintSur(){ inti,j; for(i=0;i<Height;i++) { for(j=0;j<Width;j++) { printf("%02x",graph[i][j].sur); } puts(""); } puts("");}voidprintH(){ inti,j; for(i=0;i<Height;i++) { for(j=0;j<Width;j++) { printf("%02d",close[i][j].H); } puts(""); } puts("");}intmain(intargc,constchar**argv){ initGraph(map,0,0,0,0); printMap(); while(scanf("%d%d%d%d",&srcX,&srcY,&dstX,&dstY)!=EOF) { if(within(srcX,srcY)&&within(dstX,dstY)) { if(shortestep=printShortest()) { printf("从(%d,%d)到(%d,%d)的最短步数是:%d ", srcX,srcY,dstX,dstY,shortestep); printMap(); clearMap(); bfs(); //printDepth(); puts((shortestep==close[dstX][dstY].G)?"正确":"错误"); clearMap(); } else { printf("从(%d,%d)不可到达(%d,%d) ", srcX,srcY,dstX,dstY); } } else { puts("输入错误!"); } } return(0);}
‘叁’ C语言字符串查找的几种实现
首先获得一个字符用ch=getchar()或者scanf ("%c", &ch);
其次判断字符相等直接用==
接着j没有定义
最后输出int数组用循环
参考代码:
#include <stdio.h>#include <string.h> int main(){ char a[80] = "abcdefgh\0"; char ch; int i, m, b[80]; int flag = 0; ch = getchar();//获取一个字符 m = strlen(a); for (i = 0; i < m; ++i){ if (a[i] == ch){//找到了,直接判断是否相等 b[flag] = i+1;//记录位置 flag += 1; } } if (flag == 0)printf ("no"); else { printf ("%d\n", flag); for (i = 0; i < flag; i++){//对位置进行输出,用循环 printf ("%d ", b[i]); } printf ("\n"); } return 0;}
‘肆’ C语言如何用函数来实现搜索
#include<stdio.h>
intsearch(inta[],intb,intc,inti)
{
intx,y,z;
x=i+1;
z=b-1;
y=(x+z)/2;
while(x<=z)
{
if(a[y]>c)
{
z=y-1;
y=(x+z)/2;
continue;
}
if(a[y]<c)
{
x=y+1;
y=(x+z)/2;
continue;
}
returny+1;
}
return-1;
}
intmain()
{
inti,m,pos;
scanf("%d",&m);
inta[m];
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<m;i++)
{
pos=search(a,m,a[i],i);
if(pos!=-1)
{
printf("FOUNDa[%d]=%d,positionis%d ",i,a[i],i+1);
return0;
}
}
if(pos==-1)
{
printf("NOTFOUND ");
}
return0;
}
这种查找方法的数组必须是从小到大的,用遍历的话就没这个问题了。
‘伍’ C语言实现二叉搜索
这东西很多的这里给你一个#include
#include
typedef struct np{
int dat;
struct np *left,*right;
} node;
node *create(void)
{
return (malloc(sizeof(node)));
}
node *t(node *a,int d)
{
if (a==NULL) {
a=create();
a->left =a->right =NULL;
a->dat=d;
}
else if (d>=a->dat) {
a->right =t(a->right,d);
}
else if (ddat) {
a->left =t(a->left ,d);
}
return a;
}
void inorder(node *r)
{
if (r!=NULL) {
inorder(r->left );
printf("%d ",r->dat );
inorder(r->right );
}
}
int ser(node *so,int a)
{
if (so==NULL)
return 0;
else if (so->dat==a)
return 1;
else if (a>so->dat)
return ser(so->right,a);
else if (adat)
return ser(so->left ,a);
}
int main(int argc, char* argv[])
{
node *bst=NULL;
FILE *fp;
int i;
fp=fopen("c:\\dat.txt","r"); /*假设数据文件是c:\dat.txt*/
while (!feof(fp)){
fscanf(fp,"%d",&i);
bst=t(bst,i); /*生成二叉排序树*/
}
fclose(fp);
inorder(bst); /*输出二叉排序树*/
putchar('\n');
scanf("%d",&i); /*输入需要查找的数字*/
if (ser(bst,i)) printf("YES"); /*如果找到,则输出yes,否则输出no*/
else printf("NO");
return 0;
}
//-
‘陆’ 用c语言编程,编写一个函数实现查找功能,给定一个数N(char)类型,在已排序的
摘要 #include
‘柒’ c语言 编写一个函数,其功能为搜索由第一个参数指定的字符串,在其中查找由第二个参数指定的字符第一次
这样:
#include<stdio.h>
// 计算字符串长度
int len(char a[])
{
int temp=0,i;
for(i=0;a[i]!='