gpt4 book ai didi

c - 我想创建一个摩尔斯电码转换器

转载 作者:行者123 更新时间:2023-11-30 21:00:31 24 4
gpt4 key购买 nike

在我项目的学校中,我需要将单词转换为摩尔斯电码。

我已经尝试过,但它不起作用:

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]);
}

运行程序并写入一个单词后,它崩溃了,在“构建消息”选项卡中,每一行都带有 if (word [letter] == '/*letter*/' wordInMorse [4 * letter] = '/*morse*/';) 表示“隐式常量转换中的溢出 [-Woverflow]”和“多字符字符常量 [-Wmultichar]”。

最佳答案

为了指导您,无需做功课,您可以采取以下方法:

  • wordInMorse 设为一个包含 26 个字符串指针的全局数组。

  • 使用摩尔斯电码字母字符串初始化此数组,因此 wordInMorse 的第一个元素指向字母 A(或 a)的摩尔斯电码。您可以使用初始值设定项来执行此操作吗?还是需要在主程序中编写代码来执行此操作?

  • 在主程序中,当用户输入字符串时,查找该数组中的每个字母并使用摩尔斯电码打印该字符串。

要将输入的字母转换为数组中的索引,请将字母变为大写并减去 'A'

祝你好运!

关于c - 我想创建一个摩尔斯电码转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41076942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com