Advertisement
lwytop

Wonyoung Lee MP#5

Jul 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. //
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <string>
  7. #include <fstream>
  8.  
  9. using namespace std;
  10.  
  11. string keyWord_In_Array(string keyword) {           // get keyword(HAPPINESS) to ready to put array.
  12.  
  13.     string code;                                    //save strong to easyly put data in array.
  14.     char ch = ' ';
  15.  
  16.     for (size_t i = 0; i < keyword.length(); i++)
  17.     {
  18.         if (keyword[i] != ch) {                     //use this to avoid duplication about code.
  19.             code += keyword[i];
  20.             ch = keyword[i];
  21.         }
  22.  
  23.  
  24.     }
  25.     return code;
  26. }
  27.  
  28. string Make_Rest_Code(string code) {                // use this fuction to remove code's alphabet on the alphabet A to Y.
  29.     string fullCode = "ABCDEFGHIJKLMNOPQRSTUVWXY";
  30.  
  31.     for (size_t i = 0; i < code.length(); i++)
  32.     {
  33.         int index = 0;
  34.         index = fullCode.find(code[i]);         // find positon of alphabet in the code.
  35.  
  36.         fullCode.erase(index, 1);               //erase it.
  37.  
  38.     }
  39.  
  40.     return fullCode;
  41. }
  42.  
  43. int main()
  44. {
  45.     string data;                //get data from file.
  46.     string code;
  47.     string restCode;
  48.  
  49.     int count1 = 0;
  50.     int count2 = 0;
  51.  
  52.     char chart[5][5];
  53.  
  54.     ifstream inputFile;
  55.     inputFile.open("c:\\temp\\mp5.txt");
  56.  
  57.     getline(inputFile, data);                   // get whole line from the file.
  58.     cout << "Keyword is " << data << endl;
  59.  
  60.     code = keyWord_In_Array(data);
  61.  
  62.     restCode = Make_Rest_Code(code);           
  63.  
  64.     for (size_t i = 0; i < size(chart); i++)
  65.     {
  66.  
  67.         for (size_t j = 0; j < size(chart[i]); j++)
  68.         {
  69.             if (count1 < code.length()) {           // use this condition to put code's alphabet first.
  70.  
  71.                 chart[i][j] = code[count1];              
  72.                 count1++;
  73.             }
  74.             else {
  75.                 chart[i][j] = restCode[count2];
  76.                 count2++;
  77.             }
  78.         }
  79.  
  80.     }
  81.  
  82.  
  83.     // use this codes to show array for users.
  84.  
  85.  
  86.     cout << endl << endl;
  87.  
  88.     cout << "         Col 0  Col 1  Col 2  Col 3  Col 4" << endl;
  89.     cout << "        __________________________________" << endl;
  90.  
  91.     for (size_t i = 0; i < size(chart); i++)
  92.     {
  93.         printf("Row %d:", i);
  94.  
  95.         for (size_t j = 0; j < size(chart[i]); j++)
  96.         {
  97.             cout << " |  ";
  98.             printf("%c ", chart[i][j]);
  99.         }
  100.         cout << " |\n";
  101.     }
  102.  
  103.     cout << "******************************************" << endl;
  104.  
  105.  
  106.  
  107.     while (!inputFile.eof()) {
  108.         getline(inputFile, data);
  109.  
  110.         cout << data.substr(2) << endl;     //use substr to remove useless data.("D "and"E ")
  111.  
  112.         string convertString = "";
  113.        
  114.  
  115.         if (data[0] == 'D') {               // get first position of data to decrypt data.
  116.            
  117.            
  118.             for (size_t i = 2; i < data.size(); i++)
  119.             {
  120.                 char convertValue = ' ';
  121.  
  122.  
  123.                 for (size_t j = 0; j < size(chart); j++)
  124.                 {
  125.  
  126.                     for (size_t k = 0; k < size(chart[j]); k++) {
  127.                         if (data[i] == chart[j][k]) {
  128.                             convertValue = chart[k][j];
  129.                             goto end;                               // if find same value on the arra, get out of for state to move (end:)
  130.                         }
  131.                         else {
  132.                             convertValue = data[i];                 // if doesn't find data on the array, save same value on the convertValue.
  133.                         }
  134.                     }
  135.                 }
  136.             end:                                                    // put data on the string.
  137.                 convertValue = tolower(convertValue);
  138.  
  139.                 convertString += convertValue;
  140.             }
  141.  
  142.             cout << "Decrypt to: " << endl;
  143.             cout << convertString << endl;
  144.             cout << "******************************************" << endl;
  145.  
  146.         }
  147.         else if (data[0] == 'E') {                                  // get data[0] to encrypt data.
  148.            
  149.  
  150.             for (size_t i = 2; i < data.size(); i++)
  151.             {
  152.                 char convertValue = ' ';
  153.  
  154.                 for (size_t j = 0; j < size(chart); j++)
  155.                 {
  156.  
  157.                     for (size_t k = 0; k < size(chart[j]); k++) {
  158.                         if (data[i] == tolower(chart[j][k])) {          // data is upper letter, so make lower.
  159.                             convertValue = chart[k][j];
  160.                             goto end2;
  161.                         }
  162.                         else {
  163.                             convertValue = data[i];
  164.                         }
  165.  
  166.                        
  167.                     }
  168.                 }
  169.             end2:
  170.  
  171.                 convertValue = toupper(convertValue);
  172.  
  173.                 convertString += convertValue;
  174.             }
  175.  
  176.  
  177.             cout << "Encrypt to: " << endl;
  178.             cout << convertString << endl;
  179.             cout << "******************************************" << endl;
  180.         }
  181.     }
  182.  
  183.     cout << "Have fun!!" << endl;
  184.  
  185.  
  186.  
  187.     return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement