2014-11-02 76 views
-1

我一直試圖在C.For循環無法正常運行

這一切都被工作完全正常,除了實際的加密循環計數器來創建一個密碼生成。它總是停在大約39處。強制它進一步,只會導致出現一些隨機的ASCII符號,而不是A-Z。 (對不起,我無法描述它特別好。)

#include <stdio.h> 
#include <stdlib.h> /* For exit() function*/ 
#include <time.h> 

#define SIZE 26 
int main(){ 

    char plainAlphabet[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',' '}; 

    char cipherAlphabet[27]; 
    char plainText[] = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"; 

    int x = 0; 
    int c = 0; 

    int numbers[SIZE]; 
    int i, n, tmp; 

    srand(time(NULL)); 

// Initialize the array 
    for(i = 0;i < SIZE;++i) 
    numbers[i] = i; 

// Shuffle the array 
    for(i = 0;i < SIZE;++i) 
    { 
     n = rand() % SIZE; 
     tmp = numbers[n]; 
     numbers[n] = numbers[i]; 
     numbers[i] = tmp; 
    } 

// Iterate through the array. Your numbers are already random 
    for(i = 0;i < SIZE;++i){ 
     cipherAlphabet[i] = plainAlphabet[numbers[i]]; 
    }; 
    int a = 0; 
    x = 0; 
    i = 0; 

    int z = strlen(plainText); 
    printf("%d\n",z); 
    for(plainText[x] != "^";c < z + 100; c++){ 

     if(plainAlphabet[c] == plainText["%d",x]){ 
      printf("%c",cipherAlphabet[c]); 
      a = a + 1; 
      x = x + 1; 
      c = 0; 
     }; 

    }; 
    printf("%d",c); 
    printf("%d",z); 
    scanf("%d", tmp); 
    return 0; 
} 

(很抱歉,如果這似乎是顯而易見的和/或重複。)

+1

'的scanf( 「%d」,TMP); '是錯的。另外,在'if(...)'中使用'plainText [x]'而不是'plainText [「%d」,x]','}'後面不需要分號。你的'for'循環對我來說看起來很可疑! – 2014-11-02 09:08:11

+0

我不明白'for(plainText [x]!=「^」; ...)'。爲什麼你在循環的初始化部分有一個比較?而且你不能將'char'與'char *'進行比較。 – Barmar 2014-11-02 09:11:43

+0

我甚至都不明白這個問題。哪部分代碼是_encrypting循環counter_? – Barmar 2014-11-02 09:13:09

回答

0

所以終於來了!它應該工作!你的主要/最大的錯誤是在你最後的for循環中設置c0

因爲在將其設置爲0之後,它會增加1,但在您的plainAlphabet 'A'中是index 0!因此,如果您的文本包含A's程序在那裏停止,因爲c它永遠不會得到0A會在您的plainAlphabet找到nerver! (解決方法:設置C爲-1 |在我的例子carrayCount

(你也必須在你的代碼的一些奇怪的錯誤!)

#include <stdio.h> 
#include <stdlib.h> /* For exit() function*/ 
#include <time.h> 
#include <string.h> 

#define SIZE 26 


int main() { 

    char plainAlphabet[27] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' '}; 
    char cipherAlphabet[50]; 
    char plainText[255] = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"; 

    int arrayCount, randomNumber, swap, charCount = 0, strLength = strlen(plainText); 
    int numbers[SIZE]; 


    srand(time(NULL)); 

    // Initialize the array 
    for(arrayCount = 0; arrayCount < SIZE; arrayCount++) 
     numbers[arrayCount] = arrayCount; 

    // Shuffle the array 
    for(arrayCount = 0; arrayCount < SIZE; arrayCount++) { 
     randomNumber = rand() % SIZE; 
     swap = numbers[randomNumber]; 
     numbers[randomNumber] = numbers[arrayCount]; 
     numbers[arrayCount] = swap; 
    } 

    // Iterate through the array. Your numbers are already random 
    for(arrayCount = 0; arrayCount < SIZE; arrayCount++) 
     cipherAlphabet[arrayCount] = plainAlphabet[numbers[arrayCount]]; 


    printf("String Length: %d\n\n", strLength); 
    printf("Encrypted Text: \n"); 
    charCount = 0; 

    for(arrayCount = 0; arrayCount < strLength; arrayCount++) { 

     if(plainAlphabet[arrayCount] == plainText[charCount] && plainText[arrayCount] != '^') { 

      printf("%c", cipherAlphabet[arrayCount]); 
      charCount += 1; 
      arrayCount = -1; 
     } 

     if(plainText[charCount] == '\0') 
      break; 

    } 

    int a = 0; //If this variable is commented out, the program gives other results 

    printf("\n\n"); 
    system("pause"); 


    return 0; 

} 
+0

它完美的工作!謝謝! – burnsriley 2014-11-03 04:39:00