#include <stdio.h>
enum CHOICE { ENCRYPT = 1, DEENCRYPT };
void GetString( char* );
void SelectMenu( int* );
void GetChangeAmount( int* );
void ChangeString( char*, const int, const int );
void PrintResult( char*, const int );
int main( void )
{
char string[64];
int choice, amount;
GetString(string);
SelectMenu(&choice);
GetChangeAmount(&amount);
ChangeString(string, choice, amount);
PrintResult(string, choice);
return 0;
}
void GetString( char *str )
{
printf("문자열 입력 : ");
scanf("%s", str);
}
void SelectMenu( int *cho )
{
printf("실행(1-암호화, 2-복호화) : ");
scanf("%d", cho);
}
void GetChangeAmount( int *amo )
{
printf("암호키(복호키) 입력 : ");
scanf("%d", amo);
}
void ChangeString( char *str, const int cho, const int amo )
{
int i;
switch( cho )
{
case ENCRYPT:
for( i = 0; str[i] != \'\\0\'; i++ )
{
if( str[i]+amo > \'z\' )
{
int less = str[i]-\'z\';
str[i] = \'a\';
str[i]+=less;
}
else
str[i]+=amo;
}
break;
case DEENCRYPT:
for( i = 0; str[i] != \'\\0\'; i++ )
{
if( str[i]-amo < \'a\' )
{
int plus = \'a\'-str[i];
str[i] = \'z\';
str[i]-=plus;
}
else
str[i]-=amo;
}
}
}
void PrintResult( char *str, const int cho )
{
switch( cho )
{
case ENCRYPT:
printf("암호문 : ");
break;
case DEENCRYPT:
printf("복호문 : ");
}
puts(str);
}