我试着做了一个计算电价的c语言程序,但是一行行不通
I tried making a electricity price calculating c language program, but one line doesn't work
我试着做了一个计算电价的c语言程序,但是一行不行。
这是我的代码。
#include <stdio.h>
int main()
{
int usageElectric; //amount of electricity used
int basicMoney; //basic price
int totalMoney; //total price
double usageMoneyPerKw; //kw per used price
double totalFinalMoney; //final usage price
double tax; //tax
printf("put in the amount of electricity used (kw) : "); //put in 150kw.
scanf("%d", &usageElectric);
basicMoney = 660; //basic price = 0
usageMoneyPerKw = 88.5; //kw per usage price : .5
totalMoney = basicMoney + (usageElectric * usageMoneyPerKw);
tax = totalMoney * (9 / 100); //This line is the problem line = doesn't work
totalFinalMoney = totalMoney + tax;
printf("Tax is %d\n", tax); // a line to show that the tax isn't being caluculated properly
printf("The final usage price is %lf.", totalFinalMoney);
return 0;
}
如果输入为 150(kw),则 totalFinalMoney 应为 $15189.150000
任何人都可以帮我解决为什么这条线不起作用吗?
tax = totalMoney * (9 / 100);
如果正常运行,结果应该如下:
tax = 13935 * (9/100) = 1254.15
因此,最终结果应该是:
The final usage price is 15189.150000
在子表达式9/100
中,两个操作数都是整数,所以除法是整数除法,意味着任何小数部分都被截断,所以它的计算结果为0。
如果改为浮点常量,就会得到浮点除法。所以把上面的改成:
9.0/100.0
或者简单地说:
0.09
您只需像这样 ((double)9 / 100)
进行类型转换 (9/10)
。从现在开始,它正在将 9/10 的输出视为整数并将结果作为 0.
并且在打印 tax
时,您应该使用 %lf
而不是 %d
。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int usageElectric; //amount of electricity used
int basicMoney; //basic price
int totalMoney; //total price
double usageMoneyPerKw; //kw per used price
double totalFinalMoney; //final usage price
double tax; //tax
printf("put in the amount of electricity used (kw) : "); //put in 150kw.
scanf("%d", &usageElectric);
basicMoney = 660; //basic price = 0
usageMoneyPerKw = 88.5; //kw per usage price : .5
totalMoney = basicMoney + (usageElectric * usageMoneyPerKw);
tax = totalMoney * ((double)9 / 100); //This line is the problem line = doesn't work
totalFinalMoney = totalMoney + tax;
printf("Tax is %lf\n", tax); // a line to show that the tax isn't being caluculated properly
printf("The final usage price is %lf.", totalFinalMoney);
}
我试着做了一个计算电价的c语言程序,但是一行不行。
这是我的代码。
#include <stdio.h>
int main()
{
int usageElectric; //amount of electricity used
int basicMoney; //basic price
int totalMoney; //total price
double usageMoneyPerKw; //kw per used price
double totalFinalMoney; //final usage price
double tax; //tax
printf("put in the amount of electricity used (kw) : "); //put in 150kw.
scanf("%d", &usageElectric);
basicMoney = 660; //basic price = 0
usageMoneyPerKw = 88.5; //kw per usage price : .5
totalMoney = basicMoney + (usageElectric * usageMoneyPerKw);
tax = totalMoney * (9 / 100); //This line is the problem line = doesn't work
totalFinalMoney = totalMoney + tax;
printf("Tax is %d\n", tax); // a line to show that the tax isn't being caluculated properly
printf("The final usage price is %lf.", totalFinalMoney);
return 0;
}
如果输入为 150(kw),则 totalFinalMoney 应为 $15189.150000
任何人都可以帮我解决为什么这条线不起作用吗?
tax = totalMoney * (9 / 100);
如果正常运行,结果应该如下:
tax = 13935 * (9/100) = 1254.15
因此,最终结果应该是:
The final usage price is 15189.150000
在子表达式9/100
中,两个操作数都是整数,所以除法是整数除法,意味着任何小数部分都被截断,所以它的计算结果为0。
如果改为浮点常量,就会得到浮点除法。所以把上面的改成:
9.0/100.0
或者简单地说:
0.09
您只需像这样 ((double)9 / 100)
进行类型转换 (9/10)
。从现在开始,它正在将 9/10 的输出视为整数并将结果作为 0.
并且在打印 tax
时,您应该使用 %lf
而不是 %d
。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int usageElectric; //amount of electricity used
int basicMoney; //basic price
int totalMoney; //total price
double usageMoneyPerKw; //kw per used price
double totalFinalMoney; //final usage price
double tax; //tax
printf("put in the amount of electricity used (kw) : "); //put in 150kw.
scanf("%d", &usageElectric);
basicMoney = 660; //basic price = 0
usageMoneyPerKw = 88.5; //kw per usage price : .5
totalMoney = basicMoney + (usageElectric * usageMoneyPerKw);
tax = totalMoney * ((double)9 / 100); //This line is the problem line = doesn't work
totalFinalMoney = totalMoney + tax;
printf("Tax is %lf\n", tax); // a line to show that the tax isn't being caluculated properly
printf("The final usage price is %lf.", totalFinalMoney);
}