Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------------OBJECT . CPP-----------------------------
- #include "Message.h"
- using namespace std;
- Message::Message(std::string filename) {
- fstream fin(filename);
- if (fin.fail())
- {
- cout << "failed";
- } else {
- length = getFileSize(fin);
- message = new char[length];
- fin.getline (message, length); {
- fin >> message;
- }
- }
- fin.close();
- }
- Message::~Message()
- {
- //dtor
- }
- void Message::decode() {
- int offset;
- strcpy(code, "iztohndbeqrkglmacsvwfuypjx");
- for (int i = 0; i < strlen(message); i++) {
- if (message[i] == ' ') continue;
- if (message[i] == ',') continue;
- if (message[i] == '.') continue;
- offset = int (message[i] - 'a');
- message[i] = code[offset];
- }
- }
- void Message::fixCapitalization() {
- for (int i = 0; i < strlen(message); i++) {
- if (message[0] != ' ' || message[0] != ',') {
- message[0] = toupper(message[0]);
- }
- if (message[i] == '.' || message[i] == '?' || message[i] == ',') {
- message[i + 2] = toupper(message[i + 2]);
- }
- }
- }
- void Message::dump() const {
- for (int i = 0; i < strlen(message); i++){
- cout << message[i];
- }
- }
- bool Message::isEmpty() const {
- if (length == 0) {
- return true;
- } else {
- return false;
- }
- }
- ----------------------- .H FILE ----------------------------
- /*
- * Message.h
- *
- * Created on: Dec 11, 2016
- * Author: hellenpacheco
- */
- #ifndef MESSAGE_H_
- #define MESSAGE_H_
- #include <fstream>
- #include <string>
- #include <cstring>
- #include <cstdlib>
- #include <iostream>
- 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;
- };
- #endif /* MESSAGE_H_ */
- ---------------------------MAIN.CPP -----------------------
- #include <iostream>
- #include <stdlib.h>
- #include <fstream>
- #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())
- {
- cout << "Could not read message";
- return EXIT_FAILURE;
- }
- cout << "Original message: " << std::endl;
- m.dump();
- cout << std::endl << std::endl;
- m.decode();
- m.fixCapitalization();
- cout << "Decoded message: " << std::endl;
- m.dump();
- cout << std::endl << std::endl;
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement