数据结构---实验4--查找(c)

  1 #include "stdio.h"
  2 #include "iostream"
  3 #include "stdlib.h"
  4 #include "time.h"
  5 
  6 const int size=1000;
  7 typedef long RecType;
  8 typedef struct
  9 {    RecType rec[size+1];
 10     int      n;
 11 } SqTable;
 12 
 13 //顺序查找
 14 void SeqSearch(SqTable R,RecType k,int &i,int &j)
 15 {    
 16     j=0;
 17     R.rec[0]=k;
 18     i=R.n;
 19     while(R.rec[i]!=k)
 20     {
 21         i--;
 22         j++;
 23     }
 24 
 25 } 
 26 
 27 void print(SqTable R,int n)
 28 {
 29     for(int i=1;i<=n;i++)
 30     {
 31         printf("%7d",R.rec[i]);
 32         if(i%10==0)
 33             printf("\n");
 34     }
 35     
 36 }
 37 
 38 //排序
 39 void sort(SqTable &R,int n)      
 40 {    
 41     RecType temp;
 42     for(int i=1;i<=n;i++)
 43     for(int j=i+1;j<=n;j++)
 44         if(R.rec[i]>R.rec[j])
 45         {
 46             temp=R.rec[i];
 47             R.rec[i]=R.rec[j];
 48             R.rec[j]=temp;
 49 
 50         }
 51 
 52 }
 53 
 54 //折半查找
 55 int BinSearch(SqTable R,RecType k,int &j)
 56 {    
 57     int low,high,mid=0;
 58     low=1;
 59     high=R.n;
 60     while(low<high)
 61     {
 62         mid=(low+high)/2;
 63         j++;
 64         if(k==R.rec[mid])
 65             return (mid);
 66         else if(k<R.rec[mid])
 67             high=mid-1;
 68         else
 69             low=mid+1;
 70     }
 71 
 72 
 73 } 
 74 
 75 void main()
 76 {
 77     SqTable R,A;
 78     RecType x;
 79     int i,j,n;
 80     int k;
 81     do 
 82     {
 83     printf("\n\n\n\n");
 84     printf("\t\t         查找子系统\n");
 85     printf("\t\t*****************************\n");
 86     printf("\t\t*       1----产生数据     *\n");
 87     printf("\t\t*       2----顺序查找    *\n");
 88     printf("\t\t*       3----二分查找    *\n");
 89     printf("\t\t*       4----打印数据    *\n");
 90     printf("\t\t*       0----返  回    *\n");
 91     printf("\t\t*****************************\n");
 92     printf("\t\t   请选择菜单项(0-4):");
 93     scanf("%d",&k);
 94     switch(k)
 95     {
 96         case 1://随机产生数据
 97             printf("请输入要产生随机数的个数(n<=%d)n= ",size);
 98             scanf("%d",&n);    
 99             srand((unsigned)time( NULL ));
100             for (i=1;i<=n;i++)
101                 A.rec[i]=R.rec[i]=rand();
102             A.n=R.n=n;
103             break;
104         case 2://顺序查找
105             printf("请输入要查找的关键字:");
106             scanf("%ld",&x);
107             SeqSearch(R,x,i,j);
108             if (i==0)    
109                 printf("没有此数据!!!");
110             else    printf("在 %d 个下标,第 %d 次找到。",i,j);
111             break;
112         case 3://二分查找
113             sort(A,n);
114             print(A,n);
115             printf("请输入要查找的关键字:");
116             scanf("%ld",&x);
117             j=0;
118             i=BinSearch(A,x,j);
119             if (i==0)    printf("没有此数据!!!");
120             else    printf("在 %d 个下标,第 %d 次找到。",i,j);
121             
122               break;            
123         case 4:
124             print(A,n);
125     }     
126     }while (k!=0);
127 }

猜你喜欢

转载自www.cnblogs.com/zongyao/p/9255390.html