?? 城市鏈表.txt
字號(hào):
城市鏈表
【問(wèn)題描述】
將若干城市的信息,存入一個(gè)帶頭結(jié)點(diǎn)的單鏈表。結(jié)點(diǎn)中的城市信息包括:城市名,城市的位置坐標(biāo)。要求能夠利用城市名和位置坐標(biāo)進(jìn)行有關(guān)查找。
【基本要求】
?。?) 給定一個(gè)城市名,返回其位置坐標(biāo);
?。?) 給定一個(gè)位置坐標(biāo)P和一個(gè)距離D,返回所有與P的距離小于等于D的城市。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define MAXSIZE 20
typedef struct city
{
char name[MAXSIZE];
float x;
float y;
struct city *next;
}
City;
int Menu()
{
int choice;
printf("************************\n");
printf(" 1.新建\n");
printf(" 2.根據(jù)城市名查找\n");
printf(" 3.根據(jù)離中心坐標(biāo)距離查找\n");
printf(" 4.退出\n");
printf("============================\n");
printf("請(qǐng)選擇:");
scanf("%d", &choice);
return choice;
}
float Distance(float x,float y,float x0,float y0)
{
float dis;
dis=(x-x0)*(x-x0)+(y-y0)*(y-y0);
return dis;
}
City *Creat()
{
City *L, *r, *s;
r=L=(City *)malloc(sizeof(City));
while (strcmp(r->name,".")!=0)
{
s=(City *)malloc(sizeof(City));
if (s==NULL)
{
printf("錯(cuò)誤:內(nèi)存不足!");
break;
} else {
printf("城市名(輸入“.”結(jié)束):");
scanf("%s", s->name);
printf("坐標(biāo)x,y:"); scanf("%f,%f",&s->x,&s->y);
r->next=s;
r=s;
}
}
r->next=NULL;
return L;
}
City *Locate(City *L, char *name)
{
City *p=L;
while ((p!=NULL) && (strcmp(p->name,name)!=0))
p=p->next;
return p;
}
GetCity(City *L, float x,float y,float d)
{
City *p=L->next;
while (p!=NULL)
{
if (Distance(p->x,p->y,x,y)<d*d)
printf("城市名:%s\t\t坐標(biāo):(%.2f,%.2f)\n",p->name,p->x,p->y);
p=p->next;
}
}
main()
{
int ch=0,i,c;
float d,x,y;
char name[MAXSIZE];
City *L,*p;
while (ch!=4)
{
ch=Menu();
switch(ch)
{
case 1:
L=Creat();
break;
case 2:
printf("請(qǐng)輸入要查找的城市名:"); scanf("%s",name);
p=Locate(L,name);
if (p!=NULL)
printf("城市名:%s\t\t坐標(biāo):(%.2f,%.2f)",p->name,p->x,p->y);
break;
case 3:
printf("請(qǐng)輸入中心坐標(biāo):"); scanf("%f,%f",&x,&y);
printf("請(qǐng)輸入距離:"); scanf("%f",&d);
GetCity(L,x,y,d);
break;
case 4:
printf("結(jié)束程序。\n");
return 0;
break;
default:
printf("輸入錯(cuò)誤!請(qǐng)重新輸入!\n\n");break;
}
printf("單擊空格鍵繼續(xù)...\n");
while ((c=getch())!=' ');
}
return 0;
}
這也是從網(wǎng)上找到的源程序,只是修改得能用了.
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -