C - While 循环中的 If 语句。连续扫描f()

Α

好吧,我希望这个函数将更改返回给我。而且我有限制,我不能插入所有类型的硬币。例如,如果我有givemeChange(0.50)我想插入钱,直到它们足以支付产品。我找不到解决办法。我在终端中插入 2 个数字,然后功能总是继续return 0.69

这是我的代码:

感谢您的时间

float givemeChange(float price){

printf("Only these coins are allowed:\n" );
printf("0.05€ 0.10€ 0.20€ 0.50€ 1€ 2€\n\n");

float str = 0 ;
float count = 0 ;
while(count <= price){

  printf("Insert coins\n" );
  scanf("%f\n",&str );

  if(str == 0.05){
    count = count + 0.05;
  }
  if(str == 0.10){
    count = count + 0.10;
  }
  if(str == 0.20){
    count = count + 0.20;
  }
  if(str == 0.50){
    count = count + 0.50;
  }
  if(str == 1){
    count = count + 1;
  }
  if(str == 2){
  count = count + 2;
  }
  else{
  return 0.69;
  }
 }
  return (price-count);
}
迈克尔·比尔

我猜你在比较时遇到了麻烦:在二进制系统中,对于分数,只能精确表示 2 的幂 -0.2不能在二进制系统中精确表示,因为在二进制中,它可能是一个永无止境的分数。你会在十进制中遇到相同的分数,如1/3大致表示0.33,但你永远无法将它精确地表示为十进制分数。因此,您可能很难进行比较==就像(在我们的十进制系统中)1.0 / 3.0 == 0.3333无论您在十进制处添加多少个 3,都永远不会为真。

而不是比较绝对值,您应该重新检查您输入的值是否足够接近您的目标值,如下所示:

...

float abs(float a) {return (a < 0) ? -a : a; }

const float epsilon = 0.005;

...

  printf("Insert coins\n" );
  scanf("%f",&str );

  if(abs(str - 0.05) < epsilon) {
      printf("Gave 0.05\n");
    count = count + 0.05;
  }

  if(abs(str - 0.10) < epsilon) {
      printf("Gave 0.10\n");
     count = count + 0.10;
  }

  ...

但是,对于您的问题,将您的值作为字符串读入可能会更容易(并且可以),然后您可以将它们strcmp与预期值进行比较并适当地对待它们,如下所示:

 char  input[100];

 ...

 scanf("%100s", input);
 input[99] = '\0';

 if(0 == strcmp(input, "0.05")) {
    printf("You gave 0.05\n");
    count += 0.05;
 }

 /* and so on with the other options */

如果您还想接受诸如此类.5输入,则必须编写自己的比较函数。

解决方案是您的选择,只是为了完整性,这里有一个紧凑的解决方案,可以立即编译 - 使用一种查找表来防止所有这些if's输入...:

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

float givemeChange(float price){

    struct {const char* input; float value;} input_map[] =
    {
        {"0.05", 0.05},
        {"0.10", 0.10},
        {"0.20", 0.20},
        {"0.50", 0.5}
    };

    printf("Only these coins are allowed:\n" );
    printf("0.05€ 0.10€ 0.20€ 0.50€ 1€ 2€\n\n");

    char input[100] = {0};
    float  count = 0 ;
    while(count <= price){

        printf("Insert coins\n" );
        scanf("%100s",input );
        input[99] = 0;

        for(size_t i = 0; i < sizeof(input_map) / sizeof(input_map[0]); ++i) {

            if(0 == strcmp(input_map[i].input, input)) {
                printf("Gave %s\n", input_map[i].input);
                count += input_map[i].value;
                break;
            }

        }

    }

    return count - price;
}


int main(int argc, char** argv) {

    printf("Your change %f\n", givemeChange(0.5));

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

while循环中的布尔语句C#

来自分类Dev

if语句在while循环中?

来自分类Dev

While 循环中的 if 语句

来自分类Dev

while循环中的扫描仪

来自分类Dev

在while循环中使用strcpy,C

来自分类Dev

从while循环中存储新值?C ++

来自分类Dev

C程序在do while循环中崩溃

来自分类Dev

While循环中的C ++分段错误

来自分类Dev

在 c 中的 do-while 循环中扫描两个变量

来自分类Dev

MySQLi在while循环中准备的语句

来自分类Dev

在while循环中使用if语句

来自分类Dev

PHP While循环中的多个SQL If语句

来自分类Dev

Python:在“ while”循环中嵌套“ If”语句吗?

来自分类Dev

使用if语句时卡在while循环中

来自分类Dev

Java:在while循环中嵌套if / else语句

来自分类Dev

While循环中的Java If与Python if语句

来自分类Dev

if语句在while循环中忽略列表

来自分类Dev

Python忽略while循环中的if语句

来自分类Dev

如何在while循环中输入if语句?

来自分类Dev

Else 语句无法在 while 循环中工作

来自分类Dev

为什么 if 语句在 while 循环中阻塞?

来自分类Dev

F#从复杂语句的while循环中打破

来自分类Dev

While循环中的For循环

来自分类Dev

扫描仪对象在While循环中给我错误

来自分类Dev

在while循环中进行扫描仪输入验证

来自分类Dev

C#-循环中的语句定位

来自分类Dev

foreach循环中的if语句c#

来自分类Dev

在while循环中读取C中的整数后,scanf被跳过

来自分类Dev

while循环中的随机数[C编程]