# include<stdio.h>
# include<malloc.h>
# include<math.h>
# include<string.h>
# define n 20
# define length sizeof(struct city)
struct city
{
float x;
float y;
char name[n];
struct city *next;
};
void Fcity(float i,float j,float l,struct city *head)
{
float distance(float x,float y,float a,float b);
void caseif(float x,float y,struct city *head);
double d;
struct city *p;
int k;
p=head;
for(p=head;p!=NULL;)
{
d=distance(i,j,p->x,p->y);
if(d<=l&&d!=0)
caseif(p->x,p->y,head);
k++;
p=p->next;
}
if(k==0)
printf("没有"); }
float distance(float x,float y,float a,float b)
{
float i,j;
double c;
i=x-a;
j=y-b;
c=sqrt(j*j+i*i);
return c;
}
void put(char a[],struct city *head)
{
struct city *p;
char b[n];
int m;
for(m=0;m<n;m++)
b[m]=a[m];
p=head;
for(p=head;p!=NULL;p=p->next)
{
if(strcmp(b,p->name)==0)
printf("%f,%f",p->x,p->y);
}
if(p==NULL)
printf("error\n");
}
void caseif(float x,float y,struct city *head)
{
struct city *p;
p=head;
for(p=head;p!=NULL;)
{
if(x==p->x&&y==p->y)
printf("%s ",p->name);
p=p->next;
}
}
void main()
{
void Fcity(float i,float j,float l,struct city *head);
void put(char a[n],struct city *head);
char str1[n]="xi'an",str2[n]="shanghai";
char str3[n]="beijing",str4[n]="yulin";
char f[n];
float i,j,l;
int k;
struct city a,b,c,d,*p1,*p2,*head;
strcpy(a.name,str1); a.x=0; a.y=0;
strcpy(b.name,str2); b.x=10; b.y=10;
strcpy(c.name,str3); c.x=3; c.y=4;
strcpy(d.name,str4); d.x=-3; d.y=-4;
head=&a;
a.next=&b;
b.next=&c;
c.next=&d;
d.next=NULL;
printf("1、查询城市坐标\n2、查询距离小于要求的城市\n");
printf("enter operation:");
scanf("%d",&k);
if(k==1)
{
printf("输入城市名:");
for(k=0;k<n;k++)
scanf("%c",&f[k]);
put(f,head);
}
else if(k==2)
{
printf("输入坐标,距离:");
scanf("%d,%d,%d",&i,&j,&l);
Fcity(i,j,l,head);
}
else printf("error");
}
2012年10月16日 14点10分
2