问答题

阅读下列C程序和程序说明,将应填入(n)处的字句写在对应栏内。<br>【说明】本程序从正文文件text.in中读入一篇英文短文,统计该短文中不同单词及出现次数,并按词典编辑顺序将单词及出现次数输出到正文文件word.out中。<br>程序用一棵有序二叉树存储这些单词及其出现的次数,边读入边建立,然后中序遍历该二叉树,将遍历经过的二叉树上的结点的内容输出。<br>include <stdio.h><br>include <malloc.h><br>include <ctype.h><br>include <string.h><br>define INF "text.in"<br>define OUTF "wotd.out"<br>typedef struct treenode{<br>char *word;<br>int count;<br>struct treenode *left,*right;<br>}BNODE<br>int getword (FILE *fpt,char *word)<br>{ char c;<br>c=fgetc (fpt);<br>if (c=EOF)<br>return 0;<br>while(!(tolower(c)>='a' && tolower(c)<='z'))<br>{ c=fgetc (fpt);<br>if (c==EOF)<br>return 0;<br>} /*跳过单词间的所有非字母字符*/<br>while (tolower (c)>='a' && tolower (c)<='z')<br>{ *word++=c;<br>c=fgetc (fpt);<br>}<br>*word='\0';<br>return 1;<br>}<br>void binary_tree(BNODE **t,char *word)<br>{ BNODE *ptr,*p;int compres;<br>P=NULL; (1);<br>while (ptr) /*寻找插入位置*/<br>{ compres=strcmp (word, (2) );/*保存当前比较结果*/<br>if (!compres)<br>{ (3);return;}<br>else<br>{ (4);<br>ptr=compres>0? ptr->right:ptr->left;<br>}<br>}<br>ptr= (BNODE*) malloc (sizeof (BNODE)) ;<br>ptr->left = ptr->right = NULL;<br>ptr->word= (char*) malloc (strlen (word) +1) ;<br>strcpy (ptr->word, word);<br>ptr->count - 1;<br>if (p==NULL)<br>(5);<br>else if (compres > 0)<br>p->right = ptr;<br>else<br>p->left = ptr;<br>}<br>void midorder (FILE **fpt, BNODE *t)<br>{ if (t==NULL)<br>return;<br>midorder (fpt, t->left);<br>fprintf (fpt, "%s %d\n", t->word, t->count)<br>midorder (fpt, t->right);<br>}<br>void main()<br>{ FILE *fpt; char word[40];<br>BNODE *root=NULL;<br>if ((fpt=fopen (INF,"r")) ==NULL)<br>{ printf ("Can't open file %s\n", INF )<br>return;<br>}<br>while (getword (fpt, word) ==1 )<br>binary_tree (&root, word );<br>fclose (fpt);<br>fpt = fopen (OUTF, "w");<br>if (fpt==NULL)<br>{ printf ("Can't open file %s\n", OUTF)<br>return;<br>}<br>midorder (fpt, root);<br>fclose(fpt);<br>}


问答题

