Error: invalid operands to binary expression ('float' and 'float')
Error: invalid operands to binary expression ('float' and 'float')
如果之前有人问过这个问题,我深表歉意。我环顾四周,找不到解决方案,我是 C 的新手。
我知道我无法从浮动中获得 %。如果我使用 2 个浮点数,我将如何捕获这个数学的剩余部分?
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
/*
** Always use the largest coin possible
** keep track of coins used
** Print the final amount of coins
*/
int main (void)
{
float change;
int counter = 0;
int division;
//float rem;
float quarter = 0.25;
//float quarter = 0.25, dime = 0.10, nickel = 0.05, penny = 0.01;
/* Prompt user for an amont of change*/
do{
printf("How much do we owe you in change? ");
change = GetFloat();
}
while (change <= 0);
if (change >= quarter)
{
division = (change / quarter);
counter += division;
//change = (int)(change % quarter);
printf("change: %.2f\n", change);
printf("counter: %d\n ", counter);
}
return (0);
}
您可能需要查看
fmod.
你也可以这样做 change = change - (int)(change / quarter) * quarter
只需将所有变量放大 100,然后使用整数而不是浮点数。
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
/*
** Always use the largest coin possible
** keep track of coins used
** Print the final amount of coins
*/
int main (void)
{
float change_f;
int change;
int counter = 0;
int division;
//float rem;
int quarter = 25;
//int quarter = 25, dime = 10, nickel = 5, penny = 1;
/* Prompt user for an amont of change*/
do{
printf("How much do we owe you in change? ");
change_f = GetFloat();
}
while (change_f <= 0);
change = (int)(change_f*100);
if (change >= quarter)
{
division = (change / quarter);
counter += division;
//change = (int)(change % quarter);
printf("change: %.2f\n", change_f);
printf("counter: %d\n ", counter);
}
return (0);
}
注意:根据输入精度选择比例因子,即如果是3位小数则选择1000等。
您可以自己实现模数:
https://en.wikipedia.org/wiki/Modulo_operation
int a = (int)(change / quarter);
int mod = (int)(change - (quarter * a));
也可以这样做:
long mod = ((long)(change * 1000) % (long)(quater * 1000));
根据浮点数的精度修改 1000 并考虑将结果除以 1000!
但也许最好重新考虑一下您真正想要的结果是什么?
如果之前有人问过这个问题,我深表歉意。我环顾四周,找不到解决方案,我是 C 的新手。 我知道我无法从浮动中获得 %。如果我使用 2 个浮点数,我将如何捕获这个数学的剩余部分?
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
/*
** Always use the largest coin possible
** keep track of coins used
** Print the final amount of coins
*/
int main (void)
{
float change;
int counter = 0;
int division;
//float rem;
float quarter = 0.25;
//float quarter = 0.25, dime = 0.10, nickel = 0.05, penny = 0.01;
/* Prompt user for an amont of change*/
do{
printf("How much do we owe you in change? ");
change = GetFloat();
}
while (change <= 0);
if (change >= quarter)
{
division = (change / quarter);
counter += division;
//change = (int)(change % quarter);
printf("change: %.2f\n", change);
printf("counter: %d\n ", counter);
}
return (0);
}
您可能需要查看 fmod.
你也可以这样做 change = change - (int)(change / quarter) * quarter
只需将所有变量放大 100,然后使用整数而不是浮点数。
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
/*
** Always use the largest coin possible
** keep track of coins used
** Print the final amount of coins
*/
int main (void)
{
float change_f;
int change;
int counter = 0;
int division;
//float rem;
int quarter = 25;
//int quarter = 25, dime = 10, nickel = 5, penny = 1;
/* Prompt user for an amont of change*/
do{
printf("How much do we owe you in change? ");
change_f = GetFloat();
}
while (change_f <= 0);
change = (int)(change_f*100);
if (change >= quarter)
{
division = (change / quarter);
counter += division;
//change = (int)(change % quarter);
printf("change: %.2f\n", change_f);
printf("counter: %d\n ", counter);
}
return (0);
}
注意:根据输入精度选择比例因子,即如果是3位小数则选择1000等。
您可以自己实现模数:
https://en.wikipedia.org/wiki/Modulo_operation
int a = (int)(change / quarter);
int mod = (int)(change - (quarter * a));
也可以这样做:
long mod = ((long)(change * 1000) % (long)(quater * 1000));
根据浮点数的精度修改 1000 并考虑将结果除以 1000!
但也许最好重新考虑一下您真正想要的结果是什么?