错误如下: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; }