阅读下列程序说明和C代码,将应填人(n)处的字句写在对应栏内。<br>[程序5说明]<br>下列文法可用来描述化学分子式的书写规则(例如,A12(CO3)3”Cu(OH)2):<br>λ→β\βλ<br>β→δ\δn<br>δ→ξ\ξθ\(λ)<br>其中:λ是—个分子式;δ或是一个元素,或是一个带括号的(子)分子式,元素或是一个大写字母(记为ξ),或是一个大写字母和一个小写字母(记为ξθ)β或是一个δ,或是在δ之后接上一个整数n,δn表示β有n个δ的元素或(子)分子式。—个完整的分子式由若干个β组成。<br>当然一个正确的分子式除符合上述文法规则外,还应满足分子式本身的语义要求。<br>下面的程序输入分子式,按上述文法分析分子式,并计算出该分子式的分子量。例如:元素H的原子量是1,元素O的原子量是16。输入分子式H2O,程序计算出它的分子量为18 (1×2+16)。程序中各元素的名及它的原子量从文件atom.dat中读入。<br>[程序5]<br>include < stdio. h ><br>include < string. h ><br>define MAXN 300<br>define GMLEN 30<br>struct elem { char name[ ]; /* 元素名*/<br>double v;/*原子量*/<br>} nTbl [MAXN];<br>char cmStr [GMLEN], * pos;<br>int c;FILE * fp;<br>double factor();<br>double atom() /* 处理文法符号δ*/<br>{char w [3];int i; double num;<br>while((c = * pos++) =='||c =='\t'); /*略过空白字符*/<br>if(c == '\n') return 0.0;<br>if(c>='A' && C <='Z') {/*将元素名存入W */<br>w[i =0]=c;c= * pos ++<br>if(c >='a'&& c <='z')w[ ++i] =c;else pos--;<br>w[ ++i] ='\0',<br>for(i =0;nTbl [i]. v >0.0;i ++)<br>if(strcmp (w,nTbl[i]. name) ==0) return nTbl [i]. v;<br>printf (" \n元素表中没有所输入的无素: \t%s\n',w); retur n - 1.0;<br>} elseif (c = ='(') {<br>if((num=(1)) <0.0)return -l.0; /*包括可能为空的情况*/<br>if(* pos ++ ! = ')') { printf (" 分子式中括号不匹配!/n") ;return - 1.0; }<br>return num;<br>}<br>printf ("分子式中存在非法字符:\t%c\n" ,c);<br>return - 1.0;<br>}<br>double mAtom() /* 处理文法符号β*/<br>{ double num ;int n = ];<br>if((num=(2)) <0.0)return-l.0;<br>c= *pos++;<br>if(c >='O'&&c <='9') {<br>n = 0; while(c > = 0&&c < ='9')<br>{n=(3);<br>c= *poss ++;<br>}<br>}<br>pos --;<br>return num * n;<br>}<br>double factor() /*处理文法符号λ*/<br>{ double num =0.0,d;<br>if((hum = mAtom ()) < 0.0) return - 1.0;<br>while(* pos >= 'A'&& * pos <= 'Z'||* pos == '(') {<br>if((d=(4)) <0.0)return-1.0;<br>(5);<br>} return num;<br>void main()<br>{ char fname[ ] ="atom. dst"; /*元素名及其原子量文件*/<br>int i;double num;<br>if((fp=fopon(fname,"r" )) == NULL) { /*以读方式打开正文文件*/<br>prinff("Can net open%s file. \n' ,fname) ;return /*程序非正常结束 */<br>i=0;<br>while(i < MAXN&&fscanf (fp," %s%lf,bTbl[i]. name,&nTbl[i]. v) ==2)<br>i++;<br>fclose(fp) ;nTbl[i]. v =-1.0;<br>while(1) [/*输入分子式和计算分子量循环,直至输入空行结束*/<br>printf(" \n 输入分子式! (空行结束) \n" ) ;gets(cmStr);<br>pos = cmStr;<br>if(cmStr[0] == '\0') break;<br>if((num = later() ) > 0.0)<br>if(* pos! = '\0')printf("分子式不完整! \n" );<br>else printf("分子式的分子量为%f\n",num);<br>}


问答题

阅读以下说明和Java代码,回答问题<br>[说明]<br>在某些系统中,存在非常复杂的对象,可以采用循序渐进的方式进行组合将小对象组合,成复杂的对象。<br>以下实例展示了Builder(生成器)模式。该实例用来建立“文件”,文件内容包括:一个标题、一串字符以及一些有项目符号的项目。Builder类规定组成文件的方法,Director类利用这个方法产生一份具体的文件。图6-1显示了各个类间的关系。<br>以下是Java语言实现,能够正确编译通过。<br>[Java代码]<br>//Builder. java文件<br>public (1) class Builder {<br>public abstract void makeTitle(String title);<br>public abstract void makeString(String str);<br>public abstract void makeItems(String[] items);<br>public abstract Object getResult();<br>}<br>//Director. java文件<br>public class Director{<br>private (2) builder;<br>public Director(Builder builder){<br>this. builder = builder;<br>}<br>public Object construct(){<br>builder.makeTitle("Greeting");<br>builder.makeString("从早上到白天结束");<br>builder.makeItems(new String[]{"早安", "午安",});<br>builder.makeString("到了晚上");<br>builder.makeItems(new String[]("晚安", "好梦",});<br>return builder.getResult();<br>}<br>}<br>//TextBuilder.java文件<br>public class TextBuilder (3) Builder{<br>private StringBuffer buffer = new StringBuffer();<br>public void makeTitle(String title){<br>buffer.append("『" + title + "』"\n\n");<br>}<br>public void makeString(String str){<br>buffer.append('■' + str + "\n\n ");<br>}<br>public void makeItems(String[] items){<br>for(int i = 0; i< (4) ; i++){<br>buffer.append('·' + items[i] + "\n");<br>}<br>buffer.append("\n");<br>}<br>public Object getResult(){<br>return buffer.toString();<br>}<br>}<br>//Main.java文件<br>public class Main {<br>public static void main(String[] args) {<br>Director director = new Director(new TextBuilder());<br>String result = (String)director. (5) ;<br>System.out.println(result);


火星搜题