| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 685 人关注过本帖
标题:[求助]二叉查找编译错误
只看楼主 加入收藏
xiaori
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-11-22
收藏
 问题点数:0 回复次数:12 
[求助]二叉查找编译错误

我自己编写递归二叉查找问题,有警告如下,请帮忙找出错误谢谢
--------------------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;
}






搜索更多相关主题的帖子: 编译 
2007-01-04 15:06
yaleond
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-12-24
收藏
得分:0 

你的编译器有点问题是不是啊?用的是什么?我在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;
}

2007-01-05 10:49
xiaori
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-11-22
收藏
得分:0 
我用的编译器是VC++,你的程序改动只有主函数么,返回了一个数值,
错误提示是说查找的情况没有考虑完全是么,是什么原因呢?

希望和大家交朋友,相互帮助,共同提高!
2007-01-06 08:45
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

可以编译..我用的也是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编辑过]


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-01-06 16:43
yaleond
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-12-24
收藏
得分:0 

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

2007-01-09 08:49
xiaori
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-11-22
收藏
得分:0 
我说了啊,编译却是能通过,但是有警告啊,如前面所说的,你的程序我运行了,已然有警告

希望和大家交朋友,相互帮助,共同提高!
2007-01-10 12:28
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 


#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编辑过]


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-01-10 18:42
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 

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

#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的时候也要返回一个值


Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2007-01-10 19:50
xiaori
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-11-22
收藏
得分:0 
上面的版主谢谢你的支持,不过你可以换个字符查找一下,比如说b,它会输出0,显然不是需要查找的数值。
我觉得我的错误出在递归过程中的函数返回值,如上面的版主所说的相同,在查找到数值的那个函数调用外的其他函数都没有返回值,
但是应该怎么改我还是没有想法,希望大家帮忙!!谢谢


希望和大家交朋友,相互帮助,共同提高!
2007-01-10 22:57
caiqiufu
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-12-14
收藏
得分: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)
{
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;
}

2007-01-11 15:13
快速回复:[求助]二叉查找编译错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024252 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved