英语到摩尔斯电码程序

English to Morse code program

本文关键字:程序 英语      更新时间:2023-10-16
#include <iostream>
#include <iomanip>
#include <conio.h>
#include <stdlib.h>
#include <string>
using namespace std;
int main()
{
    string text[39] = {"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",
                       "1","2","3","4","5","6","7","8","9","0","Stop",",","?"};
    string code[39] = {".-","-...","-.-.","-..",".","..-","--.","....","..",".---","-.-",".-..","--",
                       "-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..",
                       ".----","..---","...--","....-",".....","-....","--....","---..","----.","-----",".-.-.-","--..--","..--.."};
    string English, Morse, output_string;
    int option, string_size = 0, location;
    char again = 'y', letter;
    while(again == 'y')
    {
        system("cls");
        cout << "1 - Encode(Text to Morse)n";
        cout << "2 - Decode(Morse Code to Text)n";
        cout << "3 - Display the Morse Coden";
        cout << "4 - Quitn";
        cout << "Enter 1,2,3 or 4:";
        cin >> option;
        cin.ignore(256,'n');
        system("cls");
        switch(option)
        {
            case 1:
                cout << "nEnter a string with multiple words to encode:";
                getline(cin, English);
                system("cls");
                cout << "nThe target string to be translated is:" << "n";
                cout << English << "n";
                string_size = English.length();
                for(int n = 0; n <= string_size-1; n++)
                {
                    letter = (char)English.at(n);
                    if(letter != ' ')
                    {
                        for(int t = 0; t <=39; t++)
                        {
                            if(letter == text[t])
                            {
                                cout << code[t] << " ";
                                break;
                            }
                        }
                    }
                    else if(letter == ' ')
                    {
                        cout << "n";
                    }
                }
                getch();
                break;    
        }
    }
}  

还没有完成它,但我不知道为什么我不能运行if(letter == text[t]),它说这是一个错误。 我该如何解决它?而且我不知道写摩尔斯到英语的代码。如何知道用户输入的数组的位置?

错误信息:

错误:与"运算符=="不匹配(操作数类型为"char"和"std::string {aka std::basic_string}"(|

您正在尝试比较字符串字符

你需要像这样写数组(如果你只想使用字符(:

char text[39] = {'A','B','C','D','E','F','G','H','I','J','K','L','M'};

而不是:

string text[39] = {"A","B","C","D","E","F","G","H","I","J","K","L","M"};
for (int t = 0; t <= 39; t++)

您有 39 个项目从零索引开始,因此您的循环应该上升到(但不包括(39

for (int t = 0; t < 39; t++)
{
    ...
}

您可以声明一个临时字符串以将每个字母复制到字符串。您还需要确保文本为大写:

letter = (char)English.at(n);
if (letter != ' ')
{
    for (int t = 0; t < 39; t++)
    {
        std::string temp;
        temp = toupper(letter);
        if (temp == text[t])
        {
            cout << code[t] << " ";
            break;
        }
    }
}

如果你希望数组是字符串 - 那么使用 strcmp(( 函数。

if(strcmp(text[t],letter)==0)
      {
        cout << code[t] << " ";
        break;
      }

祝你好运!