Skip to content

Instantly share code, notes, and snippets.

@seiyaKai
Last active December 16, 2015 16:19
Show Gist options
  • Save seiyaKai/5461823 to your computer and use it in GitHub Desktop.
Save seiyaKai/5461823 to your computer and use it in GitHub Desktop.
シーザー暗号化のテスト。汚いソースですが…すみません。
#include <iostream>
#include <iomanip>
#include <string>
class Caesar
{
public:
Caesar();
void setString(std::string *in_str);
void run();
private:
static const int ALPHABET_LENGTH;
static const int ALPHABET_FIRST;
std::string str;
};
Caesar::Caesar()
{
str="";
}
const int Caesar::ALPHABET_LENGTH = 26;
const int Caesar::ALPHABET_FIRST = 97;
void Caesar::setString(std::string *in_str)
{
str = *in_str;
std::cout << "set '" << str << "'" << std::endl << std::endl;
}
void Caesar::run()
{
for(int i = -ALPHABET_LENGTH; i < ALPHABET_LENGTH+1; i++){
std::cout << std::setw(4) << i << " ";
for(int j=0 ; j<str.length() ; j++){
//a=97 z=122
int dec=(int)str[j];
std::cout << (char)(ALPHABET_FIRST + (dec - ALPHABET_FIRST + ALPHABET_LENGTH + i) % ALPHABET_LENGTH) << "";
}
if(i==0) {
std::cout << " <-input";
}
std::cout << std::endl;
}
}
int main(int argc, const char *argv)
{
Caesar caesar;
std::string str;
std::cout << "string > ";
std::cin >> str;
caesar.setString(&str);
caesar.run();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment