2016-05-26 25 views
0

この質問が以前に聞かれた場合はお詫び申し上げます。私は周りを見回して解決策を見つけることができませんでした。私はCには新しいです。 私はフロートから%を得ることができないことを理解しています。私が2つの浮動小数点数を使用している場合、私はこの数学の残りをどのようにキャプチャすることができますか?エラー:バイナリ式のオペランドが無効です( 'float'および 'float')

#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); 
} 

答えて

5

あなたは fmodをチェックすることもできます。

また、単に100で、すべての変数をスケールアップchange = change - (int)(change/quarter) * quarter

+0

ありがとうございましたゲーリー、それはトリックでした、私はまた、あなたのrecomendationとこの変更= fmod(変更、四半期)ごとにfmodをチェックしました。動作します!再度、感謝します。 – Lucky500

0

ような何かをして、代わりにフロートの整数を使用することができます。

#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型)(変更/四半期);:

2

あなたは自分自身を法を実装することができ int mod =(int)(変更 - (四半期* a));

また、このようにそれを行うことは可能かもしれません:=

長いMOD()(ロング(変化* 1000)%(ロング)(QUATER * 1000));

あなたのフロートの精度によって1000を変更し、結果を1000で割ることを考えてください!

しかし、結果として本当に欲しいものを考え直すほうがいいでしょうか?

関連する問題