您好,例如,我对Caesar密码有新问题
键:3
平原:ABCÇDEFGĞHIİJKLMNOÖPRSŞTUÜVYZ
密码:DEFGĞHIİJKLMNOÖPRSŞTUÜVYZABCD
这些是土耳其字母“ç,ı,ğ,ö,ş,ü,Ç,İ,Ğ,Ö,Ş,Ü”
我需要进行加密和解密,并且程序不应该区分大小写。应该像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之类的其他内容。
Here's Microsoft开发人员的一篇文章,解释了为什么这样做。
在线信息已经很多,涵盖了许多解决编码不匹配的方法。
关于c++ - C++错误的输出字母,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15576061/