gpt4 book ai didi

c++ - 计算器程序错误

转载 作者:行者123 更新时间:2023-11-28 03:23:44 25 4
gpt4 key购买 nike

我正在使用一个程序来计算地球表面两个地理点之间的距离。我是 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;
}

最佳答案

程序看起来不错。至于 switch 语句,它们只适用于整数或单个字符值。 switch 不比较字符串或 float 。也许发布您的替代方案的完整代码将有助于确定问题。

编辑

int main ()
double getdistance(double x1,double y1,double x2,double y2)
{

这是错误的。当你声明函数时,你必须在 before main 之前声明它们,并在 main 之前或之后定义它们,尽管通常你会在 after.

//#include directives

double getdistance(double x1,double y1,double x2,double y2);

int main() {
// code for main()
}

double getdistance(double x1,double y1,double x2,double y2) {
//code for your function
}

关于c++ - 计算器程序错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14715897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com