我正在使用一个程序来计算地球表面上两个地理点之间的距离。我对 C++ 很陌生,如果我的英语不好,我深表歉意
关于如何将底部程序中的内容无错误地放入顶部程序的任何建议?
这是不工作的程序。
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main ()
double getdistance(double x1,double y1,double x2,double y2)
{
const double PI = 3.1415927;
double radius, sum;
int exit = 1;
char choice;
while (exit == 1)
{
cout << "**** Calculate Program **** \n\n"
<< "1. Volume\n"
<< "2. Div\n"
<< "3. Calculate two geographical points\n"
<< "4. Working\n\n"
<< "5. Quit program\n\n";
cout << "Choose one of the options and write it here: ";
cin >> choice;
switch (choice)
{
//
case '1':
float volume (float radius);
{
cout << "radius you want to calculate: " << endl;
cin >> radius;
sum = 4.0/3.0 * PI * pow (radius, 3);
cout << "Volume: " << sum << endl;
}
break;
//
case '2':
cout<<"div \n";
break;
//
case '3':
radius=6371; // radius km
double pi=3.14159265;
double x1,y1,x2,y2;
// X og Y coords
x1=(x1/180)*pi;
y1=(y1/180)*pi;
x2=(x2/180)*pi;
y2=(y2/180)*pi;
// if else
if (x1==x2 && y1==y2)
return 0;
else
{
if ((sin(x2)*sin(x1)+cos(x2)*cos(x1)*cos(y2-y1))>1)
return radius*acos(1.0);
else
return radius*acos(sin(x2)*sin(x1)+cos(x2)*cos(x1)*cos(y2-y1));
//
double x1,y1,x2,y2;
cout<<"Write the coords you like to calculate distance from: \n";
cin>>x1>>y1;
cin>>x2>>y2;
cout<<"distance in km : \n";
cout<<getdistance(x1,y1,x2,y2)<<endl;
return 0;
}
break;
case '4':
cout<<"working \n";
break;
//
case '5':
exit = 0;
break;
default:
exit = 0;
cout << "ERROR!";
} // Switch quit
} // While quit
return 0;
}
这是可以工作的程序,但我必须把它放在上面程序的案例 3 中。
# include <iostream>
# include <cstdio>
# include <cmath>
using namespace std;
double getdistance(double x1,double y1,double x2,double y2)
{
double radius;
radius=6371; // radius km
double pi=3.14159265;
// X og Y coord
x1=(x1/180)*pi;
y1=(y1/180)*pi;
x2=(x2/180)*pi;
y2=(y2/180)*pi;
if (x1==x2 && y1==y2)
return 0;
else
{
if ((sin(x2)*sin(x1)+cos(x2)*cos(x1)*cos(y2-y1))>1)
return radius*acos(1.0);
else
return radius*acos(sin(x2)*sin(x1)+cos(x2)*cos(x1)*cos(y2-y1));
}
}
int main()
{
double x1,y1,x2,y2;
cout<<"Write the coords you like to calculate distance from: \n";
cin>>x1>>y1;
cin>>x2>>y2;
cout<<"distance in km : \n";
cout<<getdistance(x1,y1,x2,y2)<<endl;
return 0;
}