Advertisement
Guest User

Encyptioon

a guest
Feb 24th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. ----------------------OBJECT . CPP-----------------------------
  2.  
  3. #include "Message.h"
  4.  
  5. using namespace std;
  6.  
  7. Message::Message(std::string filename) {
  8. fstream fin(filename);
  9. if (fin.fail())
  10. {
  11. cout << "failed";
  12. } else {
  13. length = getFileSize(fin);
  14. message = new char[length];
  15. fin.getline (message, length); {
  16. fin >> message;
  17. }
  18. }
  19. fin.close();
  20. }
  21.  
  22. Message::~Message()
  23. {
  24. //dtor
  25. }
  26. void Message::decode() {
  27. int offset;
  28. strcpy(code, "iztohndbeqrkglmacsvwfuypjx");
  29. for (int i = 0; i < strlen(message); i++) {
  30. if (message[i] == ' ') continue;
  31. if (message[i] == ',') continue;
  32. if (message[i] == '.') continue;
  33. offset = int (message[i] - 'a');
  34. message[i] = code[offset];
  35. }
  36. }
  37.  
  38. void Message::fixCapitalization() {
  39. for (int i = 0; i < strlen(message); i++) {
  40. if (message[0] != ' ' || message[0] != ',') {
  41. message[0] = toupper(message[0]);
  42. }
  43. if (message[i] == '.' || message[i] == '?' || message[i] == ',') {
  44. message[i + 2] = toupper(message[i + 2]);
  45. }
  46. }
  47. }
  48.  
  49. void Message::dump() const {
  50. for (int i = 0; i < strlen(message); i++){
  51. cout << message[i];
  52. }
  53. }
  54.  
  55. bool Message::isEmpty() const {
  56. if (length == 0) {
  57. return true;
  58. } else {
  59. return false;
  60. }
  61. }
  62.  
  63. ----------------------- .H FILE ----------------------------
  64. /*
  65. * Message.h
  66. *
  67. * Created on: Dec 11, 2016
  68. * Author: hellenpacheco
  69. */
  70.  
  71. #ifndef MESSAGE_H_
  72. #define MESSAGE_H_
  73.  
  74. #include <fstream>
  75. #include <string>
  76. #include <cstring>
  77. #include <cstdlib>
  78. #include <iostream>
  79.  
  80. class Message
  81. {
  82. private:
  83. char *message; // holds the message
  84. int length; // holds the the message length
  85. static const short ALPHABET_SIZE = 26;
  86. char code[ALPHABET_SIZE]; // holds the cypher alphabet
  87. // iztohndbeqrkglmacsvwfuypjx
  88. // ex: an 'a' in the original message should be converted to 'i', 'b' should be converted to 'z' and so forth
  89.  
  90. // returns the input file size in bytes
  91. std::streamsize getFileSize(std::fstream &file) const
  92. {
  93. std::streamsize fsize = 0;
  94. file.seekg (0, std::ios::end);
  95. fsize = file.tellg();
  96. file.seekg (0, std::ios::beg); // moves file pointer back to the beginning
  97. return fsize;
  98. }
  99. public:
  100. /*
  101. * This constructor tries to open the file whose name is passed
  102. * to it in filename. If file opens successfully, calls function
  103. * getFileSize to determine how many bytes should be allocated
  104. * for the message. Allocates space for message and reads the
  105. * content from the file into it. Closes the file at the end.
  106. * Member variable length should be set to the file size.
  107. * If file cannot be found, length should be set to zero.
  108. */
  109. Message(std::string filename);
  110.  
  111. // The destructor frees the space allocated to message
  112. virtual ~Message();
  113.  
  114. // Decodes the message
  115. void decode();
  116.  
  117. // Capitalizes first letter in each sentence
  118. void fixCapitalization();
  119.  
  120. // Prints the content of message on the screen
  121. void dump() const;
  122.  
  123. // Returns true if the message is empty
  124. bool isEmpty() const;
  125. };
  126.  
  127.  
  128.  
  129. #endif /* MESSAGE_H_ */
  130.  
  131. ---------------------------MAIN.CPP -----------------------
  132. #include <iostream>
  133. #include <stdlib.h>
  134. #include <fstream>
  135. #include "Message.h"
  136.  
  137. using namespace std;
  138.  
  139. int main()
  140. {
  141. // create a message object with the content of Encrypted.txt
  142. Message m ("Encrypted.txt");
  143.  
  144. if (m.isEmpty())
  145. {
  146. cout << "Could not read message";
  147. return EXIT_FAILURE;
  148. }
  149. cout << "Original message: " << std::endl;
  150. m.dump();
  151. cout << std::endl << std::endl;
  152. m.decode();
  153. m.fixCapitalization();
  154. cout << "Decoded message: " << std::endl;
  155. m.dump();
  156. cout << std::endl << std::endl;
  157.  
  158. return EXIT_SUCCESS;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement