用c语言编程找到变化计算

时间:2015-09-28 16:01:58

标签: c

实际上,它没有显示出正确的变化,而且total_price是相同的,而且它是负面的,而且loonies,分也有问题。 有谁能告诉我为什么?

int main()
{
    int items;
    int loonies=0;
    int cent=0;
    float hst=0.13;
    double change,tax, unit_price,purchase_price,total_price,cash_tendered;

    printf("enter the number of items:\n");
            scanf("%d",&items);
    printf("enter the unit_price:\n");
            scanf("%lf",&unit_price);
    printf("cash_tendered:\n");
            scanf("%.2lf",&cash_tendered);

    purchase_price=items*unit_price;
    tax=purchase_price*hst;
    total_price=purchase_price+tax;

    printf("price for items:%.2lf\n",purchase_price);
    printf("price for items with tax:%.2lf\n",total_price);

    change = cash_tendered - total_price;
    loonies=(int)change;
    cent=((change - loonies)*100);

    printf("change:%.2lf\n",change);
    printf("loonies:%d\n",loonies);
    printf("cent:%d\n",cent);

    return 0;
}

2 个答案:

答案 0 :(得分:1)

输入语句中有错误

scanf("%.2lf",&cash_tendered);

应该是

scanf("%lf",&cash_tendered);

使用printf %lf格式的double语句中也存在错误,%f格式应为scanfprintf请注意,float的格式与double的格式不同。

我也注意到,由于某种原因,您正在混合intint

在纠正这些之后,我的版本给出了1美分的错误更改。这可能是因为您正在处理可能导致舍入错误的实数。 tax=purchase_price*hst/100中的最佳作品。也可以使用int作为税率,并使用{{1}}计算。找到一种输入十进制数的方法,然后以最快的机会转换为{{1}}美分。

我会让你改进这个细节。

答案 1 :(得分:0)

  1. 代码没有充分启用编译器警告或OP正在使用弱编译器。以下显示了错误且更正的格式。

    // scanf("%.2lf", &cash_tendered);  // compiler showed warning
    scanf("%lf", &cash_tendered);
    
  2. 代码不会检查scanf()的结果。除非检查返回值,否则不会注意到简单错误,例如从格式无效的行读取失败。 (为简洁起见,下面的代码也没有。)

  3. 在进行任何计算(如税务计算)后,可能会产生非整数结果并需要更正为“便士”(最小单位),值应该四舍五入。

  4. 将货币转换为整数,如美分不应使用int转化,(int)强制转换,floor()truncate()。代码应首先使用round()

    // loonies=(int)change;
    // cent=((change - loonies)*100);
    long long change_cents = (long long) round(change * 100);
    int cent = change_cents % 100;
    long long loonie = change_cents / 100;
    
  5. 修改后的代码如下:

    double round_to_nearest(double x, unsigned unit) {
      return round(x*unit)/unit;
    }
    
    int main(void) {
      int items;
      // These 2 are not money
      double hst = 0.13;  // tax rate
      double unit_price;  // price/unit
      // Keep money type on separate line
      double change, tax, purchase_price, total_price, cash_tendered;
    
      printf("enter the number of items:\n");
      scanf("%d", &items);
    
      // Note that unit price could be fractional like $1.234 each
      printf("enter the unit_price:\n");
      scanf("%lf", &unit_price);
    
      printf("cash_tendered:\n");
      scanf("%lf", &cash_tendered);
      // Could add check that the cash entered in not fractional money
      if (cash_tendered != round_to_nearest(cash_tendered, 100)) 
        Handle_Error();
    
      purchase_price = round_to_nearest(items * unit_price, 100);
      tax = round_to_nearest(purchase_price * hst, 100);
      total_price = round_to_nearest(purchase_price + tax, 100);
    
      printf("price for items:%.2lf\n", purchase_price);
      printf("price for items with tax:%.2lf\n", total_price);
    
      change = round_to_nearest(cash_tendered - total_price, 100);
    
      // round change to the nearest whole number of cents
      long long change_cents = (long long) round(change * 100);
      int cent = change_cents % 100;
      long long loonie = change_cents / 100;
    
      printf("change:%.2lf\n", change);
      printf("loonies:%lld\n", loonie);
      printf("cent:%d\n", cent);
    
      return 0;
    }
    

    候选人简化:

    在内部使用全部最低钱单位(例如美分)作为 通过简单的加法/减法不需要舍入。

    OR

    如果十进制浮点可用,则使用该类型作为简单加法/减法不需要的舍入。