问答题

[说明]
下面的词典类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......

(↓↓↓ 点击下方‘点击查看答案’看完整答案、解析 ↓↓↓)
热门 试题

问答题
[说明] 下面的程序演示了根据随机产生的奖牌数,生成金银奖牌榜的过程。程序使用的排序法是简单排序法。以金牌得数为例,其思想是选择最大的元素,将它交换到最前面;然后对剩下的部分采用同样的方法,直到全部排序完成。 程序界面中,左右两个文本框分别用于存放随机产生的奖牌数以及生成的奖牌榜,名为Text1和Text2,上下两个按钮分别名为Command1和Command2。代码中使用的量主要有:一维数组 cntries,用于存储10个国家的名称,二维数组medals,其元素medals(i,0)和medals(i,1)分别用于存放第i个(i从0开始)国家的金、银牌数目。[Visual Basic代码]Dim cntries(10) As String, medals(10,2) As Integer’随机产生奖牌数Sub newMedals() …… ’为数组cntries和medals赋值End Sub’输出奖牌榜Sub printOut(txt As (1) ) Dim strResuh As String, i As Integer strResult=“国家”& Chr(9) &“金牌数”& Chr(9) &“银牌数”& vbCrLf For i=0 To 9 strResult = strResult & cntries(i) & Chr(9) & medals(i,0) & Chr(9) & medals(i,1) & vbCrLf Next txt.Text = strResultEnd Sub’交换两变量的值Sub exchange( (2) a As Variant, (2) b As Variant) Dim temp As Variant temp = a: a = b: b = tempEnd Sub’随机产生并输出奖牌数Private Sub Command1_Click() newMedals printOut Text1End Sub’生成并输出奖牌榜Private Sub Command2_Click() Dim i,j, k As Integer, temp As String For i = 0 To 9 ’按金牌数排序 j = i ’找到自第i个位置起全部数据中金牌得数最多者,记其下标为j For k = i + 1 To 9 If (3) Then j=k Next If i < > j Then ’若i,j不等,则交换对应位置的国家名、金银牌数目 exchange cntnes(i), entries(j) exchange medals(i, 0), medals(j, 0) exchange medals(i, 1), medals(j, 1) End If Next For i = 0 To 9 ’按银牌数进行二次排序 j = i For k = i + 1 To 9 If medals(k,0) <> medals(j, 0) Then (4) If (5) Then j = k Next If i < > j Then exchange cntries(i), cntries(j) exchange medals(i, 1), medals(j, 1) End If Next printOut Text2End Sub