You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++金额转纸币硬币程序求助:小数金额与模运算处理问题

问题描述

我有一项作业任务,要编写程序实现用户输入金额后,输出所需纸币和硬币的数量,但在存储找零及获取所需找零金额时遇到了麻烦。我知道不能用整数存储硬币金额,但用double类型的话,又会出现%模运算不能用于double的错误。现在想请教,处理完纸币部分后,怎么用double类型提取剩余金额?当前程序输入456.56时,能正确输出4张百元、2张20元、1张10元等纸币,但没法处理0.56的小数部分。以下是我当前的C++代码:

/*This program will convert the amount of money entered by the user into the amount of bills and change*/

#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
    const int HUNDRED = 100;
    const int TWENTY = 20;
    const int TEN = 10;
    const int FIVE = 5;
    const int DOLLAR = 1;
    const int QUARTER = 25;
    const int DIME = 10;
    const int NICKEL = 05;
    const int PENNY = 01;
    int changeAmount; 

    cout << "Enter amount of money to convert: $";
    cin >> changeAmount;

    cout << "\n";
    
    cout << "Numbe of 100 dollar bills: " << (int)changeAmount / HUNDRED << endl;
    changeAmount = changeAmount % HUNDRED;

    cout << "Numbe of 20 dollar bills: " << (int)changeAmount / TWENTY << endl;
    changeAmount = changeAmount % TWENTY;

    cout << "Numbe of 10 dollar bills: " << (int)changeAmount / TEN << endl;
    changeAmount = changeAmount % TEN;

    cout << "Numbe of 5 dollar bills: " << (int)changeAmount / FIVE << endl;
    changeAmount = changeAmount % FIVE;

    cout << "Numbe of 1 dollar bills: " << (int)changeAmount / DOLLAR << endl;
    changeAmount = changeAmount % DOLLAR;

    cout << "Numbe of Quarters: " << (int)changeAmount / QUARTER << endl;
    changeAmount = changeAmount % QUARTER;

    cout << "Numbe of Dimes: " << (int)changeAmount / DIME << endl;
    changeAmount = changeAmount % DIME;

    cout << "Numbe of Nickles: " << (int)changeAmount / NICKEL << endl;
    changeAmount = changeAmount % NICKEL;

    cout << "Numbe of Pennies: " << (int)changeAmount / PENNY << endl;
    changeAmount = changeAmount % PENNY;

    return 0;
}

解决思路

处理货币金额最稳妥的方式是把所有金额转换成整数(以分为单位),完全避免浮点数精度问题。因为浮点数(比如double)存储十进制小数时会有精度丢失(比如0.1无法精确表示),导致计算错误。具体步骤:

  • 将用户输入的金额乘以100,转换为整数分,这样所有计算都用整数完成,模运算%也能正常使用。
  • 纸币部分按“元”对应的分来计算(比如100元=10000分),硬币部分直接按分的面值计算。
  • 修正原代码中的拼写错误(Numbe→Number)和不规范的八进制数写法(05→5,01→1)。

修改后的完整代码

/*This program will convert the amount of money entered by the user into the amount of bills and change*/

#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
    // 所有面值以分为单位
    const int HUNDRED = 10000;   // 100元 = 10000分
    const int TWENTY = 2000;     // 20元 = 2000分
    const int TEN = 1000;        // 10元 = 1000分
    const int FIVE = 500;        // 5元 = 500分
    const int DOLLAR = 100;      // 1元 = 100分
    const int QUARTER = 25;      // 25分
    const int DIME = 10;         // 10分
    const int NICKEL = 5;        // 5分
    const int PENNY = 1;         // 1分

    double inputAmount;
    cout << "Enter amount of money to convert: $";
    cin >> inputAmount;

    // 转换为整数分,加0.5避免浮点数精度误差(比如456.56*100可能得到45655.99999999999)
    int totalCents = static_cast<int>(inputAmount * 100 + 0.5);

    cout << "\n";
    
    cout << "Number of 100 dollar bills: " << totalCents / HUNDRED << endl;
    totalCents = totalCents % HUNDRED;

    cout << "Number of 20 dollar bills: " << totalCents / TWENTY << endl;
    totalCents = totalCents % TWENTY;

    cout << "Number of 10 dollar bills: " << totalCents / TEN << endl;
    totalCents = totalCents % TEN;

    cout << "Number of 5 dollar bills: " << totalCents / FIVE << endl;
    totalCents = totalCents % FIVE;

    cout << "Number of 1 dollar bills: " << totalCents / DOLLAR << endl;
    totalCents = totalCents % DOLLAR;

    cout << "Number of Quarters: " << totalCents / QUARTER << endl;
    totalCents = totalCents % QUARTER;

    cout << "Number of Dimes: " << totalCents / DIME << endl;
    totalCents = totalCents % DIME;

    cout << "Number of Nickels: " << totalCents / NICKEL << endl;
    totalCents = totalCents % NICKEL;

    cout << "Number of Pennies: " << totalCents / PENNY << endl;
    totalCents = totalCents % PENNY;

    return 0;
}

关键说明

  • 用double接收用户输入后,立即转换为整数分,通过+0.5解决浮点数精度问题(比如0.56*100可能计算为55.99999999999999,加0.5后转为整数就是56)。
  • 所有面值统一用分作为单位,全程用整数运算,彻底避免模运算不能用于浮点数的问题,同时解决了小数部分无法处理的问题。

内容的提问来源于stack exchange,提问作者needusername

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 02:15:39