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

C语言找零程序显示金额后终止运行求助(Java转C新手)

Hey there! Let's figure out why your change-making program is freezing up after displaying the tendered amount. As someone who's jumped between Java and C before, I totally get how easy it is to hit language-specific quirks. Let's break down each version's issues and fix them step by step:

Key Issues in Each Version

Version 1

  1. Floating-Point Precision Disaster
    Using double for currency is a huge red flag. Binary floats can't represent decimal values like 0.1 exactly, so your while(changeDue != 0.00) loop will never exit—changeDue will never be perfectly 0. This is why your program hangs.
  2. Backwards Condition Logic
    Your if(changeDue <= 100.00) check is reversed! You want to subtract a bill/coin only if changeDue is greater than or equal to its value. For example, if changeDue is 50, subtracting 100 would make it negative, leading to endless incorrect calculations.
  3. Useless For Loop
    The loop for(int i=0;i<changeDue;i++) makes no sense here: i is an integer, so if changeDue is less than 1 (like 0.75), the loop won't run at all. Plus, checking i == 0.25 is impossible—i is an int, it can never equal a decimal.

Version 2

  1. Type Mismatch in Input
    You declared price and tendered as double, but used scanf("%d", &price)—%d is for integers, not doubles. This corrupts memory and causes undefined behavior (which can lead to crashes or hangs).
  2. Incorrect Output
    printf("%d", &tendered); passes the address of tendered instead of its value. Printing an address with %d is invalid and will output garbage, plus this contributes to the program's unstable state.

Version 3

  1. Same Floating-Point Problem
    Switching to float doesn't fix the precision issue—you'll still get an infinite loop when checking changeDue != 0.
  2. Wrong Types for Coin Counts
    Variables like toonoe and quarter count the number of coins/bills, which are integers. Declaring them as float is unnecessary and can lead to weird rounding errors.

Fixed Version (Using Integer Currency for Precision)

The standard fix for currency in C is to work in cents (use integers) instead of dollars. This eliminates all floating-point precision issues. Here's a corrected implementation:

#include <stdio.h>

int main(void) {
    int priceCents;  // Total price in cents
    int tenderedCents;  // Amount received in cents
    int changeDue;

    // Bill/coin counts (all integers)
    int hundred = 0, twenty = 0, ten = 0, five = 0;
    int toonie = 0, loonie = 0;
    int quarter = 0, dime = 0, nickel = 0, penny = 0;

    printf("Enter total purchase price (dollars.cents): ");
    // Read as dollars and cents, convert to total cents
    int dollars, cents;
    scanf("%d.%d", &dollars, &cents);
    priceCents = dollars * 100 + cents;

    printf("Enter amount received (dollars.cents): ");
    scanf("%d.%d", &dollars, &cents);
    tenderedCents = dollars * 100 + cents;

    changeDue = tenderedCents - priceCents;
    if (changeDue < 0) {
        printf("Insufficient amount received!\n");
        return 1;
    }

    // Calculate bills
    hundred = changeDue / 10000;  // $100 = 10000 cents
    changeDue %= 10000;

    twenty = changeDue / 2000;  // $20 = 2000 cents
    changeDue %= 2000;

    ten = changeDue / 1000;
    changeDue %= 1000;

    five = changeDue / 500;
    changeDue %= 500;

    // Calculate coins (CAD)
    toonie = changeDue / 200;  // $2 = 200 cents
    changeDue %= 200;

    loonie = changeDue / 100;
    changeDue %= 100;

    quarter = changeDue / 25;
    changeDue %= 25;

    dime = changeDue / 10;
    changeDue %= 10;

    nickel = changeDue / 5;
    changeDue %= 5;

    penny = changeDue;

    // Print change breakdown
    printf("\nChange Breakdown:\n");
    if (hundred > 0) printf("%d x $100 bills\n", hundred);
    if (twenty > 0) printf("%d x $20 bills\n", twenty);
    if (ten > 0) printf("%d x $10 bills\n", ten);
    if (five > 0) printf("%d x $5 bills\n", five);
    if (toonie > 0) printf("%d x $2 toonies\n", toonie);
    if (loonie > 0) printf("%d x $1 loonies\n", loonie);
    if (quarter > 0) printf("%d x 25¢ quarters\n", quarter);
    if (dime > 0) printf("%d x 10¢ dimes\n", dime);
    if (nickel > 0) printf("%d x 5¢ nickels\n", nickel);
    if (penny > 0) printf("%d x 1¢ pennies\n", penny);

    return 0;
}

Why This Works:

  • Using integers for cents avoids all floating-point precision errors.
  • We use division and modulus to count each bill/coin efficiently (no messy loops).
  • Input is read as dollars and cents to make it user-friendly, then converted to total cents.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:10:04