我是一个完整的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;
}
答案 0 :(得分: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';
}
答案 1 :(得分: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)
它所做的只是将一个字符作为参数,并将其莫尔斯代码作为一个简单的字符串返回。
更容易。