#include "iostream.h"
const int n=7;
char R[n+1];
class cz
{
public:
char binsearch(char R[n+1],char k);
};
char cz::binsearch(char R[n+1],char k)
{
int low=1,high=n;
while(low<high)
{
int mid=(low+high)/2; //取区间中点
if(R[mid]==k)
{ cout<<"已找到要查找的字母"<<"在第"<<mid<<"个位置!"<<endl;
return mid; //查找成功
}
else if(R[mid]>k)
high=mid-1; //在左子区间中查找
else
low=mid+1; //在右子区间查找
}
cout<<"未找到要查找的字母"<<endl;
return 0; //查找失败
}
void main(int argc, char* argv[])
{
char k;
cz c;
int flag=0;
cout<<"输入"<<n<<"个字母:";
for(int i=1;i<=n;i++)
cin>>R[i]; //输入顺序表字母
do{
cout<<"输入要查找的字母:";
cin>>k; //输入要查找的字母
c.binsearch(R,k);
cout<<"--输入1 继续,输入0 结束--: ";//输入1继续查找,0就结束
cin>>flag;
}
while(flag==1);
}