Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //
- #include "stdafx.h"
- #include <iostream>
- #include <string>
- #include <fstream>
- using namespace std;
- string keyWord_In_Array(string keyword) { // get keyword(HAPPINESS) to ready to put array.
- string code; //save strong to easyly put data in array.
- char ch = ' ';
- for (size_t i = 0; i < keyword.length(); i++)
- {
- if (keyword[i] != ch) { //use this to avoid duplication about code.
- code += keyword[i];
- ch = keyword[i];
- }
- }
- return code;
- }
- string Make_Rest_Code(string code) { // use this fuction to remove code's alphabet on the alphabet A to Y.
- string fullCode = "ABCDEFGHIJKLMNOPQRSTUVWXY";
- for (size_t i = 0; i < code.length(); i++)
- {
- int index = 0;
- index = fullCode.find(code[i]); // find positon of alphabet in the code.
- fullCode.erase(index, 1); //erase it.
- }
- return fullCode;
- }
- int main()
- {
- string data; //get data from file.
- string code;
- string restCode;
- int count1 = 0;
- int count2 = 0;
- char chart[5][5];
- ifstream inputFile;
- inputFile.open("c:\\temp\\mp5.txt");
- getline(inputFile, data); // get whole line from the file.
- cout << "Keyword is " << data << endl;
- code = keyWord_In_Array(data);
- restCode = Make_Rest_Code(code);
- for (size_t i = 0; i < size(chart); i++)
- {
- for (size_t j = 0; j < size(chart[i]); j++)
- {
- if (count1 < code.length()) { // use this condition to put code's alphabet first.
- chart[i][j] = code[count1];
- count1++;
- }
- else {
- chart[i][j] = restCode[count2];
- count2++;
- }
- }
- }
- // use this codes to show array for users.
- cout << endl << endl;
- cout << " Col 0 Col 1 Col 2 Col 3 Col 4" << endl;
- cout << " __________________________________" << endl;
- for (size_t i = 0; i < size(chart); i++)
- {
- printf("Row %d:", i);
- for (size_t j = 0; j < size(chart[i]); j++)
- {
- cout << " | ";
- printf("%c ", chart[i][j]);
- }
- cout << " |\n";
- }
- cout << "******************************************" << endl;
- while (!inputFile.eof()) {
- getline(inputFile, data);
- cout << data.substr(2) << endl; //use substr to remove useless data.("D "and"E ")
- string convertString = "";
- if (data[0] == 'D') { // get first position of data to decrypt data.
- for (size_t i = 2; i < data.size(); i++)
- {
- char convertValue = ' ';
- for (size_t j = 0; j < size(chart); j++)
- {
- for (size_t k = 0; k < size(chart[j]); k++) {
- if (data[i] == chart[j][k]) {
- convertValue = chart[k][j];
- goto end; // if find same value on the arra, get out of for state to move (end:)
- }
- else {
- convertValue = data[i]; // if doesn't find data on the array, save same value on the convertValue.
- }
- }
- }
- end: // put data on the string.
- convertValue = tolower(convertValue);
- convertString += convertValue;
- }
- cout << "Decrypt to: " << endl;
- cout << convertString << endl;
- cout << "******************************************" << endl;
- }
- else if (data[0] == 'E') { // get data[0] to encrypt data.
- for (size_t i = 2; i < data.size(); i++)
- {
- char convertValue = ' ';
- for (size_t j = 0; j < size(chart); j++)
- {
- for (size_t k = 0; k < size(chart[j]); k++) {
- if (data[i] == tolower(chart[j][k])) { // data is upper letter, so make lower.
- convertValue = chart[k][j];
- goto end2;
- }
- else {
- convertValue = data[i];
- }
- }
- }
- end2:
- convertValue = toupper(convertValue);
- convertString += convertValue;
- }
- cout << "Encrypt to: " << endl;
- cout << convertString << endl;
- cout << "******************************************" << endl;
- }
- }
- cout << "Have fun!!" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement