對於我的C++類我們最新的實驗室是寫一個解碼程序,像這樣:憋屈的解碼程序
Write a program to read a file containing an encrypted message, which must be decoded and printed on the screen. Use the following key to decode: input text : abcdefghijklmnopqrstuvwxyz decoded text: iztohndbeqrkglmacsvwfuypjx
That means each 'a' in the input text should be replaced with an 'i', each 'b' with a 'z' and so forth. Punctuation and space should be kept as is. You will notice that all the letters in the text are lowercase, so the second step will be to fix the captalization. First letter of each sentence should be capitalized. Print the decoded text to the screen. You must use an object-oriented approach on this lab. The specification of class Message is given in header file Message.h. You need to implement each of the member functions in the Message.cpp, which you will turn in. The main function is also given so you can test your class, but you don't have to turn it in. I have added in the class definition in "Message.h" that explain how to implement each member function. Constructor: Should open the text file and determine its size. To do this, call getFileSize() which is implemented in "Message.h". Check for errors when opening the input file and don't forget to close it at the end. If there is an error, such as the file can't be located, make sure to set length to zero. Otherwise, use the file size to allocate space for message. Destructor: should free the space allocated for message.
decode: decodes the message according to the given key.
fixCapitalization: capitalizes the first letter of each sentence.
dump: prints the content of message on the screen
isEmpty: returns whether message is empty of not.
You only have to turn in message.cpp. You have to make sure it works with the header file that I'm providing, since it will be used to compile your message.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;
};
什麼我很堅持on是如何使用構造函數打開文件並讀取它的大小。
我甚至無法弄清楚如何用構造函數打開文件。 我想在我的構造函數中使用ifstream的,像這樣:
編輯17年2月24日10:36時三十分:
#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;
}
這是我到目前爲止已經放在一起。當我運行該程序時,它會告訴我「無法讀取消息」,因此我認爲該文件未正確打開。有人可以檢查我的代碼,並告訴我哪裏出錯了嗎?
當我嘗試過,日食給了我一個錯誤信息,說沒有找到成員聲明。 –
更新你的聲明,像這樣'Message(const std :: string&filename);'在頭文件中 – Rishi
我不應該改變頭文件,反正我可以在不調整頭的情況下工作嗎? –