问答题
[说明]
下面的词典类Dic实现了简单的英译汉功能。程序运行后的输出为“我是一个学生”。
[C++程序]
#include <iostream.h>
#include <string.h>
#define Max 100
class Dic
int top;
char words[Max] [12];
char mean[Max] [20];
public:
Die()top=0;
void add(char w[],char m[])
strcpy(words[top],w);
strcpy(mean[top],m);
(1) ;
void trans(char str[])
int i=0,j=0,k=0,s;
char w[12],h[200];
while(l)
if(str[i]! =’’&&str[i]! =’\0’)
w[j++]=str[i]; //读取单词的一个字符,记录在w中
else
w[j]=’\0’;
for(s=0;s < top;s++)
if(strcmp(words[s],w) (2) 0) break;
if(s<top) //找到了,翻译成对应的mean[s]
strcpy(w,mean[s]); j= (3) ;
else //未找到,翻译成(unknown)
strcpy(w,"(unknown)"; j=9;
for(s=0;s<j;s++)
h[k++]=w[s];
if(str[i]==’\0’) (4) ; break;
j=0;
i++;
cout<<h<<endl;
;
void main()
Dic obj;
obj.add("I","我");
obj.add("am","是");
obj.add("student","学生");
obj.add("a","一个");
obj.trans(" (5) ");
【参考答案】
(1) top++ (2) = = (3) strlen(w) (4) h[k]=’\0’ (5) I am a stu......
(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)