2017-01-16 64 views
-4

這是我的代碼。對於輸入:開關盒可能不觸發

A 1

它應該輸出2,但它打印1由於某種原因,我不知道,這裏就是問題所在。

我需要反轉值L次,更改後的值取決於焦炭T的

倒黴,它總是打印1.

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

int main(void) { 
    int N, aa, bb; 
    int k1 = 1, k2 = 0, k3 = 0, k4 = 0, k5 = 0; 
    scanf("%d", &N); 
    char *T = new char[N]; 
    int *L = new int[N]; 

    for (int length = 0; length < N; length++) { 
     scanf("%c", &T[length]); 
     scanf("%d", &L[length]); 
    } 
    for (int i = 0; i < N; i++) { 
     switch (toupper(T[i])) { 
     case 'A': 
      for (int j = 0; j < L[i]; j++) { 
       aa = k1; 
       bb = k2; 
       k2 = aa; 
       k1 = bb; 
      } 
      break; 
     case 'B': 
      for (int j = 0; j < L[i]; j++) { 
       aa = k1; 
       bb = k3; 
       k3 = aa; 
       k1 = bb; 
      } 
      break; 
     case 'C': 
      for (int j = 0; j < L[i]; j++) { 
       aa = k1; 
       bb = k4; 
       k4 = aa; 
       k1 = bb; 
      } 
      break; 
     case 'D': 
      for (int j = 0; j < L[i]; j++) { 
       aa = k1; 
       bb = k5; 
       k5 = aa; 
       k1 = bb; 
      } 
      break; 
     case 'E': 
      for (int j = 0; j < L[i]; j++) { 
       aa = k2; 
       bb = k3; 
       k3 = aa; 
       k2 = bb; 
      } 
      break; 
     case 'F': 
      for (int j = 0; j < L[i]; j++) { 
       aa = k2; 
       bb = k4; 
       k4 = aa; 
       k2 = bb; 
      } 
      break; 
     case 'G': 
      for (int j = 0; j < L[i]; j++) { 
       aa = k2; 
       bb = k5; 
       k5 = aa; 
       k2 = bb; 
      } 
      break; 
     case 'H': 
      for (int j = 0; j < L[i]; j++) { 
       aa = k3; 
       bb = k4; 
       k4 = aa; 
       k3 = bb; 
      } 
      break; 
     case 'I': 
      for (int j = 0; j < L[i]; j++) { 
       aa = k3; 
       bb = k5; 
       k5 = aa; 
       k3 = bb; 
      } 
      break; 
     case 'J': 
      for (int j = 0; j < L[i]; j++) { 
       aa = k4; 
       bb = k5; 
       k5 = aa; 
       k4 = bb; 
      } 
      break; 
     } 
    } 
    if (k1 == 1) 
     printf("1"); 
    else if (k2 == 1) 
     printf("2"); 
    else if (k3 == 1) 
     printf("3"); 
    else if (k4 == 1) 
     printf("4"); 
    else 
     printf("5"); 
    return 0; 
} 
+2

這些變量想成爲一個數組......你不需要開關太多:) – Rakete1111

+0

你真的想'k [4]'和'k [5] '。就像Rakete說的那樣,**使用數組**。你也可以使用'std :: vector'而不是'new x []'來簡化內存管理。 – tadman

+2

使用調試器。 – mascoj

回答

1

看看這裏:

for(int length = 0; length < N; length++){ 
    scanf("%c", &T[length]); 
    scanf("%d", &L[length]); 
} 

如果輸入是「1 A1」,那麼空白空間進入T,並且scanf盡最大努力將「A1」掃描爲整數。在嘗試使用這些值之前,您可能已經發現了這個錯誤。