[英]CS50 Vigenere - Output Incorrect
Man I thought I had it! 男人,我以为我有它! I've been working on the Vigenere problem and have gotten close but keep getting this error when I check.
我一直在研究Vigenere问题,并且已经很接近了,但是当我检查时一直收到此错误。 It looks like there's problem when the key has to loop back around.
密钥必须环回时似乎存在问题。 Thoughts?
有什么想法吗?
Here is the error: 这是错误:
:) vigenere.c exists
:) vigenere.c compiles
:) encrypts "a" as "a" using "a" as keyword :( encrypts "world, say hello!" as "xoqmd, rby gflkp!" using "baz" as keyword \ expected output, but not "xoqmj, yfz gflkp!\n"
:( encrypts "BaRFoo" as "CaQGon" using "BaZ" as keyword \ expected output, but not "CaQAun\n"
:( encrypts "BARFOO" as "CAQGON" using "BAZ" as keyword \ expected output, but not "CAQAON\n"
:) handles lack of argv[1]
:) handles argc > 2
:) rejects "Hax0r2" as keyword
and here's my code: 这是我的代码:
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define FALSE 0
#define TRUE 1
int main(int argc, string argv[])
{
string key = argv[1];
if (argc!= 2)
{
printf("please provide only one perameter \n");
// stop the program
return 1;
}
// iterate over the key to make sure its all alpha
int i,j;
for (i = 0, j = strlen(key); i < j; i++)
{
if (!isalpha(key[i]))
{
printf("please use only alphanumeric values \n");
return 1;
}
}
// now we have a key, "key" from the ONE perameter that is all alpha
string message = GetString();
int k = 0;
int keyindex;
for (i = 0, j = strlen(message); i < j; i++, k++)
{
if (isalpha(message[i]))
{
keyindex = k % strlen(argv[1]);
// covering the upper case letters
if (isupper(message[i]))
{
// covering the upper case letters with upper case key letters
if isupper(key[i])
{
// print cipher according to two upper case
int cipher = ((message[i] - 65 + key[keyindex] - 65) % 26)
+ 65;
printf("%c", cipher);
}
else
{
// print according to upper case message lower case key
int cipher = ((message[i] - 65 + key[keyindex] - 97) % 26)
+ 65;
printf("%c", cipher);
}
}
// this is for the non upper case letters
if (islower(message[i]))
{
if isupper(key[i])
{
// print cipher according to lower case message and
// upper case key letter
int cipher = ((message[i] - 97 + key[keyindex] - 65) % 26)
+ 97;
printf("%c", cipher);
}
else
{
// print according to lower case message and lower case key
int cipher = ((message[i] - 97 + key[keyindex] - 97) % 26)
+ 97;
printf("%c", cipher);
}
}
}
// non alpha symbols
else
{
printf("%c", message[i]);
}
}
// end program after iterating
printf("\n");
}
Problems with your program: 您的程序存在问题:
1) Sytax error that should keep it from compiling: 1)Sytax错误,应避免对其进行编译:
if isupper(key[i])
-> if (isupper(key[i]))
if isupper(key[i])
-> if (isupper(key[i]))
There are two of these so make sure to fix them both. 其中有两个,因此请确保将它们都修复。
2) Incrementing k
2)递增
k
int k = 0;
...
for (i = 0, j = strlen(message); i < j; i++, k++)
{
if (isalpha(message[i]))
{
keyindex = k % strlen(argv[1]);
There two was to approach this, either increment k
on ever character or increment k
on every letter . 有两种方法可以解决这个问题,要么在每个字符上增加
k
,要么在每个字母上增加k
。 The designer of this problem chose letter so instead we need to do: 这个问题的设计者选择了letter,因此我们需要做的是:
int k = 0;
...
for (i = 0, j = strlen(message); i < j; i++)
{
if (isalpha(message[i]))
{
keyindex = k++ % strlen(argv[1]);
3) Use keyindex
now that we've defined it: 3)现在我们已经定义了
keyindex
:
if isupper(key[i])
-> if isupper(key[keyindex])
if isupper(key[i])
-> if isupper(key[keyindex])
There are two of these so make sure to fix them both. 其中有两个,因此请确保将它们都修复。
Applying these changes and a little bit of style cleanup, we get: 应用这些更改和一点样式清除,我们得到:
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("please provide only one parameter \n");
return 1; // stop the program
}
string key = argv[1];
// iterate over the key to make sure it's all alpha
for (int i = 0, j = strlen(key); i < j; i++)
{
if (!isalpha(key[i]))
{
printf("please use only alphanumeric values \n");
return 1;
}
}
// now we have a key, "key" from the ONE parameter that is all alpha
string message = GetString();
for (int i = 0, k = 0, j = strlen(message); i < j; i++)
{
if (isalpha(message[i]))
{
int keyindex = k++ % strlen(key);
if (isupper(message[i])) // covering the upper case letters
{
if (isupper(key[keyindex]))
{
// print cipher according to both upper case
int cipher = ((message[i] - 'A' + key[keyindex] - 'A') % 26) + 'A';
printf("%c", cipher);
}
else
{
// print cipher according to upper case message and lower case key
int cipher = ((message[i] - 'A' + key[keyindex] - 'a') % 26) + 'A';
printf("%c", cipher);
}
}
else // this is for the non upper case letters
{
if (isupper(key[keyindex]))
{
// print cipher according to lower case message and upper case key letter
int cipher = ((message[i] - 'a' + key[keyindex] - 'A') % 26) + 'a';
printf("%c", cipher);
}
else
{
// print cipher according to both lower case
int cipher = ((message[i] - 'a' + key[keyindex] - 'a') % 26) + 'a';
printf("%c", cipher);
}
}
}
else // non alpha symbols
{
printf("%c", message[i]);
}
}
printf("\n"); // end program after iterating
}
Your code duplicates a lot of logic that you could combine with minor changes -- duplicated logic is one way that hard to find errors creep into code. 您的代码重复了很多逻辑,可以将这些逻辑与较小的更改结合在一起-重复的逻辑是一种难以发现错误的错误代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.