英语到摩尔斯电码程序

时间:2016-04-17 05:58:08

标签: c++

#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 Code\n";
        cout << "4 - Quit\n";
        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]),它说这是一个错误。我该怎么解决?我不知道将莫尔斯的代码写成英文。如何知道用户输入的数组的位置?

错误讯息:

  

错误:不匹配&#39;运营商==&#39; (操作数类型是&#39; char&#39;和&#39; std :: string {aka std :: basic_string}&#39;)|

3 个答案:

答案 0 :(得分:4)

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

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

List.hd

而不是:

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

答案 1 :(得分:1)

SELECT COUNT(*) FROM Student t1 inner join Teacher t2 
WHERE (t1.UserName = 'abc' AND t1.Password = 'abc') or 
(t2.UserName = 'abc' AND t2.Password = 'abc')

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

for (int t = 0; t <= 39; t++)

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

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

答案 2 :(得分:0)

如果您希望数组为字符串 - 则使用 strcmp()函数。

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

祝你好运!