当我运行check50时,我得到的是:
:) caesar.c exists.
:) caesar.c compiles.
:) encrypts "a" as "b" using 1 as key
:( encrypts "barfoo" as "yxocll" using 23 as key
output not valid ASCII text
:) encrypts "BARFOO" as "EDUIRR" using 3 as key
:) encrypts "BaRFoo" as "FeVJss" using 4 as key
:( encrypts "barfoo" as "onesbb" using 65 as key
output not valid ASCII text
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
output not valid ASCII text
:) handles lack of argv[1]
:) handles non-numeric key
:) handles too many arguments
我不知道";无效ASCII文本";或者错误是什么;错误",我在终端中得到了正确的输出,但check50将其标记为错误。
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
return 1;
}
string bob=argv[1];
int lower_letters[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
int upper_letters[]={'A', 'B', 'C', 'D', 'E','F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
for (int e=0; e<strlen(bob); e++)
{
for (int g=0; g<26; g++)
{
if ((bob[e])==(lower_letters[g]))
{
printf("do not enter something that is not a number n");
return 1;
}
}
}
int total = atoi(argv[1]);
if (total<0)
{
printf("do not input something that is a negative valuen");
return 1;
}
string word1 =get_string("plaintext: ");
printf("ciphertext: ");
for (int i =0; i<strlen(word1); i++)
{
if ((word1[i]==' ') || (word1[i]==',') || (word1[i]=='.') || (word1[i]=='!'))
{
printf("%c",word1[i]);
}
for (int a =0; a<26; a++)
{
if ((word1[i]==lower_letters[a]) && (a+total>26))
{
printf("%c",lower_letters[(a+total)-26]);
}
if (word1[i]==lower_letters[a])
{
printf("%c",lower_letters[a+total]);
}
if ((word1[i]==upper_letters[a]) && (a+total>26))
{
printf("%c",upper_letters[(a+total)-26]);
}
if (word1[i]==upper_letters[a])
{
printf("%c",upper_letters[a+total]);
}
}
}
printf("n");
}
我能请人帮我消除这个错误吗;无效ASCII文本";请
if ((word1[i]==lower_letters[a]) && (a+total>26)) { printf("%c",lower_letters[(a+total)-26]); } if (word1[i]==lower_letters[a]) { printf("%c",lower_letters[a+total]); }
尽管您考虑到a+total
的值大于26,但您错误地允许第二个if
主体也为这些值执行,而这并没有考虑到这一点。此外,减法CCD_ 3对于值52或更高是不合适的。
您可以用替换上述代码
if (word1[i]==lower_letters[a])
putchar(lower_letters[(a+total)%26]);
-当然这同样适用于CCD_ 4。