凯撒密码删除数据
Caesar Cipher deleting Data
我想知道是否有人会帮助我麻烦拍摄我的代码。它似乎正在正常工作,但是当我运行它的一半时,即使我从未打电话给任何内容来删除它。
所以例如,我的文件来自:
=======================
Rotated by 11 positions
=======================
Lnwypeyw lala sqz xa na lnwypeyw eb pdau sqz pwa w eppa
na pea bn znawec.
-- F. L. IyAru**
to
=======================
Rotated by 12 positions
=======================
Moxzqzx mbmb tra yb ob moxzqzx c qebv tra qxb x qqb
ob qb co aobxd.
-- G. M. JzBsv
这是我的代码,我尝试过几次,这在逻辑上都很有意义,并且在丢失数据时不知道。
#include <iostream>
#include <cctype>
#include <fstream>
using namespace std;
bool isUpper(char character){
//this will test and see if it's upper or lowercase
bool upper;
if(character>= 'A' && character <= 'Z'){
upper=true;
}
else {
upper= false;
}
return upper;
}
bool isLower(char character){
//this will test and see if it's upper or lowercase
bool lower;
if(character >= 'a' && character <= 'z'){
lower=true;
}
else {
lower= false;
}
return lower;
}
//Actual function that will rotate the character
char rotate(char character, int offset) {
char next_letter;
//Changes it if it's a lower case
if (isLower(character)) {
next_letter = character + offset;
if (next_letter > 'z'){
next_letter = (next_letter - 26);
return next_letter;
}
return next_letter;
}
else if(isUpper(character)) {
next_letter = character + offset;
if (next_letter > 'Z'){
next_letter = (next_letter - 26);
return next_letter;
}
return next_letter;
}
else {
return character;
}
}
int main() {
//variables for program
char character = 'a';
int offset = 0;
while(offset < 26){
//opens the file
ifstream fin;
fin.open("secretMessage.txt");
if(!fin.good()) {
cout << "Please check your file name!!!";
return 0;
}
//report for reading
cout << "=======================" << endl
<< "Rotated by " << offset << " positions" << endl
<< "=======================" << endl;
//Reads until it's at the end of the file
while ((! fin.eof())){
character = fin.get();
cout << rotate(character,offset);
}
//makes it run 26 times
++offset;
fin.close();
cout << endl << endl;
}
//Closes the file output.
return 0;
}
在此逻辑中:
if (next_letter > 'z') {
next_letter = (next_letter - 26);
return next_letter;
}
您没有想到签署Char会发生什么。测试它:
assert(rotate('z', 0) == 'z');
assert(rotate('z', 1) == 'a');
assert(rotate('z', 2) == 'b');
assert(rotate('z', 3) == 'c');
assert(rotate('z', 4) == 'd');
assert(rotate('z', 5) == 'e');
到目前为止很好,但是现在:
assert(rotate('z', 6) == 'f'); // WHOOPS
ascii'z' 5是127,'z' 6是128。但是,z = 6结果为负,因此检查> 'z'
失败。
建议的简化:
bool isUpper(char ch) { return (ch >= 'A' && ch <= 'Z'); }
bool isLower(char ch) { return (ch >= 'a' && ch <= 'z'); }
// Actual function that will rotate the character
char rotate(char ch, int offset) {
if (isLower(ch)) return (ch - 'a' + offset) % 26 + 'a';
if (isUpper(ch)) return (ch - 'A' + offset) % 26 + 'A';
return ch;
}
由于您包括<cctype>
,因此您还可以使用std::islower
和std::isupper
。
活在coliru
#include <cctype>
#include <fstream>
#include <iostream>
#include <algorithm>
// Actual function that will rotate the character
char rotate(char ch, int offset) {
if (std::islower(ch)) return (ch - 'a' + offset) % 26 + 'a';
if (std::isupper(ch)) return (ch - 'A' + offset) % 26 + 'A';
return ch;
}
std::string readFile(std::string const& fname) {
std::ifstream fin;
fin.exceptions(std::ios::failbit);
fin.open(fname);
return { std::istreambuf_iterator<char>(fin), {} };
}
int main() {
auto const original = readFile("main.cpp");
// makes it run 26 times
for (int offset = 0; offset < 26; ++offset) {
std::cout
<< "n"
<< "=======================n"
<< "Rotated by " << offset << " positionsn"
<< "=======================n";
// Reads until it's at the end of the file
std::transform(
original.begin(), original.end(),
std::ostreambuf_iterator<char>(std::cout),
[offset](char ch) { return rotate(ch, offset); });
}
}
奖金
实际上,您可以通过在原地旋转16次而无需主的复杂性:
活在coliru
auto text = readFile("main.cpp");
for (int offset = 0; offset < 26; ++offset) {
std::cout
<< "n"
<< "=======================n"
<< "Rotated by " << offset << " positionsn"
<< "=======================n";
std::cout << text;
for (auto& ch : text) ch = rotate(ch, 1);
}
相关文章:
- 在CRC-16 CCITT中将数据从二进制解码为文本,我应该输入一个码字,使用CRC生成器进行编码
- 从 Typedef 数据类型中删除常量
- 从输入中删除重复项,而不使用任何数据结构
- 删除使数据处于"错误状态"的默认构造函数的模式?
- 从自定义数据类型向量中删除重复元素
- 删除K的背景掩码-意味着Python或C++中的集群/
- 如何使用C++删除/更新txt文件中的特定数据
- 如何使用C++删除/更新txt文件中的特定数据?
- 从文本文件中读取数据并删除所有换行符空格,并在 C++ 控制台中显示
- C++ - 按自定义数据类型向量的值删除元素
- 如何从 txt 文件中删除数据/内容
- 如何通过其持有的数据从链表中删除某个节点?
- 基于给定字符串数据类型的链表删除节点
- 如何在 C++ 中使用删除运算符删除单个数据
- QR码生成算法数据掩盖实现案例分析
- 如何在每次循环迭代期间生成向量,存储数据,然后删除该向量?
- 如何从 C 中的数据中删除 Ì
- 为什么我不能从此数据集中删除虚假的 0?
- C++二叉搜索树比较节点的数据并删除重复项
- 链表数据编辑删除整个链表