⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 每条数据库记录包括城市名(任意长的字符串)和城市的坐标(用整数x和y表示)。你的数据库应该允许插入记录、按照名字或者坐标删除或检索记录
💻 CPP
字号:
#include "AList.h"
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#include <iostream>
#include <string>
using namespace std;
struct tCity
{
	string name;
	int x;
	int y;
}city;

class CDatabase:public AList<tCity>
{
public:
	void PrintOne (int pos);     //打印数组中下标为pos的项
	void PrintLocal(tCity&, double);  //搜索某个区域的地图
                  
};
void CDatabase::PrintOne(int pos)
{
	if(pos>=0 && pos<listSize)
	{
		cout<<"  "<<listArray[pos].name<<"  坐标("<<listArray[pos].x<<  \
				","<<listArray[pos].y<<")"<<endl;
	}
}
void CDatabase::PrintLocal(tCity& City, double dist)
{
	double d;
	bool flag=true;  //标志变量,true指示不在该区域找到城市
	cout<<"查找结果如下:"<<endl;
	for(int i=0; i<listSize; i++)
	{
		d = pow(fabs(listArray[i].x-City.x),2)//fabs函数为取绝对值。。。。pow是什么
			+pow(fabs(listArray[i].y-City.y),2);
		d = sqrt(d);//sqrt函数为取开平方
		if (d <= dist)  //在查找范围内
		{
			flag = false;   //标志变量改变
			PrintOne(i);
		}
	}
	if (flag == true) cout<<" 该区域没有城市!"<<endl;
}



void main()
{
	tCity city; 
	CDatabase dbList;
	int pos;
	int dist;
	string aff;
	int Select = 8;//什么意思
	while(Select!=0)
	{
		cout<<"输入1添加\n"<<"输入2删除\n"<<"输入3城市名字查找坐标\n"<<"输入4坐标查找城市名字\n"
			<<"输入5查找的城市搜索坐标范围\n";
		cin>>Select;
		switch(Select)
		{
		case 1:
			cout<<"输入新城市名:"; cin>>city.name;
			cout<<"输入城市坐标:\nx="; cin>>city.x;
			cout<<"y="; cin>>city.y;
			if (dbList.findName(city)!=-1 ||   //确认地图中是否存在同名或同坐标城市
				dbList.findXY(city)!=-1   )
			{
				cout<<"\n已经存在同名或同坐标的城市,不需加入!按任意键返回:";
				getch();  //等待用户按任意键确认
				break;
			}
			else if(dbList.append(city)) cout<<"\n插入记录成功!按任意键返回:";  //追加到数组尾
			else cout<<"\n插入记录失败,这是因为内存不足造成的!按任意键返回:";
			getch();     //等待用户按任意键确认
			break;
		case 2:
			cout<<"输入要删除的城市名:"; cin>>city.name;
			pos = dbList.findName(city);   //查找该项位置
			if(pos==-1)  //找不到
			{
				cout<<"\n没有这个城市!按任意键返回:";
				getch();   break;
			}
			dbList.PrintOne(pos);
			dbList.setPos(pos);   //移动栅栏到删除点
			dbList.remove();  //删除
			cout<<"\n已经删除这个城市!按任意键返回:";
			getch();   break;
		case 3:
			cout<<"输入要查找的城市名字:"; cin>>city.name;
			pos = dbList.findName(city);
			if(pos==-1)
			{
				cout<<"\n没有这个城市!按任意键返回:";
				getch();   break;
			}
			cout<<"找到城市:"<<endl;
			dbList.PrintOne(pos);
			cout<<"\n按任意键返回:";
			getch();   break;
		case 4:
			cout<<"输入要查找的城市坐标:\nx="; cin>>city.x;
			cout<<"y="; cin>>city.y;
			pos = dbList.findXY(city);
			if(pos==-1)
			{
				cout<<"\n没有这个城市!按任意键返回:";
				getch();   break;
			}
			cout<<"找到城市:"<<endl;
			dbList.PrintOne(pos);
			cout<<"\n按任意键返回:";
			getch();   break;
		case 5: 
			cout<<"输入搜索中心坐标:\nx="; cin>>city.x;
			cout<<"y="; cin>>city.y;
			cout<<"输入搜索范围(半径):"; cin>>dist;
			dbList.PrintLocal(city, dist);
			cout<<"\n按任意键返回:";
			getch();   break;
		
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -