卡在一个解码程序上

Stuck on a decoding program

本文关键字:一个 解码 程序上      更新时间:2023-10-16

对于我的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;
}