0

我一直在研究一个应该将英语转换为莫尔斯电码的程序。我很难处理弦乐。例如,我不知道为什么我可以让 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;
}
4

2 回答 2

0

当前代码中的错误是错误地使用了i当你的意思counter和不正确的迭代顺序。这应该可以解决问题:

for (unsigned i = 0; i<latinPhrase.length(); ++i)
    {
        for (int counter = 0; counter < 30; counter++)
        {
                if (latinPhrase.at(i) == latinAlphabet[counter])
                {
                    cout << morseAlphabet[counter];
                    break;
                }
        }
    }

要回答有关声明和初始化问题的问题std::string latinAlphabet[30],您正在尝试使用std::strings 的花括号初始化列表初始化chars数组{'A', 'B', ...}。将其更改为{"A", "B", ...}或更改为的类型,latinAlphabetchar[30]将能够以数组的方式将其初始化。

于 2014-12-14T07:37:10.217 回答
0
#include<iostream>
#include<stdio.h>
#include<string.h>
#include <windows.h>
#include<fstream>
using namespace std;
int main()
{
//  Beep(523,700);
//  Beep(0,400);
//  Beep(523,200);
//  Beep(523,200);
//  Beep(523,200);
    char string[1000];
    cout << "Enter string : ";
    gets(string);

    int len;
    len=strlen(string);
    ofstream out("output.txt");
    ifstream in;
    for(int i=0; i<len; i++)
    {
        switch(string[i])
        {
            case 'a' :
            case 'A' :
                {
                    out << ".- ";
                    break;
                }
            case 'b' :
            case 'B' :
                {
                    out << "-... ";
                    break;
                }
            case 'c' :
            case 'C' :
                {
                    out << "-.-. ";
                    break;
                }
            case 'd' :
            case 'D' :
                {
                    out << "-.. ";
                    break;
                }   
            case 'e' :
            case 'E' :
                {
                    out << ". ";
                    break;
                }
            case 'f' :
            case 'F' :
                {
                    out << "..-. ";
                    break;
                }
            case 'g' :
            case 'G' :
                {
                    out << "--. ";
                    break;
                }
            case 'h' :
            case 'H' :
                {
                    out << ".... ";
                    break;
                }
            case 'i' :
            case 'I' :
                {
                    out << ".. ";
                    break;
                }   
            case 'j' :
            case 'J' :
                {
                    out << ".--- "; 
                    break;
                }
            case 'k' :
            case 'K' :
                {
                    out << "-.- ";
                    break;
                }
            case 'l' :
            case 'L' :
                {
                    out << ".-.. ";
                    break;
                }
            case 'm' :
            case 'M' :
                {
                    out << "-- ";
                    break;
                }
            case 'n' :
            case 'N' :
                {
                    out << "-. ";
                    break;
                }   
            case 'o' :
            case 'O' :
                {
                    out << "--- ";
                    break;
                }
            case 'p' :
            case 'P' :
                {
                    out << ".--. ";
                    break;
                }
            case 'q' :
            case 'Q' :
                {
                    out << "--.- ";
                    break;
                }
            case 'r' :
            case 'R' :
                {
                    out << ".-. ";
                    break;
                }
            case 's' :
            case 'S' :
                {
                    out << "... ";
                    break;
                }   
            case 't' :
            case 'T' :
                {
                    out << "- ";
                    break;
                }
            case 'u' :
            case 'U' :
                {
                    out << "..- ";
                    break;
                }
            case 'v' :
            case 'V' :
                {
                    out << "...- ";
                    break;
                }
            case 'w' :
            case 'W' :
                {
                    out << ".-- ";
                    break;
                }
            case 'x' :
            case 'X' :
                {
                    out << "-..- ";
                    break;
                }
            case 'y' :
            case 'Y' :
                {
                    out << "-.-- ";
                    break;
                }
            case 'z' :
            case 'Z' :
                {
                    out << "--.. ";
                    break;
                }
            case ' ' :
                {
                    out << " /  ";
                    break;
                }
            case '0' :
                {
                    out << "----- ";
                    break;
                }
            case '1' :
                {
                    out << ".---- ";
                    break;
                }
            case '2' :
                {
                    out << "..--- ";
                    break;
                }
            case '3' :
                {
                    out << "...-- ";
                    break;
                }
            case '4' :
                {
                    out << "....- ";
                    break;
                }
            case '5' :
                {
                    out << "..... ";
                    break;
                }
            case '6' :
                {
                    out << "-.... ";
                    break;
                }
            case '7' :
                {
                    out << "--... ";
                    break;
                }
            case '8' :
                {
                    out << "---.. ";
                    break;
                }
            case '9' :
                {
                    out << "----. ";
                    break;
                }



        }


    }
        out.close();
        char ch;
        in.open("output.txt");
        while(!in.eof())
        {
            in>>ch;
            if(ch=='.')
            {
                Beep(523,200);
            }
            else if(ch=='-')
            {
                Beep(523,700);
            }
            else if(ch==' ')
            {
                Beep(0,100);
            }
            else if(ch == '/')
            {
                Beep(0,400);
            }
        }
        in.close();

}
于 2016-11-07T15:28:03.397 回答