C++ C++;摩尔斯电码转换成英语
所以我有一个任务,我必须创建一个程序,将英文文本翻译成摩尔斯电码,并将摩尔斯电码翻译成英文文本。到目前为止,我已经完成了英文文本到摩尔斯电码的转换,而我目前仍停留在摩尔斯电码到英文文本的转换上。我试着用英语对莫尔斯做同样的事情,但我学到了艰难的方法,那没有帮助,我不知道从哪里开始。我已经像这样被困了很长一段时间,任何关于如何做到这一点的建议将不胜感激。到目前为止,我有这个C++ C++;摩尔斯电码转换成英语,c++,C++,所以我有一个任务,我必须创建一个程序,将英文文本翻译成摩尔斯电码,并将摩尔斯电码翻译成英文文本。到目前为止,我已经完成了英文文本到摩尔斯电码的转换,而我目前仍停留在摩尔斯电码到英文文本的转换上。我试着用英语对莫尔斯做同样的事情,但我学到了艰难的方法,那没有帮助,我不知道从哪里开始。我已经像这样被困了很长一段时间,任何关于如何做到这一点的建议将不胜感激。到目前为止,我有这个 string Morse [arraysize] = {".-", "-...", "-.-.", "-..",".", "
string Morse [arraysize] = {".-", "-...", "-.-.", "-..",".", "..-.", "--.", "....", "..", ".---",
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--",
"....-", ".....", "-....", "--...", "---..", "----.", "-----", ".-.-.-",
"--..--", "..--.."};
string Letter [arraysize] = {"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", ",", "?"};
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\n";
cout << "Enter 1, 2, 3 or 4: ";
cin >> choice;
case 2:
cin.ignore();
cout << "Enter a Coded Morse Message (e.g. .- -... -.-.)";
cout << "\nSeparate Morse Code by blanks:\n";
getline(cin,english);
english2 = english;
system("CLS");
cout << "You entered:\n";
cout << english;
length1 = english.length();
cout << "\n\nThe decoded message is:\n";
for(int n = 0; n < length1+1; n++)
{
letter = english.at(n);
if(letter != ' ')
{
for(int y = 0; y < 39; y++)
{
string temp;
temp = toupper(letter);
if(temp == Morse[y])
{
cout << Letter[y];
break;
}
}
}
}
cout << ".-.-.-\n";
system("pause");
english.erase();
break;
string Morse[arraysize]={“-”,“-…”,“-.-”,“-…,”,“-”,“-”,“-”,“-”,“-”,“-”,
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--",
"....-", ".....", "-....", "--...", "---..", "----.", "-----", ".-.-.-",
"--..--", "..--.."};
字符串字母[arraysize]={“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”、“停止”、“、”、“?”};
cout
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
向量{“-”、“-…”、“-。-”、“-…”、“-…”、“-”、“-”、“-”、“-”、“-”、“-”、“-”,
"-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-",
"..-", "...-", ".--", "-..-", "-.--", "--..", ".----", "..---", "...--",
"....-", ".....", "-....", "--...", "---..", "----.", "-----", ".-.-.-",
"--..--", "..--.."};
向量字母{“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”、“停止”、“、”、“?”};
int main()
{
字符串莫尔斯电码;
cout不使用贴图或向量
您可以使用一个简单的动态数组来添加转换到数组中的每个字符。然后使用for loop
将每个莫尔斯序列转换回英语。当计数器小于string.length();
时,您可以循环需要转换的字符串
这里是一个仅使用字母a、b、c的示例,它不区分大小写。但是请注意,注释掉的数组大小是3,我们只测试3个或更少的字符,请确保使用动态数组。您可以看到,我使用string.length();
设置了大小
#包括
#包括
int main()
{
//测试句
std::string-station=“abc”;
//删除后,可以使用std::string morse[3]进行测试
//设置为句子长度大小的动态数组
auto*morse=new std::string[句子长度()];
//循环检查句子的每个字符,
//增加位置计数器,存储值
//阵列中
int位置=0;
用于(自动和控制:句子)
{
如果(c='a'| | c='a')
{
我没注意到你不允许使用地图(谢谢,@Raymond)。这里有另一个解决方案
ASCII到Morse。您可以使用查找表来代替映射。LUT是一个数组,其索引是键。在字符到Morse LUT的情况下,键/索引是字符,值是Morse码。不要忘记字符实际上是一个数字(操作系统以不同的方式处理它,但它仍然是一个数字)。'a'
是0x41
(65 decimal
),因此在表的索引65处,您将找到“-”
,依此类推。没有关联莫尔斯电码的条目将设置为零
摩尔斯电码转换为ASCII。如果你将摩尔斯电码视为五个符号长度的电码,你会注意到它需要三个符号来代表任何字母:a=.-=***.-
,B=-…=*-…
等等。这意味着任何摩尔斯电码都是用base-3写的数字。
是1
,-
是2
和*
是0
。我们可以使用此观察结果生成另一个LUT。因此,要从莫尔斯转换为ASCII,您只需要一个三值转换器和一个LUT
您的程序:
#include <iostream>
#include <string>
#include <fstream>
const char* ascii_to_morse[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
"-----", ".----","..---","...--","....-",".....","-....","--...","---..","----.", // 0...9
0, 0, 0, 0, 0, 0, 0,
".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.",
"---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..", // A...Z
};
const char morse_to_ascii[] =
{
0, 'E', 'T', 0, 'I', 'A', 0, 'N', 'M', 0, 0, 0, 0, 'S', 'U', 0, 'R', 'W', 0, 0, 0, 0, 'D', 'K',
0, 'G', 'O', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'H', 'V', 0, 'F', 0, 0, 0, 0, 0, 'L', 0, 0,
'P', 'J', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'B', 'X', 0, 'C', 'Y', 0, 0, 0, 0, 'Z', 'Q', 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '5', '4', 0, 0, '3', 0, 0, 0, 0, 0, 0, 0, 0, '2', 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '1', 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'6', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', 0, 0,
0, 0, 0, 0, 0, 0, '8', 0, 0, '9', '0',
};
int main()
{
// ascii to morse test
{
ifstream is{ "c:\\temp\\ascii.txt" };
if (!is)
return -1;
ofstream os{ "c:\\temp\\morse.txt" };
if (!os)
return -2;
// ascii to morse
char c;
while (is >> c)
os << ascii_to_morse[c] << ' ';
}
// morse to ascii test
{
ifstream is{ "c:\\temp\\morse.txt" };
if (!is)
return -1;
ofstream os{ "c:\\temp\\ascii_check.txt" };
if (!os)
return -2;
// morse to ascii
string m;
while (is >> m)
{
// convert m to index (ternary converter)
int index = 0;
for (int i = 0; i < (int)m.length(); ++i)
{
int digit = m[i] == '.' ? 1 : 2;
index = index * 3 + digit;
}
// look for the associated ascii code
os << morse_to_ascii[index] << ' ';
}
}
}
每个莫尔斯都是“字母”由delimeter分隔?摩尔斯电码没有固定长度的电码,而且是模糊的。可能就是这样。看起来从你的英语到摩尔斯电码有一些保留电码。例如,当你需要查看子字符串以获取摩尔斯电码中的所有字符时,你只需要查看英语
字符串中的一个字符。阅读aboutstd::map
。为什么到目前为止你所做的不正确?有什么具体不起作用?你预期的结果是什么,你的实际结果是什么?我的老师也说应该在主要方面做()--为什么不使用函数来完成这4个不同的选项呢?将所有内容都粘贴在main
中,从而创建一个巨大的main
程序,这不是正确的编程教学方法。OP在评论中说他不能使用地图或向量。@Raymond我在帖子中没有注意到这一点。它一定在评论中的某个地方。@ZDF很抱歉没有向我解释我的问题,这是我第一次发帖。谢谢你的帮助,谢谢。
#include <iostream>
#include <vector>
int main()
{
// Test sentence
std::string sentence = "abc";
// Removed, you could test with std::string morse[3]
// Dynamic array set to the size of the length sentence
auto *morse = new std::string[sentence.length()];
// Loop through each character of the sentence,
// increment the position counter, store the value
// in the array
int position = 0;
for (auto &c : sentence)
{
if (c == 'a' || c == 'A')
{
std::cout << ".-";
morse[position++] = (".-");
}
if (c == 'b' || c == 'B')
{
std::cout << "-...";
morse[position++] = ("-...");
}
if (c == 'c' || c == 'C')
{
std::cout << "-.-.";
morse[position++] = ("-.-.");
}
}
std::cout << std::endl;
// Loop through the array, and convert
// each morse sequence back, for loop
// loops based on the size of the position
for (int i = 0; i < position; i++)
{
if (morse[i] == ".-")
std::cout << 'A';
if (morse[i] == "-...")
std::cout << 'B';
if (morse[i] == "-.-.")
std::cout << 'C';
}
return 0;
}
#include <iostream>
#include <string>
#include <fstream>
const char* ascii_to_morse[] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
"-----", ".----","..---","...--","....-",".....","-....","--...","---..","----.", // 0...9
0, 0, 0, 0, 0, 0, 0,
".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.",
"---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..", // A...Z
};
const char morse_to_ascii[] =
{
0, 'E', 'T', 0, 'I', 'A', 0, 'N', 'M', 0, 0, 0, 0, 'S', 'U', 0, 'R', 'W', 0, 0, 0, 0, 'D', 'K',
0, 'G', 'O', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'H', 'V', 0, 'F', 0, 0, 0, 0, 0, 'L', 0, 0,
'P', 'J', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'B', 'X', 0, 'C', 'Y', 0, 0, 0, 0, 'Z', 'Q', 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '5', '4', 0, 0, '3', 0, 0, 0, 0, 0, 0, 0, 0, '2', 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '1', 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
'6', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '7', 0, 0,
0, 0, 0, 0, 0, 0, '8', 0, 0, '9', '0',
};
int main()
{
// ascii to morse test
{
ifstream is{ "c:\\temp\\ascii.txt" };
if (!is)
return -1;
ofstream os{ "c:\\temp\\morse.txt" };
if (!os)
return -2;
// ascii to morse
char c;
while (is >> c)
os << ascii_to_morse[c] << ' ';
}
// morse to ascii test
{
ifstream is{ "c:\\temp\\morse.txt" };
if (!is)
return -1;
ofstream os{ "c:\\temp\\ascii_check.txt" };
if (!os)
return -2;
// morse to ascii
string m;
while (is >> m)
{
// convert m to index (ternary converter)
int index = 0;
for (int i = 0; i < (int)m.length(); ++i)
{
int digit = m[i] == '.' ? 1 : 2;
index = index * 3 + digit;
}
// look for the associated ascii code
os << morse_to_ascii[index] << ' ';
}
}
}
#include <iostream>
#include <string>
#include <fstream>
#include <map>
using namespace std;
// http://www.csgnetwork.com/morsecodechrtbl.html
map< char, string > ascii_to_morse =
{
{'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',"--.."},
{'0',"-----"},
{'1',".----"},
{'2',"..---"},
{'3',"...--"},
{'4',"....-"},
{'5',"....."},
{'6',"-...."},
{'7',"--..."},
{'8',"---.."},
{'9',"----."},
{'.',".-.-.-"},
{',',"--..--"},
{'?',"..--.."},
{'=',"-...-"},
};
map< string, char > morse_to_ascii;
int main()
{
// build the reversed map
for (auto kv : ascii_to_morse)
morse_to_ascii[kv.second] = kv.first;
// ascii to morse test
{
ifstream is{ "c:\\temp\\ascii.txt" };
if (!is)
return -1;
ofstream os{ "c:\\temp\\morse.txt" };
if (!os)
return -2;
// ascii to morse
char c;
while (is >> c)
os << ascii_to_morse[c] << ' ';
}
// morse to ascii test
{
ifstream is{ "c:\\temp\\morse.txt" };
if (!is)
return -1;
ofstream os{ "c:\\temp\\ascii_check.txt" };
if (!os)
return -2;
// morse to ascii
string m;
while (is >> m)
os << morse_to_ascii[m] << ' ';
}
}