#include<iostream>
#include<string>
#include<cmath>
#include<iomanip> using namespace std; struct City //城市结构体
{
string name;
int EorW;//东经还是西经?东经1,西经0
int NorS;//北纬还是南纬?北纬1,南纬0
double lon;//经度
double lat;//纬度
City()
{
EorW = ;
NorS = ;
}
}; const double PI = 3.1415926;
const double R = 6371.0;//地球半径 int Iputn() //input number输入城市数量
{
int n;
bool flag = true;
while (flag)
{
cout << "请输入城市个数(正整数):" << '\n';
cin >> n;
if ( >= n)
{
cout << "您的输入不符合要求!重新输入请按1,退出请按0:" << '\n';
int choice;
cin >> choice;
if ( != choice){ flag = false; }
}
if (flag)flag = false;
else flag = true;
}
return n;
} int ChoDir(int dir) //choose direction 选择方向(东西经,南北纬)
{
string str1, str2;
if ( == dir){ str1 = "东经"; str2 = "西经"; }
else{ str1 = "北纬"; str2 = "南纬"; }
bool flag = true;
int choice;
while (flag)
{
cout << "请您选择"<<str1<<"还是"<<str2<<":" <<"1." <<str1 << ";" << "0."<<str2 << '\n';
cin >> choice;
if (choice< || choice>)
{
cout << "您的输入不符合要求!重新输入请按1,退出程序请按0:" << '\n';
int choice1;
cin >> choice1;
if (!= choice1)flag = false;
}
if (flag)flag = false;
else flag = true;
}
return choice;
} double Iputcoo(int option) //input coordinate 输入坐标
{
string str;
double up;
if ( == option){ str = "东经"; up = 180.0; }
else if ( == option){ str = "西经"; up = 180.0; }
else if ( == option){ str = "北纬"; up = 90.0; }
else { str = "南纬"; up = 90.0; }
double coor;
bool flag = true;
while (flag)
{
cout << "请您输入" << str << "(0到"<<up<<"间的有理数):" << '\n';
cin >> coor;
if (coor<0.0||coor>up)
{
cout << "您的输入不符合要求,重新输入请按1,退出请按0:" << '\n';
int choise;
cin >> choise;
if ( != choise)flag = false;
}
if (flag)flag = false;
else flag = true;
}
return coor;
} double Calcu(City& P1, City& P2) //calculate 计算两点加球面距
{
if (P1.name == P2.name)return 0.0;
//求AB
double AB;
double OA = sin(P1.lat); double OB = sin(P2.lat);
if (P1.NorS == P2.NorS) AB = fabs(OA - OB);
else AB = OA + OB;
//求夹角
double angle;
if (P1.EorW == P2.EorW) angle = fabs(P1.lon - P2.lon);
else angle = P1.lon + P2.lon;
//求P1A、P2B
double P1A = cos(P1.lat); double P2B = cos(P2.lat);
//求_P1P2_2 (P1'P2的平方)
double _P1P2_2 = P1A*P1A + P2B*P2B - * cos(angle)*P1A*P2B;
//求P1P2_2 (P1P2的平方)
double P1P2_2 = _P1P2_2 + AB*AB;
//求夹角1 (OP1与OP2间的夹角)
double angle1 = acos(( - P1P2_2) / 2.0);
//求球面距
return R*angle1;
} int main()
{
int num = Iputn(); //输入城市数量
if ( >= num){ exit(); }
struct City* city = new City[num];//创建数组
for (int i = ; i < num; i++)
{
getchar(); //输入城市名称
cout << "请您输入城市" << i + << "的名称:" << '\n';
getline(cin, city[i].name);
int choice = ChoDir(); //输入东西经
if (choice< || choice>){ delete[] city; exit(); }
if ( == choice)
{
double elon = Iputcoo();
if (elon<0.0|| elon>180.0)
{
delete[] city;
exit();
}
city[i].lon = PI*elon / 180.0; //角度转弧度
city[i].EorW = ; //东经
}
else
{
double wlon = Iputcoo();
if (wlon<0.0 || wlon>180.0)
{
delete[] city;
exit();
}
city[i].lon = PI*wlon / 180.0;
city[i].EorW = ; //西经
}
choice = ChoDir(); //输入南北纬
if (choice< || choice>){ delete[] city; exit(); }
if ( == choice)
{
double nlat = Iputcoo();
if (nlat<0.0 || nlat>90.0)
{
delete[] city;
exit();
}
city[i].lat = PI*nlat / 180.0;
city[i].NorS = ; //北纬
}
else
{
double slat = Iputcoo();
if (slat<0.0 || slat>90.0)
{
delete[] city;
exit();
}
city[i].lat = PI*slat / 180.0;
city[i].NorS = ; //南纬
}
}
cout << "Start\\End ";
for (int i = ; i < num; i++)
cout << setw()<<setiosflags(ios::left)<<city[i].name;
cout << '\n';
for (int i = ; i < num; i++)
{
cout << setw()<<setiosflags(ios::left)<<city[i].name;
for (int j = ; j < num; j++)
cout << setw() << setiosflags(ios::fixed) << setprecision() << Calcu(city[i], city[j]);
cout << '\n';
}
delete[] city;//delete掉new的数组
return ;
}

