-2

这是终端给出的错误:

greedy.c:26:21: error: invalid operands to binary expression
      ('float' and 'float')
        float x = x % q;
                  ~ ^ ~
1 error generated.

从以下代码:

#include <stdio.h>
#include <cs50.h>
#include <math.h>

int main(void) 
{
    float x;
    float y = 0;
    float q = 25.0;
    float d = 10.0;
    float n = 5.0;
    float p = 1.0;

    printf("How much I owe you? Enter here: ");

    x = GetFloat();

    while (x <= 0)
    {
        printf("Please enter the sum with a decimal point (e.g. .50; 1.37): ");
        x = GetFloat();
    } 

    if (x > 25.0) 
    {
        float x = x % q;
        y++;
    }

    printf("The modulo of %f and the coins used: %f\n", x, y);

}

我想你对CS50的“贪婪”任务很熟悉。我需要得到剩余的x. 也许我在教程中遗漏了一些东西,但我认为他们没有指定如何使用模%运算符。

4

1 回答 1

3

模运算符仅适用于整数类型。您想使用该fmodf功能。

于 2014-06-25T13:11:01.543 回答