简体   繁体   English

C ++错误的输出字母

[英]C++ wrong letter for output

Hello I have new question about Caesar cipher for example 您好,例如,我对Caesar密码有新问题

Key: 3 键:3

Plain: ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ 平原:ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ

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

These are Turkish letters " ç ,ı ,ğ, ö , ş , ü , Ç, İ , Ğ, Ö, Ş, Ü " 这些是土耳其语字母“ç,ı,ğ,ö,ş,ü,Ç,İ,Ğ,Ö,Ş,Ü”

I need to make encryption and decryption and program shouldnt have case sensitive. 我需要进行加密和解密,并且程序不应该区分大小写。 It should be like s=S, ç=Ç 应该像s = S,ç=Ç

You can see my program below ,but I have some problems 您可以在下面看到我的程序,但是我有一些问题

1) Text(Plain) and key should entered by user but I couldnt do it. 1)文本(普通)和密钥应该由用户输入,但我做不到。

2) char text[] = "DEF"; 2)char text [] =“ DEF”; this input should give (for decrypt) "CÇD" but it gives "CÃD" 此输入应给出(用于解密)“CÇD”,但给出“CÃD”

normally it should give "Ç" instead of "Ã" 通常应使用“Ç”代替“Ô

I need help :( 我需要帮助 :(

# 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;
}

This issue is that your program is using a different encoding than the one the console expects. 此问题是您的程序使用的编码与控制台期望的编码不同。 Windows is configured this way by default; Windows默认情况下是用这种方式配置的; programs use encodings like cp1252 or cp1254 and the console expects something else like cp437. 程序使用诸如cp1252或cp1254之类的编码,并且控制台期望使用诸如cp437之类的其他内容。

Here's an article from a Microsoft developer that explains why this is. 是Microsoft开发人员的文章,解释了为什么会这样。

There's already a lot of information online covering the numerous ways you can fix the encoding mismatch. 在线信息已经很多,涵盖了许多解决编码不匹配的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM