プニグマ punigma C言語
#include <stdio.h>
#include <conio.h>
typedef struct
{
int wire[26];
int pos;
} PUNI_ROTER;
void Connect(PUNI_ROTER* proter, int n1, int n2)
{
proter->wire[n1] = n2;
proter->wire[n2] = n1;
}
int Encode(PUNI_ROTER* proter, int ch)
{
int n = (ch - 'A' + proter->pos) % 26;
int encoded = 'A' + ((proter->wire[n] - proter->pos + 26) % 26);
proter->pos++;
return encoded;
}
void ShowPunigma(const PUNI_ROTER* proter, 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' + proter->pos;
puts(str0);
puts(str1);
puts(str2);
puts(str3);
printf("\n%s %s\n", input, encoded);
}
void main(void)
{
PUNI_ROTER roter;
char input[100] = "";
char encoded[100] = "";
int count = 0;
char* guide =
"1~9の数字を入力するとローターが回転します。\n"
"A~Zを入力すると右側に変換した文字を表示します。\n"
"0を入力するとプログラムを終了します。";
puts(guide);
Connect(&roter, 16, 6);
Connect(&roter, 15, 20);
Connect(&roter, 0, 21);
Connect(&roter, 17, 22);
Connect(&roter, 7, 2);
Connect(&roter, 5, 11);
Connect(&roter, 12, 23);
Connect(&roter, 10, 1);
Connect(&roter, 8, 24);
Connect(&roter, 3, 25);
Connect(&roter, 18, 9);
Connect(&roter, 13, 4);
Connect(&roter, 14, 19);
roter.pos = 0;
while (count < 99)
{
int ch = 0;
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] = Encode(&roter, ch);
count++;
input[count] = 0;
encoded[count] = 0;
}
else if ('1' <= ch && ch <= '9')
{
roter.pos = (roter.pos + ch - '0') % 26;
count = 0;
}
else{
break;
}
}
}