●试题四
阅读下列程序说明和C程序,将应填入(n)处的字句写在答卷纸的对应栏内。
【程序说明】
该程序定义了两个子函数strsort和strmerge。它们分别实现了将一个字符串按字母顺序排序和将两个字符串合并排序,并删去相同字符。在主函数里,先输入两个字符串s1和s2,然后调用strsort函数对它们分别排序,然后调用strmerge函数将s1和s2合并,将合并后的字符串赋给字符串s3,最后输出字符串s3。
【程序】
#include
void strmerge(char*a,char*b,char*c)//将字符串a,b合并到字符串c中
{
char t,*w;
w=c;
while( (1) )
{
//找到字符串a,b当前字符中较小的字符
if(*a<*b)
{
t=*a;
(2) ;
}
else if(*a>*b)
{
t=*b;
(3) ;
}
else//字符串a,b当前字符相等
{
t=*a;
a++;
b++;
}
if( (4) )//开始,可直接赋值
*w=t;
else if(t!=*w)
//如果a,b中较小的当前字符与c中当前字符不相等,才赋值 (5) ;
}
if(*a!=\′\0′)//如果字符串a还没有结束,则将a的剩余部分赋给c
while(*a!=′\0′)
if(*a!=*w)
{
*(++w)=*a;
a++;
}
else
(6) ;
if(*6!=′\0′)//如果字符串b还没有结束,则将b的剩余部分赋给c
while(*b!=′\0′)
if(*b!=*w)
{
*(++w)=*b;
b++;
}
else
b++;
(7) ;
}
void strsort(char*s)//将字符串S中的字符排序
{
int i,j,n;
char t,*w;
W=S;
for(n=0;*w!=′\0′;n++)//得到字符串长度n
w++;
for(i=0;i for(j=i+1;j if( (8) ) { t=s[i];s[i]=s[j]; (9) ; } } void main() { char s1[100],s2[100],s3[100];printf("\nPlease,input the first string:"); scanf("%s",s1); printf("\nPlease input the second string:"); scanf("%s",s2); strsort(s1);//将字符串s1排序 strsort(s2);//将字符串s2排序 printf("%s\n",s1); printf("%s\n",s2); s3[0]=′\0′;//字符串s3的第一个字符先置′\0′结束标志 (10) //将s1和s2合并,按照字母顺序排列, //且要删去相同字符,存入s3中 printf("%s",s3); }
●试题六
阅读下列程序说明和C++代码,将应填入(n)处的字句写在答卷的对应栏内。
【程序6说明】
本程序实现两个多项式的乘积运算。多项式的每一项由类Item描述,而多项式由类List描述。类List的成员函数有:
createList():创建按指数降序链接的多项式链表,以表示多项式。
reverseList():将多项式链表的表元链接顺序颠倒。
multiplyList(List L1,List L2):计算多项式L1和多项式L2的乘积多项式。
【程序6】
#include
class List;
class ltem{
friend class List;
private:
double quot;
int exp;
Item*next;
public:
Item(double_quot,int_exp)
{ (1) ;}
};
class List{
private:
Item*list;
public:
List(){list=NULL;}
void reverseList();
void multiplyList(List L1,List L2);
void createList();
};
void List::createList()
{Item*p,*u,*pre;
int exp;
double quot;
list=NULL;
while (1) {
cout<<"输入多项式中的一项(系数、指数):"< cin>>quot>>exp: if(exp<0)break;//指数小于零,结束输入 if(quot==0)continue; p=list; while( (2) ){//查找插入点 pre=p;p=p->next;} if(p!=NULL&&exp==p->exp){ p->quot+=quot;continue;} u= (3) ; if(p==list) list=u; else pre->next=u; u->next=p;} } void List::reverseList() {Item*p,*u; if(list==NULL)return; p=list->next;list->next=NULL; while(p!=NULL){ u=p->next;p->next=list; list=p;p=u;} } void List::multiplyList(List L1,List L2) {Item*pLl,*pL2,*u; int k,maxExp; double quot; maxExp= (4) ; L2.reverseList();list=NULL; for(k=maxExp;k>=0;k--){ pL1=L1.list; while(pL1!=NULL&&pL1->exp>k)pL1=pL1->next; pL2=L2.list; while(pL2!=NULL&& (5) pL2=pL2->next; quot=0.0; while(pL1!=NULL&&pL2!=NULL){ if(pL1->exp+pL2->exp==k){ (6) ; pL1=pL1->next;pL2=pL2->next; }else if(pL1->exp+pL2->exp>k)pL1=pL1->next; else pL2=pL2->next; } if(quot!=0.0){ u=new Item(quot,k); u->next=list;list=u;} } reverseList();L2.reverseList(): } void main() {ListL1,L2,L; cout<<"创建第一个多项式链表\n";L1.createList(); cout<<"创建第二个多项式链表\n";L2.createList(); L.multiplyList(L1,L2); }
●试题七
【说明】
下面是一个Applet程序,其功能是根据给出的小时,分钟和秒数计算相等的秒数,即将1分钟化为60秒,依此类推。要求建立一个时间类,时间参数均作为类的成员变量,并且给出换算时间的方法,也作为这个类的成员函数,可以供外部对象进行调用。同时还需要在输出窗口中显示换算结果,并且将结果写到out3_3.txt文件中,本题给出确定的时间为4小时23分47秒,要求换算成以秒做单位的时间。
程序运行结果如图11所示。
图11
import javA.io.*;
import javA.awt.*;
import javA.applet.*;
/*
*/
public class ex7_7 extends Applet{
public void paint(Graphics g){
int nSum;
class myTime7_7{
public int h;
public int m;
public int s;
public int out;
public int caculateSecond(){
(1) ;
return out;
}
}
myTime7_7 objTime7_7 = new myTime7_7();
objTime7_7.h = 4;
objTime7_7.m = 23;
objTime7_7.s = 47;
nSum = objTime7_7. (2) ;
g.drawString ("时:"+objTime7_7.h, 20, 30);
g.drawString ("分:"+objTime7_7.m, 20, 50);
g.drawString ("秒:"+objTime7_7.s, 20, 70);
g.drawString ( (3) );
try {
FileOutputStream fos7_7 = new FileOutputStream("out7_7.txt");
BufferedOutputStream bos7_7=new BufferedOutputStream(fos7_7,1024);
PrintStream ps7_7=new PrintStream(bos7_7,false);
System.setOut(ps7_7);
System.out.println( (4) );
ps7_7.close();
} catch(IOException ioe) {
(5) (ioe);
}
}
}
ex7_7.html
code="ex7_7.class" width=800 height=400 >