第九题

一:问题
在这里插入图片描述
二:解答
#include
#include <math.h>
#include<conio.h>
#include
using namespace std;
struct Node
{
char Name[20];
double x, y;
Node* next;
};
class List
{
private:
Node* head, * final;
int lengh;
public:
List();
void Insert();
void remove();
void Search();
void update();
void cha();
void show();
};
List::List()
{
head = new Node;
final = head;
lengh = 0;
}
void List::Insert()
{
Node* p = new Node;
cout << “请给出你要插入的城市的信息:”;
cin >> p->Name;
cin >> p->x;
cin >> p->y;
int i;
if (lengh == 0)
{
cout << “城市地图上没有任何城市信息!”;
final->next = p;
final = p;
p->next = NULL;
lengh++;
return;
}
else
{
final->next = p;
lengh++;
final = p;
p->next = NULL;
}
}
void List::remove()
{
Node* p;
Node* t;
char name[20];
cout << “请给出你要删除的城市的名称:”;
cin >> name;
for (p = head; p->next != NULL; p = p->next)
if (!strcmp(p->next->Name, name))
{
t = p->next;
p->next = t->next;
delete(t);
lengh–;
break;
}
}
void List::Search()
{
char name[20];
Node* p;
cout << “请给出你想搜索的城市的名称:”;
cin >> name;
for (p = head->next; p != NULL; p = p->next)
if (!strcmp(p->Name, name))
{
cout << p->Name;
cout << ‘(’ << p->x << ‘,’ << p->y << ‘)’ << endl;
break;
}
}
void List::update()
{
char name[20];
Node* p;
cout << “请给出你想修改的城市的名称:”;
cin >> name;
for (p = head->next; p != NULL; p = p->next)
if (!strcmp(p->Name, name))
{
cout << “请输入新的城市名称:”;
cin >> p->Name;
cout << “请输入城市的坐标x,y:”;
cin >> p->x >> p->y;
break;
}
}

void List::cha()
{
char name[20];
Node* p;
Node n;
int i;
double d;
double de;
cout << “你给的城市是 :”;
cin >> name;
cout << “你给的距离是 :”;
cin >> d;
for (p = head->next; p != NULL; p = p->next)
if (!strcmp(p->Name, name))
break;
n = p;
p = head->next;
for (i = 0; i < lengh; i++, p = p->next)
{
de = sqrt(((p).x - n.x) * ((p).x - n.x) + ((p).y - n.y) * ((p).y - n.y));
if (de <= d && strcmp(p->Name, name))
{
cout << p->Name;
cout << ‘(’ << p->x << ‘,’ << p->y << ‘)’ << endl;
}
}
}
void List::show()
{
Node
p;
p = head->next;
int i;
for (i = 0; i < lengh; i++)
{
cout << p->Name;
cout << ‘(’ << p->x << ‘,’ << p->y << ‘)’ << endl;
p = p->next;
}
}
void main()
{
List t;
int k;
do {
cout << “\n\n 1.增加城市”;
cout << “\n\n 2.删除城市”;
cout << “\n\n 3.修改城市信息”;
cout << “\n\n 4.根据城市名查询其坐标”;
cout << “\n\n 5.给坐标P和距离D,查询所有与P的距离小于等于D的城市”;
cout << "\n
***********************************************************" << endl;
cout << “\n 请输入你的选择操作(1、2、3、4、5) :\n”;
cin >> k;
switch (k)
{
case 1: {
t.Insert();
t.show();

	}break;
	case 2: {
		t.remove();
		t.show();
	}break;
	case 3: {
		t.update();
		t.show();
	}break;
	case 4: {t.Search(); }break;
	case 5: {t.cha(); }break;
	default:break;
	}
} while (k > 0 && k < 6);
cout << " 按任意键返回......";
_getch();
return;

}
三:结果
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值