卡在一个解码程序上
Stuck on a decoding program
对于我的C 类,我们最新的实验室是写一个解码程序:
编写一个程序以读取包含加密消息的文件,其中 必须在屏幕上解码并打印。使用以下键 解码:输入文本:abcdefghijklmnopqrstuvwxyz解码文本: iztohndbeqrkglmacsvwfuypjx
这意味着输入文本中的每个'a'应该用" i"替换 每个" b"带有" z",依此类推。标点符号和空间应该是 保持原样。您会注意到文本中的所有字母都是 小写,因此第二步将是修复小载体化。第一的 每个句子的字母应大写。打印解码 发短信到屏幕。您必须对此使用面向对象的方法 实验室。类消息的规范在标题文件中给出 消息。您需要在 Message.cpp,您将上交。还给出了主要功能 因此您可以测试您的课程,但您不必上交。我有 在" message.h"中的类定义中添加,该定义说明了如何 实现每个成员功能。构造函数:应打开文本 文件并确定其大小。为此,请致电GetFilesize() 在" message.h"中实现。打开输入时检查错误 文件,不要忘记在最后关闭它。如果有错误, 例如该文件无法找到,请确保将长度设置为零。 否则,请使用文件大小为消息分配空间。 驱动器:应释放分配消息的空间。
解码:根据给定密钥解码消息。
fixcapitation:大写每个句子的首字母。
转储:在屏幕上打印消息的内容
isempty:返回消息是否空白。
您只需要上交消息。您必须确保它有效 使用我提供的标头文件,因为它将用于 编译您的消息.cpp。
提供的标头文件是:
class Message
{
private:
char *message; // holds the message
int length; // holds the the message length
static const short ALPHABET_SIZE = 26;
char code[ALPHABET_SIZE]; // holds the cypher alphabet
// iztohndbeqrkglmacsvwfuypjx
// ex: an 'a' in the original message should be converted to 'i', 'b' should be converted to 'z' and so forth
// returns the input file size in bytes
std::streamsize getFileSize(std::fstream &file) const
{
std::streamsize fsize = 0;
file.seekg (0, std::ios::end);
fsize = file.tellg();
file.seekg (0, std::ios::beg); // moves file pointer back to the beginning
return fsize;
}
public:
/*
* This constructor tries to open the file whose name is passed
* to it in filename. If file opens successfully, calls function
* getFileSize to determine how many bytes should be allocated
* for the message. Allocates space for message and reads the
* content from the file into it. Closes the file at the end.
* Member variable length should be set to the file size.
* If file cannot be found, length should be set to zero.
*/
Message(std::string filename);
// The destructor frees the space allocated to message
virtual ~Message();
// Decodes the message
void decode();
// Capitalizes first letter in each sentence
void fixCapitalization();
// Prints the content of message on the screen
void dump() const;
// Returns true if the message is empty
bool isEmpty() const;
};
我真正坚持的是如何使用构造函数打开文件并读取其大小。
我什至无法弄清楚如何使用构造函数打开文件。我试图在构造函数中使用ifstream这样的ifstream:
编辑2/24/17 10:36 A.M。:
#include <iostream>
#include <fstream>
#include <string>
#include "Message.h"
using namespace std;
int main()
{
// create a message object with the content of Encrypted.txt
Message m ("Encrypted.txt");
if (m.isEmpty())
{
std::cout << "Could not read message";
return EXIT_FAILURE;
}
std::cout << "Original message: " << std::endl;
m.dump();
std::cout << std::endl << std::endl;
m.decode();
m.fixCapitalization();
std::cout << "Decoded message: " << std::endl;
m.dump();
std::cout << std::endl << std::endl;
return EXIT_SUCCESS;
}
Message::Message(std::string filename)
{
fstream infile;
infile.open(filename.c_str(), std::ifstream::in);
// read contents
// close the file
if (!infile)
{
length=0;
}else
{
// getFileSize and allocate so much space for message
std::streamsize length = getFileSize(infile);
message = new char[length + 1];
//copy contents from file to message
for (int i = 0; i < length + 1; i++)
{
infile >> new char[i];
}
}
infile.close();
}
Message::~Message()
{
}
void Message::decode()
{
}
void Message::fixCapitalization()
{
}
void Message::dump() const
{
cout << message;
}
bool Message::isEmpty() const
{
if (length == 0)
{
return true;
}else
return false;
}
这就是我到目前为止所组合的。当我运行程序时,它告诉我"无法读取消息",因此我认为该文件未正确打开。有人可以为我检查我的代码,并告诉我我在哪里做错了吗?
char *message = new char;
*message = message[length];
为一个 char
分配空间,然后读取无效的位置message[length]
,该位置不确定。
您需要分配length + 1
字符。
我会这样写:
Message::Message(std::string filename)
: message(nullptr),
length(0)
{
std::ifstream infile(filename);
if (infile)
{
length = getFileSize(infile);
message = new char[length + 1];
// ...
}
}
std::ifstream
的攻击函数在开放的情况下关闭流,因此您无需手动执行此操作。(告诉您的老师有关;他们似乎被困在C的心态中。)
旁注:更有用的getFileSize
将恢复以前的读取位置,而不是假设它在开始时:
std::streamsize getFileSize(std::fstream &file) const
{
std::streamsize old_pos = file.tellg();
file.seekg (0, std::ios::end);
std::streamsize fsize = file.tellg();
file.seekg (old_pos, std::ios::beg); // moves file pointer back to the previous position
return fsize;
}
这应该为您提供帮助:
Message::Message(std::string filename)
{
std::ifstream infile;
infile.open(filename.c_str(), std::ifstream::in);
// read contents & initialize other member variables.
if (infile.is_open()) {
// getFileSize and allocate so much space for message
length = getFileSize(infile);
message = new char[length + 1];
//copy contents from file to message
}
// close the file finally
infile.close()
}
用法编辑
int main () {
Message m("Encrypted.txt");
// other operations on m
return 0;
}
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 挂起和取消挂起一个文件DLL
- 用C++中的一个变量定义一个常量
- 函数向量_指针有不同的原型,我可以构建一个吗
- 在c++中用vector填充一个简单的动态数组
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 预处理器:插入结构名称中的前一个行号
- 我在c++代码中生成了一个运行时#3异常
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 从链接列表c++中删除一个项目
- 在CRC-16 CCITT中将数据从二进制解码为文本,我应该输入一个码字,使用CRC生成器进行编码
- 霍夫曼解码功能反复反复压缩一个字符
- 卡在一个解码程序上
- 我如何解码一个列表的列表
- 对rapidjson,编码只是一个数字字符串,然后解码它
- 如何解码一个动画gif文件在MFC2010
- 如何解码一个AAC帧在一个时间使用c++