2017-06-22 8 views
1

だから私は現在、CS50とCを学んでいると私は現在、PSET1からgreedy problemをやって、このプログラムの目的は、ユーザーに出力する彼は彼が負っています変更を受け取ることになりますコインの最低量です。たとえば彼が32セントの変更を取り戻せば、合計4コインのために1クォーター、1ニッケル、2ペニーが得られます。私はエラーを取得しておくコインをカウントするモジュロ関数を使用した後、彼が受け取るコインの枚数をカウントして、トラブルのかなりのビットを持ってきました:無効なオペランドをバイナリ表現に(「ダブル」と「ダブル」)なぜ誰かが明確にしたり、コードを修正するのを助けることができないのか、私には分かりません。あなたがfmod使用する必要が誰かがこのコードのモジュロ関数がなぜ機能しないのか説明できますか?

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

''%演算子は整数のみのために定義されています。 –

+2

エラーメッセージをクリアすることができませんでした。 – Olaf

+0

@EugeneSh。整数型に整数型を好む? – Bathsheba

答えて

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

あなたが金種を確認し、それに応じて総コインをインクリメントしながら残りをデクリメントすることによって、各タイプの全コインの数を計算することができます。

+0

テストは冗長なので除算とモジュロ '1'です。インクルードファイルを追加し、コードをインデントし、 'main()'から 'return 0;'を返します。 – chqrlie

+0

'int cents;の結果です。 cents = cents%25; '常に〜[24〜24]の範囲になるでしょう。 – chux

+0

BTW:'セント=ラウンド(ドル* 100);は良いステップです。 'lround(dollars * 100.0)'も考慮してください。 – chux

0

%だけintに広がっintよりも小さいタイプの任意の引数を指定して、C言語で整数型に対して定義されています。誤解を避けるために

(。関心のうち、%は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; 
} 

参照してください。あなたはfmodを使用する必要がhttps://ideone.com/9Za4T9

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; 
} 
関連する問題