2015-10-17 61 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及以上)。 –