游戏 - 计算两个坐标之间的距离
Game- calculating of distance between two coordinates
我的代码有问题。所以我们的任务是创建一个代码,它从txt文件输入信息(包括一个城市的名称及其经度和纬度),并制作一个游戏,询问"哪个城市离_____最近",并给你3个选项,你必须从中选择。然后它会告诉您正确/不正确并显示距离。问题是,我的代码不是以公里为单位计算城镇之间的距离,而是一种非常奇怪的形式。结果是例如 3.582E-307 和类似。你知道可能是什么问题吗?
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <fstream>
#include <sstream>
#include <string>
#include <time.h>
#include <random>
#include <numeric>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <ctime>
#define PI 3.14159265
using namespace std;
struct District {
double lng ;
double lat ;
string city ;
};
struct Distance {
double lng;
double lat;
};
//void for changing degrees to rad
double dtr(double deg) {
return (deg * PI / 180);
};
//void calculation of distance
// this doesnt work correctly - the output isnt the distance in KM, i used the formula from - https://en.m.wikipedia.org/wiki/Great-circle_distance
double* distancecalc (double pos[], double distance[]) {
Distance locat [4] ;
District districts [79] ;
double posi [4];
for (int x=0; x<4; x++) {
posi [x] = pos [x] ;
}
for (int u=posi[0]; u<posi[4]; u++) {
locat [u].lat = districts [u].lat ;
locat [u].lng = districts [u]. lng;
locat [u].lat = dtr(locat [u].lat );
locat [u].lng =dtr (locat [u].lng) ;
}
//calculate the distance between cities (0 (base) and 1-3)
for (int u=0; u<4; u++) {
double ax = locat[0].lat, ay = locat[0].lng, bx = locat[u].lat, by = locat[u].lng;
distance[u] = acos (sqrt(pow(sin((bx-ax)/2.0), 2.0)+cos(ax)*cos(bx)*pow(sin((by-ay)/2.0),2.0))) * 2 * 6371;
return distance ;
}
}
int main () {
int z=1;
do { districts [79] ;
ifstream inputFile;
inputFile.open ("districts.txt") ;
if (!inputFile.is_open()){
cout << "opening txt document" << endl;
return 0 ;
}
//shuffle
int line ;
stringstream ss;
string sdouble ;
for (line=0; line<79; line++) {
getline(inputFile, districts[line].city, ';') ;
getline(inputFile, sdouble, ';') ;
ss<< sdouble;
ss>> districts[line].lng ;
getline(inputFile, sdouble, ';' ) ;
ss<<sdouble;
ss>>districts[line].lat;
getline(inputFile, sdouble) ;
}
//shuffle the cities+ coordinates- array
srand(time(NULL));
double tempn;
int pos1,pos2;
string temp;
for (int i = 0; i < 79; ++i){
pos1 = rand()%79;
pos2 = rand()%79;
temp = districts [pos1].city ;
districts [pos1].city = districts [pos2].city;
districts [pos2].city = temp;
tempn = districts [pos1].lat;
districts [pos1].lat = districts [pos2].lat ;
districts [pos2].lat = tempn;
tempn = districts [pos1].lng ;
districts [pos1].lng = districts [pos2].lng ;
districts [pos2].lng = tempn;
}
//game
double pos [4];
double distance [3];
int loop=0, answer=0, guess, total=0;
do {
double min=999999;
//cout - city + options
cout<< endl ;
cout << "Which city is the closest to "<< districts[loop].city<< "? Pick one of the following: " <<endl;
pos [1] = loop;
loop++ ;
cout << "1) " << districts[loop].city << endl;
pos [2]= loop;
loop++ ;
cout << "2) " << districts[loop].city << endl;
pos [3] = loop;
loop++ ;
cout << "3) " << districts[loop].city << endl;
pos [4] = loop;
loop++ ;
//calculate the difference+ find answer (which is the closest)
cout << "Write your selection: " ;
cin>> guess;
for (int x=1; x<4; x++) {
if (min>distance[x] ) {
min= distance[x];
answer= x;
}
}
//if you guess the correct answer -
if (guess==answer) {
total++;
cout<< "Correct! The distances are: " << endl;
}
// if you guess the incorrect answer-
else {
cout<< "Incorrect! The distances are:" <<endl;
}
//witing the distances between 1 city
cout<< "1) " << districts[loop-3].city << "- " << distance [1] << endl;
cout<< "2) " << districts[loop-2].city << "- " << distance [2] << endl;
cout<< "3) " << districts[loop-1].city << "- " << distance [3] << endl << endl;
for (int u=0 ; u< 80; u++) {
cout << "-" ;
}
} while(loop<40) ;
//end of game
cout << endl << "Conratulations, you finished the game! Your score is: " << total << " out of 10" << endl;
int selection ;
cout <<endl << "Do you want to play again?" << endl << "1= Yes" << endl << "0= No" << endl;
cout << "Your selection: " ;
cin>> selection;
if (selection== 0) {
z= 0;
}
else {
z=1;
if (system("CLS")) system("clear") ;
}
} while (z==1 );
}
**END OF CODE**
你的纬度和经度都是以度为单位,对吧?n 您可以避免调用函数将它们转换为弧度:
constexpr double DEG2RAD = 3.141592653589793 / 180;
因此:
for (int u=posi[0]; u<posi[4]; u++) {
locat [u].lat = districts [u].lat * DEG2RAD;
locat [u].lng = districts [u].lng * DEG2RAD;
}
然后,在您发布的链接中,公式是不同的,您使用acos
而不是asin
,但最重要的是您没有将districts[]
传递给函数,因此它是该函数的本地且未初始化。您必须决定是否将该数组设为全局。
然后你必须在某个地方打电话给distancecalc()
...
在
代码的这一部分:
double* distancecalc (double pos[], double distance[]) {
Distance locat [4] ;
District districts [79] ;
....
for (int u=posi[0]; u<posi[4]; u++) {
locat [u].lat = districts [u].lat ;
locat [u].lng = districts [u]. lng;
....
districts
未初始化。 localt
正在获取随机值
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 两个字符串在 c++ 中不相等
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 两个文件使用彼此的功能-如何解决
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 将fold表达式与std::一起用于两个元组
- 比较两个节点坐标的最佳方法是什么?
- 两个'rare'矩阵的总和,读取为坐标 X、坐标 Y 和一个值
- 如何使用 std::multimap 将整数键映射到两个用作多维数组坐标的整数值(对于井字)?
- 如何实现 std::map::查找包含两个结构'Pos'结构的比较逻辑,每个结构包含 x 和 y 坐标
- 输入坐标分为与文件故障的两个单独的数组
- 如何区分具有对象坐标的两个图像
- 游戏 - 计算两个坐标之间的距离