2017-06-22 46 views
1

因此,我目前正在學習C與CS50,我目前正在從pset1做greedy problem,這個程序的目的是向用戶輸出他將爲他所欠的變化而收到的最少數量的硬幣:例如如果他獲得32美分的變化,他將獲得1個季度,1個鎳和2個便士,總共4個硬幣。在使用模函數來計算我繼續收到錯誤的硬幣之後,我一直在計算他將接收到的硬幣數量時遇到了一些麻煩:對二進制表達式的無效操作數('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); 
} 
+1

「%」運算符僅用於整數。 –

+2

錯誤信息不能更清晰! – Olaf

+0

@EugeneSh。更喜歡「整型」到「整數」? – Bathsheba

回答

1

雖然這是正確的,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); 
} 

您可以通過檢查面額並在相應增加總金額的同時減少餘數來計算每種類型的整個硬幣的數量。

+0

測試是多餘的,分割和模「1」也是多餘的。添加包含文件,從'main()'中縮進你的代碼和'return 0;'。 – chqrlie

+0

int int結果; .....美分=美分%25;'會一直在範圍內[-24 ... 24] – chux

+0

順便說一句:'美分= roundf(美元* 100);'是一個好的一步。還要考慮'lround(美元* 100.0)'。 – chux

0

您需要使用fmod%只對整型在C中定義,用類型比擴大到了intint較小的任何參數。

(出於興趣,%用於浮點類型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

0

您不應該使用帶有浮點值的%運算符,因爲它計算整數除法的餘數。浮點模數函數爲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; 
} 
相關問題