首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CS50 - pset2 - Vigenere

CS50 - pset2 - Vigenere
EN

Stack Overflow用户
提问于 2016-02-25 22:21:18
回答 1查看 295关注 0票数 0

我目前正在为pset2而苦苦挣扎,尤其是vigenere。

下面是我的代码:

代码语言:javascript
复制
# include <cs50.h>
# include <ctype.h>
# include <stdio.h>
# include <stdlib.h>
# include <string.h>

int main (int argc, string argv[])
{

    //Assess the fact that there is only 1 command line argument
    if(argc != 2)
    {
        printf("You should only have 1 command line argument !\n") ;
        return 1 ;
    }

    string k = argv[1] ;
    int klength = strlen(k) ;

    for(int i = 0; i < klength; i++)
    {
        if(!isalpha(k[i]))
        {
            printf("Please make sure the argument is only composed of alphabetical characters\n") ;
            return 1 ;
        }
    }

    //Get the text to be crypted
    string s = GetString() ;
    int slength = strlen(s) ;

    //Deliver the crypted text
    for( int i = 0, j = 0 ; i < slength ; i++)
    {
        int kindex = j % klength ;

        if(isalpha(s[i]))
        {
            if(isupper(s[i]))
            {
                if(isupper(k[kindex]))
                {
                    int crypt = (((s[i] - 'A') + (k[kindex] - 'A') % 26)) + 'A' ;
                    printf("%c", crypt ) ;
                }
                else
                {
                    int crypt = (((s[i] - 'A') + (k[kindex] - 'a')) % 26) + 'A' ;
                    printf("%c", crypt ) ;
                }
            }
            if(islower(s[i]))
            {
                if(isupper(k[kindex]))
                {
                    int crypt = (((s[i] - 'a') + (k[kindex] - 'A')) % 26) + 'a' ;
                    printf("%c", crypt) ;
                }
                else
                {
                    int crypt = (((s[i] - 'a') + (k[kindex] - 'a')) % 26) + 'a' ;
                    printf("%c", crypt ) ;
                }
            }
        j++ ;
        }
        else
        {
            printf("%c" , s[i]) ;
        }
    }

    printf("\n") ;
    return 0 ;
}  

使用check50时,以下是我收到的错误:

:(使用"BaRFoo“作为关键字\预期输出将"CaQGon”加密为"BaZ“,而不是"CakGon\n”:(使用"BAZ“作为关键字\预期输出将"BARFOO”加密为"CAQGON“,但不使用"CAkGOh\n”

这是我的沙箱:sandbox我不明白为什么两个输出不一样(cakgon vs cakoh),以及为什么它与预期的不同。问题可能出在“//交付加密测试”部分。

我已经花了几个小时试图弄清楚它,但没有成功。

提前感谢您的任何帮助/提示/建议。

巴蒂斯特

EN

回答 1

Stack Overflow用户

发布于 2016-02-26 01:17:34

我终于明白了。其中一个"%26“的前面缺少括号。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35629898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档