自动售货机 - 只接受硬币

时间:2014-12-12 19:32:26

标签: c

我正在做自动售货机模拟器。我只需要接受真正的硬币(0.05€/ 0.10€/ 0.20€等......)我不想要0.25欧元。

代码1:

if(coin!= 0.05||  coin!= 0.10 || coin!= 0.20 || coin!= 0.50 || coin!= 1 || coin!= 2 )
        printf("It isnt a coin");
    else
        return coin;

code2(尝试这也是因为flots):

if((aux-0.05 >= 0.001) ||  (aux-0.10 >= 0.0001)||(aux-0.20 >= 0.0001)||(aux-0.50 >= 0.0001)||(aux-1 >= 0000.1)||(aux-2 >= 0000.1))
        printf("It isnt a coin");
    else
        insert_value += aux;

代码1或2都不起作用...... code2接受0.05硬币,但忽略其他代码

2 个答案:

答案 0 :(得分:1)

你需要在你的子句中使用AND而不是OR。

例如:coin != 1 || coin != 2 总是为真。

答案 1 :(得分:1)

通过宣布一系列可接受的硬币,您可以轻松修改自动售货机上的现金槽以接受1或2美分硬币或未来5欧元硬币。

#include <stdio.h>
#include <stdlib.h>

int acceptable [] = {5, 10, 20, 50, 100, 200};

// return 1 if acceptable coin
int test_coin (int cents) {
    int num_accept = sizeof(acceptable) / sizeof(int);  
    int index;
    for (index=0; index<num_accept; index++)
        if (cents == acceptable[index])
            return 1;
    printf ("Unacceptable coin\n");
    return 0;
    }

int main()
{
    int cents;
    char str [10];
    do {
        printf ("Enter coin value in cents ");
        *str = 0;
        fgets (str, 10, stdin);
        cents = atoi(str);
    } while (!test_coin(cents));
    printf("You inserted %d cents coin\n", cents);
    return 0;
}
相关问题