处理int,char,float

时间:2015-10-17 03:06:59

标签: c input scanf

我正在尝试编写一个程序,循环要求用户连续输入float,int或char并将它回显给它们,直到它们进入'q',然后循环结束。我不明白在进入循环之前如何在int,char或float之间进行解密。我已经尝试过if(scanf(“%c”,ch))等等,这对于float和int来说效果很好,但是一旦我添加了循环就搞砸了我。我尝试了几种不同的组合,但我还是没有找到答案 这是一次尝试向您展示我正在尝试做的事情:

    char ch;
    int num = 0;
    float fl = 0;

    printf("Enter a value: ");
        while(ch != 'q') {
            if (scanf("%c", &ch) && !isdigit(ch)) {
                printf("You entered a character %c\n", ch);
            }
            else if (scanf("%d", &num)) }
                printf("You entered an integer %d\n", num);
            }
            else if (scanf("%d", &num)) {
                printf("You entered a floating point number %f\n", fl);
            }
        printf("Enter another value: ");
      }
   }

这一直在做一些奇怪的事情,我无法查明我的问题。提前谢谢!

1 个答案:

答案 0 :(得分:1)

用你的方法无法做到这一点。您可以扫描一行并相应地解析它:

char line[128]; /* Create a buffer to store the line */

char ch = 0;
int num;
float fl; /* Variables to store data in */

int r;
size_t n; /* For checking from `sscanf` */

/* A `do...while` loop is best for your case */
do {
  printf("Enter a value: ");

  if(fgets(line, sizeof(line), stdin) == NULL) /* If scanning a line failed */
  {
    fputs("`fgets` failed", stderr);
    exit(1); /* Exits the program with a return value `1`; Requires `stdlib.h` */
  }

  line[strcspn(line, "\n")] = '\0'; /* Replace `\n` with `'\0'` */

  r = sscanf(buffer, "%d%zn", &num, &n);
  if(r == 1 && n == strlen(line)) { /* If true, entered data is an integer; `strlen` requires `string.h` */
    printf("You entered an integer %d\n", num);
  }
  else{
    r = sscanf(buffer, "%f%zn", &fl, &n);
    if(r == 1 && n == strlen(line)) { /* If true, entered data is a float; `strlen` requires `string.h` */
      printf("You entered a floating point number %f\n", fl);
    }
    else{
      if(strlen(line) == 1) /* If true, entered data is a character; `strlen` requires `string.h` */
      {
        ch = line[0];
        printf("You entered a character %c\n", ch);
      }
      else{ /* Entered data is something else */
        printf("You entered \"%s\"\n", line);
      }
    }
  }
}while(c != 'q');

免责声明:我使用手机编写了上述代码,但尚未对其进行测试。

更新(未使用我的手机进行测试和编写)

#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>

int main(void)
{

    int c = 0;

    bool random = false;
    bool flag = true;
    bool is_float = false, is_char = false, is_number = false;

    do{
      c = getchar();

      if(c == EOF)
        break;

      if(!random)
      {
        if(isdigit(c))
        {
          is_number = true;
        }
        else if(c == '.')
        {
          if(is_number)
          {
            if(is_float)
            {
              random = true; 
            }
            else
            {
              is_float = true;
            }
          }
          else if(!is_number && !is_float && !is_char)
          {
            is_float = true;
          }
        }
        else if(c == '-' && !is_float && !is_number && !is_char);
        else if(isalpha(c))
        {
          if(is_char)
            random = true;
          else
          {
            is_char = true;
            if(c == 'q')
              flag = false;
          }
        }
        else
        {
          random = true;
        }

        if((is_char && is_float) || (is_char && is_number))
          random = true;

        if(c == '\n' && !is_char && !is_float && !is_number)
          random = true;
      }

      if(c == '\n')
      {
        if(random)
          /* puts("You entered a random string!"); */
          puts("Invalid input!");
        else if(is_float)
          puts("You entered a float!");
        else if(is_number)
          puts("You entered a number!");
        else if(is_char)
          puts("You entered a character!");
        else
          puts("Error!");

        if(!flag && !is_number && !is_float && !random)
          flag = false;
        else
          flag = true;

        is_char = is_float = is_number = random = false;
      }
    }while(flag);

    puts("Done");
    return 0;
}