2017-02-24 36 views
2

對於我的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; 
} 

這是我到目前爲止已經放在一起。當我運行該程序時,它會告訴我「無法讀取消息」,因此我認爲該文件未正確打開。有人可以檢查我的代碼,並告訴我哪裏出錯了嗎?

回答

0

這會幫助你:

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() 
} 

爲使用EDIT

int main() { 
    Message m("Encrypted.txt"); 
    // other operations on m 
    return 0; 
} 
+0

當我嘗試過,日食給了我一個錯誤信息,說沒有找到成員聲明。 –

+0

更新你的聲明,像這樣'Message(const std :: string&filename);'在頭文件中 – Rishi

+0

我不應該改變頭文件,反正我可以在不調整頭的情況下工作嗎? –

1
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; 
} 
+0

啊感謝你的支持。你碰巧知道爲什麼我會得到「啓動失敗,未找到二進制文件」。錯誤? –

+1

@ A.huy可能是因爲您使用的任何IDE都無法找到該程序,可能是因爲編譯或鏈接失敗。從這裏無法確定。 – molbdnilo

+0

我認爲鏈接失敗,但我不知道如何解決這個問題。這是完整的消息:「架構x86_64沒有找到ld:符號 clang:錯誤:連接器命令失敗,退出代碼1」 –