gpt4 book ai didi

将输入从 int 转换为 double

转载 作者:行者123 更新时间:2023-11-30 18:32:29 25 4
gpt4 key购买 nike

有没有办法将实际输入的int转换为double。我不想获取字符串值并使用 isDigit 来检查它是 double 还是 int。

int main(void)
{

// Initializing variables
int a,b,c,res;

// Getting input from user
printf("Please enter the sides of triangle");

printf("\nPlease enter side 1:");
scanf("%d",&a);

printf("Please enter side 2:");
scanf("%d",&b);

printf("Please enter side 3:");
scanf("%d",&c);


// Calling function in order to print type of the triangle.
checkTriangle(a,b,c);



}

// A function which decides the type of the triangle and print it
void checkTriangle(int s1, int s2,int s3)
{


// Check the values whether it is triangle or not.
if ((s1 + s2 > s3 && s1 + s3 > s2 && s2 + s3 > s1) && (s1 > 0 && s2 > 0 && s3 > 0))
{
// Deciding type of triangle according to given input.
if (s1 == s2 && s2 == s3)
printf("EQUILATERAL TRIANGLE");
else if (s1 == s2 || s2 == s3 || s1 == s3)
printf("ISOSCELES TRIANGLE\n");
else
printf("SCALENE TRIANGLE \n");
}
else
printf("Triangle could not be formed.");


}

当用户输入 double 值时我想要做什么,我想给出一条消息,表明您可以输入 double 值。有没有办法在不输入字符的情况下做到这一点?

最佳答案

如果您希望将 int 值转换为 double,只需使用强制转换:

int foo = 5;
double bar = (double) foo;

关于将输入从 int 转换为 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12886195/

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