【发布时间】:2019-07-28 05:20:02
【问题描述】:
我正在用 C 编写代码来确定给定三角形是等边、等腰还是不等边。我把它写成:
#include<stdio.h>
#include<math.h>
int main()
{
float x1,x2,x3,y1,y2,y3;
float d1,d2,d3;
printf("Enter the co-ordinates of 1st vertex: \n");
scanf("%f%f", &x1,&y1);
printf("Enter the co-ordinates of 2nd vertex: \n");
scanf("%f%f", &x2,&y2);
printf("Enter the co-ordinates of 3rd vertex: \n");
scanf("%f%f", &x3,&y3);
d1= sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
d2= sqrt((x1-x3)*(x1-x3) + (y1-y3)*(y1-y3));
d3= sqrt((x2-x3)*(x2-x3) + (y2-y3)*(y2-y3));
if((d1==d2) && (d2==d3))
printf("Given triangle is equilateral");
else if((d1!=d2) && (d2!=d3) && (d1!=d3))
printf("Given triangle is scalene");
else
printf("Given triangle is isosceles but not equilateral");
}
现在,我的问题是如何在运行时将无理数的输入作为输入?例如,如果我必须取 √3 作为输入,那么如何取它,有没有办法在运行时取平方根?
请帮忙。
【问题讨论】:
-
那么您希望用户能够输入无理平方根的准确值吗?
-
请注意,您有直接的浮点比较 may cause problems。
-
@AlexanderZhang,不,我想知道,是否可以编写类似 sqrt(3) 的输入。我们可以取无理数的值直到小数点后 2 位,但它不会给出正确的输出。对吗?
-
这就是我的意思。您必须编写代码来检测和解析此类输入,但首先您必须修复浮点错误问题
-
如果您的作业没有明确要求您的程序接受精确的无理数或公式,请不要尝试执行此操作。这样的壮举将远远超出初学者程序员的能力。让用户输入一个近似值,如 5.19615242271。
标签: c square-root