【发布时间】:2017-06-22 13:44:21
【问题描述】:
所以我目前正在用 CS50 学习 C 并且我目前正在从 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);
}
【问题讨论】:
-
%运算符仅用于整数。 -
错误信息再清楚不过了!
-
@EugeneSh。更喜欢“整数类型”而不是“整数”?
-
@Bathsheba port70.net/~nsz/c/c11/n1570.html#6.5.5p2 % 运算符的操作数应为整数类型。.
-
@Bathsheba 显然你是 :) 标准对两者都使用“整数类型”。