2016-05-15 59 views
1

我是一个完整的C++初学者,这种知识来自我尝试学习的其他语言。下面的代码是我正在尝试构建的莫尔斯码翻译器的一个函数,我很确定这甚至不是接近它的“好方法”。我的问题是,如何让程序查看用户输入的字符串,并将每个字母改为莫尔斯。如何检查字符串中的字母出现?

string ReplaceAll(std::string str, const std::string& from, const std::string& to){ 
size_t start_pos = 0; 
while ((start_pos = str.find(from, start_pos)) != std::string::npos) { 
    str.replace(start_pos, from.length(), to); 
    start_pos += to.length(); // Handles case where 'to' is a substring of 'from' 
} 
return str;} 


void Translate(string s) { 
static string s2 = ReplaceAll(string(s), std::string("a"), std::string(".- ")); 
static string s3 = ReplaceAll(string(s2), std::string("b"), std::string("-... ")); 
static string s4 = ReplaceAll(string(s3), std::string("c"), std::string("-.-. ")); 
static string s5 = ReplaceAll(string(s4), std::string("d"), std::string("-.. ")); 
static string s6 = ReplaceAll(string(s5), std::string("e"), std::string(". ")); 
static string s7 = ReplaceAll(string(s6), std::string("f"), std::string("..-. ")); 
static string s8 = ReplaceAll(string(s7), std::string("g"), std::string("--. ")); 
static string s9 = ReplaceAll(string(s8), std::string("h"), std::string(".... ")); 
static string s10 = ReplaceAll(string(s9), std::string("i"), std::string(".. ")); 
static string s11 = ReplaceAll(string(s10), std::string("j"), std::string(".--- ")); 
static string s12 = ReplaceAll(string(s11), std::string("k"), std::string("-.- ")); 
static string s13 = ReplaceAll(string(s12), std::string("l"), std::string(".-.. ")); 
static string s14 = ReplaceAll(string(s13), std::string("m"), std::string("-- ")); 
static string s15 = ReplaceAll(string(s14), std::string("n"), std::string("-. ")); 
static string s16 = ReplaceAll(string(s15), std::string("o"), std::string("--- ")); 
static string s17 = ReplaceAll(string(s16), std::string("p"), std::string(".--. ")); 
static string s18 = ReplaceAll(string(s17), std::string("q"), std::string("--.- ")); 
static string s19 = ReplaceAll(string(s18), std::string("r"), std::string(".-. ")); 
static string s20 = ReplaceAll(string(s19), std::string("s"), std::string("... ")); 
static string s21 = ReplaceAll(string(s20), std::string("t"), std::string("- ")); 
static string s22 = ReplaceAll(string(s21), std::string("u"), std::string("..- ")); 
static string s23 = ReplaceAll(string(s22), std::string("v"), std::string("...- ")); 
static string s24 = ReplaceAll(string(s23), std::string("w"), std::string(".-- ")); 
static string s25 = ReplaceAll(string(s24), std::string("x"), std::string("-..- ")); 
static string s26 = ReplaceAll(string(s25), std::string("y"), std::string("-.-- ")); 
static string s27 = ReplaceAll(string(s26), std::string("z"), std::string("--.. ")); 

cout << s27 << endl; 

}

+5

制作将字母映射到其莫尔斯电码表示的字符和字符串的映射。创建某种缓冲区,例如一个'std :: stringstream'。迭代输入中的字符,并对每个字符在映射中查找并将相应的值写入缓冲区。用缓冲区做你想做的事情;把它变成一个字符串,打印出来等等。 – Biffen

+1

你可能更想用一个'std :: map '来做到这一点。 –

回答

0

这是一种错误的做法。而不是试图替换字符串的内容,它更容易简单地创建一个新的字符串:

std::string TranslateAll(const std::string &s) 
{ 
    std::ostringstream o; 

    for (char c:s) 
     o << Translate(c); 

    return o.str(); 
} 

现在,你可以写一个简单得多的

const char *Translate(char c) 

所有这一切确实是需要单个字符作为参数,并返回其莫尔斯码,作为一个简单的字符串。

更容易。

+0

为什么'ostringstream'不只是一个'string'? – Qwertiy

2

这是一个更好的解决方案,建立一个std::map与替换,并通过所有的字符,并建立一个新的字符串。使用解决方案字符.,-,!

#include <iostream> 
#include <map> 

typedef std::map<char, const char*> Replacements; 

std::string Translate(const Replacements& r, std::string s) 
{ 
    std::string result; 
    result.reserve(s.size() * 5); // optional: reserve guessed number of elements for new string 

    // for every element of the string 
    for (char c : s) 
    { 
     // search for replacement 
     Replacements::const_iterator iter = r.find(c); 
     if (iter != r.end()) 
     { 
      // found replacement 
      result += iter->second; 
      result.push_back(' '); 
     }  
    } 

    return result; 
} 


int main() 
{ 
    Replacements morse_code; 
    morse_code['a'] = ".-"; 
    morse_code['b'] = "-..."; 
    morse_code['c'] = "-.-."; 
    // ... 

    std::string in; 
    if (std::cin >> in) 
     std::cout << Translate(morse_code, in) << '\n'; 
} 
+0

几乎完美,但我会使用'use'而不是'typedef','auto'而不是'Replacements :: const_iterator'并且避免使用'char const *'元素类型。为什么不'std :: string'? –

+0

'auto'和'using'需要C++ 11。 'std :: string'不需要分配。 – R1tschY

+0

您已经使用了基于范围的for循环,它也需要C++ 11。如果内容足够短并且使用SSO(即使它分配了,对于这样的小数据无关紧要),std :: string将不会进行任何分配。此外,你可能并不总是想要分配字符串,但是例如做一些类似'morse_code ['a'] = {dot,minus};','dot'和'minus'是字符常量来允许不同的表示摩尔斯电码。 –

相关问题