|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
对已排好序的字符指针数组进行指定字符的查找,字符串按字典顺序排列,查找算法采用二分法。
- #include <stdio.h>
- #include <alloc.h>
- #include <string.h>
- char *binary(char *ptr[],char *str,int n);
- void main ()
- {
- char *temp,*ptr1[]={"BASIC","C","DBASE IV","PASCAL","SQL SERVER"};
- int i,j;
- printf("\n");
- printf("original string :\n");
- for(i=0;i<5;i++)
- printf("%s\n",ptr1[i]);
- printf("input search string:\n");
- temp=(char *)malloc(20);
- gets(temp);
- i=5;
- temp=binary(ptr1,temp,i);
- if(temp)printf("succesful-----%s\n",temp);
- else printf("no succesful\n");
- }
- char *binary(char *[],char *str,int n)
- {
- int hig,low,mid;
- low=0;
- hig=n-1;
- while(low<=hig)
- {
- mid=(low+hig)/2;
- if(strcmp(str,ptr[mid])<0)
- hig=mid-1;
- else if(strcmp(str,ptr[mid])>0)
- low=mid+1;
- else
- return str;
- }
- return NULL;
- }
复制代码
编译以后出现Cannot open include file: 'alloc.h': No such file or directory的错误,请问下怎么改,不太懂
还有一些BUG,都帮你改回来了
- #include <stdio.h>
- #include <malloc.h>
- #include <string.h>
- char *binary(char *ptr[],char *str,int n);
- void main ()
- {
- char *temp,*ptr1[]={"BASIC","C","DBASE IV","PASCAL","SQL SERVER"};
- int i,j;
- printf("\n");
- printf("original string :\n");
- for(i=0;i<5;i++)
- printf("%s\n",ptr1[i]);
- printf("input search string:\n");
- temp=(char *)malloc(20);
- gets(temp);
- i=5;
- temp=binary(ptr1,temp,i);
- if(temp)printf("succesful-----%s\n",temp);
- else printf("no succesful\n");
- }
- char *binary(char *ptr[],char *str,int n)
- {
- int hig,low,mid;
- low=0;
- hig=n-1;
- while(low<=hig)
- {
- mid=(low+hig)/2;
- if(strcmp(str,ptr[mid])<0)
- hig=mid-1;
- else if(strcmp(str,ptr[mid])>0)
- low=mid+1;
- else
- return str;
- }
- return NULL;
- }
复制代码
|
|