gpt4 book ai didi

c - 自动售货机 - 只接受硬币

转载 作者:太空宇宙 更新时间:2023-11-04 08:32:56 26 4
gpt4 key购买 nike

我正在做一个自动售货机模拟器。我只需要接受真正的硬币,例如(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(也试过这个导致 float ):

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 都不起作用...代码 2 接受 0.05 个硬币,但忽略其他的

最佳答案

通过声明一系列可接受的硬币,您的自动售货机上的现金槽可以轻松修改以接受 1 或 2 美分的硬币或 future 的 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;
}

关于c - 自动售货机 - 只接受硬币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27450800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com