簡體   English   中英

有人可以解釋為什么此代碼中的模函數不起作用嗎?

[英]can someone explain why the modulo function in this code isn't working?

因此,我目前正在使用CS50學習C,並且目前正在處理pset1中的貪婪問題 ,該程序的目的是向用戶輸出他所欠的零錢數量最少:例如他將獲得32美分的找零,他將獲得四分之一硬幣,四分之一硬幣,1枚鎳幣和2便士硬幣。 在使用模函數對硬幣進行計數之后,我在計算他將要收到的硬幣數量時遇到了很多麻煩,但我不斷收到錯誤消息:二進制表達式(“ double”和“ double”)的無效操作數我不知道為什么,有人可以澄清和/或幫助我修復代碼嗎?

#include <stdio.h>
#include <math.h>

int main(void) {
    float coins;
    int quarters, dimes, nickles, pennies;

    // This part of the code prompts the user to input the amount of money that he's owed
    // making sure that the value entered is positive and bigger than 0 or else the 
    // program will reprompt the user for input
    do {
        printf("How much money are you owed?");
        coins = get_float();
    } while (coins <= 0.0);

    /* this is where the problem is, I'm trying to count the change given to the user with this
     formula but the compiler keeps telling me that there is something wrong with the modolo
     function that im using but im not sure what the problem is exactly */

    quarters = coins / 0.25;
    dimes = (coins % 0.25) / 0.10;
    nickles = ((coins % 0.25) % 0.10) / 0.05;
    pennies = ((coins % 0.25) % 0.10) % 0.05;

    int SumOfCoins = quarters + dimes + nickles + pennies;

    printf("%i\n", SumOfCoins);
}

雖然將fmod用作浮點數是正確的,但是我不確定為什么要將硬幣視為代碼中的浮點數。 硬幣的數量總是需要為整數,因為您不能擁有半個硬幣。

int main(void){
float dollars;
int cents;
int coins;

do{
    printf("O hai! How much change is owed?");
    dollars = get_float();
} while(dollars < 0);

cents = roundf(dollars * 100);

coins = cents / 25;
cents = cents % 25;

if (cents < 25){
    coins += (cents / 10);
    cents = cents % 10;
}

if (cents < 10){
    coins += (cents / 5);
    cents = cents % 5;
}

if (cents < 5){
    coins += (cents / 1);
    cents = cents % 1;
}

printf("%d\n", coins);
}

您可以通過檢查面額並減少余數,同時相應增加總硬幣數,來計算每種類型的總硬幣數。

您需要使用fmod%僅在C中為整數類型定義,所有類型小於int參數都將擴展為int

(出於興趣, %為Java中的浮點類型定義。)


為避免疑問, %定義了比int寬的類型:

#include <stdio.h>
#include <limits.h>

int main(void) {
    long i = LONG_MAX - 1;
    long j = LONG_MAX;
    long k = i % j;
    printf("%ld", k);
    return 0;
}

參見https://ideone.com/9Za4T9

您不應將%運算符與浮點值一起使用,因為它會計算整數除法的余數。 浮點模數函數是fmod() ,但是不建議使用浮點類型來處理美元和美分金額,因為它們的表示形式對於許多金額而言都不准確,會產生錯誤的結果。

相反,您應該將金額仔細轉換為整數,然后使用整數算術來計算硬幣的數量:

#include <stdio.h>
#include <cs50.h>

int main(void) {
    float amount;
    int cents, quarters, dimes, nickels, pennies, coins;

    do {
        printf("How much money are you owed?");
        amount = get_float();
    } while (amount <= 0.0);

    // compute the number of cents with proper rounding
    cents = (int)(amount * 100 + 0.5);

    quarters = cents / 25;
    cents %= 25;
    dimes = cents / 10;
    cents %= 10;
    nickels = cents / 5;
    cents %= 5;
    pennies = cents;

    coins = quarters + dimes + nickels + pennies;

    printf("%d coins: %d quarter, %d dimes, %d nickels, %d pennies\n",
           coins, quarters, dimes, nickels, pennies);
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM