英语到莫尔斯转换器

时间:2014-12-14 07:18:03

标签: c++ visual-c++ morse-code

我一直致力于一个应该将英语转换为莫尔斯代码的程序。我很难处理字符串。例如,我不知道为什么我可以让morseAlphabet在[30]有一定数量的位置,但我不能对latinAlphabet做同样的事情。总的来说,我不知道我应该如何翻译这些词语。

我的想法是看到字母表中的哪个字符出现在要翻译的短语的第一个位置,然后打印莫尔斯字母表的相应字母位置,然后移动到短语中的第二个位置,但是我一直在乱搞循环刚刚结束,我得到错误的循环变得太大和内存错误或只是给了我一个空白。

我现在每当我输入要翻译的短语时,它就会停止下标超出范围错误,而我早期的一些小问题让它返回乱码(内存位置?)而我真的只是出于想法。我希望这是正确的,有人可以帮助我,因为过去四个小时的互联网搜索并没有真正帮助我,说实话,我怀疑我写的任何东西是否有任何用处

#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;
}

2 个答案:

答案 0 :(得分: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::string s {{char {{{}的braced-init-list初始化{'A', 'B', ...}数组。 1}}。可以将其更改为{"A", "B", ...}或将latinAlphabet的类型更改为char[30],您可以将其初始化为数组。

答案 1 :(得分: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();

}