c - 平方根作为运行时的输入

标签 c square-root

我正在用 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 作为输入,那么如何取它,有没有办法在运行时取平方根?
请帮忙。

最佳答案

解决方案可以是编写一段代码询问用户是否要输入无理数,然后在程序内部处理输入。

  1. 如果第 n 个顶点包含无理数则打印
  2. 获取其位置
  3. 接受整数输入并将其作为无理实体处理

sqrt(integer)

在程序内部。

关于c - 平方根作为运行时的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57238171/

相关文章:

c - 在文本文件中搜索电子邮件

c - c 中带有指针的 strtok 函数出现意外错误

Python,平方根函数?

c++ - 如何将 double 转换为整数

c - 我怎样才能让 g_dbus_connection_signal_subscribe 函数告诉我预先存在的对象/接口(interface)?

在 C 中的线程内定期调用函数

c - C 上的 float 问题

assembly - ARM frsqrts 是否需要与额外的 fmul 指令一起使用以进行牛顿迭代?

Java Prime 查找器效率

javascript - Math.sqrt() 返回无穷大?