【问题标题】:can someone explain why the modulo function in this code isn't working?有人可以解释为什么这段代码中的模函数不起作用吗?
【发布时间】: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 显然你是 :) 标准对两者都使用“整数类型”。

标签: c cs50


【解决方案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);
}

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

【讨论】:

  • 测试是多余的,除法和模1也是多余的。添加包含文件,从main() 缩进你的代码和return 0;
  • int cents; ..... cents = cents % 25; 的结果_总是在 [-24 ... 24] 范围内
  • 顺便说一句:cents = roundf(dollars * 100); 是一个很好的步骤。也可以考虑lround(dollars * 100.0)
  • 感谢 cmets。我会考虑它们来改进我的代码。
  • 你的回答真的很有趣,我想说你解决问题的方法比我的方法简单得多;以一种好的方式!但是,您忘记考虑一个侧面案例,以我有限的知识很难解决,有没有办法对用户输入设置验证规则,以防止添加超过 2 dp 的数字。?
【解决方案2】:
【解决方案3】:

您需要使用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;
}

https://ideone.com/9Za4T9

【讨论】:

    【解决方案4】:

    您不应将% 运算符与浮点值一起使用,因为它会计算整数除法的余数。浮点模数函数是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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-07-26
      • 2016-06-01
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多