プニグマ punigma C++
#include <stdio.h>
#include <conio.h>
class Roter
{
protected:
int mWire[26];
int mPosition;
void Connect(int n1, int n2)
{
mWire[n1] = n2;
mWire[n2] = n1;
}
public:
Roter()
{
Connect(16, 6);
Connect(15, 20);
Connect(0, 21);
Connect(17, 22);
Connect(7, 2);
Connect(5, 11);
Connect(12, 23);
Connect(10, 1);
Connect(8, 24);
Connect(3, 25);
Connect(18, 9);
Connect(13, 4);
Connect(14, 19);
mPosition = 0;
}
int Encode(int ch)
{
int n = (ch - 'A' + mPosition) % 26;
int encoded = 'A' + ((mWire[n] - mPosition + 26) % 26);
mPosition++;
return encoded;
}
int GetPosition() const
{
return mPosition;
}
void Rotate(int n)
{
mPosition = (mPosition + n) % 26;
}
};
void ShowPunigma(const Roter& rroter, char* input, char* encoded)
{
char str0[] = "-------------------------";
char str1[] = "Q W E R T Y U I O P |||";
char str2[] = " A S D F G H J K L | |";
char str3[] = " Z X C V B N M |||";
str2[23] = 'A' + rroter.GetPosition();
puts(str0);
puts(str1);
puts(str2);
puts(str3);
printf("\n%s %s\n", input, encoded);
}
void main(void)
{
Roter roter;
int ch = 0;
char input[100] = "";
char encoded[100] = "";
int count = 0;
char* guide =
"1~9の数字を入力するとローターが回転します。\n"
"A~Zを入力すると右側に変換した文字を表示します。\n"
"0を入力するとプログラムを終了します。";
puts(guide);
while (count < 98)
{
ShowPunigma(roter, input, encoded);
ch = _getche();
if ('a' <= ch && ch <= 'z')
ch = ch - 'a' + 'A';
if ('A' <= ch && ch <= 'Z')
{
input[count] = ch;
encoded[count] = roter.Encode(ch);
count++;
input[count] = 0;
encoded[count] = 0;
}
else if ('1' <= ch && ch <= '9')
{
roter.Rotate(ch - '0');
count = 0;
}
else{
break;
}
}
}