补全下面代码:#include "stdio.h" #include "iostream.h" #include "stdlib.h" #include "time.h" const int size=1000; typedef long RecType; typedef struct { RecType rec[size+1]; int n; } SqTable; //顺序查找 void SeqSearch(SqTable R,RecType k,int &i,int &j) { } void print(SqTable R,int n) { } //排序 void sort(SqTable &R,int n) { } //折半查找 int BinSearch(SqTable R,RecType k,int &j) { } void main() { SqTable R,A; RecType x; int i,j,n; int k; do { printf("\n\n\n\n"); printf("\t\t 查找子系统\n"); printf("\t\t*****************************\n"); printf("\t\t* 1----产生数据 *\n"); printf("\t\t* 2----顺序查找 *\n"); printf("\t\t* 3----二分查找 *\n"); printf("\t\t* 4----打印数据 *\n"); printf("\t\t* 0----返 回 *\n"); printf("\t\t*****************************\n"); printf("\t\t 请选择菜单项(0-4):"); scanf("%d",&k); switch(k) { case 1://随机产生数据 printf("请输入要产生随机数的个数(n<=%d)n= ",size); scanf("%d",&n); srand((unsigned)time( NULL )); for (i=1;i<=n;i++) A.rec[i]=R.rec[i]=rand(); A.n=R.n=n; break; case 2://顺序查找 printf("请输入要查找的关键字:"); scanf("%ld",&x); SeqSearch(R,x,i,j); if (i==0) printf("没有此数据!!!"); else printf("在 %d 个下标,第 %d 次找到。",i,j); break; case 3://二分查找 sort(A,n); print(A,n); printf("请输入要查找的关键字:"); scanf("%ld",&x); j=0; i=BinSearch(A,x,j); if (i==0) printf("没有此数据!!!"); else printf("在 %d 个下标,第 %d 次找到。",i,j); break; case 4: print(A,n); } }while (k!=0); }
时间: 2023-09-26 21:08:59 浏览: 268
C++ 压缩文件及文件夹方法 使用zlib开源库
这是一个简单的顺序查找和折半查找的程序,补全代码如下:
```c
#include "stdio.h"
#include "iostream.h"
#include "stdlib.h"
#include "time.h"
const int size = 1000;
typedef long RecType;
typedef struct {
RecType rec[size+1];
int n;
} SqTable;
//顺序查找
void SeqSearch(SqTable R, RecType k, int &i, int &j) {
i = 1;
while (i <= R.n && R.rec[i] != k) {
i++;
j++;
}
if (i <= R.n) {
j++;
} else {
i = 0;
}
}
void print(SqTable R, int n) {
for (int i = 1; i <= n; i++) {
printf("%d ", R.rec[i]);
}
printf("\n");
}
//排序
void sort(SqTable &R, int n) {
int i, j;
RecType temp;
for (i = 1; i < n; i++) {
for (j = n; j >= i+1; j--) {
if (R.rec[j] < R.rec[j-1]) {
temp = R.rec[j];
R.rec[j] = R.rec[j-1];
R.rec[j-1] = temp;
}
}
}
}
//折半查找
int BinSearch(SqTable R, RecType k, int &j) {
int low = 1, high = R.n, mid;
while (low <= high) {
mid = (low + high) / 2;
j++;
if (R.rec[mid] == k) {
return mid;
} else if (R.rec[mid] > k) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return 0;
}
void main() {
SqTable R, A;
RecType x;
int i, j, n;
int k;
do {
printf("\n\n\n\n");
printf("\t\t 查找子系统\n");
printf("\t\t*****************************\n");
printf("\t\t* 1----产生数据 *\n");
printf("\t\t* 2----顺序查找 *\n");
printf("\t\t* 3----二分查找 *\n");
printf("\t\t* 4----打印数据 *\n");
printf("\t\t* 0----返 回 *\n");
printf("\t\t*****************************\n");
printf("\t\t 请选择菜单项(0-4):");
scanf("%d", &k);
switch(k) {
case 1://随机产生数据
printf("请输入要产生随机数的个数(n<=%d)n= ",size);
scanf("%d",&n);
srand((unsigned)time(NULL));
for (i = 1; i <= n; i++) {
A.rec[i] = R.rec[i] = rand();
}
A.n = R.n = n;
break;
case 2://顺序查找
printf("请输入要查找的关键字:");
scanf("%ld",&x);
SeqSearch(R,x,i,j);
if (i == 0) {
printf("没有此数据!!!");
} else {
printf("在 %d 个下标,第 %d 次找到。", i, j);
}
break;
case 3://二分查找
sort(A,n);
print(A,n);
printf("请输入要查找的关键字:");
scanf("%ld",&x);
j = 0;
i = BinSearch(A,x,j);
if (i == 0) {
printf("没有此数据!!!");
} else {
printf("在 %d 个下标,第 %d 次找到。", i, j);
}
break;
case 4:
print(A,n);
}
} while (k != 0);
}
```
阅读全文