我遇到配方问题,程序运行正常,但答案似乎是错误的

时间:2021-08-07 07:05:10

The following program should calculate the distance between two points:

以下程序应计算两点之间的距离:

header.h

class point{
private:
    double x, y, length;
public:
    point();
    point(double a, double b);

    int set_length(point,point);
};

header.cpp

#include<iostream>
#include"header.h"
#include<math.h> 
using namespace std;

point::point() :x(0), y(0) { }

point::point(double a, double b){
    x = a;
    y = b;
}

int point::set_length(point p1, point p2){

    length = sqrt(((p2.x - p1.x)*(p2.x - p1.x)) +
                  ((p2.y - p1.y)*(p2.y -p1.y)));
    return length;
}

main.cpp

#include<iostream>
#include"header.h"
using namespace std;

int main(){
    point p1(4, 1);
    point p2(8, 2);

    point length;

    cout << length.set_length(p1, p2) << endl;
}

The answer should be 3 but its not.

答案应该是3但不是。

Could someone help me implement the distance formula?

有人可以帮我实施距离公式吗?

2 个解决方案

#1


1  

You didn't provide the actual output you get, but, here are possible causes of errors:

您没有提供实际输出,但是,这里可能是错误的原因:

  1. You're on a UNIX-like system and you didn't give -lm parameter to the linker, thus the math library is not linked in, and you get weird behavior. You can also get similar problems if you don't link the math library on some other systems. So, be sure to link the math library.

    你是一个类UNIX系统而你没有给链接器提供-lm参数,因此没有链接数学库,你会得到奇怪的行为。如果不在其他系统上链接数学库,也会遇到类似的问题。因此,请务必链接数学库。

  2. You are returning int from point::set_lenght which will do a cast from double - a cast is not rounding, most likely it's truncating. Change to return double.

    你将从point :: set_lenght返回int,它将从double执行转换 - 转换不是舍入,很可能是截断。改为返回双倍。

BTW, distance between points (4,1) and (8,2) is not 3. It's something like 4.12310563.

顺便说一句,点(4,1)和(8,2)之间的距离不是3.它类似于4.12310563。

#2


0  

Firstly, you should probably read something about file organization.

首先,您应该阅读有关文件组织的内容。

Regarding you code, the formula for distance between two points, let's say A (x1 , y1) and B (x2 , y2) is:

关于你的代码,两点之间距离的公式,假设A(x1,y1)和B(x2,y2)是:

我遇到配方问题,程序运行正常,但答案似乎是错误的

which in code looks like:

在代码中看起来像:

int x1, x2, y1, y2;

Point A(x1, x2), B(y1, y2);

double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));

some casting may be necessary inside the pow or sqrt as the coordinates are ints.

由于坐标是整数,因此在pow或sqrt内部可能需要一些铸造。

In class point the function should be named double get_lenght() to avoid implicit conversions of the returned result and to make sense, as you are not setting but rather getting the distance.

在类中,该函数应该被命名为double get_lenght()以避免返回结果的隐式转换并且有意义,因为您没有设置而是获得距离。


Side note: This function shouldn't be part of the class as it involves two points in the class called point, it is logically more suitable for class named line probably.

旁注:这个函数不应该是类的一部分,因为它涉及类中的两个点,称为point,它在逻辑上更适合类命名行。

#1


1  

You didn't provide the actual output you get, but, here are possible causes of errors:

您没有提供实际输出,但是,这里可能是错误的原因:

  1. You're on a UNIX-like system and you didn't give -lm parameter to the linker, thus the math library is not linked in, and you get weird behavior. You can also get similar problems if you don't link the math library on some other systems. So, be sure to link the math library.

    你是一个类UNIX系统而你没有给链接器提供-lm参数,因此没有链接数学库,你会得到奇怪的行为。如果不在其他系统上链接数学库,也会遇到类似的问题。因此,请务必链接数学库。

  2. You are returning int from point::set_lenght which will do a cast from double - a cast is not rounding, most likely it's truncating. Change to return double.

    你将从point :: set_lenght返回int,它将从double执行转换 - 转换不是舍入,很可能是截断。改为返回双倍。

BTW, distance between points (4,1) and (8,2) is not 3. It's something like 4.12310563.

顺便说一句,点(4,1)和(8,2)之间的距离不是3.它类似于4.12310563。

#2


0  

Firstly, you should probably read something about file organization.

首先,您应该阅读有关文件组织的内容。

Regarding you code, the formula for distance between two points, let's say A (x1 , y1) and B (x2 , y2) is:

关于你的代码,两点之间距离的公式,假设A(x1,y1)和B(x2,y2)是:

我遇到配方问题,程序运行正常,但答案似乎是错误的

which in code looks like:

在代码中看起来像:

int x1, x2, y1, y2;

Point A(x1, x2), B(y1, y2);

double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));

some casting may be necessary inside the pow or sqrt as the coordinates are ints.

由于坐标是整数,因此在pow或sqrt内部可能需要一些铸造。

In class point the function should be named double get_lenght() to avoid implicit conversions of the returned result and to make sense, as you are not setting but rather getting the distance.

在类中,该函数应该被命名为double get_lenght()以避免返回结果的隐式转换并且有意义,因为您没有设置而是获得距离。


Side note: This function shouldn't be part of the class as it involves two points in the class called point, it is logically more suitable for class named line probably.

旁注:这个函数不应该是类的一部分,因为它涉及类中的两个点,称为point,它在逻辑上更适合类命名行。