计算城市间的球面距离(C++实现)的更多相关文章

  1. PTA数据结构与算法题目集(中文) 7-35 城市间紧急救援 (25 分)

    PTA数据结构与算法题目集(中文)  7-35 城市间紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市 ...

  2. HDOJ2001计算两点间的距离

    计算两点间的距离 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. 计算两点间的距离,hdu-2001

    计算两点间的距离 Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离.   Input 输入数据有多组,每组占一行,由4个实数组成,分别表示x1 ...

  4. TSQL 根据经纬度计算两点间的距离;返回米(m)

    -- ============================================= -- Author:Forrest -- Create date: 2013-07-16 -- Des ...

  5. 转:Math: Math.atan() 与 Math.atan2() 计算两点间连线的夹角

    我们可以使用正切操作将角度转变为斜率,那么怎样利用斜率来转换为角度呢?可以利用斜率的反正切函数将他转换为相应的角度.as中有两个函数可以计算反正切,我们来看一下. 1.Math.atan() Math ...

  6. 城市间紧急救援(25 分)(dijstra变形)

    城市间紧急救援(25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标 ...

  7. PTA-数据结构 Dijkstra 城市间紧急救援

    城市间紧急救援(25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速道路长度都标 ...

  8. 5-5 城市间紧急救援 (25分)【最短路spfa】

    5-5 城市间紧急救援   (25分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快速 ...

  9. hdu2001 计算两点间的距离【C++】

    计算两点间的距离 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

随机推荐

  1. segement fault常见错误

    1.指针赋值前就用它引用内存,或释放后继续访问它的内容. 2.释放同一块内存两次.

  2. php课程---Windows.open()方法参数详解

    Window.open()方法参数详解 1, 最基本的弹出窗口代码   window.open('page.html'); 2, 经过设置后的弹出窗口   window.open('page.html ...

  3. C# 代码笔记

    一.使循环不卡 Application.DoEvents(); System.Threading.Thread.Sleep(5); 二.计算代码运行时间 Stopwatch sw = new Stop ...

  4. 浏览器中Javascript的加载和执行

    在刚学习Javascript时曾对该问题在小组内做个一次StudyReport,发现其中的基础还是值得分析的. 从标题分析,可以加个Javascript的加载和执行分为两个阶段:加载.执行.而加载即浏 ...

  5. java中PriorityQueue优先级队列使用方法

    优先级队列是不同于先进先出队列的另一种队列.每次从队列中取出的是具有最高优先权的元素. PriorityQueue是从JDK1.5开始提供的新的数据结构接口. 如果不提供Comparator的话,优先 ...

  6. 降维技术---PCA

    数据计算和结果展示一直是数据挖掘领域的难点,一般情况下,数据都拥有超过三维,维数越多,处理上就越吃力.所以,采用降维技术对数据进行简化一直是数据挖掘工作者感兴趣的方向. 对数据进行简化的好处:使得数据 ...

  7. Django,数据模型创建之数据库API参考(转载)

    一旦 数据模型 创建完毕, 自然会有存取数据的需要.本文档介绍了由 models 衍生而来的数据库抽象API,及如何创建,得到及更新对象. 贯穿本参考, 我们都会引用下面的民意测验(Poll)应用程序 ...

  8. cocos2dx 3.x(屏幕截图的两种方法)

    [RenderTexture] RenderTexture这个动态纹理类,顾名思义就是可以动态创建纹理图片. 屏幕截图主要步骤: > 开始截图:render->begin(); >  ...

  9. Anaconda安装与使用

    bash Anaconda.....sh命令安装成功 安装位置:/home/amelie/anaconda2 修改路径:vi ~/.bashrc vi编辑器在最后写上:export PATH=/hom ...

  10. MC, MCMC, Gibbs采样 原理&实现(in R)

    本文用讲一下指定分布的随机抽样方法:MC(Monte Carlo), MC(Markov Chain), MCMC(Markov Chain Monte Carlo)的基本原理,并用R语言实现了几个例 ...