gpt4 book ai didi

C++::toupper 不允许相等比较?

转载 作者:行者123 更新时间:2023-11-28 02:21:16 24 4
gpt4 key购买 nike

我正在尝试将字符串转换为大写以便我可以对其进行操作,但是虽然我可以成功地操作自然大写字符串以及将小写转换为大写,但使用这种转换方法无法进行操作。

例如,如果我通过加密传递“hello”,我的加密字符串将变为“HELLO”,但是当我通过(自然大写)传递“HELLO”时,它会正确移位。

我需要使用其他强制大写的方法还是我做错了什么?

int Caesar::encrypt (const std::string &message, std::string &emessage) {
int count = 0;
emessage = message;
std::transform(emessage.begin(), emessage.end(), emessage.begin(), ::toupper);
for (std::string::size_type i = 0; i < message.size(); i++) {
for (int j = 0; j < 26; j++) {
if (emessage[i] == std_alphabet[j]) {
std::replace(emessage.begin(), emessage.end(), message[i], c_alphabet[j]);
}
}
count++;
}
return count;
}

构造函数:

Caesar::Caesar (int shift) {
// loop to populate vector with 26 letters of English alphabet
// using ASCII uppcase letter codes
for (int i = 0; i < 26; i++) {
std_alphabet.push_back(i + 65);
}
// fills Caesar alphabet with standard generated alphabet
c_alphabet = std_alphabet;
// shifts Caesar alphabet based off the constructor parameter
std::rotate(c_alphabet.begin(), c_alphabet.begin() + shift, c_alphabet.end());
}

测试文件:

void testCaesar() {
Caesar test(4);
std::string original = "HELLO";
std::string encrypted = "";
test.encrypt(original,encrypted);
std::cout << encrypted << std::endl;
std::cout << original << std::endl;
}

int main() {
testCaesar();
return 0;
}

显然有一个标题和包含内容,但这是基本代码

头文件包含两个私有(private) vector

最佳答案

您看到的具体问题是您在此处替换了错误的内容:

std::replace(emessage.begin(), emessage.end(), message[i], c_alphabet[j]);

如果 message 是小写字母,那么 emessage 将全部是大写字母——没有一个是 message[i]。所以替换不会做任何事情。你的意思是:

std::replace(emessage.begin(), emessage.end(), emessage[i], c_alphabet[j]);
^^^^^^^^^^^

也就是说,您的算法是完全错误的,因为 HELLO 加密为 BCBBA,移位 4。字母上存在 1-1 映射,所以 HL 不能同时转到 B。你想要做的是移动每个字母,只需将它替换为下一个字母应该是的。即:

for (std::string::size_type i = 0; i < emessage.size(); ++i) {
emessage[i] = c_alphabet[emessage[i] - 'A'];
}

您实际上不需要初始转换步骤:

emessage = message;
for (std::string::size_type i = 0; i < emessage.size(); ++i) {
emessage[i] = c_alphabet[::toupper(emessage[i]) - 'A'];
}

只需删除 count(这只是大小,所以是多余的)并按值获取消息,整个事情就可以大大简化:

std::string encrypt(std::string from) { // intentionally copying
for (char& c : from) {
c = c_alphabet[::toupper(c) - 'A'];
}
return from;
}

关于C++::toupper 不允许相等比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32381877/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com