11

问题是:Totals 游戏可以由任意数量的人玩。它从总共 100 开始,然后每个玩家依次在 -20 到 20 之间进行整数位移。获胜者是调整使总数等于 5 的玩家。仅使用给定的三个变量: 总调整计数器 这是我目前所拥有的:

#include <stdio.h>
int main (void)
{
    int counter=0;
    float adj;
    int ttl=100;

    printf("You all know the rules now lets begin!!!\n\n\nWe start with 100. What is\n");

    while (ttl!=5)
    {
        printf("YOUR ADJUSTMENT?");
        scanf("%f",&adj);
        counter++;
        if (adj<=20 && adj>=-20)
        {
        ttl=ttl+adj;
        printf("The total is %d\n",ttl);
        }
        else
        {
        printf ("I'm sorry. Do you not know the rules?\n");
        }
    }
    printf("The game is won in %d steps!",counter);

}

我需要什么:输入十进制数字时,它会转到其他地方。如何确定浮点数是否有小数部分。

4

5 回答 5

24

You can cast the float to an int and then compare it to your original variable. If they are the same there was no fractional part.

By using this method, there is no need for a temporary variable or a function call.

  float adj;

  ....     

  if (adj == (int)adj)
    printf ("no fractional!\n");
  else
    printf ("fractional!\n");
  

Explanation

Since an int cannot handle fractions the value of your float will be truncated into an int (as an example (float)14.3 will be truncated into (int)14).

When comparing 14 to 14.3 it's obvious that they are not the same value, and therefore "fractional!" will be printed.

于 2011-12-18T02:14:21.790 回答
14
#include <stdio.h>
#include <math.h>

int main ()
{
  float param, fractpart, intpart;

  param = 3.14159265;
  fractpart = modff (param , &intpart);
  return 0;
}

http://www.cplusplus.com/reference/clibrary/cmath/modf/

modff找到小数部分,所以我想测试它是否等于0null会回答你的问题。

于 2011-12-18T02:13:58.063 回答
3

如果您想知道实数x是否没有小数部分,请尝试x==floor(x).

于 2011-12-18T02:13:05.860 回答
1

我只学习C,如果我错了,请告诉我。

但如果不是使用

scanf("%f",&adj);

如果您使用:

scanf("%d%d", &adj, &IsUndef);

因此,如果用户键入了除整数以外的任何内容,&IsUndef 将不等于 NULL,并且必须有一个小数部分将用户发送到 else。

也许。

于 2011-12-18T13:02:21.843 回答
0

使用scanf()是有问题的。如果用户在输入-5 +10 -15 -15的第一行输入,然后按回车,您将依次处理 4 个数字scanf()。这可能不是您想要的。此外,当然,如果用户键入+3 or more,则在读取空间后第一次转换停止,并且所有后续转换都在oor上失败or,并且代码进入循环。您必须检查返回值scanf()以了解它是否能够转换任何内容。

预读问题非常严重,我会选择使用准标准替代方法fgets()来读取一行数据,然后使用sscanf()(额外s的很重要)来解析一个数字。

要确定浮点数是否具有小数部分和整数部分,您可以使用modf()ormodff()函数 - 后者因为您adj是 a float

#include <math.h>

double modf(double x, double *iptr);
float modff(float value, float *iptr);

返回值是有符号的小数部分x;in 的值iptr是整数部分。请注意,modff()在不支持 C99 的编译器(运行时库)中可能不可用。在这种情况下,您可能必须使用doubleand modf()%d但是,限制用户输入具有格式和整数类型的整数可能很简单adj;这就是我从一开始就要做的。

另一个细节点:您真的要在尝试总数中计算无效数字吗?

#include <stdio.h>
#include <math.h>

int main(void)
{
    int counter=0;
    int ttl=100;

    printf("You all know the rules now lets begin!!!\n"
           "\n\nWe start with 100. What is\n");

    while (ttl != 5)
    {
        char  buffer[4096];
        float a_int;
        float adj;

        printf("YOUR ADJUSTMENT?");
        if (fgets(buffer, sizeof(buffer), stdin) == 0)
            break;
        if (sscanf("%f", &adj) != 1)
            break;
        if (adj<=20 && adj>=-20 && modff(adj, &a_int) == 0.0)
        {
            counter++;  // Not counting invalid numbers
            ttl += adj;
            printf("The total is %d\n", ttl);
        }
        else
        {
            printf ("I'm sorry. Do you not know the rules?\n");
        }
    }

    if (ttl == 5)
        printf("The game is won in %d steps!\n", counter);
    else
        printf("No-one wins; the total is not 5\n");
    return(0);
}

显然,我故意忽略了有人可能会在输入 return 之前输入超过 4095 个字符的可能性。

于 2011-12-18T02:42:15.487 回答