英語をモールス信号に変換するプログラムに取り組んでいます。弦の扱いにかなり苦労しました。たとえば、morseAlphabet の位置を [30] に設定できる理由がわかりませんが、latinAlphabet については同じことができません。全体として、単語をどのように翻訳すればよいかわかりません。
私の考えは、翻訳するフレーズの最初の位置にアルファベットのどの文字が現れるかを確認し、モールス文字に対応するアルファベットの位置を出力してから、フレーズの2番目の位置に移動することでしたが、forループをいじりましたfor ループが大きくなりすぎてメモリ エラーが発生したり、空白になったりするというエラーが発生しました。
私が今持っているものでは、翻訳するフレーズを入力するたびに、添字が範囲外のエラーで停止し、以前のいじりのいくつかは、それがぎこちないもの(メモリの場所?)を返すことがあり、本当にアイデアがありません. 過去 4 時間のインターネット検索はあまり役に立たなかったので、これが正しい言い回しで誰かが私を助けてくれることを願っています。 .
#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;
}