我试着用c语言编写一个电价计算程序,但有一行不行';行不通

我试着用c语言编写一个电价计算程序,但有一行不行';行不通,c,line,C,Line,我试着用c语言编写一个电价计算程序,但有一行不行 这是我的密码 #include <stdio.h> int main() { int usageElectric; //amount of electricity used int basicMoney; //basic price int totalMoney; //total price double usageMoneyPerKw; //kw per u

我试着用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 = $660
    usageMoneyPerKw = 88.5; //kw per usage price : $88.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;
}
如果工作正常,应得出如下结果:

tax = 13935 * (9/100) = 1254.15
因此,最终结果应该是:

The final usage price is 15189.150000

在子表达式
9/100
中,两个操作数都是整数,因此除法是整数除法,这意味着任何小数部分都被截断,因此其计算结果为0

如果更改为浮点常量,则会得到浮点除法。因此,将上述内容更改为:

9.0/100.0
或者简单地说:

0.09

您只需像这样键入
(9/10)
((双)9/100)。从现在起,它将9/10的输出视为整数,并将结果视为0

在打印
tax
时,您应该使用
%lf
istead of
%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 = $660
    usageMoneyPerKw = 88.5; //kw per usage price : $88.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);
}
#包括
使用名称空间std;
int main()
{
int usageElectric;//使用的电量
int basicMoney;//基本价格
int totalMoney;//总价
双重使用MoneyPerkw;//kw/每使用价格
双倍总最终金额;//最终使用价格
双重税;//税
printf(“输入用电量(kw):”;//输入150kw。
scanf(“%d”和usageElectric);
基本货币=660;//基本价格=660美元
usageMoneyPerKw=88.5;//kw/每使用价格:$88.5
totalMoney=基本货币+(USAGEEElectric*usageMoneyPerKw);
tax=totalMoney*((double)9/100);//这行是问题行=不起作用
totalFinalMoney=总货币+税收;
printf(“Tax是%lf\n”,Tax);//一行显示未正确计算税款
printf(“最终使用价格为%lf.”,总最终金额);
}

9/100
采用整数数学计算;强制其中至少一个为浮点,例如
(9.0/100.0)
,这样整个计算都是在浮点中完成的。它应该是
tax=(totalMoney*9)/100.0您真的不应该使用浮点数来表示货币金额。您应该改为使用整数。这是否回答了您的问题?好的,我明白你的意思了,这是无意的打字错误。
#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 = $660
    usageMoneyPerKw = 88.5; //kw per usage price : $88.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);
}