凯撒 Check50 失败
Caesar Check50 fail
我正在尝试解决 Caesar 问题,请参阅下面的代码:
// INCLUDE THE APPROPRIATE LIBRARIES
#include <math.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int eoc;
int feoc;
int eolc;
int feolc;
// INITIALIZE PROGRAMME THAT REQUIRES A COMMAND LINE ARGUMENT
int main (int argc, string argv[])
// OBTAIN A VALID KEY INPUT FROM USER
{
int minimumkeyinput = 2;
if (argc < minimumkeyinput)
{
printf("Caesar requires a Key input in order to execute e.g. ./caesar 2\n");
return 1;
}
// KEY CONVERSION (kc) DECLARED AS AN INT CONVERTED FROM ARGV
int kc = atoi(argv[1]);
if (kc < 0)
// ENSURE A POSITIVE INTEGER IS GIVEN AS PROGRAMME "KEY"
{
printf("programme requires a positive integer in order to execute\n");
return 1;
}
// OBTAIN PLAIN TEXT INPUT (pti) FROM USER
string pti;
do
{
printf("plaintext: ");
pti = get_string();
}
while (pti == NULL);
// BEGIN ENCRYPTION PROCESS OF PLAINTEXT INPUT
printf("ciphertext: ");
int ptic = strlen(pti);
// ITERATE OVER PLAINTEXT
int ptii = 0;
for (; ptii <= ptic; ptii++)
{
// SHIFT THE CHARACTERS OF PLAINTEXT INPUT (pti) TO ENCRYPTED OUTPUT (eo) BY USER KEY CONVERSION (kc)
// CONVERT USER INPUTS ENCRYPTION RESULT INTO AN ALPHABETICAL INDEX
// LOOP AROUND THE APLHABET IF REQUIRED BEFORE PRINTING TO SCREEN (UPPPERCASE ASCII 65-90)
eoc = (pti[ptii] -65 + kc) %26;
// LIMITING THE SCOPE OF THE UPPERCASE LOOP
// CONVERT CHARACTERS FROM ALPHABETICAL INDEX BACK TO ASCII INDEX (UPPERCASE)
if (isalpha (pti[ptii]))
{
if isupper (pti[ptii])
{
feoc = eoc + 65;
printf("%c", feoc);
}
// LOOP AROUND ALPHABET IF REQUIRED BEFORE PRINTNG TO SCREEN (LOWERCASE ASCII 97 - 122)
// LIMIT THE SCOPE OF THE LOWERCASE LOOP
if islower (pti[ptii])
{
eolc = (pti[ptii] -97 + kc) %26;
// CONVERT CHARACTERS FROM ALPHABETICAL INDEX BACK TO ASCII INDEX (LOWERCASE)
feolc = eolc + 97;
printf("%c", feolc);
}
}
if (isalpha (pti[ptii]) == false)
{
printf("%c", pti[ptii]);
}
}
printf("\n");
}
不幸的是,当我 运行 'check50'
时,我仍然收到以下失败消息
:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
expected "ciphertext: b\n", not "ciphertext: b\x..."
:( encrypts "barfoo" as "yxocll" using 23 as key
expected "ciphertext: yxo...", not "ciphertext: yxo..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
expected "ciphertext: EDU...", not "ciphertext: EDU..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
expected "ciphertext: FeV...", not "ciphertext: FeV..."
:( encrypts "barfoo" as "onesbb" using 65 as key
expected "ciphertext: one...", not "ciphertext: one..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
expected "ciphertext: iad...", not "ciphertext: iad..."
:) handles lack of argv[1]
有人可以帮忙吗?
运行 'check50' 我自己的例子,输出看起来不错。如果您理解我的意思,我认为计算机读取数据的方式可能与屏幕上显示的方式不同。
我的猜测是我错过了一个简单的步骤,但我终其一生都找不到它。
因为我没有 <cs50.h>
可用,所以我无法测试我写的东西,但是你的 for
循环条件似乎是错误的:
int ptii = 0;
for (; ptii <= ptic; ptii++)
应该是
int ptii = 0;
for (; ptii < ptic; ptii++)
我猜你有一个额外的字符来输出 printf 虽然它看起来很相似,因为来自 int ptic = strlen(pti);
的额外循环
int ptii = 0;
for (; ptii <= ptic; ptii++)
应该改为
int ptii = 0;
for (; ptii < ptic; ptii++)
我正在尝试解决 Caesar 问题,请参阅下面的代码:
// INCLUDE THE APPROPRIATE LIBRARIES
#include <math.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int eoc;
int feoc;
int eolc;
int feolc;
// INITIALIZE PROGRAMME THAT REQUIRES A COMMAND LINE ARGUMENT
int main (int argc, string argv[])
// OBTAIN A VALID KEY INPUT FROM USER
{
int minimumkeyinput = 2;
if (argc < minimumkeyinput)
{
printf("Caesar requires a Key input in order to execute e.g. ./caesar 2\n");
return 1;
}
// KEY CONVERSION (kc) DECLARED AS AN INT CONVERTED FROM ARGV
int kc = atoi(argv[1]);
if (kc < 0)
// ENSURE A POSITIVE INTEGER IS GIVEN AS PROGRAMME "KEY"
{
printf("programme requires a positive integer in order to execute\n");
return 1;
}
// OBTAIN PLAIN TEXT INPUT (pti) FROM USER
string pti;
do
{
printf("plaintext: ");
pti = get_string();
}
while (pti == NULL);
// BEGIN ENCRYPTION PROCESS OF PLAINTEXT INPUT
printf("ciphertext: ");
int ptic = strlen(pti);
// ITERATE OVER PLAINTEXT
int ptii = 0;
for (; ptii <= ptic; ptii++)
{
// SHIFT THE CHARACTERS OF PLAINTEXT INPUT (pti) TO ENCRYPTED OUTPUT (eo) BY USER KEY CONVERSION (kc)
// CONVERT USER INPUTS ENCRYPTION RESULT INTO AN ALPHABETICAL INDEX
// LOOP AROUND THE APLHABET IF REQUIRED BEFORE PRINTING TO SCREEN (UPPPERCASE ASCII 65-90)
eoc = (pti[ptii] -65 + kc) %26;
// LIMITING THE SCOPE OF THE UPPERCASE LOOP
// CONVERT CHARACTERS FROM ALPHABETICAL INDEX BACK TO ASCII INDEX (UPPERCASE)
if (isalpha (pti[ptii]))
{
if isupper (pti[ptii])
{
feoc = eoc + 65;
printf("%c", feoc);
}
// LOOP AROUND ALPHABET IF REQUIRED BEFORE PRINTNG TO SCREEN (LOWERCASE ASCII 97 - 122)
// LIMIT THE SCOPE OF THE LOWERCASE LOOP
if islower (pti[ptii])
{
eolc = (pti[ptii] -97 + kc) %26;
// CONVERT CHARACTERS FROM ALPHABETICAL INDEX BACK TO ASCII INDEX (LOWERCASE)
feolc = eolc + 97;
printf("%c", feolc);
}
}
if (isalpha (pti[ptii]) == false)
{
printf("%c", pti[ptii]);
}
}
printf("\n");
}
不幸的是,当我 运行 'check50'
时,我仍然收到以下失败消息:) caesar.c exists.
:) caesar.c compiles.
:( encrypts "a" as "b" using 1 as key
expected "ciphertext: b\n", not "ciphertext: b\x..."
:( encrypts "barfoo" as "yxocll" using 23 as key
expected "ciphertext: yxo...", not "ciphertext: yxo..."
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
expected "ciphertext: EDU...", not "ciphertext: EDU..."
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
expected "ciphertext: FeV...", not "ciphertext: FeV..."
:( encrypts "barfoo" as "onesbb" using 65 as key
expected "ciphertext: one...", not "ciphertext: one..."
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
expected "ciphertext: iad...", not "ciphertext: iad..."
:) handles lack of argv[1]
有人可以帮忙吗?
运行 'check50' 我自己的例子,输出看起来不错。如果您理解我的意思,我认为计算机读取数据的方式可能与屏幕上显示的方式不同。
我的猜测是我错过了一个简单的步骤,但我终其一生都找不到它。
因为我没有 <cs50.h>
可用,所以我无法测试我写的东西,但是你的 for
循环条件似乎是错误的:
int ptii = 0;
for (; ptii <= ptic; ptii++)
应该是
int ptii = 0;
for (; ptii < ptic; ptii++)
我猜你有一个额外的字符来输出 printf 虽然它看起来很相似,因为来自 int ptic = strlen(pti);
int ptii = 0;
for (; ptii <= ptic; ptii++)
应该改为
int ptii = 0;
for (; ptii < ptic; ptii++)