| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1031 人关注过本帖
标题:我自己编的一个二叉查找的程序,出错了,可以帮我看看吗?
只看楼主 加入收藏
kappa314
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2004-10-9
收藏
 问题点数:0 回复次数:4 
我自己编的一个二叉查找的程序,出错了,可以帮我看看吗?

错误如下:1。error C2601: 'main' : local function definitions are illegal

2。fatal error C1004: unexpected end of file found

编译时,发现,如果Compare函数不写在BinarySearch函数之前,编译会出错啊?为什么啊?

程序如下:

#include"iostream" #include"stdlib.h" using namespace std; int Compare(int elem1,int elem2){ if(elem1<elem2) return -1; if(elem1==elem2) return 0; if(elem1>elem2) return 1; }//Compare int BinarySearch(int list[],int target,int n){ //list the elements to be searched //target the value being searched for //n the number of elements in the list int start=1,end=n,middle; while(start<=end){ middle=(start+end)/2; switch(Compare(list[middle],target)){ case -1:start=middle+1;break; //list[middle]<target case 0:return middle;break; //list[middle]==target case 1:end=middle-1;break; //list[middle]>target }//while return 0; }//BinarySearch int main(){ int a[16],i,location,Target; //location the position of Target //Target the element to be found a[0]=0; cout<<"\nPlease input the Target:"; cin>>Target; cout<<"\n"; for(i=1;i<16;i++){ a[i]=2*i-1; a[0]++; }//the elements of the list are from 1,3,5 to 25,27,29 and the length of the list is a[0] if(Targe<a[1]||Taget>a[15]){//Input is illegal cout<<"\nIllegal input!\n"; exit(0); }//if location=BinarySearch(a[16],Target,a[0]); cout<<"\nThe elements of the list are:\n"; for(i=1;i<16;i++) cout<<a[i]; cout<<"\nThe length is:"<<a[0]<<"\n"; cout<<"\nThe Target is:"<<Target<<"\n"; cout<<"\nThe location is:"<<location<<"\n"; if(location==0) cout<<"\nThe target doesn't exist!\n"; return 0; }

搜索更多相关主题的帖子: int Compare return 
2004-11-06 10:54
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 

你的 int main() 上面漏了一个"}",不过新的错误又来了,估计是你代码写错了。

[此贴子已经被作者于2004-11-06 17:47:41编辑过]

2004-11-06 17:46
kappa314
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2004-10-9
收藏
得分:0 

谢谢啊,我今早看了自己的程序,找到了错误啊。

自己好粗心啊,呵呵,把Target都写错了。

#include"iostream"

#include"stdlib.h"

using namespace std;

int Compare(int elem1,int elem2){//注意自己的if语句的写法

if(elem1<elem2)

return -1;

else

if(elem1==elem2)

return 0;

else return 1;

}//Compare

int BinarySearch(int list[],int target,int n){ //实参是一个指针,形参也要是一个指针

//list the elements to be searched

//target the value being searched for

//n the number of elements in the list

int start=1,end=n,middle;

while(start<=end){

middle=(start+end)/2;

switch(Compare(list[middle],target)){

case -1:start=middle+1;break;//list[middle]<target

case 0:return middle;break;//list[middle]=target

case 1:end=middle-1;break;//list[middle]>target

}

}//while

return 0;

}//BinarySearch

int main(){

int A[16],i,location,Target;

//location the position of Target

//Target the element to be found

A[0]=0;

cout<<"\nPlease input the Target:";

cin>>Target;

cout<<"\n";

for(i=1;i<16;i++){

A [i]=2*i-1;

A[0]++;

}//the elements of the list are from 1,3,5 to 25,27,29 and the length of the list is A[0]

if(Target<A[1]||Target>A[15]){//Input is illegal

cout<<"\nIllegal input!\n";

return 0;

}//if

location=BinarySearch(A,Target,A[0]);//实参是一个指针,形参也要是一个指针

cout<<"\nThe elements of the list are:\n";

for(i=1;i<16;i++)

cout<<A[i];

cout<<"\nThe length is:"<<A[0]<<"\n";//注意输出语句的写法

cout<<"\nThe Target is:"<<Target<<"\n";

cout<<"\nThe location is:"<<location<<"\n";

if(location==0)

cout<<"\nThe target doesn't exist!\n";

return 0;

}

2004-11-07 10:46
kappa314
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2004-10-9
收藏
得分:0 
以上打了黑体的描述都是自己易出错的地方啊,呵呵
2004-11-07 10:47
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 

孺子可教...

2004-11-07 11:11
快速回复:我自己编的一个二叉查找的程序,出错了,可以帮我看看吗?
数据加载中...
 
   



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

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