我应该更改书面代码中的某些内容吗?编译器说一切都是正确的——没有错误或警告。
- 您正在建造一个新家,并且您已经准确计算了地基所需的水泥量。
- 理想情况下,您想购买确切数量的水泥,但商店只出售 120 磅袋装的水泥。
- 这些袋子每个售价 45 美元。
请编写一个 C 程序,计算您为建造地基而必须购买的水泥成本。
- 您的程序应首先读取一个十进制数字,该数字表示新家地基所需的水泥量(以磅为单位)。
- 然后,您的程序应该显示您必须购买的水泥袋的总成本,以便有足够的水泥来建造您的地基。
- 为了使您的程序更简单,您可以保证所需的水泥量永远不会是 120 的倍数。
到目前为止我的代码:
#include <stdio.h>
#include <math.h>
int main(void) {
int price=45, totalPrice, OneBag=120;
float needed;
do
{
printf("Enter the amount of cement you need for your foundation that is not dividable by 120: ");
scanf("%f", &needed);
} while (fmodf(needed, OneBag)==0);
totalPrice = ((int)needed/OneBag+1)*(price);
printf("Total cost of cement you will need for your foundation is %d", totalPrice);
return 0;
}