谁能解释为什么这段代码中的模运算功能不起作用?

时间:2017-06-22 13:44:21

标签: c cs50

所以我目前正在用CS50学习C,我目前正在从pset1进行greedy problem,这个程序的目的是输出给用户,他将收到的最少量的硬币用于改变他的欠款:例如,如果他获得32美分的回报,他将获得1个季度,1个镍和2个便士,总计4个硬币。在使用模数函数来计算我不断收到错误的硬币之后,我计算他将收到的硬币数量时会遇到很多麻烦:无效操作数到二进制表达式(' double&# 39;以及' 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);
}

4 个答案:

答案 0 :(得分:1)

The modulus operator ('%'), computes the remainder that results from performing integer division. 您需要使用void recorroHorizontal(){ for(int i=0;i<480;i++){ colorAux=enHSB.at<Vec3b>(0,i); //Cuidado con el acceso al revés int punto_anterior = (int)(colorAux[2]); for(int j=1;j<640-1;j++){ colorAux=enHSB.at<Vec3b>(i,j); //al reves j e i float brightness=colorAux[2]; int brightnessi=(int)(brightness); h_pendientes[i][j]=brightnessi- punto_anterior; if(!(h_pendientes[i][j]>VALOR_PENDIENTE || h_pendientes[i][j]<-VALOR_PENDIENTE)){ if(!(h_pendientes[i][j] + h_pendientes[i][j-1] >VALOR_PENDIENTE_TRUNCAR || h_pendientes[i][j] + h_pendientes[i][j+1]<-VALOR_PENDIENTE_TRUNCAR)){ h_pendientes[i][j]=0; } } if(j<2 || i<2 || 640-1 ==i){h_pendientes[i][j]=0;} punto_anterior=brightnessi; original[i][j]=brightnessi; } } }

中定义的fmod

答案 1 :(得分: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);
}

您可以通过检查面额并减少剩余部分来计算每种类型的整个硬币数量,同时相应地增加总硬币。

答案 2 :(得分:0)

您需要使用fmod%仅针对C中的整数类型定义,其中任何类型小于int的参数扩展为int。< / p>

(出于兴趣,%是为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

答案 3 :(得分: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;
}