【发布时间】:2017-01-18 02:40:16
【问题描述】:
我需要做一个简单的翻译器。例如:
- 输入:“foo” 输出:“bar”
- 输入:“the” 输出:“teh”
- 输入:“what” 输出:“wut”
我知道我可以这样写:
if (!strcmp(input, "foo"))
puts("bar");
else if (!strcmp(input, "the"))
puts("teh");
else if (!strcmp(input, "what"))
puts("wut");
但这又大又乱。有没有捷径可以做到这一点?我知道在 PHP 中(对不起,不可避免的语法错误,我不精通)有这样的东西:
value = array(
"foo" => "bar",
"the" => "teh",
"what" => "wut"
);
如何使用 PHP 数组之类的东西缩短原始代码?
【问题讨论】: