gpt4 book ai didi

c++ 两点赋值之间的距离

转载 作者:行者123 更新时间:2023-11-28 04:49:35 24 4
gpt4 key购买 nike

各位,我有一个关于我的编程任务的问题。作业是编写一个完整的 C++ 程序,称为 closest.cpp,它以下列格式从标准输入中读取三个点 p1 = (x1, y1)、p2 = (x2, y2) 和 p3 = (x3, y3)。我创建的功能对于我的老师希望我们在主程序中完全按照我的设置方式使用它们的项目是强制性的。我的程序可以编译,但每次我运行它输出的程序时似乎都没有从用户那里获得输入

x1 y1
x2 y2
x3 y3

1.0 3.0
1.5 7.2
4.0 2.0
The three points are:
0.000 0.000
0.000 0.000
0.000 0.000
The closest two points are:
0.000 0.000
0.000 0.000

The distance between those two points is: 0.000
The closest two points are:
0.000 0.000
0.000 0.000

The distance between those two points is: 0.000
The closest two points are:
0.000 0.000
0.000 0.000

The distance between those two points is: 0.000

什么时候应该打印出来

The three points are:
1.000 3.000
1.500 7.200
4.000 2.000

The closest two points are:
1.000 3.000
4.000 2.000

The distance between those two points is: 3.162

这是正确的输出

#include <cstdio>
#include <cmath>
using namespace std;

void displayInput(double x1, double y1, double x2, double y2, double x3,
double y3)
{
printf("The three points are:\n %10.3f %10.3f \n %10.3f %10.3f \n %10.3f
%10.3f \n",
x1, y1, x2, y2, x3, y3);
}


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

void showOutput(double x1, double y1, double x2, double y2, double d)
{
printf("The closest two points are:\n%10.3f %10.3f\n%10.3f %10.3f \n"
"\nThe distance between those two points is: %10.3f\n",
x1, y1, x2, y2,d);
}

void consider(double x1, double y1, double x2, double y2, double x3, double
y3)
{
double ans1, ans2, ans3;
ans1 = distance(x1, y1, x2, y2);
ans2 = distance(x1, y1, x3, y3);
ans3 = distance(x2, y2, x3, y3);

if (ans1 <= ans2 && ans1 <= ans3)
{
showOutput(x1, y1, x2, y2, ans1);
}
}


int main()
{
double x1, x2, y1, y2, x3, y3;

scanf("%f%f%f%f%f%f", &x1, &y1, &x2, &y2, &x3, &y3);

displayInput(x1, y1, x2, y2, x3, y3);

consider(x1, y1, x2, y2, x3, y3);
consider(x1, y1, x3, y3, x2, x2);
consider(x2, y2, x3, y3, x1, y1);
return 0;
}

最佳答案

scanf 的格式更改为 "%lf%lf%lf%lf%lf%lf" 后,您不妨更正此行:

consider(x1, y1, x3, y3, x2, x2);
^^^^ //<-- should be y2, no?

Copypasta 是无情的!

关于c++ 两点赋值之间的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48511500/

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