gpt4 book ai didi

c - For 循环未按预期工作

转载 作者:行者123 更新时间:2023-11-30 19:45:15 25 4
gpt4 key购买 nike

我一直在尝试用 C 语言创建一个密码生成器。

除了实际的加密循环计数器之外,一切都工作得很好。它总是停在 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;
}

(抱歉,如果它看起来很明显和/或重复。)

最佳答案

最后!它应该有效!您的主要/最大错误是在最后一个 for 循环中将 c 设置为 0!

因为将其设置为 0 后,它会增加 1,但在 plainAlphabet 中 'A'索引 0 !因此,如果您的文本包含 A ,程序就会在那里停止,因为 c 它永远不会得到 0A 会得到根本不可能在你的普通字母表中找到! (解决方案:将 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;

}

关于c - For 循环未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26697698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com