我是一个完全使用c ++的初学者,到目前为止,在学校里我们只学习和使用Java。今年的第一个项目是创建凯撒密码,但是我们必须使用教授提供的头文件。在这一点上,我只想转移字母并证明我的概念,然后再对加密和解密方法进行编码。关于我做错了什么以及为什么不编译该代码的任何帮助将是惊人的。这是我们的Header文件,我们完全不允许对此文件进行任何更改:// include file for Caesar cipher code//#ifndef CAESAR_H#define CAESAR_H#include <string>class Caesar {private: //pointers used to refer to the standard alphabet array and the Caesar shift array char* std_alphabet; char* c_alphabet;public: // The constructor . . . // create the two arrays with the c_alphabet array contents representing the std_alphabet // characters shifted by a value of the shift parameter Caesar(int shift = 0); // encrypt a message. Returns count of characters processed // first string is message, second is encrypted string int encrypt(const std::string& message, std::string& emessage); // decrypt a message. Returns count of characters processed // first string is encrypted string, second is decrypted string int decrypt(const std::string& message, std::string& dmessage); //delete any memory resources used by the class ~Caesar();}; // end of class . . .#endif这是我的.cpp文件,我仅处于当前尝试在数组中移动字母的阶段,但是我不明白为什么我的头文件使用指针或我是否正确创建了数组(我能够得到这个可以在单独的文件中使用但不使用头文件的概念)。我只是在继续进行任何编码之前尝试打印这些行以确保其有效:#ifndef CAESAR_C#define CAESAR_C#include <string>#include <iostream>#include "Caesar.h"using namespace std;int shift, i, k;char letter = 'a';Caesar::Caesar(const int n) { shift = n; std_alphabet[26]; c_alphabet[26]; for (i = 0; i < 26; i++) { std_alphabet[i] = letter; letter++; } for (i = 0; i < 26; i++) { cout << std_alphabet[i] << " "; } cout << endl; for (i = 0; i < 26; i++) { k = (i + shift) % 26; c_alphabet[i] = std_alphabet[k]; } for (i = 0; i < 26; i++) { cout << c_alphabet[i] << " "; }};#endif这是我的测试文件,我真的不知道如何正确启动Caesar对象。就像我说的那样,我是使用c ++的完整入门者,并且非常感谢任何指导:#include <string>#include <iostream>#include "Caesar.h"using namespace std;int main() { Caesar* test = new Caesar(5); cout << test; system("PAUSE"); return 0;}; 最佳答案 请不要介意我的答案是否含糊,因为我刚参加Stackoverflow似乎您尚未对此问题进行过多研究,甚至在提出此问题之前试图自己找出解决方案。但是无论如何,我将尽力为您提供帮助。首先执行这些操作(我想已经知道这些事情了):首先,当然,将头文件和cpp文件分别保存为Caesar.h和Caesar.cpp,而test.cpp可能是测试文件。然后,由于使用函数system(),因此在测试文件中包含标头“ stdlib.h”。然后使用命令g ++ Caesar.cpp test.cpp -o test编译程序并运行它。现在,您的代码的主要问题是cout 首先必须为类'Caesar'重载运算符'http://www.geeksforgeeks.org/overloading-stream-insertion-operators-c/而且,“凯撒”一类的设计策略并不令人满意。我的意思是'Caesar.cpp'中的变量'shift'应该是'Caesar'类的私有成员变量,这更有意义。无论如何,这些只是面向对象的问题。还有一件事是,构造函数包含两行:std_alphabet [26]和c_alphabet [26]老实说,我不知道这是什么奇怪的语法,但是它却以某种方式在我的系统中被编译。但这就是导致程序在运行时崩溃的原因。因此,我建议只用std_alphabet = new char [26]替换这两行。与c_alphabet类似。最后,您只需要在文件'Ceasar.cpp'中定义函数crypto()和crypto(),并且我希望您可以做很多,因为这是您的工作:D 祝你好运!关于c++ - 凯撒密码C++(数组和指针),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46102748/
10-10 00:45