计算器程序中的错误

时间:2013-02-05 19:59:28

标签: c++ haversine

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

1 个答案:

答案 0 :(得分:2)

该程序似乎很好。至于switch语句,它们只适用于整数或单个字符值。 switch不比较字符串或浮点数。也许发布替代方案的完整代码有助于确定问题。

修改

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

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

//#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
}