📄 cpp1.cpp
字号:
#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.根据城市名查找 $\n");
printf(" $ 3.根据离中心坐标距离查找 $\n");
printf(" $ 4.退出程序 $\n");
printf(" ===============================\n");
printf("请选择:");
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)); //建立新结点r和L
while (strcmp(r->name,".")!=0) //判断城市名是否为空,输入“.”结束
{
s=(City *)malloc(sizeof(City)); //建立新结点s
if (s==NULL) //判断是否已满
{
printf("错误:内存不足!");
break;
}
else //键入城市信息
printf("请输入城市名(输入“.”结束):");
scanf("%s", s->name);
printf("请输入城市坐标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坐标:(%.f,%.f)\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("请输入要查找的城市名:"); scanf("%s",name);
p=Locate(L,name);
if (p!=NULL)
printf("城市名:%s\t\t坐标:(%.2f,%.2f)",p->name,p->x,p->y);
break;
case 3://键入中心坐标并与所给定的距离作比较
printf("请输入中心坐标:"); scanf("%f,%f",&x,&y);
printf("请输入距离:"); scanf("%f",&d);
GetCity(L,x,y,d);
break;
case 4://结束程序
printf("结束程序!\n");
return 0;
break;
default:
printf("输入错误!请重新输入!\n\n");break;
}
printf(" 单击空格键继续 \n");
while ((c=getch())!=' ');//按空格键继续操作
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -