65,168
社区成员




输入过程中输入的n是10以内的数字,然后在顺序查找表里面输出是正常的,但在其他函数里面输出就变成了16导致输出总是出现很多其他数字
源码如下:
#include <stdio.h>
#include <iostream>
using namespace std;
#define MAX 10
typedef struct{
int key;
int otherinfo;
}ElemType;
typedef struct{
ElemType *R;
int length;
}SSTable;
void Creat_SSTable(SSTable ST)
{
int n,i;
cout<<"请输入要查找的总数(不多于10个):"<<endl;
cin>>n;
ST.length=n;
cout<<"请输入需要查找的"<<n<<"个数:"<<endl;
for(i=1;i<=n;i++){
cin>>ST.R[i].key;
}
}//创建顺序查找表
int Search_Seq(SSTable ST,int key)
{
int i;
ST.R[0].key = key;
for(i=ST.length;i>=1;--i){
if(ST.R[i].key==key){
return i;
};
};
return 0;
}//顺序查找
void Seq_Show(SSTable ST)
{
int key;
Creat_SSTable(ST);
cout<<"请输入要查找的数:"<<endl;
cin>>key;
int m = Search_Seq(ST,key);
if(m!=0){
cout<<"查找成功,数据在位置"<<m<<endl;
}else{
cout<<"查找失败"<<endl;
}
}//顺序查找输出
void InsertSort(SSTable ST)
{
int i,j;
cout<<"排序前数据位置:";
for(i=1;i<=ST.length;i++){
cout<<ST.R[i].key<<' ';
}
cout<<endl;
for(i=2;i<=ST.length;++i){
if(ST.R[i].key<ST.R[i-1].key){
ST.R[0]=ST.R[i];
ST.R[i]=ST.R[i-1];
for(j=i-2;ST.R[0].key<ST.R[j].key;--j){
ST.R[j+1]=ST.R[j];
}
ST.R[j+1]=ST.R[0];
}
}
cout<<"排序后数据位置:";
for(i=1;i<=ST.length;i++){
cout<<ST.R[i].key<<' ';
}
}//插入排序
void BubbleSort(SSTable ST)
{
int m=ST.length-1,flag=1,i,j,t;
cout<<"排序前数据位置:";
cout<<endl;
for(i=1;i<=ST.length;i++){
cout<<ST.R[i].key<<' ';
}
while((m>0)&&(flag==1))
{
flag=0;
for(j=1;j<=m;j++){
if(ST.R[j].key>ST.R[j+1].key){
flag=1;
t=ST.R[j].key;ST.R[j].key = ST.R[j+1].key;ST.R[j+1].key=t;
}
}
--m;
}
cout<<"排序后数据位置:";
for(i=1;i<=ST.length;i++){
cout<<ST.R[i].key<<' ';
}
}//冒泡排序
void SelectSort(SSTable ST)
{
int i,j,k,t;
cout<<"排序前数据位置:";
for(i=1;i<=ST.length;i++){
cout<<ST.R[i].key<<' ';
}
cout<<endl;
for(i=1;i<ST.length;++i){
k=i;
for(j=i+1;j<=ST.length;++j){
if(ST.R[j].key<ST.R[k].key) k=j;
}
if(k!=i){
t=ST.R[i].key;ST.R[i].key=ST.R[k].key;ST.R[k].key=t;
}
}
cout<<"排序后数据位置:";
for(i=1;i<=ST.length;i++){
cout<<ST.R[i].key<<' ';
}
}//选择排序
void Bin_Sort(SSTable ST)
{
int c;
cout<<"*****排序*****\n";
cout<<"1.直接插入排序\n";
cout<<"2.冒泡排序\n";
cout<<"3.简单选择排序\n";
cin>>c;
switch(c)
{
case 1:
InsertSort(ST);
break;
case 2:
BubbleSort(ST);
break;
case 3:
SelectSort(ST);
break;
}
}//排序选择表
int Search_Bin(SSTable ST,int key)
{
int low=1,high=ST.length,mid;
while(low<=high){
mid=(low+high)/2;
if(key==ST.R[mid].key) return mid;
else if(key<ST.R[mid].key) high=mid-1;
else low=high+1;
}
return 0;
}//折半查找
void Bin_Show(SSTable ST)
{
int key;
Creat_SSTable(ST);
cout<<"进行折半查找前需要进行排序,请选择"<<endl;
Bin_Sort(ST);
cout<<"\n请输入要查找的数:"<<endl;
cin>>key;
Search_Bin(ST,key);
}//折半查找输出
void All_Menu(SSTable ST)
{
int a;
cout<<"*****查找*****"<<endl;
cout<<"1.顺序查找"<<endl;
cout<<"2.折半查找"<<endl;
cout<<"3.退出"<<endl;
fflush(stdin);
cout<<"请选择功能选项:";
cin>>a;
while(a!=3){
switch(a){
case 1:
Seq_Show(ST);
break;
case 2:
Bin_Show(ST);
break;
}
cout<<"*****查找*****"<<endl;
cout<<"1.顺序查找"<<endl;
cout<<"2.折半查找"<<endl;
cout<<"3.退出"<<endl;
fflush(stdin);
cout<<"请选择功能选项:";
cin>>a;
}
}
int main()
{
SSTable ST;
All_Menu(ST);
return 0;
}
那么您应该是没有很好理解参数单向传值的意思,建议复习一下。
主函数里面的ST从来就没有保存过有效的length。