gpt4 book ai didi

c - 打印解决方案(C 语言新手)

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

我不明白为什么我的程序不会打印最终的解决方案(totalDough)。输入为 8,10,12,然后是 40,100 和 200:

const int DOUGH_PER_SQFT = 0.75;
const int INCHES_PER_FEET = 12;
#define M_PI 3.14159265358979323846

int main(){

// Declare and initialize variables

int smallRIn;
printf("What is the radius of your small pizza, in inches?\n");
scanf("%d", &smallRIn);
int mediumRIn;
printf("What is the radius of your medium pizza, in inches?\n");
scanf("%d", &mediumRIn);
int largeRIn;
printf("What is the radius of your large pizza, in inches?\n");
scanf("%d", &largeRIn);


// Get number of pizzas sold
int smallSold;
printf("How many small pizzas do you expect to sell this week?\n");
scanf("%d", &smallSold);
int mediumSold;
printf("How many medium pizzas do you expect to sell this week?\n");
scanf("%d", &mediumSold);
int largeSold;
printf("How many large pizzas do you expect to sell this week?\n");
scanf("%d", &largeSold);

// Convert radii to feet

double smallRFeet, mediumRFeet, largeRFeet;
smallRFeet = smallRIn/INCHES_PER_FEET;
mediumRFeet = mediumRIn/INCHES_PER_FEET;
largeRFeet = largeRIn/INCHES_PER_FEET;

// Calculate top surface areas of each type of pizza.

double areaSmall, areaMedium, areaLarge;
areaSmall = smallSold*M_PI*pow(smallRFeet,2);
areaMedium = mediumSold*M_PI*pow(mediumRFeet,2);
areaLarge = largeSold*M_PI*pow(largeRFeet,2);

// Print solution
double dough;
dough = areaSmall+areaMedium+areaLarge;
double total_dough;
total_dough = dough * DOUGH_PER_SQFT;

printf("The total amount of dough you need to order this week is", total_dough);

return 0;
}

我做错了什么?

最佳答案

const int DOUGH_PER_SQFT = 0.75;

这是一个 int 值,而 int 不是 float ,因此这会转换为:

0

这基本上意味着在你的最终方程中

total_dough = dough * DOUGH_PER_SQFT;

它将计算为 0,因为每平方英尺的面团等于 0。

这可以通过更改来纠正:

const int DOUGH_PER_SGFT = 0.75

进入:

const double DOUGH_PER_SGFT = 0.75

关于c - 打印解决方案(C 语言新手),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25960453/

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