C++ 简易翻译器:英文到中文
以下是一个简单的英文到中文的翻译器示例:\n\nc++\n#include <iostream>\n#include <map>\n#include <string>\n\nstd::map<std::string, std::string> dictionary;\n\nvoid initializeDictionary() {\n dictionary["hello"] = "你好";\n dictionary["world"] = "世界";\n dictionary["computer"] = "计算机";\n // 添加更多的翻译对\n}\n\nstd::string translate(const std::string& word) {\n if (dictionary.count(word) > 0) {\n return dictionary[word];\n }\n return "未知单词";\n}\n\nint main() {\n initializeDictionary();\n\n std::string input;\n std::cout << "请输入要翻译的英文单词:" << std::endl;\n std::cin >> input;\n\n std::string translation = translate(input);\n std::cout << "翻译结果:" << translation << std::endl;\n\n return 0;\n}\n\n\n这个简单的翻译器使用 std::map 来存储英文单词和其对应的中文翻译。initializeDictionary() 函数用于初始化字典,你可以根据需要添加更多的翻译对。translate() 函数接受一个英文单词作为参数,并返回其对应的中文翻译。在 main() 函数中,用户可以输入一个英文单词,程序会输出其对应的中文翻译。
原文地址: https://www.cveoy.top/t/topic/pOK8 著作权归作者所有。请勿转载和采集!