【问题标题】:What's wrong with my CS50 Vigenere code?我的 CS50 Vigenere 代码有什么问题?
【发布时间】:2015-07-01 22:36:53
【问题描述】:

我已经在这个圈子里转了几个小时了。它管理推荐测试的第一个单词(早上 11 点在公园见)越过第一个空格,给出一个正确的 m 字母,然后在结束前打印几个空格。非常感谢。

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

int allstralpha();

int main(int argc, string argv[])
{
    string keyw = argv[1];
    if(argc == 2 && allstralpha(keyw))
    {
        string plaint = GetString();
        int c = 0;
        int kl = strlen(keyw);
        int k = 0;
        int p = 0;
        int j = 0;
        for(int i = 0, n = strlen(plaint); i < n; i++)
        {      
            if(isalpha(plaint[i]))
            {        
                if(isupper(keyw[j]))
                {
                    k = keyw[(j % kl)] - 65;
                    if(isupper(plaint[i]))
                    {
                        p = plaint[i] -65;
                        c = ((k + p) % 26) + 65;
                        printf("%c", (char) c);                     
                    }   
                    else if(islower(plaint[i]))
                    {
                        p = plaint[i] -97;
                        c = ((k + p) % 26) + 97;
                        printf("%c", (char) c);
                    }
                }
                else if(islower(keyw[j]))
                {   
                    k = keyw[(j % kl)] - 97;
                    if(isupper(plaint[i]))
                    {  
                        p = plaint[i] - 65;                                  
                        c = ((k + p) % 26) + 65;             
                        printf("%c", (char) c);

                    }
                    else if(islower(plaint[i]))
                    {
                        p = plaint[i] - 97; 
                        c = ((k + p) % 26) + 97;
                        printf("%c", (char) c);
                    }   
                }
                j++;    
            }
            else
            {
                printf("%c", (char) plaint[i]);
            }
        }       
    }
    else
    {
        printf("Sorry that is not a vaild parameter\n");
        return 1;
    }  
}
int allstralpha(string s)
{   
    for(int i = 0, n = strlen(s); i < n; i++)
    {
        if(!isalpha(s[i]))
        {
        return 0;
        }
    }
    return 1; 
}

【问题讨论】:

  • 您能否详细说明输入以及预期输出应该是什么
  • 现在,不用过多研究您的代码,让我问您:您是否向程序提供了在引号之间包含短语的参数?请显示您如何从命令行调用它。
  • 上午 11 点在公园见我 Negh zf av huf pcfx bt gzrwep oz
  • am 之前的英文应该如下加密。对不起,我的 iPhone 现在!如果我正确理解你的论点,我不会使用引号。
  • 检查您的所有keyw 访问。有几个地方你忘了(j % kl)

标签: c vigenere cs50


【解决方案1】:
int allstralpha();
int allstralpha(string s)
{   
...
}

您的函数定义和声明不匹配。你应该声明int allstralpha(string s);

在 main 的第一行:

int main(int argc, string argv[])
{
    string keyw = argv[1];
    ...
}

首先你应该在访问argv[1]之前检查if (argc &gt; 1)

对于实际代码本身,您提供纯文本,但我看不到关键字。

我使用来自wikipedia, vigenère cipher 的这些值进行测试:

Plaintext:  ATTACKATDAWN
Key:        LEMONLEMONLE
Ciphertext: LXFOPVEFRNHR

完成此操作的最少代码:

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

int main(int argc, const char* argv[])
{
    const char *str = "Meet me at the park at eleven am";
    const char *key = "bacon";

    int keylen = strlen(key);
    int len = strlen(str);
    for (int i = 0, j = 0; i < len; i++)
    {
        int c = str[i];
        if (isalnum(c))
        {
            //int k = function of key and `j`...
            //offset k...
            if (islower(c))
            {
                c = (c - 'a' + k) % 26 + 'a';
            }
            else
            {
                c = (c - 'A' + k) % 26 + 'A';
            }
            j++;
        }
        putchar(c);
    }
    putchar('\n');
    return 0;
}

【讨论】:

  • 我更新了答案。剩下的就是功课了,你自己完成吧。
猜你喜欢
  • 1970-01-01
  • 2015-01-01
  • 2017-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多