因此,我有这段代码,该代码用于计算总价,然后给他们打折,最后向他们展示带或不带折扣的总价。但是由于某种原因,我从计算中得出的结果确实很奇怪。
#include <stdio.h>
int main()
{
double discountPercentage=0.0;
double numbUnits=0.0, perUnitPrice=0.0, priceWDiscount=0.0;
printf("Input # of units purchased:");
scanf("%lf", &numbUnits);
printf("Input pricer per unit:");
scanf("%lf", &perUnitPrice);
if (numbUnits*perUnitPrice >= 1000.0 && numbUnits*perUnitPrice <= 2000.0)
{
discountPercentage = 0.10;
}
else if (numbUnits*perUnitPrice >= 2000.0 && numbUnits*perUnitPrice <= 3000.0)
{
discountPercentage = 0.15;
}
else if (numbUnits*perUnitPrice >= 3000.0)
{
discountPercentage = 0.20;
}
else
{
discountPercentage = 0.0;
}
priceWDiscount = (numbUnits*perUnitPrice) - (numbUnits*perUnitPrice*discountPercentage);
double price = numbUnits*perUnitPrice;
printf("Without discount your price would be %d.\nIncluding discount (%d) your price is %d"), price, discountPercentage, priceWDiscount;
return 0;
}
输出:
Input # of units purchased:50
Input pricer per unit:50
Without discount your price would be 266310.
Including discount (266310) your price is 2126139392
那我在做什么错?我曾尝试搜索论坛,但找不到任何可以解决我的问题的东西。任何帮助表示赞赏。另外,很抱歉,如果这是一个非常明显的问题,但对我而言,不是...
您正在以双精度计算内容,但仅以整数精度打印它。
在您的对printf
帐单中,将所有更改%d
为%lf
。
您也用括号将太早关闭了printf语句。您需要将其移到半冒号之前的末尾,以便printf
实际知道应该打印什么变量。
因此,总的来说,您需要更改以下内容:
printf("Without discount your price would be %d.\nIncluding discount (%d) your price is %d"), price, discountPercentage, priceWDiscount;
对此:
printf("Without discount your price would be %lf.\nIncluding discount (%lf) your price is %lf", price, discountPercentage, priceWDiscount);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句