0

このプログラムを実行したところ、エラー式の結果が未使用になりました。私は何か単純な間違いをしているかもしれませんが、それを理解しようとして一日を費やしましたが、役に立ちませんでした. あなたが提供できるどんな助けも大歓迎です。

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

int main()
{
    int x, y = 0;
    printf("Enter the amount of change ");
    x = GetFloat() * 100;
    while (x != 0)
    {
        if (x >= 25)
        {
            x - 25;
            y = y + 1;
        }
        if (x >= 10 && x < 25)
        {
            x - 10;
        }   y = y + 1;
        if (x >= 5 && x < 10)
        {
            x - 5;
        }   y = y + 1;
        if (x >= 1 && x < 5)
        {   x - 1;
            y= y + 1;
        }
    }
    printf("The number of coins neccessary is %d", y);
}
4

3 に答える 3

3
    if (x >= 25)
    {
        x - 25;               // This accomplishes nothing
        y = y + 1;
    }
    if (x >= 10 && x < 25)
    {
        x - 10;               // This accomplishes nothing
    }   y = y + 1;
    if (x >= 5 && x < 10)
    {
        x - 5;                // This accomplishes  nothing
    }   y = y + 1;
    if (x >= 1 && x < 5)
    {
        x - 1;                // This accomplishes nothing
        y= y + 1;
    }

これらの各行では、 から数値を減算してxいますが、結果に対して何もしていません。結果を更新しようとしているx場合は、 で行っているのと同じようにして、式の前にy置く必要があります。x =

xしたがって、まで下がりたい場合は25、次のように記述します。

x = x - 25; 

別の方法として、次の省略形を書くこともできます。

x -= 25;     // Note the equal sign 
于 2014-03-16T05:03:16.157 に答える
0

4 つのステートメント x - 25、x- 10、x- 5、x - 1 はすべて、その値を x に割り当てない限り役に立たないことが判明します。x から値を減算しようとしているが、新しい値を x に割り当てていないためです。

問題の解決策は次のとおりです。

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

int main()
{
    int x, y = 0;
    printf("Enter the amount of change ");
    x = GetFloat() * 100;
    while (x != 0)
    {
        if (x >= 25)
        {
            x = x - 25;   //or x-=25;
            y = y + 1;
        }


        if (x >= 10 && x < 25)
        {
            x = x - 10;   //or x-=10;
            y = y + 1;
        }
        if (x >= 5 && x < 10)
        {
            x = x - 5;    //or x-=5;
            y = y + 1;
        }
        if (x >= 1 && x < 5)
        {
            x = x - 1;     //or x-=1; or x--; or --x; :)
            y = y + 1;
        }
   }
   printf("The number of coins neccessary is %d", y);
}
于 2014-03-16T05:13:09.027 に答える
0

私はあなたのループ構造について確信を持っています。効果的に使用できる除算演算子があります。

int total = 0;
int ncoins;
int amount = GetFloat() * 100;

assert(amount >= 0);

ncoins = amount / 25;
total += ncoins;
amount -= ncoins * 25;

assert(amount < 25);
ncoins = amount / 10;
total += ncoins;
amount -= ncoins * 10;

assert(amount < 10);
ncoins = amount / 5;
total += ncoins;
amount -= ncoins * 5;

assert(amount < 5);
total += amount;

それは手書きで書かれています。ループを考案することもできます:

int values[] = { 25, 10, 5, 1 };
enum { N_VALUES = sizeof(values) / sizeof(values[0]) };

int total = 0;
int ncoins;
int amount = GetFloat() * 100;

assert(amount >= 0);

for (int i = 0; i < N_VALUES && amount > 0; i++)
{
    ncoins = amount / values[i];
    total += ncoins;
    amount -= ncoins * values[i];
}

assert(amount == 0);
于 2014-03-16T06:41:21.307 に答える