我正在用 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 作为输入,那么如何取它,有没有办法在运行时取平方根?
请帮忙。
最佳答案
解决方案可以是编写一段代码询问用户是否要输入无理数,然后在程序内部处理输入。
- 如果第 n 个顶点包含无理数则打印
- 获取其位置
- 接受整数输入并将其作为无理实体处理
sqrt(integer)
在程序内部。
关于c - 平方根作为运行时的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57238171/