2015-09-07 86 views
-1

我一直坚持这一段时间了,我以为我有几次工作,只是为了找到更多的东西,我忘了。基本上问题集是创建使用C的Vigenere密码,规则可以发现hereVigenere的密码在C(CS50)

我基本上有它的工作,它只会允许正确的字符,如果关键字是相同的长度作为消息一切都很好。

我遇到的问题是想办法在消息被加密时重置关键字的循环,因此如果我输入关键字'A'和消息'This is a test',则应该读取加密的消息'这是一个测试',因为'A'在ASCII表中值得零移。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 
#include <stdbool.h> 

int main(int argc, char * keyWord[]) { 
    int cipher[64], i, j, k, l; 
    char message[128]; 

    // Validation - using alphaCheck Function 
    if (argc != 2 || alphaCheck(keyWord) == 2) { 
    return 1; 
    } 

    // For loop to convert to upper and set value ie A = 0 B = 1 
    for (i = 0, j = strlen(keyWord[1]); i < j; i++) { 
    cipher[i] = (toupper(keyWord[1][i]) - 65); 
    printf("%i\n", cipher[i]); 
    } 

    // Prompt the user for the message to encrypt 
    printf("Enter your secret message: "); 
    fgets(message, 128, stdin); 


    int keyCount = 0; 
    int p = strlen(keyWord[1]); 

    if (keyCount < p) { 
    keyCount++; 
    } else { 
    keyCount = 0; 
    } 

    for (i = 0, k = strlen(message); i < k; i++) { 
    if (isspace(message[i])) { 
     printf(" "); 
    } else if (isupper(message[i])) { 
     char c = (message[i] - 65 + cipher[i]) % 26 + 65; 
     printf("%c", c); 
     } 
     else { 
     char d = (message[i] - 97 + cipher[i]) % 26 + 97; 
     printf("%c", d); 
     } 
    } 
} 



// Function to check if alphabet characters. 
int alphaCheck(char * argv[]) { 
    int length = strlen(argv[1]), n; 
    for (n = 0; n < length; n++) { 
    if (!isalpha(argv[1][n])) { 
     printf("Characters 'A-Z' for Keyword.\n"); 
     return 2; 
    } 
    } 
} 

以下部分在我将其留在此处显示了我是如何尝试解决问题但失败的。

int keyCount = 0; 
     int p = strlen(keyWord[1]); 

     if (keyCount < p) { 
     keyCount++; 
     } else { 
     keyCount = 0; 
     } 

回答

2

首先,您应该启用更多的编译器警告。例如,用gcc和我最喜欢的一组标志(-pedantic -Wall -Wextra -Wundef -Wendif-labels -Wshadow -Wbad-function-cast -Wcast-align -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Winline -Wdisabled-optimization -O2)我得到:

$ cc prog.c 
prog.c: In function ‘main’: 
prog.c:12:20: warning: implicit declaration of function ‘alphaCheck’ [-Wimplicit-function-declaration] 
    if (argc != 2 || alphaCheck(keyWord) == 2) { 
        ^
prog.c:12:3: warning: nested extern declaration of ‘alphaCheck’ [-Wnested-externs] 
    if (argc != 2 || alphaCheck(keyWord) == 2) { 
^
prog.c:8:28: warning: unused variable ‘l’ [-Wunused-variable] 
    int cipher[64], i, j, k, l; 
          ^
prog.c: At top level: 
prog.c:53:5: warning: no previous prototype for ‘alphaCheck’ [-Wmissing-prototypes] 
int alphaCheck(char * argv[]) { 
    ^
prog.c: In function ‘alphaCheck’: 
prog.c:61:1: warning: control reaches end of non-void function [-Wreturn-type] 
} 
^ 

所有这些应该是固定的:

  • alphaCheck
  • 末尾添加return 0删除l
  • int alphaCheck(char *[]);之前main(或在main之前移动整个功能并使之成为static

您的主循环有三种情况。它检查空格,大写字符,其他所有内容都被认为是小写字符。而应该检查大写,小写,并通过改变通过一切(比如像.,标点符号):

if (isupper(message[i])) { 
    ... 
} else if (islower(message[i])) { 
    ... 
} else { 
    printf("%c", message[i]); 
} 

您与keyCount的想法是不坏,但它需要与集成主循环。也就是说,您应该在循环中使用cipher[keyCount],而不是cipher[i],并且每次使用它时都会增加keyCount

然后,在每次迭代结束时,你可以做你检查,如果你已经用完了关键的(和复位keyCount):

for (...) { 
    ... 

    if (keyCount >= p) { 
     keyCount = 0; 
    } 
} 
+0

感谢。这很好,当我运行gcc时,我肯定会添加这些标志。我目前只使用-lm。我将执行您所建议的更改,这些提示将有望用于未来的问题集。 –

+0

添加了更改并可在此处查看http://codereview.stackexchange.com/questions/102737/vigeneres-cipher-in-c/104099#104099 –