折半查找程序

//查找程序
#include <stdio.h>
typedef struct{
        int number;
        char name[20];
        float score1;
       }StuInfo;//元素信息结构体    
typedef struct {
        StuInfo *elem;

        int length;
        }SSTable;//总结构体
 //*****************比较函数********************
  int EQ(int num1,int num2)
     {
           if(num1==num2)
              return 1;
            else
               return 0;
     }    
 int LT(int num1,int num2)
       {
              if(num1<num2)
                  return 1;
              else
                   return 0;
       }       
 //******************创建表信息**********************
 void CreatStable(SSTable *ST,int length)     
          {
                  int i;
                  ST->elem =(StuInfo *) malloc(length*sizeof(StuInfo));  
                  printf("please input the length:");
                  scanf("%d",&ST->length);
                  for(i=1;i<=ST->length;i++)
                      {       
                            printf("please input the %d's Student inforntion :/n",i);
                            printf("number:");
                            scanf("%d",&ST->elem[i].number);
                            printf("name:");
                            scanf("%s",&ST->elem[i].name);
                            printf("score1:");
                            scanf("%f",&ST->elem[i].score1);
                      }
          }
  //*******************顺序查找*********************
  int Search(SSTable *ST,int number)
       {        
               
                int i=ST->length;   
                 ST->elem[0].number=number;
                 while(!EQ(ST->elem[i].number,number))
                       i--;
                  return i;  //i是多少????
       }
  //********************有序表的折半查找****************
  int Search_Bin(SSTable *ST,int number)
        {
                 int low=1,high=ST->length;
                 int mid;
                 while(low<=high)
                      {
                                 mid=(low+high)/2;
                                 if(EQ(ST->elem[mid].number,number))
                                     return mid;
                                 else if(LT(number,ST->elem[mid].number))
                                     high=mid-1;
                                  else
                                      low=mid+1;
                      }
                      return 0;
        }                                                    
                                          
      
 //*************************主函数**********************
 int main()
 {
     int i,length,number;
     SSTable ST;
     CreatStable(&ST,length);
      printf("please input you want find number:");
          scanf("%d",&number);
      if(Search_Bin(&ST,number))
           printf("The Binary Search found!/n");
      else
            printf("Bianry Search not found!/n");    
      if( Search(&ST,number))
           printf("found!");
     else
        printf("notfound!");
        return 0;
 }      
                                      

你可能感兴趣的:(struct,search,input,float)