首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

怎么解决这个警告有关问题

2012-02-16 
如何解决这个警告问题?#include iostream.h#include stdlib.htypedefchardatatypeintbinerysearch(da

如何解决这个警告问题?
#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;
}  


warning   C4715:   'binerysearch '   :   not   all   control   paths   return   a   value



[解决办法]
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;
}
return 0;//add
}
[解决办法]
//这样
#include <iostream>
#include <cstdlib>
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)
return binerysearch(a, goal, low, mid-1);
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, 'b ',0,5);
cout < <i < <endl;
}
[解决办法]
DonaldKnuth 正解,
[解决办法]
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);
else if(goal> midvalue&&low!=hight)
return binerysearch(a,goal,mid+1,hight);
else
{
cout < < "No that number!! " < <endl;
return -1;
}

}

热点排行