gpt4 book ai didi

C 打印问题,小数等于 0

转载 作者:太空宇宙 更新时间:2023-11-04 02:02:08 28 4
gpt4 key购买 nike

我基本上是 C 语言的新手,所以请多多包涵。所以基本上我必须创建一个程序来计算每平方英尺的面团量。经过很长时间的重新检查我的数学和调试;我发现 sradius/INCHES_PER_FEET 变为 0。

INCHES_PER_FEET = 12。现在的问题是,如果我输入任何等于或大于 12 的半径值,该函数将正常工作;但如果我输入 8,而 8/12 小于小数点后的 1,程序会自动将其等同于 0,我不确定该如何解决。非常感谢您的见解。

int main(){
//define radius variables
int sradius;
printf("What is the radius of your small pizza, in inches?\n");
scanf("%d", &sradius);
int mradius;
printf("What is the radius of your medium pizza, in inches?\n");
scanf("%d", &mradius);
int lradius;
printf("What is the radius of your large pizza, in inches?\n");
scanf("%d", &lradius);


//define pizzas sold variables
int spizzas;
printf("How many small pizzas do you expect to sell this week?\n");
scanf("%d", &spizzas);
int mpizzas;
printf("How many medium pizzas do you expect to sell this week?\n");
scanf("%d", &mpizzas);
int lpizzas;
printf("How many large pizzas do you expect to sell this week?\n");
scanf("%d", &lpizzas);

//dough calculation per size
double sdough,mdough,ldough;
sdough = (PI*((sradius/INCHES_PER_FEET)*(sradius/INCHES_PER_FEET))*DOUGH_PER_SQFT);
mdough = (PI*((mradius/INCHES_PER_FEET)*(mradius/INCHES_PER_FEET))*DOUGH_PER_SQFT);
ldough = (PI*((lradius/INCHES_PER_FEET)*(lradius/INCHES_PER_FEET))*DOUGH_PER_SQFT);

//final amount of dough
double fdough;
fdough = ((sdough*spizzas)+(mdough*mpizzas)+(ldough*lpizzas));

//print statement
printf("you need to order %.3f " ,fdough);

return 0;


}

最佳答案

在计算之前将所有半径转换为 double 值,您将得到一个 double 值,如下所示:

double sdough,mdough,ldough;
sdough = (PI*(((double)sradius/INCHES_PER_FEET)*((double)sradius/INCHES_PER_FEET))*DOUGH_PER_SQFT);
mdough = (PI*(((double)mradius/INCHES_PER_FEET)*((double)mradius/INCHES_PER_FEET))*DOUGH_PER_SQFT);
ldough = (PI*(((double)lradius/INCHES_PER_FEET)*((double)lradius/INCHES_PER_FEET))*DOUGH_PER_SQFT);

关于C 打印问题,小数等于 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25965689/

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