鱼C论坛

 找回密码
 立即注册
查看: 767|回复: 2

[已解决]帮忙找错

[复制链接]
发表于 2020-3-2 13:12:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
对已排好序的字符指针数组进行指定字符的查找,字符串按字典顺序排列,查找算法采用二分法。
  1. #include <stdio.h>
  2. #include <alloc.h>
  3. #include <string.h>
  4. char *binary(char *ptr[],char *str,int n);
  5. void main ()
  6. {
  7.         char *temp,*ptr1[]={"BASIC","C","DBASE IV","PASCAL","SQL SERVER"};
  8.         int i,j;
  9.         printf("\n");
  10.         printf("original string :\n");
  11.         for(i=0;i<5;i++)
  12.                 printf("%s\n",ptr1[i]);
  13.         printf("input search string:\n");
  14.         temp=(char *)malloc(20);
  15.         gets(temp);
  16.         i=5;
  17.         temp=binary(ptr1,temp,i);
  18.         if(temp)printf("succesful-----%s\n",temp);
  19.         else printf("no succesful\n");
  20. }
  21. char *binary(char *[],char *str,int n)
  22. {
  23.         int hig,low,mid;
  24.         low=0;
  25.         hig=n-1;
  26.         while(low<=hig)
  27.         {
  28.                 mid=(low+hig)/2;
  29.                 if(strcmp(str,ptr[mid])<0)
  30.                         hig=mid-1;
  31.                 else if(strcmp(str,ptr[mid])>0)
  32.                         low=mid+1;
  33.                 else
  34.                         return str;

  35.         }
  36.         return NULL;
  37. }
复制代码

编译以后出现Cannot open include file: 'alloc.h': No such file or directory的错误,请问下怎么改,不太懂
最佳答案
2020-3-2 13:19:35
还有一些BUG,都帮你改回来了
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4. char *binary(char *ptr[],char *str,int n);
  5. void main ()
  6. {
  7.         char *temp,*ptr1[]={"BASIC","C","DBASE IV","PASCAL","SQL SERVER"};
  8.         int i,j;
  9.         printf("\n");
  10.         printf("original string :\n");
  11.         for(i=0;i<5;i++)
  12.                 printf("%s\n",ptr1[i]);
  13.         printf("input search string:\n");
  14.         temp=(char *)malloc(20);
  15.         gets(temp);
  16.         i=5;
  17.         temp=binary(ptr1,temp,i);
  18.         if(temp)printf("succesful-----%s\n",temp);
  19.         else printf("no succesful\n");
  20. }
  21. char *binary(char *ptr[],char *str,int n)
  22. {
  23.         int hig,low,mid;
  24.         low=0;
  25.         hig=n-1;
  26.         while(low<=hig)
  27.         {
  28.                 mid=(low+hig)/2;
  29.                 if(strcmp(str,ptr[mid])<0)
  30.                         hig=mid-1;
  31.                 else if(strcmp(str,ptr[mid])>0)
  32.                         low=mid+1;
  33.                 else
  34.                         return str;

  35.         }
  36.         return NULL;
  37. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-3-2 13:18:23 | 显示全部楼层
改成malloc.h就行了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-2 13:19:35 | 显示全部楼层    本楼为最佳答案   
还有一些BUG,都帮你改回来了
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <string.h>
  4. char *binary(char *ptr[],char *str,int n);
  5. void main ()
  6. {
  7.         char *temp,*ptr1[]={"BASIC","C","DBASE IV","PASCAL","SQL SERVER"};
  8.         int i,j;
  9.         printf("\n");
  10.         printf("original string :\n");
  11.         for(i=0;i<5;i++)
  12.                 printf("%s\n",ptr1[i]);
  13.         printf("input search string:\n");
  14.         temp=(char *)malloc(20);
  15.         gets(temp);
  16.         i=5;
  17.         temp=binary(ptr1,temp,i);
  18.         if(temp)printf("succesful-----%s\n",temp);
  19.         else printf("no succesful\n");
  20. }
  21. char *binary(char *ptr[],char *str,int n)
  22. {
  23.         int hig,low,mid;
  24.         low=0;
  25.         hig=n-1;
  26.         while(low<=hig)
  27.         {
  28.                 mid=(low+hig)/2;
  29.                 if(strcmp(str,ptr[mid])<0)
  30.                         hig=mid-1;
  31.                 else if(strcmp(str,ptr[mid])>0)
  32.                         low=mid+1;
  33.                 else
  34.                         return str;

  35.         }
  36.         return NULL;
  37. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-2 12:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表