与Vigenere一起苦苦挣扎! (在C中)二进制表达式('int *'和'int')和其他东西的无效操作数

问题描述 投票:-2回答:1

我们现在正在与凯撒,vigenere合作。我管理完凯撒但vigenere效果不佳。 C给了我回复:二进制表达式的无效操作数('int *'和'int')。我不确定程序的确切含义以及我的代码有什么问题。有人可以帮我或给我建议吗?我想这是因为我不能指望不同类型的数字?我不确定!

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

int main(int argc, string argv[])
{
if (argc != 2)
{
    printf("You have to input a key, try again!\n");
    return 1;
}

string key = argv[1];
int keylength = strlen(key);

for (int i = 0; i < keylength; i++)
{
    if (!isalpha(argv[1][i]))
    {
        printf("Please insert letters, nothing else\n");
        return 1;
    }
}

printf("plaintext: ");
string plain = get_string();


int keycipher[keylength];
for(int i = 0; i < keylength; i++)
{
    keycipher[i] = toupper(key[i]) - 65;
}

if (plain != 0)
{
    printf("ciphertext: ");
    int i;
    for (i = 0, keylength = strlen(key); i < keylength; i++)
    {
        if (isupper(plain[i]))
        {
            printf("%c", (plain[i] - 65 + keycipher) % 26);
        }
        else if (islower(plain[i]))
        {
            printf("%c", (plain[i] - 97 + keycipher) % 26);
        }
        else if (plain[i] == ' ')
        {
            printf(" ");
        }
        else
        {
            printf("%c", plain[i]);
        }
    }
}
return 0;
}
c cs50 vigenere
1个回答
1
投票

正如评论中提到的,你必须使用+ keycipher[index]而不是+ keycipher。这是因为keycipher是一个数组,你需要一次只使用数组中的一个元素。

此外,您将需要2个计数器变量,而不是在您找到ciphertext的部分中只有一个。这是因为你需要在每次迭代中增加plaintext指数和key指数,但是一旦plaintext长度超过key长度,你将需要重置密钥指数。如果您查看规格,您将了解我对使用2个计数器的看法。

© www.soinside.com 2019 - 2024. All rights reserved.