c++输出字母错误

C++ wrong letter for output

本文关键字:错误 输出 c++      更新时间:2023-10-16

你好,我有一个关于凯撒密码的新问题

关键:

3

平原:ABCCDEFGĞ嗨İJKLMNOOPRSŞTUUVYZ

密码:DEFGĞHIİJKLMNOÖPRSŞTUÜVYZABCD

这是土耳其字母"cığ,o,ş,u, c,İ,Ğ,o,Ş,u"

我需要加密和解密和程序不应该有大小写敏感。应该是s= s, ç=Ç

你可以看到下面的程序,但是我有一些问题

1)文本(纯文本)和密钥应该由用户输入,但我不能这样做。

2) char text[] = "DEF";这个输入应该给出(用于解密)"CÇD"但是它给出了"CÃD"

通常它应该给出"Ç"而不是"Ã"

我需要帮助:(

# include <iostream>
# include <cstring>
const char alphabet[] ={'A', 'B', 'C', 'Ç', 'D', 'E', 'F', 'G', 'Ğ', 'H', 'I',
                        'İ', 'J', 'K', 'L', 'M', 'N', 'O', 'Ö', 'P', 'R', 'S',
                        'Ş', 'T', 'U', 'Ü', 'V', 'Y', 'Z', '0', '1', '2', '3',
                        '4', '5', '6', '7', '8', '9', '.', ',', ':', ';', ' '};
const int char_num =44;
void cipher(char word[], int count, int key)
{
    int i = 0;
    while(i < count) {
        int ind = -1;
        while(alphabet[++ind] != word[i]) ;
        ind += key;
        if(ind >= char_num)
            ind -= char_num;
        word[i] = alphabet[ind];
        ++i;
    }
}
void decipher(char word[], int count, int key)
{
    int i = 0;
        while(i < count) {
        int ind = -1;
        while(alphabet[++ind] != word[i]) ;
        ind -= key;
        if(ind < 0)
            ind += char_num;
        word[i] = alphabet[ind];
        ++i;
    }
}

int main()
{
    char text[] = "ABC";
    int len = strlen(text);
    std::cout << text << std::endl;
    cipher(text, len, 2);
    std::cout << text << std::endl;
    decipher(text, len, 2);
    std::cout << text << std::endl;
    system("pause");
    return 0;
}

这个问题是您的程序使用的编码与控制台期望的编码不同。Windows在默认情况下是这样配置的;程序使用cp1252或cp1254这样的编码,而控制台期望使用cp437这样的编码。

这是一篇来自微软开发人员的文章,解释了为什么会这样。

网上已经有很多关于解决编码不匹配的方法的信息