用c语言写一个实现英汉翻译的程序代码

107 阅读1分钟

以下是一个简单的英汉翻译程序示例,使用数组来存储一些常见的单词和其对应的翻译:

c 复制

#include <stdio.h> #include <string.h>

// 定义单词和翻译的结构体 typedef struct { char english[50]; char chinese[50]; } WordTranslation;

int main() { WordTranslation translations[] = { {"apple", "苹果"}, {"banana", "香蕉"}, {"orange", "橙子"}, {"dog", "狗"}, {"cat", "猫"} };

int numTranslations = sizeof(translations) / sizeof(WordTranslation);

char inputWord[50];
printf("请输入要翻译的单词: ");
scanf("%s", inputWord);

int found = 0;
for (int i = 0; i < numTranslations; i++) {
    if (strcmp(inputWord, translations[i].english) == 0) {
        printf("%s 的翻译是: %s\n", inputWord, translations[i].chinese);
        found = 1;
        break;
    }
}

if (!found) {
    printf("未找到 %s 的翻译\n", inputWord);
}

return 0;

}  

在上述代码中,您可以根据需要添加更多的单词和翻译。