#include <iostream>
#include<string >
#include <vector>
#include <fstream>
#include <sstream>
#include <math.h>
#include<stdlib.h>
#define cit 162
#define kom 493
using namespace std;
int (*arr)[kom]=new int[cit][kom];
double arr_city[cit][3];
double arr_km[kom][3];
struct city
{
string city_name;
double city_lg;
double city_lt;
};
struct cityadd
{
static const int citysize=cit;
city citydata[cit];
};
struct kongmiao
{
string km_name;
double km_lg;
double km_lt;
};
struct kongmiaoadd
{
static const int kmsize=kom;
kongmiao kmdata[kom];
};
// 角度转弧度
double rad(double d)
{
const double PI = 3.1415926535898;
return d * PI / 180.0;
}
// 传入两个经纬度,计算之间的大致直线距离
int CalcDistance(double fLati1, double fLong1,double fLati2, double fLong2)
{
const float EARTH_RADIUS = 6378.137;
double radLat1 = rad(fLati1);
double radLat2 = rad(fLati2);
double a = radLat1 - radLat2;
double b = rad(fLong1) - rad(fLong2);
double s = 2 * asin(sqrt(pow(sin(a/2),2) + cos(radLat1)*cos(radLat2)*pow(sin(b/2),2)));
s = s * EARTH_RADIUS;
s = (int)(s * 1000) ;
return s;
}
void length(cityadd*citys,kongmiaoadd*kms)
{
for(int i=0;i<citys->citysize;i++)
{
for(int j=0;j<kms->kmsize;j++)
{
arr[i][j]=CalcDistance(citys->citydata[i].city_lt,citys->citydata[i].city_lg,kms->kmdata[j].km_lt,kms->kmdata[j].km_lg);
//cout<<arr[i][j]<<" "<<endl;
}
}
}
void store() //保存文件
{
ofstream ofs;
ofs.open("C:/Users/Administrator/Desktop/out.csv",ios::out);
if(!ofs.is_open())
{
cout<<"文件打开失败!";
return;
}
else
{
cout<<"文件打开成功!";
for(int i=0;i<cit;i++)
{
for(int j=0;j<kom;j++)
{
ofs<<arr[i][j]<<",";
}
ofs<<endl;
}
}
ofs.close();
cout<<"保存成功"<<endl;
}
void test_01(cityadd*citys)
{
ifstream inFile("city2.csv", ios::in);
string lineStr;
vector<vector<string>> strArray;
if(inFile.fail())
{ cout<<"false"<<endl;
return;
}
int i,j;
i=0;
while (getline(inFile, lineStr))
{
j=0;
// 打印整行字符串
//cout << lineStr << endl;
// 存成二维表结构
stringstream ss(lineStr);
string str;
vector<string> lineArray;
// 按照逗号分隔
while (getline(ss, str, ','))
{
arr_city[i][j]=static_cast<double>(strtod(str.c_str(),NULL)); //string -> float
j++;
}
i++;
}
for(int i=0;i<cit;i++)
{
citys->citydata[i].city_name=arr_city[i][0];
citys->citydata[i].city_lg=arr_city[i][1];
citys->citydata[i].city_lt=arr_city[i][2];
}
inFile.close();
}
void test_02(kongmiaoadd*kms)
{
ifstream inFile("km.csv", ios::in);
string lineStr;
vector<vector<string>> strArray;
int i,j;
i=0;
if(inFile.fail())
cout<<"false2"<<endl;
while (getline(inFile, lineStr))
{
j=0;
// 打印整行字符串
//cout << lineStr << endl;
// 存成二维表结构
stringstream ss(lineStr);
string str;
vector<string> lineArray;
// 按照逗号分隔
while (getline(ss, str, ','))
{
arr_km[i][j]=static_cast<float>(strtod(str.c_str(),NULL)); //string -> float
j++;
}
i++;
}
for(int i=0;i<kom;i++)
{
kms->kmdata[i].km_name=arr_km[i][0];
kms->kmdata[i].km_lg=arr_km[i][1];
kms->kmdata[i].km_lt=arr_km[i][2];
}
inFile.close();
}
int main()
{
kongmiaoadd *kms=new kongmiaoadd;
cityadd *citys=new cityadd;
while(1)
{
cout
<<"*****************************"<<endl
<<"******1、添加城市经纬度******"<<endl
<<"******2、添加孔庙经纬度******"<<endl
<<"******5、计算距离******"<<endl
<<"******0、退出系统 ******"<<endl
<<"*****************************"<<endl;
int key=0;
cin>>key;
switch (key) {
case 1:
test_01(citys);
system("pause");
system("cls");
break;
case 2:
test_02(kms);
system("pause");
system("cls");
break;
case 5:
length(citys,kms);
store();
system("pause");
system("cls");
break;
case 0:
cout << "see you" << endl;
return 0;
break;
}
}
}
怎么跑出公司和事务所之间的距离