我的解码程序的两个部分没有按预期工作
Two parts of my decoding program aren't working as expected
几天前我来这里寻求帮助(更具体的部分(,但我得到的解决方案不太有效。基本上,我正在编写一个用于3个目的的程序:解码Rot 13密码,解码Rot 6密码,并通过等式"x=2n-1"输入用户输入,其中n是用户输入。
Rot 13 工作正常,但 Rot 6 输出乱码,方程输出一个字母(输入"8"会给你 o 而不是 15(
我知道这可以用更少的功能来完成,我可能不需要列表,但这是为了作业,我需要它们
我知道我不擅长这个,但任何帮助都会很棒
#include <iostream>
#include <string>
#include <list>
#include <array>
using namespace std;
string coffeeCode(string input) { //Coffee code= 2n-1 where n=a number in a string
double index{};
input[index] = 2*input[index]-1;
return input;
};
string rot6(string input) {
int lower[] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ' };
int upper[] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };
int inputSize = input.size(); // rot 6 rotates letters so that a{0}->g[6]
int index{}; // m[12]->s[18]
// n->t
while (index != inputSize) { // z->f
if (input[index] >= lower[0] && input[index] <= lower[19])
input[index] = input[index] + 6;
else if (input[index] >= lower[20] && input[index] <= lower[25])
input[index] = input[index] - 20;
else if (input[index] >= upper[0] && input[index] <= upper[19])
input[index] = input[index] + 6;
else if (input[index] <= upper[20] && input[index] <= upper[25])
input[index] = input[index] - 20;
index++;
}
return input;
}
string rot13(string input) { //Decodes into rot 13
int lower[] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',' ' };
int upper[] = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z' };
int inputSize = input.size();
int index{};
while (index != inputSize) {
if (input[index] >= lower[0] && input[index] <= lower[12])
input[index] = input[index] + 13;
else if (input[index] >= lower[13] && input[index] <= lower[25])
input[index] = input[index] - 13;
else if (input[index] >= upper[0] && input[index] <= upper[12])
input[index] = input[index] + 13;
else if (input[index] <= upper[13] && input[index] <= upper[25])
input[index] = input[index] - 13;
index++;
}
return input;
}
int main() {
string plaintext;
string ans13;
string ans6;
string ansCoffee;
cout << "Whats the message Spy Guy: ";
getline(cin, plaintext);
ans13 = rot13(plaintext);
ans6 = rot6(plaintext);
ansCoffee = coffeeCode(plaintext);
cout << "One of these is your decoded message" << endl << "In Rot 13: " << ans13 << endl << "In Rot 6: " << ans6 << endl
<< "In Coffee Code: " << ansCoffee << endl;
return 0;
}
了解您正在做什么的最佳机会是阅读 c++ 如何处理字符以及 ASCII 是什么。
[http://www.asciitable.com/]
只是一个快速总结:
ASCII 表为英文字母表中的每个字符分配一个数字(或代码(,该数字是C++实际存储在内存中的数字。 因此,当您执行char c = 'a'
时,这相当于执行char c = 97
。
ASCII表也非常有条理,因此所有大写字母都按字母顺序排列,从65(即A(到90(即Z(。非大写字母也是如此,从 97 到 122,数字字母从 48 到 57。 这可用于确定变量是哪种字符:
if ('a' <= input[index] && input[index] <= 'z') {
// It's lower case
}
if('A' <= input[index] && input[index] <= 'Z') {
// It's upper case
}
请注意,当您将单字符放在单引号中时,编译器会将其替换为其 ASCII 代码,因此您实际上不需要记住该表。考虑如何构造 if 来确定字符是否为数字。
以下是我将如何实现 rot6。它可能不是最好的,但我认为没关系。
string rot6(string input) {
for (int index = 0; index < input.size(); ++ index) {
if ('a' <= input[index] && input[index] <= 'z') { // It's lower case
input[index] = 'a' + ((input[index] - 'a') + 6) % 26;
}
else if ('A' <= input[index] && input[index] <= 'Z') { // It's upper case
input[index] = 'A' + ((input[index] - 'A') + 6) % 26;
}
else if ('0' <= input[index] && input[index] <= '9') { // It's a digit
input[index] = '0' + ((input[index] - '0') + 6) % 10;
}
else { // It's an error
return "Error, bad input!";
}
}
return input;
}
相关文章:
- C++控制台应用,其中有两个冲突的对象不工作
- 我的解码程序的两个部分没有按预期工作
- 给定的两个代码有什么区别.一个在 ideone 上运行时超出时间限制,另一个工作正常
- 使用两个命名空间只能部分工作
- 在循环工作时,首先将两个文件读为向量,但仅次于迭代一次
- OpenGL Ping Pong可与一张通行证一起工作,而不是两个通行证
- 两个超载的STD ::前进工作如何
- 除了两个构造函数外,C++库导入也可以工作
- 在打印出动态分配的数组中的前两个数字时遇到问题,其他数字工作正常
- 如何使其工作,两个类的问题
- 如何在路径规格中工作两个点
- 具有两个OpenGL图形视图的MFC MDI拆分器无法工作
- 逗号(,)在条件部分中两个表达式之间的for循环中的工作方式
- 提升:两个工作线程,使主线程休眠,直到它们都完成
- C++模需要在两个*无*符号字节之间进行减法转换才能工作,为什么
- 具有相同名称和不同模板参数的两个结构如何工作
- 两个双精度值的 C++ 比较无法正常工作
- 当两个不同的参数中有空格时,C++系统()不工作
- 如何使这两个模板类协同工作?(C++中的属性)
- 通过使用两个凸壳的切线来合并它们的算法在实践中是如何工作的