以下是引用苯宝宝有毒在2016-5-7 19:09:49的发言:
思路我已经明白了,只是有些函数还没有学到而已,再次感谢层主。
这类问题可以不用 switch,用查字典法。
程序代码:
#include <stdio.h>
#include <assert.h>
#include <string.h>
#define RECORD_COUNT 10
#define BUFFER_SIZE 20
const char dictionary[RECORD_COUNT][BUFFER_SIZE][BUFFER_SIZE]=
{
{"香蕉", "banana"},
{"苹果", "apple"},
{"葡萄", "Grape"},
{"柠檬", "Lemon "},
{"橙子", "Orange"},
{"梨", "Pear"},
{"西瓜", "Watermelon"}
};
char* _fruit(const char *inChinese, char *outEnglish)
{
assert((inChinese != NULL) && (outEnglish != NULL));
for (int i=0; i<RECORD_COUNT; i++)
{
if (strcmp(dictionary[i][0], inChinese) == 0)
{
strcpy(outEnglish, dictionary[i][1]);
return outEnglish;
}
}
strcpy(outEnglish, "没找到");
return outEnglish;
}
main ()
{
char sz[BUFFER_SIZE], buff[BUFFER_SIZE];
printf("请输入需要翻译的水果中文名称:");
scanf("%s", sz);
printf("%s -> %s\n", sz, _fruit(sz, buff));
}
[此贴子已经被作者于2016-5-8 08:33编辑过]