/////////////////////////////用struct 建立摩斯密碼表 計算摩斯密碼對應的password 回傳result//////////////////
以下為參考解法
#define password_info_table_max 26
typedef struct password_info
{
int password;
char result;
}SI;
SI table[password_info_table_max] = /////////////////////////////用struct 建立摩斯密碼表 計算摩斯密碼對應的password 回傳result//////////////////
{
{ 505 ,'A' },
{50106,'B' },
{50096,'C'},
{5006,'D'},
{46,'E'},
{51096,'F'},
{4996,'G'},
{51106,'H'},
{506,'I'},
{50995,'J'},
{5005,'K'},
{51006,'L'},
{495,'M'},
{496,'N'},
{4995,'O'},
{50996,'P'},
{50005,'Q'},
{5096,'R'},
{5106,'S'},
{45,'T'},
{5105,'U'},
{51105,'V'},
{5095,'W'},
{50105,'X'},
{50095,'Y'},
{50006,'Z'},
};
// . 0X2E(32+14=46)
// - 0X2D(32+13=45)
int main()
{
int i = 0, j = 0,k=0;
int n = 0,count=0,temp=0; //2<=n<=5
char input[100] = { '\0' };
scanf("%d", &n);
n += 1;
while(n)
{
gets(input);
for (j = 0; j < strlen(input); j++)
{
if (input[j] != ' ')
{
count++;
}
else { //input == ' '
switch (count)
{
case 1:
for (k = 0; k < 26; k++)
{
if (input[j - 1] == table[k].password)
{
printf("%c", table[k].result);
}
}
break;
case 2:
temp = input[j - 1] + input[j - 2] * 10;
for ( k = 0; k < 26; k++)
{
if (temp == table[k].password)
{
printf("%c", table[k].result);
}
}
break;
case 3:
temp = input[j - 1] + input[j - 2] * 10 + input[j - 3] * 100;
for ( k = 0; k < 26; k++)
{
if (temp == table[k].password)
{
printf("%c", table[k].result);
}
}
break;
case 4:
temp = input[j - 1] + input[j - 2] * 10 + input[j - 3] * 100+input[j-4]*1000;
for (k = 0; k < 26; k++)
{
if (temp == table[k].password)
{
printf("%c", table[k].result);
}
}
break;
default:
printf("password error");
break;
}
temp = 0;
count = 0;
}
}
switch (count)
{
case 1:
for (k = 0; k < 26; k++)
{
if (input[j - 1] == table[k].password)
{
printf("%c\n", table[k].result);
}
}
break;
case 2:
temp = input[j - 1] + input[j - 2] * 10;
for (k = 0; k < 26; k++)
{
if (temp == table[k].password)
{
printf("%c\n", table[k].result);
}
}
break;
case 3:
temp = input[j - 1] + input[j - 2] * 10 + input[j - 3] * 100;
for (k = 0; k < 26; k++)
{
if (temp == table[k].password)
{
printf("%c\n", table[k].result);
}
}
break;
case 4:
temp = input[j - 1] + input[j - 2] * 10 + input[j - 3] * 100 + input[j - 4] * 1000;
for (k = 0; k < 26; k++)
{
if (temp == table[k].password)
{
printf("%c\n", table[k].result);
}
}
break;
default:
break;
}
count = 0;
n--;
}
return 0;
}