|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码是这样的
#include<iostream>
class Data{
public:
Data(int data[]);
int Binary_Search(int a,int low,int high);
private:
int data[5];
};
Data::Data(int data[]){
for(int i = 0;i<5;i++){
this->data[i]=data[i];
}
}
int Data::Binary_Search(int a,int low,int high){
int mid =(low+high)/2+1;
while(low<high){
if(data[mid]<a){
low = mid;
mid =(low+high)/2+1;
return Binary_Search(a,low,high);
}
if(data[mid]>a){
high=mid;
mid =(low+high)/2+1;
return Binary_Search(a,low,high);
}
if(data[mid]=a){
return mid;
}
}
}
int main(){
int a[5];
int temp;
std::cout<<"请输入5个数";
for(int i=0;i<5;i++){
std::cin>>a[i];
}
for(int j=0;j<5;j++){
for (int k=5;k>j;k--){
if(a[k-1]>a[k]){
temp=a[k];
a[k]=a[k-1];
a[k-1]=temp;
}
}
}
for(int i=0;i<5;i++){
std::cout<<a;
}
Data d(int a[]);
int x;
std::cout<<"请输入要查找的数";
std::cin>>x;
int located=d.Binary_Search(x,0,4);//这里出错 报错是[Error] request for member 'Binary_Search' in 'd', which is of non-class type 'Data(int*)'
std::cout<<"您要查找的数位于数组中的第"<<located<<"个位置";
} [/i][/i][/i] |
|