2016-05-15 58 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'是字符常量來允許不同的表示摩爾斯電碼。 –

相關問題