因此,我目前正在學習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);
}
「%」運算符僅用於整數。 –
錯誤信息不能更清晰! – Olaf
@EugeneSh。更喜歡「整型」到「整數」? – Bathsheba