Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 试图把一个字母编码成摩尔斯电码_C++_Encoding_Binary Tree - Fatal编程技术网

C++ 试图把一个字母编码成摩尔斯电码

C++ 试图把一个字母编码成摩尔斯电码,c++,encoding,binary-tree,C++,Encoding,Binary Tree,所以我正在制作一个程序,可以把一个字符串编码成莫尔斯电码。反之亦然。尽管在mc_tree.cpp中,我的ChartOMOSE方法有问题 字母及其摩尔斯电码等价物按其在morse-code.txt中的位置顺序存储在树中 摩尔斯电码.txt: e . t - i .. a .- n -. m -- s ... u ..- r .-. w .-- d -.. k -.- g --. o --- h .... v ...- f ..-. l .-.. p .--. j .--- b -... x -..

所以我正在制作一个程序,可以把一个字符串编码成莫尔斯电码。反之亦然。尽管在mc_tree.cpp中,我的ChartOMOSE方法有问题


字母及其摩尔斯电码等价物按其在morse-code.txt中的位置顺序存储在树中

摩尔斯电码.txt:

e .
t -
i ..
a .-
n -.
m --
s ...
u ..-
r .-.
w .--
d -..
k -.-
g --.
o ---
h ....
v ...-
f ..-.
l .-..
p .--.
j .---
b -...
x -..-
c -.-.
y -.--
z --..
q --.-
当用户在encode()中输入一个字符串时,该字符串将通过stringToMorse()和charToMorse()方法分解为字母。chartomose()是一个递归方法,它使用char的参数在树中搜索,并使用指向树根的指针

它应该找到字符的匹配项,然后返回摩尔斯电码中的等效值。然而,我的代码并没有完成我认为在这一部分应该做的事情。我得到奇怪的输出错误,我不明白。我希望能够在树中进行chartomose()搜索,找到字母,然后输出该字母的莫尔斯电码值

输出:

inside encode
Please enter a message to encode: stringhere
inside stringToMorse
s is = stringhere
inside charToMorse
_, _, 0x10fd410, 0x10fd4b0
inside charToMorse
., e, 0x10fd550, 0x10fd5f0
inside charToMorse
.., i, 0x10fd7d0, 0x10fd870
inside charToMorse
..., s, 0x10fdcd0, 0x10fdd70
inside charToMorse
...., h, 0, 0
terminate called after throwing an instance of 'std::length_error'
what():  basic_string::_S_create
Aborted
以下是我的课程:

#include <iostream>
#include <fstream>
#include <sstream>
#include "mc_tree.h"
#include "MTNode.h"

using namespace std;

mc_tree::mc_tree(){
    root = new MTNode("_", "_", NULL, NULL);
    build_tree();   
}

void mc_tree::build_tree(){
    string line;
    ifstream morse_code_file;
    morse_code_file.open("morse-code.txt");
    //cout << "loading data file" << endl;
    while(!morse_code_file.eof())
    {
        //cout << "inside build_tree() while loop" << endl;
        if(morse_code_file.eof())
        {            
            break;
        }
        else{
            getline(morse_code_file, line);
            addToTree(line);                    
        }                            
    }
    morse_code_file.close();
    //cout << "exiting build_tree()" << endl;   
}

你需要使用树吗?由于您的字母表是有限的,而且非常小,因此最好在查找表中对值进行编码。这将比树搜索简单得多,执行速度也快得多。

因为另一个答案表明查找表是一个更干净的解决方案

关于您的代码: 您使用NULL终止叶节点。。。现在,当您选择ChartOMOSE时,即使您尚未检查整个树,此代码仍将终止搜索:

else if(current == NULL) 
    return "";

因此,当您执行递归调用时,请验证返回值,并仅在您实际找到字母时终止。

我假设这是学习使用树的家庭作业? 如果不是,一个更简单的解决方案可能是如下所示:

#include <iostream>
#include <string>
#include <map>
#include <sstream>
using namespace std;

typedef map<string, string> MorseMap;

MorseMap setupAlphaToMorse()
{
  MorseMap map;
  // could populate from a file, or just add them here:
  map["e"] = ".";
  map["t"] = "-";
  // ...
  map["h"] = "....";
  // ...
  return map;
}

string encode(MorseMap& map, const string& source)
{
  stringstream ss;
  for (size_t i = 0; i < source.size(); ++i)
  {
    ss << map[string(1,source[i])] << " ";
  }
  return ss.str();
}
#包括
#包括
#包括
#包括
使用名称空间std;
typedef映射MorseMap;
MorseMap设置Alphatomorse()
{
莫尔斯地图;
//可以从文件填充,或仅在此处添加:
地图[“e”]=”;
地图[“t”]=“-”;
// ...
地图[“h”]=“…”;
// ...
返回图;
}
字符串编码(MorseMap和map、常量字符串和源)
{
细流ss;
对于(size_t i=0;i学生:你能修正你的缩进吗?“字母和它们的摩尔斯电码等价物按照它们在morse code.txt中的位置顺序存储在一棵树中。”-什么?@yi_H See:还有1)你的代码太长;在发布之前尽量简化它;2)你的代码太长;当代码出现故障时,停止添加代码;3)你所有的问题都是这样的。不要每次卡住时都在这里转储代码。它必须是一棵树。否则我就已经处理完了作业:)是的,它必须是一棵树。
string mc_tree::charToMorse(char c, MTNode* current){
    string encoded_char;
    char l = atoi(current->letter.c_str());
    cout << "inside charToMorse " << endl;
    cout << current->code << ", " << current->letter << ", "\
        << current->dot << ", " << current->dash << endl;

    if(l == c)
        return current->code;
    else if(current == NULL) 
        return "";
    else{
        if(current->dot != NULL)            
            return charToMorse(c, current->dot);
        //cout << current->data;
        if(current->dash != NULL)
            return charToMorse(c, current->dash);
    }

}

void mc_tree::print(MTNode *r){
    if (r) {
        print(r->dot);
        cout << r->letter << " " << r->code << endl;
        print(r->dash);
    }       
}
#ifndef _MTNODE_
#define _MTNODE_

using std::string;

struct MTNode{
    std::string code;
    std::string letter;
    MTNode* dot;
    MTNode* dash;

    //constructor 
    MTNode(const std::string c, const std::string l, MTNode* left = NULL,
        MTNode* right = NULL) : code(c), letter(l), dot(left), dash(right) {}
    //MTNode(): dot(NULL), dash(NULL) {}

    //destructor
    virtual ~MTNode() {}

    //accessors
    std::string get_code(){
        return code;    
    }

    std::string get_letter(){
        return letter;  
    }   
};

#endif 
else if(current == NULL) 
    return "";
#include <iostream>
#include <string>
#include <map>
#include <sstream>
using namespace std;

typedef map<string, string> MorseMap;

MorseMap setupAlphaToMorse()
{
  MorseMap map;
  // could populate from a file, or just add them here:
  map["e"] = ".";
  map["t"] = "-";
  // ...
  map["h"] = "....";
  // ...
  return map;
}

string encode(MorseMap& map, const string& source)
{
  stringstream ss;
  for (size_t i = 0; i < source.size(); ++i)
  {
    ss << map[string(1,source[i])] << " ";
  }
  return ss.str();
}
int main()
{
  MorseMap alphaToMorse = setupAlphaToMorse();

  cout << "Result: " << encode(alphaToMorse, "the") << endl;
}