【发布时间】:2017-04-27 01:21:31
【问题描述】:
我一直在开发一个应该将英语转换为莫尔斯电码的程序。我很难处理弦乐。例如,我不知道为什么我可以让 morseAlphabet 在 [30] 处有一定数量的位置,但我不能对 latinAlphabet 做同样的事情。总的来说,我不知道我应该如何翻译这些词。
我的想法是查看字母表中的哪个字符出现在要翻译的短语的第一个位置,然后打印莫尔斯字母表的相应字母位置,然后移动到短语中的第二个位置,但是我搞砸了 for循环刚刚结束,我收到关于 for 循环太大和内存错误的错误,或者只是给了我一个空白。
每当我输入要翻译的短语时,我现在所拥有的内容就会停止并出现下标超出范围错误,并且我之前的一些摆弄让它返回了乱码(内存位置?),我真的只是没有想法.我希望这是正确的措辞,有人可以帮助我,因为过去四个小时的互联网搜索并没有真正帮助我,老实说,在这一点上,我怀疑我写的任何东西是否有任何用处.
#include <iostream>
#include <string>
int main()
{
int operatingMode = 0;
using namespace std;
std::string latinPhrase;
std::string morsePhrase;
std::string latinAlphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '.', ',' };
std::string morseAlphabet[30] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".-.-.-", "--..--" };
std::string translatedMorsePhrase;
int wordSearch = 0;
std::cout << "Please select a mode of operation. " << endl;
std::cout << "Input 1 for English to Morse and 2 for Morse to English. " << endl;
std::cin >> operatingMode;
std::cout << "Your mode of operation is " << operatingMode << endl;
if (operatingMode == 1)
{
std::cout << "You have selected English to Morse." << endl;
std::cout << "Please enter the phrase you would like translated." << endl;
std::cin.ignore();
std::getline(std::cin, latinPhrase);
}
for (int counter = 0; counter < 30; counter++)
{
for (unsigned i = 0; i<latinPhrase.length(); ++i)
{
if (latinPhrase.at(i) == latinAlphabet[i])
{
cout << morseAlphabet[i];
}
}
std::cout << "The translated phrase is: " << translatedMorsePhrase << " stop" << endl;
return 0;
}
【问题讨论】:
-
摩尔斯电码有大写字母吗?即汤姆帮助杰克下马。那句话需要大写字母
-
摩尔斯电码全部大写。不过你是对的,如果我将输入作为小写字母,它就不起作用。
-
摩尔斯电码既没有大写也没有小写。只是指出其局限性之一
标签: c++ visual-c++ morse-code