1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
//--------------------------------------
class Code
{
unsigned row[256]{}, col[256]{};
vector<string> master;
public:
Code( const string &cipherFile );
bool encode( unsigned char ch, unsigned int &r, unsigned int &c );
bool decode( unsigned int r, unsigned int c, unsigned char &ch );
};
//--------------------------------------
Code::Code( const string &cipherFile )
{
ifstream in( cipherFile );
string line;
for ( int r = 0; getline( in, line ); r++ )
{
master.push_back( line );
for ( int c = 0; c < line.size(); c++ )
{
unsigned char i = line[c];
row[i] = r + 1;
col[i] = c + 1;
}
}
}
//--------------------------------------
bool Code::encode( unsigned char ch, unsigned int &r, unsigned int &c )
{
if ( row[ch] )
{
r = row[ch] - 1;
c = col[ch] - 1;
}
return row[ch];
}
//--------------------------------------
bool Code::decode( unsigned r, unsigned c, unsigned char &ch )
{
if ( r < master.size() && c < master[r].size() )
{
ch = master[r][c];
return true;
}
return false;
}
//=============================================================================
int main()
{
Code code( "cipherFile" );
vector<pair<int,int>> check;
ifstream in( "inputFile" );
for ( string line; getline( in, line ); )
{
for ( unsigned char ch : line )
{
unsigned int r, c;
if ( code.encode( ch, r, c ) ) cout << r << " " << c << " '" << ch << "' is found on line " << r << ", position " << c << "\n";
else cout << ch << " is not found in the cipher definition file\n";
check.push_back( { r, c } );
}
}
// Back-check
for ( auto pr : check )
{
unsigned char ch;
if ( code.decode( pr.first, pr.second, ch ) ) cout << ch;
}
}
| |