我试图制作一个程序,将莫尔斯代码转换为文本,但它返回代码0xC0000005。我已尽可能多地搜索但我无法找到解决方案。这是我写的代码:
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int find_position(string toFind){
string codes[26]={ ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.." };
for(int a=0;a<26;a++){
if(codes[a]==toFind){
return a;
}
}
}
int main(){
cout << "Enter the code(seprate with space)";
char to_translate[1000];
cin.getline(to_translate,1000);
int c,code;
string words[1000];
for(int a=0;a<sizeof(to_translate);a++){
if(to_translate[a]!=' '){
words[c] += to_translate[a];
}else{c++;}
}
string words2[sizeof(words)];
c=0;
for(int a=0;a<sizeof(words);a++){
if(words[a]=="/"){
c++;
} else{
words2[c] += (char) (find_position(words[a])+64);
}
}
cout << words2;
return 0;
}
答案 0 :(得分:1)
你有一行文字,用"/"
排除形成莫尔斯词,用" "
形成莫尔斯字母。
std::getline
接受deliminator参数,可让您轻松将文本拆分为相关细分。
#include <iostream>
#include <sstream>
#include <string>
#include <map>
int main()
{
std::map<std::string, char> morse = { { ".-", 'A' }, }; // etc
std::cout << "Enter the code(separate letters with space and words with slash)";
std::string line;
std::getline(std::cin, line);
std::stringstream line_stream(line);
for (std::string morse_word; std::getline(line_stream, morse_word, '\\');)
{
std::stringstream word_stream(morse_word);
std::stringstream word;
for (std::string letter; std::getline(word_stream, letter, ' ');)
{
word.put(morse[letter]);
}
std::cout << word.str();
}
return 0;
}