[英]I'm working on a cipher code using modulos and the letter “u” won't decode properly
this is the code when I run it "u" won't show if I type in "under" it will decode as "nder" I'm not sure whether it's the use of modulus or the math in my problem I've tried with many other letters and the only real issue is with "u"
这是我运行它时的代码,如果我键入“ under”,则不会显示,它将解码为“ nder”。我不确定是我尝试过的问题中使用的是模数还是数学还有很多其他字母,唯一真正的问题是“ u”
#include <iostream>
std::string decode(std::string input, int shift_amount);
std::string encode(std::string input, int shift_amount);
int main(int argc, char **argv)
{
std::string x = "{qr-or-{rny";
std::string output;
output = decode(x, 13);
std::cout << output << std::endl;
}
std::string encode(std::string input, int shift_amount)
{
for (int i = 0; i < input.length(); i++)
{
input[i] = (input[i] + shift_amount) % 255;
}
return input;
}
std::string decode(std::string input, int shift_amount)
{
for (int i = 0; i < input.length(); i++)
{
input[i] = (input[i] - shift_amount) % -255;
}
return input;
}
Simple test: 简单测试:
#include <iostream>
int
main()
{
std::cout << (- 13) % (- 255);;
return 0;
}
The program prints -13. 程序打印-13。
The %
operation is not the modulo operation , but the remainder: you will not go back to a positive number from a negative one. %
运算不是取模运算 ,而是余数:您不会从负数返回正数。 You have to add 255 for negative numbers. 您必须为负数添加255。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.