有人可以解释为什么这段代码中的模函数不起作用吗?
can someone explain why the modulo function in this code isn't working?
所以我目前正在用 CS50 学习 C,我目前正在做 pset1 的 greedy problem,这个程序的目的是向用户输出,他将收到的最少数量的硬币找回他所欠的零钱:例如,如果他要找回 32 美分的零钱,他将得到 1 夸特、1 镍和 2 便士,总共 4 个硬币。我在计算他将收到的硬币数量时遇到了很多麻烦,在使用模函数计算硬币后我不断收到错误:二进制表达式的无效操作数('double' 和 'double'),我不知道为什么,有人可以澄清一下 and/or 可能会帮助我修复代码吗?
#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
:%
仅针对 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;
}
模数运算符(“%”)计算执行整数除法所得的余数。
你需要使用fmod,定义在<math.h>
您不应将 %
运算符与浮点值一起使用,因为它会计算整数除法的余数。浮点模函数是 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;
}
虽然浮点数的 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);
}
您可以通过检查面额并减少余数同时相应地增加总硬币来计算每种类型的完整硬币数量。
所以我目前正在用 CS50 学习 C,我目前正在做 pset1 的 greedy problem,这个程序的目的是向用户输出,他将收到的最少数量的硬币找回他所欠的零钱:例如,如果他要找回 32 美分的零钱,他将得到 1 夸特、1 镍和 2 便士,总共 4 个硬币。我在计算他将收到的硬币数量时遇到了很多麻烦,在使用模函数计算硬币后我不断收到错误:二进制表达式的无效操作数('double' 和 'double'),我不知道为什么,有人可以澄清一下 and/or 可能会帮助我修复代码吗?
#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
:%
仅针对 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;
}
模数运算符(“%”)计算执行整数除法所得的余数。
你需要使用fmod,定义在<math.h>
您不应将 %
运算符与浮点值一起使用,因为它会计算整数除法的余数。浮点模函数是 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;
}
虽然浮点数的 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);
}
您可以通过检查面额并减少余数同时相应地增加总硬币来计算每种类型的完整硬币数量。