标题:课程设计 数据查找 第三种方法索引查找求解
只看楼主
caogang11144
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-1-2
结帖率:0
已结贴  问题点数:20 回复次数:4 
课程设计 数据查找 第三种方法索引查找求解
#include<iostream.h>
#include<iomanip.h>
#include<iostream.h>



void menu();
void sequenceSeqsch();//顺序查找
void binSearch();//二分查找
int indsch();//索引查找
void hashSearch();//散列查找


void main()
{
    int select;
    while(1)
    {
        menu();
        cout<<"请输入你的选择:";
        cin>>select;
        switch(select)
        {
        case 1:
            sequenceSeqsch();
            break;
        case 2:
            binSearch();
            break;
        case 3:
            indsch();
            break;
        case 4:
            hashSearch();
            break;     
        
        case 0:
            return;
        }
    }
    cout<<endl;
}
void menu()
{
    cout<<"\t数据查找"<<endl;
    cout<<"================================="<<endl;
    cout<<"\t 1 --------顺序查找"<<endl;
    cout<<"\t 2 --------二分查找"<<endl;
    cout<<"\t 3 --------索引查找"<<endl;
    cout<<"\t 4 --------散列查找"<<endl;
    cout<<"\t 0 --------退出"<<endl;
    cout<<"================================="<<endl;
    cout<<"\t请选择(1 ~ 4)"<<endl;
}



void sequenceSeqsch()
{
    int a[5];int b;
    int i;
    cout<<"输入数组(包括五个元素):"<<endl;
    for(i=0;i<5;i++)
    cin>>a[i];
    cout<<"要查询的数据:"<<endl;
        cin>>b;
    for(i=0;i<5;i++)
        if(a[i]==b)
        cout<<"要查询的数据是该数组中的第"<<i+1<<"个数"<<endl;
        if(i==4)
            cout<<"在该数组中没有要查找的数据"<<endl;
}

void binSearch()//二分查找

{
int a[5] ,m;
int i,j,temp,k=0;
    int x=0,y=4,mid;
    cout<<"输入数组:"<<endl;
    for(i=0;i<5;i++)
        for(j=0;j<5-i;j++)
            if(a[j]<a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
            cout<<"按降序排序后的数组为:"<<endl;
            for(i=0;i<5;i++)
                cout<<a[i]<<" "<<endl;
            cout<<"要查询的数据:"<<endl;
            cin>>m;
            while(x<=y)
            {
                mid=(x+y)/2;
                if(a[mid]==m)
                {
                    k=1;
                    break;
                }
                if(a[mid]>m)
                    x=mid+1;
                else y=mid-1;
            }
            if(k==1)
                cout<<"要查找的数据是该数组中的第:"<<mid+1<<"个数"<<endl;
            else cout<<"在该数组中没有要查询的数据"<<endl;
}
 


struct f1
{
    int id;
    char name[10];
    double ms;
};
struct f2
{
    int index;
    int start;
    int length;
};
int indsch()


{
    f1 a[5];int n;f2 b[5];int m;int k1;int k2;
    int i,j;
    for(i=0;i<m;i++)
        if(k1==b[i].index)
            break;
        if(i==m)
            return -1;
        j=b[i].start;
        while(j<b[i].start +b[i].length )
            if(a[j].id ==k2)
                break;
            else j++;
            if(j<b[i].start +b[i].length )
                return j;
            else return -1;
}


void hashSearch()//散列查找

   
{ int a[5];
 int b[50];
      cout<<"请输入5个整数"<<endl;
         for(int i=0;i<5;i++)
          cin>>a[i];
         for(i=0;i<50;i++)
            b[i]=-2;  
         for(i=0;i<5;i++)
        {
            int j=a[i]%13;
            if(b[j]==-2)
            b[j]=a[i];
              else
            {
                for(int k=j+1;k<13;k++)  
                {  
                   if(b[k]==-2)
                    {
                         b[k]=a[i];
                          break;  
                    }
                }
                     if(k==13)
                {
                    for(k=j-1;k>=0;k--)
                    {
                         if(b[k]==-2)
                        {
                           b[k]=a[i];
                           break;
                        }
                    }
                }
            }
        }
                int key;
                cout<<"请输入要查找的元素:"<<endl;
                cin>>key;
                int p=key%13;
                if(b[p]==key)
                    cout<<"要查找的数据元素:"<<endl;
                    else
                {
                    for(i=p+1;i<50;i++)
                    {
                        if(b[i]==key)
                        {
                            cout<<"找到要查询的数据元素:"<<"其地址为:"<<p<<endl;
                            break;
                        }
                        if(b[i]==-2)
                        {
                            cout<<"在原信息中没有找到要查询的数据:"<<endl;
                            break;
                        }
                    }
                    if(i=50)
                    {
                        for(i=p-1;i>=0;i--)
                        {
                            if(b[i]==key)
                            {
                                cout<<"找到要查询的数据元素:"<<endl;
                                break;
                            }
                            if(b[i]==-2)
                            {cout<<"在原信息中没有找到要查询的数据:"<<endl;
                            break;
                            }
                        }
                    }
                }
}
搜索更多相关主题的帖子: include 你的选择 课程 
2014-01-02 14:18
caogang11144
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-1-2
得分:0 
运行时索引查找无法运行
2014-01-02 14:19
i80286
Rank: 6Rank: 6
等 级:侠之大者
威 望:5
帖 子:99
专家分:428
注 册:2013-9-30
得分:20 
int indsch()


{
    f1 a[5];int n;f2 b[5];int m;int k1;int k2;
    int i,j;
    for(i=0;i<m;i++)
        if(k1==b[i].index)
            break;
        if(i==m)
            return -1;
        j=b[i].start;
        while(j<b[i].start +b[i].length )
            if(a[j].id ==k2)
                break;
            else j++;
            if(j<b[i].start +b[i].length )
                return j;
            else return -1;
}
m有赋值吗?
2014-01-02 14:26
caogang11144
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-1-2
得分:0 
回复 3楼 i80286
没有赋值
2014-01-02 14:33
i80286
Rank: 6Rank: 6
等 级:侠之大者
威 望:5
帖 子:99
专家分:428
注 册:2013-9-30
得分:0 
整个函数内就没有赋值的过程(所有的变量)
2014-01-02 14:40



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-426021-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053075 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved