在我的项目学校,我需要将一个单词转换为摩尔斯电码。
我已经尝试了这个,但它不起作用:
for (letter = 0; letter > 1000; letter ++)
{
scanf ("%s", &word [letter]);
if (word [letter] == 'a') wordInMorse [4 * letter] = '.-';
if (word [letter] == 'b') wordInMorse [4 * letter] = '-...';
if (word [letter] == 'c') wordInMorse [4 * letter] = '-.-.';
if (word [letter] == 'd') wordInMorse [4 * letter] = '-..';
if (word [letter] == 'e') wordInMorse [4 * letter] = '.';
if (word [letter] == 'f') wordInMorse [4 * letter] = '..-.';
if (word [letter] == 'g') wordInMorse [4 * letter] = '--.';
if (word [letter] == 'h') wordInMorse [4 * letter] = '....';
if (word [letter] == 'i') wordInMorse [4 * letter] = '..';
if (word [letter] == 'j') wordInMorse [4 * letter] = '.---';
if (word [letter] == 'k') wordInMorse [4 * letter] = '-.-';
if (word [letter] == 'l') wordInMorse [4 * letter] = '.-..';
if (word [letter] == 'm') wordInMorse [4 * letter] = '--';
if (word [letter] == 'n') wordInMorse [4 * letter] = '-.';
if (word [letter] == 'o') wordInMorse [4 * letter] = '---';
if (word [letter] == 'p') wordInMorse [4 * letter] = '.--.';
if (word [letter] == 'q') wordInMorse [4 * letter] = '--.-';
if (word [letter] == 'r') wordInMorse [4 * letter] = '.-.';
if (word [letter] == 's') wordInMorse [4 * letter] = '...';
if (word [letter] == 't') wordInMorse [4 * letter] = '-';
if (word [letter] == 'u') wordInMorse [4 * letter] = '..-';
if (word [letter] == 'v') wordInMorse [4 * letter] = '...-';
if (word [letter] == 'w') wordInMorse [4 * letter] = '.--';
if (word [letter] == 'x') wordInMorse [4 * letter] = '-..-';
if (word [letter] == 'y') wordInMorse [4 * letter] = '-.--';
if (word [letter] == 'z') wordInMorse [4 * letter] = '--..';
printf ("%s", &wordInMorse [letter]);
}
运行程序并写入一个单词后,它崩溃了,在Build messages选项卡中,if (word [letter] == '/*letter*/' wordInMorse [4 * letter] = '/*morse*/';
}的每一行都表示"在隐式常量转换中溢出[-Woverflow]"和"多字符字符常量[-Wmultichar]"。
答案 0 :(得分:1)
为了指导您,不做作业,您可以采取以下方法:
使wordInMorse
成为一个包含26个字符串指针的全局数组。
使用莫尔斯代码中的字母字符串初始化此数组,因此wordInMorse
的第一个元素指向字母A(或a)的摩尔斯电码。您可以使用初始化程序执行此操作,还是需要在主程序中执行此操作的代码?
在主程序中,当用户输入字符串时,查找此数组中的每个字母并使用摩尔斯电码打印字符串。
要将输入字母转换为数组中的索引,请将字母设为大写并减去'A'
。