Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : P.cpp
- // Author : Oliver Mattos
- // Version :
- // Copyright :
- // Description : Hello World in C++, Ansi-style
- //============================================================================
- #include <iostream>
- #include <string>
- #include <algorithm>
- #include <cctype>
- using namespace std;
- int mod5(int x) {
- return (x+5)%5;
- }
- int mod25(int x) {
- return (x+25)%25;
- }
- string digraph(string keyword, string di) {
- int posa = keyword.find_first_of(di[0], 0);
- int posb = keyword.find_first_of(di[1], 0);
- if (posa%5 == posb%5) {
- // same col
- return string(1, keyword[mod25(posa+5)]) + string(1,keyword[mod25(posb+5)]);
- }
- if (posa/5 == posb/5) {
- // same row
- return string(1, keyword[(posa/5)*5 + mod5(posa+1)]) + string(1, keyword[(posb/5)*5 + mod5(posb+1)]);
- }
- return string(1, keyword[(posa/5)*5+posb%5]) + string(1, keyword[(posb/5)*5+posa%5]);
- }
- int main() {
- string tmp;
- string keyword;
- cin >> tmp >> keyword; // prints !!!Hello World!!!
- std::transform(keyword.begin(), keyword.end(),keyword.begin(), ::toupper);
- keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- for (int i=0; i<(int)keyword.length(); i++)
- if (keyword[i] == 'J') keyword[i] = 'I';
- for (int i=0; i<(int)keyword.length(); i++) {
- while (keyword.find_first_of(keyword[i], i+1)<100000)
- keyword.erase(keyword.begin()+keyword.find_first_of(keyword[i], i+1));
- }
- cin >> tmp; // prints !!!Hello World!!!
- cout << "Message: ";
- cin.get();
- while (!cin.fail()) {
- char a, b;
- bool acase, bcase, end=false;
- while (!isalpha(a = cin.get())) { if (cin.fail() || a != ' ') { end = true; break;} cout << a; };
- if (end) break;
- acase = isupper(a);
- a = toupper(a);
- int gotA;
- do {
- gotA = false;
- string whitespace = "";
- string encoded;
- while (!isalpha(b = cin.get())) { if (cin.fail() || b != ' ') { end=true; break;} whitespace +=b; };
- if (end) {
- b = 'X';
- bcase=false;
- } else {
- bcase = isupper(b);
- b = toupper(b);
- }
- if (a == 'J') a = 'I';
- if (b == 'J') b = 'I';
- if (a==b) {
- // bah, fail case...
- encoded = digraph(keyword, string(1, a) + "X" );
- acase = bcase;
- bcase = false;
- a =b;
- gotA = true;
- } else {
- encoded = digraph(keyword, string(1, a) + string(1, b) );
- }
- cout << string(1, acase?encoded[0]:(encoded[0]-'A'+'a'));
- cout << whitespace;
- cout << string(1, bcase?encoded[1]:(encoded[1]-'A'+'a'));
- } while (gotA && !end);
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement