以下是一个简单的英文到中文的翻译器示例:

#include <iostream>
#include <map>
#include <string>

std::map<std::string, std::string> dictionary;

void initializeDictionary() {
    dictionary["hello"] = "你好";
    dictionary["world"] = "世界";
    dictionary["computer"] = "计算机";
    // 添加更多的翻译对
}

std::string translate(const std::string& word) {
    if (dictionary.count(word) > 0) {
        return dictionary[word];
    }
    return "未知单词";
}

int main() {
    initializeDictionary();

    std::string input;
    std::cout << "请输入要翻译的英文单词:" << std::endl;
    std::cin >> input;

    std::string translation = translate(input);
    std::cout << "翻译结果:" << translation << std::endl;

    return 0;
}

这个简单的翻译器使用std::map来存储英文单词和其对应的中文翻译。initializeDictionary()函数用于初始化字典,你可以根据需要添加更多的翻译对。translate()函数接受一个英文单词作为参数,并返回其对应的中文翻译。在main()函数中,用户可以输入一个英文单词,程序会输出其对应的中文翻译

请帮我用c++写一个简单的翻译器

原文地址: https://www.cveoy.top/t/topic/h5Sx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录