#include <stdio.h>
#include <string.h> 
#include <math.h> 
#include <stdlib.h>
#include <malloc.h>

typedef struct
{
	double x;
	double y;
}coordinate;

typedef struct LNode
{
	char name[10];
	coordinate coor;
	struct LNode * next;
}pLinklist;

pLinklist* createCityinfoList(int n)
{
	int i;
	pLinklist *head,*q=NULL;
	head=(pLinklist*)malloc(sizeof(pLinklist));
	q=head;
	for(i=1;i<=n;++i)
	{
		q->next=(pLinklist*)malloc(sizeof(pLinklist));
		q=q->next;
		printf("请输入城市名:");
		scanf("%s",q->name);
		printf("请输入城市坐标x y:");
		scanf("%lf %lf",&((q->coor).x),&((q->coor).y));
	}//逐个输入城市名与城市坐标
	q->next=NULL;
	return head;
}//创建存储城市信息的单链表

void coordinateSureCityname(pLinklist *CityinfoList,char name[])
{
	if(NULL==CityinfoList)
	{
		printf("该链表不存在!\n");
		exit(-1);
	}
	pLinklist * pCursor =CityinfoList->next;
	while(NULL!=pCursor)
	{
		if(strcmp(pCursor->name,name)==0)
		{
			printf("%s的坐标为: x:%lf,y:%lf\n",pCursor->name,(pCursor->coor).x,(pCursor->coor).y);
			break;
		}
		else
		{ pCursor=pCursor->next; }
	}
	if (pCursor==NULL)
	{
		printf("您输入的城市不存在!\n");
	}
}//根据城市名查找其坐标

void nearerCity(pLinklist *CityinfoList, coordinate coor22, double dis)
{
	if(CityinfoList==NULL)
	{
		printf("该链表不存在!\n");
		exit(-1);
	}
	pLinklist * pCursor = CityinfoList->next; 
	printf("与位置坐标x:%lf,y:%lf的距离小于等于%f的城市有:",coor22.x,coor22.y,dis);
	double temp;
	int count=0;
	while(pCursor!=NULL)
	{
		temp = sqrt(pow((pCursor->coor.x - coor22.x),2) + pow((pCursor->coor.y - coor22.y),2)); 
		if(temp<=dis)
		{
			printf("%s",pCursor->name);
			count++;
		}
		pCursor=pCursor->next;
	}
	printf("共%d个满足条件的城市\n", count);	
}

int main()
{
	int Num;
	double dis;
	printf("请输入城市个数:");
	scanf("%d",&Num);
	pLinklist *list = createCityinfoList(Num);

	char name[10];
	printf("请输入要查找的城市名称:");
	scanf("%s",name);
	coordinateSureCityname(list, name);

	coordinate coor1;
	printf("请输入位置坐标x,y:");
	scanf("%lf%lf", &(coor1.x), &(coor1.y));
	printf("请输入距离:\n");
	scanf("%lf",&dis);
	nearerCity(list, coor1, dis);
}
Advertisements