#include<iostream>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
#define TABLESIZE 10
typedef int ElemType;
typedef struct
{
ElemType *pBase;
int length;
}SSTable;
void InitTable(SSTable &ST)
{
ST.pBase = (ElemType *)malloc(TABLESIZE*sizeof(ElemType));
ST.length = 0;
return;
}
void CreateTable(SSTable &ST, int count)
{
cout << "按值由小到大的顺序输入" << count << "个元素:"<<endl;
for (int i=1; i<=count; i++)
cin >> ST.pBase[i];
ST.length = count;
return;
}
int SearchTable(SSTable ST, ElemType e)
{
int low = 1, high = ST.length; int mid;
while (low <= high){
mid = (low + high) / 2;
if (ST.pBase[mid] == e) return mid;
else if (ST.pBase[mid] > e)
high = mid - 1;
else
low = mid + 1;
}
return 0;
}
int main(void)
{
SSTable ST;
InitTable(ST);
CreateTable(ST,8);
int i = SearchTable(ST,2);
if (i)
{
cout << "查询成功!" << endl;
cout << "查询元素所在的位置为"<<i << endl;
cout << ST.pBase[i] << endl;
}
else
cout << "查询失败!" << endl;
return(0);
}