我目前正在为pset2而苦苦挣扎,尤其是vigenere。
下面是我的代码:
# 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),以及为什么它与预期的不同。问题可能出在“//交付加密测试”部分。
我已经花了几个小时试图弄清楚它,但没有成功。
提前感谢您的任何帮助/提示/建议。
巴蒂斯特
发布于 2016-02-26 01:17:34
我终于明白了。其中一个"%26“的前面缺少括号。
https://stackoverflow.com/questions/35629898
复制相似问题