Advertisement
lwytop

Wonyoung Lee Machine Problem #5

Jul 19th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 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) {
  12.  
  13.     string code;
  14.     char ch = ' ';
  15.  
  16.     for (size_t i = 0; i < keyword.length(); i++)
  17.     {
  18.         if (keyword[i] != ch) {
  19.             code += keyword[i];
  20.             ch = keyword[i];
  21.         }
  22.  
  23.  
  24.     }
  25.     return code;
  26. }
  27.  
  28. string Make_Rest_Code(string code) {
  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]);
  35.  
  36.         fullCode.erase(index, 1);
  37.  
  38.     }
  39.  
  40.     return fullCode;
  41. }
  42.  
  43. int main()
  44. {
  45.     string data;
  46.     string code;
  47.     string restCode;
  48.     int count1 = 0;
  49.     int count2 = 0;
  50.  
  51.     char chart[5][5];
  52.  
  53.     ifstream inputFile;
  54.     inputFile.open("c:\\temp\\mp5.txt");
  55.  
  56.     getline(inputFile, data);
  57.     cout << "Keyword is " << data << endl;
  58.  
  59.     code = keyWord_In_Array(data);
  60.  
  61.     restCode = Make_Rest_Code(code);
  62.  
  63.     cout << restCode;
  64.  
  65.     for (size_t i = 0; i < size(chart); i++)
  66.     {
  67.  
  68.         for (size_t j = 0; j < size(chart[i]); j++)
  69.         {
  70.             if (count1 < code.length()) {
  71.  
  72.                 chart[i][j] = code[count1];
  73.                 count1++;
  74.             }
  75.             else {
  76.                 chart[i][j] = restCode[count2];
  77.                 count2++;
  78.             }
  79.         }
  80.  
  81.     }
  82.  
  83.     cout << endl << endl;
  84.  
  85.     cout << "         Col 0  Col 1  Col 2  Col 3  Col 4" << endl;
  86.     cout << "        __________________________________" << endl;
  87.  
  88.     for (size_t i = 0; i < size(chart); i++)
  89.     {
  90.         printf("Row %d:", i);
  91.  
  92.         for (size_t j = 0; j < size(chart[i]); j++)
  93.         {
  94.             cout << " |  ";
  95.             printf("%c ", chart[i][j]);
  96.         }
  97.         cout << " |\n";
  98.     }
  99.  
  100.     cout << "******************************************" << endl;
  101.  
  102.  
  103.  
  104.     while (!inputFile.eof()) {
  105.         getline(inputFile, data);
  106.  
  107.         cout << data.substr(2) << endl;
  108.  
  109.         string convertString = "";
  110.        
  111.  
  112.         if (data[0] == 'D') {
  113.            
  114.            
  115.             for (size_t i = 2; i < data.size(); i++)
  116.             {
  117.                 char convertValue = ' ';
  118.  
  119.  
  120.                 for (size_t j = 0; j < size(chart); j++)
  121.                 {
  122.  
  123.                     for (size_t k = 0; k < size(chart[j]); k++) {
  124.                         if (data[i] == chart[j][k]) {
  125.                             convertValue = chart[k][j];
  126.                             goto end;
  127.                         }
  128.                         else {
  129.                             convertValue = data[i];
  130.                         }
  131.                     }
  132.                 }
  133.             end:
  134.                 convertValue = tolower(convertValue);
  135.  
  136.                 convertString += convertValue;
  137.             }
  138.  
  139.             cout << "Decrypt to: " << endl;
  140.             cout << convertString << endl;
  141.             cout << "******************************************" << endl;
  142.  
  143.         }
  144.         else if (data[0] == 'E') {
  145.            
  146.  
  147.             for (size_t i = 2; i < data.size(); i++)
  148.             {
  149.                 char convertValue = ' ';
  150.  
  151.                 for (size_t j = 0; j < size(chart); j++)
  152.                 {
  153.  
  154.                     for (size_t k = 0; k < size(chart[j]); k++) {
  155.                         if (data[i] == tolower(chart[j][k])) {
  156.                             convertValue = chart[k][j];
  157.                             goto end2;
  158.                         }
  159.                         else {
  160.                             convertValue = data[i];
  161.                         }
  162.  
  163.                        
  164.                     }
  165.                 }
  166.             end2:
  167.  
  168.                 convertValue = toupper(convertValue);
  169.  
  170.                 convertString += convertValue;
  171.             }
  172.  
  173.  
  174.             cout << "Encrypt to: " << endl;
  175.             cout << convertString << endl;
  176.             cout << "******************************************" << endl;
  177.         }
  178.     }
  179.  
  180.     cout << "Have fun!!" << endl;
  181.  
  182.  
  183.  
  184.     return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement