[英]c++ distance between 2 points assignment
people, i have a question regarding one of my programming assignment. 朋友,我对我的一项编程任务有疑问。 the assignment is Write a complete C++ program called closest.cpp that reads three points p1 = (x1, y1), p2 = (x2, y2) and p3 = (x3, y3) from the standard input in the following format.
任务是编写一个完整的C ++程序,称为closest.cpp,该程序以下列格式从标准输入中读取三个点p1 =(x1,y1),p2 =(x2,y2)和p3 =(x3,y3)。 the functions I created are mandatory for the project my teacher wanted us to use them in the main program exactly how i have it set up.
我创建的功能对于该项目是必不可少的,我的老师希望我们在主程序中完全按照我的设置使用它们。 my program compiles but it does not seem to get the input from the user each time I run my program it outputs
我的程序可以编译,但每次运行程序时似乎都无法从用户那里得到输入
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
when it should print out this instead 什么时候应该打印出来
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
this is the correct output 这是正确的输出
#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;
}
After you changed the format of scanf
to "%lf%lf%lf%lf%lf%lf"
you might as well correct this line: 将
scanf
的格式更改为"%lf%lf%lf%lf%lf%lf"
您还可以更正此行:
consider(x1, y1, x3, y3, x2, x2);
^^^^ //<-- should be y2, no?
Copypasta is unforgiving! Copypasta是不可原谅的!
Your comparison function doesn't account for all combinations. 您的比较功能无法说明所有组合。
Here is an example: 这是一个例子:
double shortest_distance = ans1;
double shortest_x1 = x1;
double shortest_y1 = y1;
double shortest_x2 = x2;
double shortest_y2 = y2;
if (ans2 < shortest_distance)
{
shortest_distance = ans2;
shortest_x1 = x1;
shortest_y1 = y1;
shortest_x2 = x3;
shortest_y2 = y3;
}
if (ans3 < shortest_distance)
{
shortest_distance = ans3;
shortest_x1 = x2;
shortest_y1 = y2;
shortest_x2 = x3;
shortest_y2 = y3;
}
showOutput(shortest_x1, shortest_y1,
shortest_x2, shortest_y2,
shortest_distance);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.