#include <stdio.h>
int main(void)
{
float price, vat, vatless, vatprice;
int vatpercentage;
printf("Enter the price of the item:n ");
scanf_s("%f", &price);
printf("what is the current vat: n");
scanf_s("%d", &vatpercentage);
vatpercentage = (float) (vatpercentage / 100);
vat = (float) (vatpercentage * vatpercentage);
vatless = price / vat;
vatprice = price - vatless;
printf("Total = %.2fn", price);
printf("Your vatless price is %.2fn", vatless);
printf("your vatprice is %.2fn", vatprice);
return 0;
}
你好,我正在尝试制作一个vat计算器,但我真的不明白为什么当我尝试这个代码时,我只得到inf和-inf。
简单是有益的。。。我迷失在纠结中,为5个名称相似的不同变量计算值。
试试这个:
int main() {
double price;
printf( "Enter the price: " );
scanf( "%lf", &price );
int vat;
printf( "Enter the VAT %%: ");
scanf( "%d", &vat );
double base = price * 100 / ( 100 + vat );
printf( "Total%9.2lfn", price );
printf( "VAT %9.2lf (%d%%)n", price - base, vat );
printf( "Base %9.2lfn", base );
return 0;
}
输出
Enter the price: 68.95
Enter the VAT %: 13
Total 68.95
VAT 7.93 (13%)
Base 61.02
为了简洁起见,省略了对输入的错误检查和验证。
"浮点";将(通常(是我们人类所认为的";整数值";。可能存在显示为的值;5.00";,但是,在内部,该值是"0";4.9998";。用户被警告;math.h";,计算的浮点值应该适当地四舍五入。
我没有";VAT";,但我认为这三个变量正在计算你所寻求的。如果这是错误的,请告诉我,我会尽快删除这个答案。
vatpercentage
是int
,因此除法vatpercentage / 100
是整数除法。将其定义为float
,您应该没事。