Advertisement
Boucherwayne78

Bouch's Final Product :D

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