二分法查找(C语言,改进)

今天的改进了昨天发的,不仅可以查找名字,还可以查找其它东西。不过至今没有找到用变量代替结构体变量中的元素的方法,所以代码有很多重复的地方不知道大家有没有什么方法。
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define num 4
struct data
{
char name[20];
char city[20];
char sex[10];
char age[10];
char job[10];
};
struct data datas[num]=
{
 "sun","Weifang","Male","24","student",
 "tom","Beijing","Male","31","doctor",
 "marry","Shanghai","Female","19","techer",
 "willing","Tianjing","Female","31","worker"
};
void qs_struct(struct data *items,int left,int right,char area[]);
void quick_struct(struct data *items,int count,char area[]);
int binaryseach(struct data *items,char name[],int n,char area[],int p);
void print_data(struct data *point);
int main(void)
{
char name[30],area[30];
int i,p;
printf("search which area!!\n");
scanf("%s",area);
printf("sort the orignal data\n");
quick_struct(datas,num,area);
printf("input what you want to search!!\n");
scanf("%s",name);
i=0;
for(p=0;p<num&&i<num;p++,i++)
{
i=binaryseach(datas,name,num,area,i);
if(i!=1)
{
printf("----------------------------\n");
print_data(&datas[i]);
}
}
return 1;
}
void quick_struct(struct data items[],int count,char area[])
{
qs_struct(items,0,count-1,area);
}
void qs_struct(struct data items[],int left,int right,char area[])
{
register int i,j;
char *x;
struct data temp;
i=left;
j=right;
if(strcmp(area,"name")==0)
x=items[(left+right)/2].name;
else if(strcmp(area,"city")==0)
x=items[(left+right)/2].city;
else if(strcmp(area,"sex")==0)
x=items[(left+right)/2].sex;
else if(strcmp(area,"age")==0)
x=items[(left+right)/2].age;
else if(strcmp(area,"job")==0)
x=items[(left+right)/2].job;
else
{
printf("not such type!!");
exit(1);
}
do
{
if(strcmp(area,"name")==0)
{
while((strcmp(items[i].name,x)<0)&&(i<right))
i++;
while((strcmp(items[j].name,x)>0)&&(j>left))
j--;
}
else if(strcmp(area,"sex")==0)
{
while((strcmp(items[i].city,x)<0)&&(i<right))
i++;
while((strcmp(items[j].city,x)>0)&&(j>left))
j--;
}
else if(strcmp(area,"sex")==0)
{
while((strcmp(items[i].sex,x)<0)&&(i<right))
i++;
while((strcmp(items[j].sex,x)>0)&&(j>left))
j--;
}
else if(strcmp(area,"age")==0)
{
while((strcmp(items[i].age,x)<0)&&(i<right))
i++;
while((strcmp(items[j].age,x)>0)&&(j>left))
j--;
}
else if(strcmp(area,"job")==0)
{
while((strcmp(items[i].job,x)<0)&&(i<right))
i++;
while((strcmp(items[j].job,x)>0)&&(j>left))
j--;
}
if(i<=j)
{
temp=items[i];
items[i]=items[j];
items[j]=temp;
i++;
j--;
}
}while(i<j);
if(left<j)
 qs_struct(items,left,j,area);
if(i<right)
 qs_struct(items,i,right,area);
}
int binaryseach(struct data items[],char name[],int n,char area[],int p)
{
int low,high,mid;
low=p;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(strcmp(area,"name")==0)
{
if((strcmp(items[mid].name,name)==0))
return mid;
else if((strcmp(items[mid].name,name)<0))
low=mid+1;
else
high=mid-1;
}
else if(strcmp(area,"city")==0)
{
if((strcmp(items[mid].city,name)==0))
return mid;
else if((strcmp(items[mid].city,name)<0))
low=mid+1;
else
high=mid-1;
}
else if(strcmp(area,"sex")==0)
{
if((strcmp(items[mid].sex,name)==0))
return mid;
else if((strcmp(items[mid].sex,name)<0))
low=mid+1;
else
high=mid-1;
}
else if(strcmp(area,"age")==0)
{
if((strcmp(items[mid].age,name)==0))
return mid;
else if((strcmp(items[mid].age,name)<0))
low=mid+1;
else
high=mid-1;
}
else if(strcmp(area,"job")==0)
{
if((strcmp(items[mid].job,name)==0))
return mid;
else if((strcmp(items[mid].job,name)<0))
low=mid+1;
else
high=mid-1;
}
}
return -1;
}
void print_data(struct data *point)
{
if(point==NULL)
return;
printf("%s\n",point->name);
printf("%s\n",point->city);
printf("%s\n",point->sex);
printf("%s\n",point->age);
printf("%s\n",point->job);
}

你可能感兴趣的:(查找,职场,C语言,二分法,休闲)