2015-10-17 59 views
0

我正在尝试编写一个程序,要求用户不断输入float,int或char并将其回显给它们,直到它们输入'q',然后循环结束。我不明白如何在进入循环之前在int,char或float之间进行解密。我试过如果(scanf(“%c”,ch))等等,对于float和int并且效果很好,但是一旦我将循环添加到它中,我就搞乱了。我尝试了几种不同的组合,但是我仍然没有找到我的答案。
这是一个试图告诉你正是我想要做的事:int,char,float的处理输入

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: "); 
     } 
    } 

这使得做一些奇怪的事情,我无法找出我的问题。先谢谢你!

+0

'如果别人(的scanf( “%d”,&NUM)){ 的printf(“您输入的浮动点数%f \ n“,fl); '没有意义。您是否意指'else if(scanf(“%f”,&fl))printf(“您输入了浮点数%f \ n”,fl); }'? –

回答

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; 
} 
+0

问题是我不允许使用字符串或数组。我知道不建议在这里发布作业问题,但事实是,我已经尝试了几天,我不知道是否有另一种方法来扫描int,chars和float,并将它们回显。这是我的问题。我已经阅读了一些提到使用ascii表来实现这一点的地方,但我不知道这是否太刺激。 – user3376654

+0

你应该在你的问题中提到这个要求。不幸的是,我认为不可能检查用户输入是否是字符,浮点数,数字或其他什么是无效的*和*打印没有数组和字符串的字符/浮点数/数字。我在回答中写了一些代码,虽然我没有测试它,因为我正在使用我的手机,并且在某些日子里不会使用我的笔记本电脑。你可以翻阅我写的代码。请注意,代码需要'ctype.h'(对于'isdigit','isalpha'等)和'stdbool.h'(对于'bool','true','false'等)('stdbool.h'可用只在C99及以上)。 –