#include <stdio.h>
#include <stdlib.h>
#define maxsize 100
#define ERROR 0
#define OK 1
#define OVERFLOW -2
typedef int Status;
int key;
typedef struct{
int *R;
int length;
}SSTable;
Status InitSSTable(SSTable *ST)
{//初始化
ST->R=(int*)malloc(sizeof(int)*maxsize);
if(!ST->R) exit(OVERFLOW);
ST->length=0;
return OK;
}
Status Search_Sq(SSTable ST,int key){
//顺序查找
ST.R[0]=key;
int i;
for(i=ST.length;ST.R[i]!=key;--i);
return i;
}
Status Search_Bin(SSTable ST,int key){
//折半查找
int low=1,high=ST.length;
while(low<=high){
int mid=(low+high)/2;
if(key==ST.R[mid]) return mid;
else if (key<ST.R[mid]) high=mid-1;
else low=mid+1;
//return mid; 错误点:添加该句后返回值一直是(1+ST.length)/2的值。
}
return 0;
}
void Sort(SSTable ST){
//排序
int temp,i,j;
for(i=1;i<ST.length;i++){
for(j=i+1;j<=ST.length;j++){
if(ST.R[i]>ST.R[j]){
temp=ST.R[i];
ST.R[i]=ST.R[j];
ST.R[j]=temp;
}
}
}
}
void Display(SSTable ST){
int t;
for(t=1;t<=ST.length;t++)
printf("%d ",ST.R[t]);
putchar('\n');
}
int main(int argc, char *argv[]) {
SSTable ST;
InitSSTable(&ST);
int a,x,i,t;
printf("输入要输入数字的个数:\n");
scanf("%d",&x);
ST.length=x;//错误点,刚开始没有设置线性表的长度,导致顺序查找返回值一直为0
for (i=1;i<=ST.length;i++){
printf("这是第%d个数字\n",i);
scanf("%d",&ST.R[i]);
}
while(1){
printf("选择查找方式:\n1.顺序查找。\n2.折半查找。\n");
scanf("%d",&t);
switch(t){
case 1:
printf("数列:");
Display(ST);
printf("输入要查找的是数字:\n");
scanf("%d",&a);
printf("这个数字位置为%d\n",Search_Sq(ST,a));
break;
case 2:
printf("排序并显示:\n");
Sort(ST);
Display(ST);
printf("输入要查找的是数字:\n");
scanf("%d",&a);
printf("这个数字位置为%d\n",Search_Bin(ST,a));
break;
}
}
}
#include <stdlib.h>
#define maxsize 100
#define ERROR 0
#define OK 1
#define OVERFLOW -2
typedef int Status;
int key;
typedef struct{
int *R;
int length;
}SSTable;
Status InitSSTable(SSTable *ST)
{//初始化
ST->R=(int*)malloc(sizeof(int)*maxsize);
if(!ST->R) exit(OVERFLOW);
ST->length=0;
return OK;
}
Status Search_Sq(SSTable ST,int key){
//顺序查找
ST.R[0]=key;
int i;
for(i=ST.length;ST.R[i]!=key;--i);
return i;
}
Status Search_Bin(SSTable ST,int key){
//折半查找
int low=1,high=ST.length;
while(low<=high){
int mid=(low+high)/2;
if(key==ST.R[mid]) return mid;
else if (key<ST.R[mid]) high=mid-1;
else low=mid+1;
//return mid; 错误点:添加该句后返回值一直是(1+ST.length)/2的值。
}
return 0;
}
void Sort(SSTable ST){
//排序
int temp,i,j;
for(i=1;i<ST.length;i++){
for(j=i+1;j<=ST.length;j++){
if(ST.R[i]>ST.R[j]){
temp=ST.R[i];
ST.R[i]=ST.R[j];
ST.R[j]=temp;
}
}
}
}
void Display(SSTable ST){
int t;
for(t=1;t<=ST.length;t++)
printf("%d ",ST.R[t]);
putchar('\n');
}
int main(int argc, char *argv[]) {
SSTable ST;
InitSSTable(&ST);
int a,x,i,t;
printf("输入要输入数字的个数:\n");
scanf("%d",&x);
ST.length=x;//错误点,刚开始没有设置线性表的长度,导致顺序查找返回值一直为0
for (i=1;i<=ST.length;i++){
printf("这是第%d个数字\n",i);
scanf("%d",&ST.R[i]);
}
while(1){
printf("选择查找方式:\n1.顺序查找。\n2.折半查找。\n");
scanf("%d",&t);
switch(t){
case 1:
printf("数列:");
Display(ST);
printf("输入要查找的是数字:\n");
scanf("%d",&a);
printf("这个数字位置为%d\n",Search_Sq(ST,a));
break;
case 2:
printf("排序并显示:\n");
Sort(ST);
Display(ST);
printf("输入要查找的是数字:\n");
scanf("%d",&a);
printf("这个数字位置为%d\n",Search_Bin(ST,a));
break;
}
}
}