Advertisement
Boucherwayne78

Bouch's Final Product :D

Dec 8th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. //function prototypes
  9. char encipher(char key, char plain);
  10. char decipher(char key, char cipher);
  11. void pressEnterToContinue(void);
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15.     //declarations
  16.     string keyWord = string(argv[2]);
  17.     char plainChar;
  18.     char cipherChar;
  19.     string inputFileName = argv[3];
  20.     string outputFileName = argv[4];
  21.  
  22.     //opening input and output streams for files
  23.     ifstream inStream;
  24.     inStream.open(inputFileName);
  25.     ofstream outStream;
  26.     outStream.open(outputFileName);
  27.     //test if fail to open file
  28.     if (inStream.fail())
  29.     {
  30.         cout << "File nonexistent!" << endl;
  31.         pressEnterToContinue();
  32.     }
  33.  
  34.     //checks if the "encode" flag (-e) is used
  35.     if (argv[1][0] == '-' && argv[1][1] == 'e')
  36.     {
  37.         //repeats getting new character, enciphering it, and dumping to output file.
  38.         int i = 0;
  39.         while (true)
  40.         {
  41.             i++;
  42.             if (i == keyWord.length()) i = 0;
  43.             inStream.get(plainChar);
  44.             if (inStream.eof()) break;
  45.             char ch = encipher(keyWord[i], plainChar);
  46.             outStream << ch;
  47.         }
  48.     }
  49.  
  50.  
  51.     //checks if the "decode" flag (-d) is used
  52.     else if (argv[1][0] == '-' && argv[1][1] == 'd')
  53.     {
  54.         //repeats getting new character, enciphering it, and dumping to output file.
  55.         int i = 0;
  56.         while (true)
  57.         {
  58.             i++;
  59.             if (i == keyWord.length()) i = 0;
  60.             inStream.get(cipherChar);
  61.             if (inStream.eof()) break;
  62.             char ch = decipher(keyWord[i], cipherChar);
  63.             outStream << ch;
  64.         }
  65.     }
  66.  
  67.     //if neither flag is used, throws incorrect input.
  68.     else cout << "incorrect flag input. " << endl;
  69.  
  70.     //closing program
  71.     inStream.close();
  72.     outStream.close();
  73.     cout << "Done!" << endl;
  74.     pressEnterToContinue();
  75.     return 0;
  76. }
  77.  
  78. //Enciphers a character with a key and plain text.
  79. //preconditions: char key, char plain
  80. //postconditions: cipher
  81. char encipher(char key, char plain)
  82. {
  83.     char cipher;
  84.     if (plain == '\n') return plain;
  85.     int offset = ' ';
  86.     int alphabet = 95; // The ASCII printable alphabet is 95 characters.
  87.     cipher = (((plain - offset) + (key - offset)) % alphabet) + offset;
  88.     return cipher;
  89. }
  90.  
  91. //Deciphers an enciphered character with a key and enciphered text.
  92. //preconditions: char key, char cipher (enciphered text)
  93. //postconditions: char plain (deciphered text)
  94. char decipher(char key, char cipher)
  95. {
  96.     char plain;
  97.     if (cipher == '\n') return cipher;
  98.     int offset = ' ';
  99.     int alphabet = 95; // The ASCII printable alphabet is 95 characters.
  100.     plain = ((cipher - offset) - (key - offset));
  101.     if (plain < 0) plain += alphabet;
  102.     plain += offset;
  103.     return plain;
  104. }
  105.  
  106. //Ends program, no pre/post conditions.
  107. void pressEnterToContinue(void)
  108. {
  109.     cout << "\nPress 'Enter' to continue... ";
  110.     cin.ignore(1024, '/n');
  111.     cin.clear();
  112.     return;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement