注册 登录
编程论坛 C++教室

[求助]二叉查找编译错误

xiaori 发布于 2007-01-04 15:06, 685 次点击

我自己编写递归二叉查找问题,有警告如下,请帮忙找出错误谢谢
--------------------Configuration: banerysearch - Win32 Debug--------------------
Compiling...
banerysearch.cpp
D:\课程\编程学习\C++文件夹\作业\fileshiyan\banerysearch.cpp(22) : warning C4715: 'binerysearch' : not all control paths return a value

banerysearch.obj - 0 error(s), 0 warning(s)


程序如下:
#include<iostream.h>
#include<stdlib.h>
typedef char datatype;

int binerysearch(datatype a[],datatype goal,int low,int hight)
{
int mid=(low+hight)/2;
datatype midvalue=a[mid];

if(midvalue==goal) return mid;

else if(goal<midvalue&&low!=hight)
binerysearch(a,goal,low,mid-1);
else if(goal>midvalue&&low!=hight)
binerysearch(a,goal,mid+1,hight);
else
{
cout<<"No that number!!"<<endl;
return -1;
}

}

void main()
{
int i;
char a[6]={'a','b','c','d','e','f'};
i=binerysearch(a,'c',0,5);
cout<<i<<endl;
}






12 回复
#2
yaleond2007-01-05 10:49

你的编译器有点问题是不是啊?用的是什么?我在Dev-c++里编译通过啊!你的程序我只是改过一点点就可以了,你把你的编译器好好弄一下吧!
#include<iostream>
using namespace std;
typedef char datatype;

int binerysearch(datatype a[],datatype goal,int low,int hight)
{
int mid=(low+hight)/2;
datatype midvalue=a[mid];

if(midvalue==goal) return mid;

else if(goal<midvalue&&low!=hight)
binerysearch(a,goal,low,mid-1);
else if(goal>midvalue&&low!=hight)
binerysearch(a,goal,mid+1,hight);
else
{
cout<<"No that number!!"<<endl;
return -1;
}

}

int main()
{
int i;
char a[6]={'a','b','c','d','e','f'};
i=binerysearch(a,'c',0,5);
cout<<i<<endl;
return 0;
}

#3
xiaori2007-01-06 08:45
我用的编译器是VC++,你的程序改动只有主函数么,返回了一个数值,
错误提示是说查找的情况没有考虑完全是么,是什么原因呢?
#4
yuyunliuhen2007-01-06 16:43

可以编译..我用的也是VC++6.0
#include<iostream.h>
#include<stdlib.h>
typedef char datatype;

int binerysearch(datatype a[],datatype goal,int low,int hight)
{
int mid=(low+hight)/2;
datatype midvalue=a[mid];

if(midvalue==goal) return mid;

else if(goal<midvalue&&low!=hight)
binerysearch(a,goal,low,mid-1);
else if(goal>midvalue&&low!=hight)
binerysearch(a,goal,mid+1,hight);
else
{
cout<<"No that number!!"<<endl;
return -1;
}

}

void main()
{
int i;
char a[6]={'a','b','c','d','e','f'};
i=binerysearch(a,'c',0,5);
cout<<i<<endl;
}

程序没问题的话,应该是编译器的问题.

[此贴子已经被作者于2007-1-6 16:51:17编辑过]

#5
yaleond2007-01-09 08:49

可以确定是编译器的问题了!

#6
xiaori2007-01-10 12:28
我说了啊,编译却是能通过,但是有警告啊,如前面所说的,你的程序我运行了,已然有警告
#7
yuyunliuhen2007-01-10 18:42


#include<iostream>
using namespace std; //
#include<stdlib.h>
typedef char datatype;

int binerysearch(datatype a[],datatype goal,int low,int hight)
{
int mid=(low+hight)/2;
datatype midvalue=a[mid];

if(midvalue==goal) return mid;

else if(goal<midvalue&&low!=hight)
binerysearch(a,goal,low,mid-1);
else if(goal>midvalue&&low!=hight)
binerysearch(a,goal,mid+1,hight);
else
{
cout<<"No that number!!"<<endl;
return -1;
}

}

int main() //
{
int i;
char a[6]={'a','b','c','d','e','f'};
i=binerysearch(a,'c',0,5);
cout<<i<<endl;
return 0; //
}

确实没有什么的大的错误,上面的程序通过了VC++6.0 VS2005. DEV-C++4.9.9.2..只改一点点就OK 了...

[此贴子已经被作者于2007-1-10 19:52:24编辑过]

#8
yuyunliuhen2007-01-10 19:50

不好意思,应该是下面这段代码

#include<iostream>
using namespace std; //
#include<stdlib.h>
typedef char datatype;

int binerysearch(datatype a[],datatype goal,int low,int hight)
{
int mid=(low+hight)/2;
datatype midvalue=a[mid];

if(midvalue==goal) return mid;

else if(goal<midvalue&&low!=hight)
{
binerysearch(a,goal,low,mid-1);
return 0;
}
else if(goal>midvalue&&low!=hight)
{
binerysearch(a,goal,mid+1,hight);
return 0;
}
else
{
cout<<"No that number!!"<<endl;
return -1;
}

}

int main() //
{
int i;
char a[6]={'a','b','c','d','e','f'};
i=binerysearch(a,'c',0,5);
cout<<i<<endl;
return 0;
}

使用if 必须考虑两种情况了,所以必须在Else的时候也要返回一个值

#9
xiaori2007-01-10 22:57
上面的版主谢谢你的支持,不过你可以换个字符查找一下,比如说b,它会输出0,显然不是需要查找的数值。
我觉得我的错误出在递归过程中的函数返回值,如上面的版主所说的相同,在查找到数值的那个函数调用外的其他函数都没有返回值,
但是应该怎么改我还是没有想法,希望大家帮忙!!谢谢

#10
caiqiufu2007-01-11 15:13

#include<iostream.h>
#include<stdlib.h>
typedef char datatype;
int binerysearch(datatype a[],datatype goal,int low,int hight)
{
int mid=(low+hight)/2;
datatype midvalue=a[mid];
if(midvalue==goal) return mid;
else if(goal<midvalue&&low!=hight)
{
return binerysearch(a,goal,low,mid-1); //binearysearch()是一个返回值
}
else if(goal>midvalue&&low!=hight)
{
return binerysearch(a,goal,mid+1,hight);
}
else
{
cout<<"No that number!!"<<endl;
return -1;
}

};
void main()
{
int i;
char a[6]={'a','b','c','d','e','f'};
i=binerysearch(a,'c',0,5);
cout<<i<<endl;
}

#11
xiaori2007-02-14 08:09

谢谢上面的兄弟了,我看你的程序差不多,我试下吧,不过先谢了

#12
lehmann2007-03-11 22:02

不是编译器问题
你把最后一个else的{}去掉就可以了
int binerysearch(datatype a[],datatype goal,int low,int hight)
{
int mid=(low+hight)/2;
datatype midvalue=a[mid];
if(midvalue==goal) return mid;
else if(goal<midvalue&&low!=hight)
{
return binerysearch(a,goal,low,mid-1); //binearysearch()是一个返回值
}
else if(goal>midvalue&&low!=hight)
{
return binerysearch(a,goal,mid+1,hight);
}
else
// {
cout<<"No that number!!"<<endl;
return -1;
// }

}

#13
清水香里2007-03-12 13:53
可以编译。
1