簡體   English   中英

我的cs50 vigenere代碼出了什么問題? 我接近輸出

[英]What's wrong with my cs50 vigenere code? I am close on the output

我已經花了幾個小時在此上,但我仍然陷於困境。 檢查時得到以下輸出。 錯誤與我打印出來的方式有關嗎?

  • :) vigenere.c存在
  • :) vigenere.c編譯
  • :(使用“ a”作為關鍵字\\預期輸出,而不是“密文:a \ù\ÿ\\ n”,將“ a”加密為“ a”
  • :)使用“ baz”作為關鍵字將“ barfoo”加密為“ caqgon”
  • :)使用“ BaZ”作為關鍵字將“ BaRFoo”加密為“ CaQGon”
  • :)使用“ BAZ”作為關鍵字將“ BARFOO”加密為“ CAQGON”
  • :(使用“ baz”作為關鍵字\\預期輸出,將“ world!$?”加密為“ xoqmd!$?”,但不加密“密文:xoqmd!$?í\\ b @ \\ n”
  • :(使用“ baz”作為關鍵字\\預期輸出,將“世界,打個招呼!”加密為“ xoqmd,rby gflkp!”,但不加密“密文:xoqmd,rby gflkp!^¿µÿ \\ n”
  • :)處理缺少argv [1]
  • :)處理argc> 2
  • :)拒絕“ Hax0r2”作為關鍵字

這是代碼:

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

#define alpha_length 26

char secret(char character, int key);

int main(int argc, string argv[]) {
    //check that there are only two strings
    if (argc != 2) {
        printf("Usage: ./vignere k\n");
        return 1;
    }
    //check that argv1 is alphabetical
    string code = argv[1];
    for (int t = 0; t < strlen(code); t++) {    
        if (!isalpha(code[t])) {
            printf("Alphabetical only!\n");
            return 1;
        }
    }
    //get string from user to encrypt
    printf("plaintext: ");
    string plaintext = get_string();

    //array created out of user inputted plain text
    char cypher[strlen(plaintext)];

    //j counts the number of alphabetical characters so that it resets based on argv length
    int j = 0; 
    //iterate over characters in array.  If they are alpha then apply the function secret
    for (int i = 0; i < strlen(plaintext); i++) {
        if (isalpha(plaintext[i])) {
            int index = j % strlen(code);
            int code_index = toupper(code[index]) - 'A' ;
            cypher[i] = secret(plaintext[i], code_index);
            j = j + 1;
        } else {
            cypher[i] = plaintext[i];
        }
    }
    printf("ciphertext: %s\n", cypher);

    return 0;
}  

char secret (char character, int key) {
    char shift;

    // if the character is upper case then start with uppercase A and shift based on the appropriate character from argv1
    if (isupper(character)) {
        shift = (int)character -'A';
        shift = shift + key;
        shift = (shift % alpha_length) + 'A';
    } else {
        // else start wit lower case a
        shift = (int)character - 'a'; 
        shift = shift + key;
        shift = (shift % alpha_length) + 'a';
    }
    return (char)shift;
}

您的代碼中存在多個問題:

  • 不要使用<cs50.h>string typedef:它隱藏了您要操作的對象的性質,這是一個簡單的char *指針,您應該學會掌握它,而不用擔心。

  • 因為類型char可以在默認情況下進行簽名,並且具有isalpha()未定義的負值,所以您應將char參數isalpha((unsigned char)code[t])為這些函數,使其成為unsigned charisalpha((unsigned char)code[t])

  • 您打算將cypher設為C字符串,因此必須為null終止符分配一個額外的字節並將其存儲在此處:

     char cypher[strlen(plaintext) + 1]; 
  • 密碼用i拼寫。

這是修改后的版本:

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

#define alpha_length 26

char secret(char character, int key);

int main(int argc, char *argv[]) {
    //check that there are only two strings
    if (argc != 2) {
        printf("Usage: ./vignere k\n");
        return 1;
    }
    //check that argv1 is alphabetical
    char *code = argv[1];
    int code_len = strlen(code);
    for (int t = 0; t < code_len; t++) {    
        if (!isalpha((unsigned char)code[t])) {
            printf("Alphabetical only!\n");
            return 1;
        }
    }
    //get string from user to encrypt
    printf("plaintext: ");
    char *plaintext = get_string();
    int text_len = strlen(plaintext);

    //array created out of user inputted plain text
    char cipher[text_len + 1];

    //j counts the number of alphabetical characters so that it resets based on argv length
    int j = 0; 
    //iterate over characters in array.  If they are alpha then apply the function secret
    for (int i = 0; i < text_len; i++) {
        if (isalpha((unsigned char)plaintext[i])) {
            int index = j % code_len;
            int code_index = toupper((unsigned char)code[index]) - 'A';
            cipher[i] = secret(plaintext[i], code_index);
            j = j + 1;
        } else {
            cipher[i] = plaintext[i];
        }
    }
    cipher[text_len] = '\0';
    printf("ciphertext: %s\n", cipher);

    return 0;
}  

char secret (char character, int key) {
    int shift;

    // if the character is upper case then start with uppercase A and shift based on the appropriate character from argv1
    if (isupper((unsigned char)character)) {
        shift = (int)character - 'A';
        shift = shift + key;
        return 'A' + (shift % alpha_length);
    } else {
        // else start with lower case a
        shift = (int)character - 'a'; 
        shift = shift + key;
        return 'a' + (shift % alpha_length);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM