题目
●试题八
阅读下列函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
以下程序的功能是:从键盘上输入一个字符串,把该字符串中的小写字母转换为大写字母,输出到文件test.txt中,然后从该文件读出字符串并显示出来。
【程序】
#include<stdio.h>
main()
{FILE*fp;
charstr[100];inti=0;
if((fp=fopen("text.txt" (1) ))==NULL)
{printf("can't open this file.\n");exit(0);}
printf("input astring:\n");gest(str);
while(str[i])
{if(str[i]>=′a′ && str[i]<=′z′)
str[i]= (2) ;
fputc(str[i], (3) );
i++;
}
fclose(fp);
fp=fopen("test.txt", (4) );
fgets(str,100,fp);
printf("%s\n",str);
(5) ;
}
第1题
●试题四
阅读下列函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
函数QuickSort是在一维数组A[n]上进行快速排序的递归算法。
【函数】
void QuickSort(int A[],int s,int t)
{int i=s,j=t+1,temp;
int x=A[s];
do{
do i++;while (1) ;
do j--;while(A[j]>x);
if(i<j){temp=A[i]; (2) ; (3) ;}
}while(i<j);
A[a]=A[j];A[j]=x;
if(s<i-1) (4) ;
if(j+1<t) (5) ;
}
第2题
●试题二
阅读下列函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
该程序运行后,输出下面的数字金字塔
【程序】
include<stdio.h>
main ()
{char max,next;
int i;
for(max=′1′;max<=′9′;max++)
{for(i=1;i<=20- (1) ;++i)
printf(" ");
for(next= (2) ;next<= (3) ;next++)
printf("%c",next);
for(next= (4) ;next>= (5) ;next--)
printf("%c",next);
printf("\n");
}
}
第3题
●试题三
阅读下列函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
函数move(int*a,int n)用于整理数组a[]的前n个元素,使其中小于0的元素移到数组的前端,大于0的元素移到数组的后端,等于0的元素留在数表中间。
令a[0]~a[low-1]小于0(初始为空);a[low]~a[i-1]等于0(初始为空);a[i]~a[high]还未考察,当前考察元素为a[i]。a[high+1]~a[n-1]大于0(初始为空)。
【函数】
move(int*a,int n)
{
int i,low,high,t;
low=i=0;high=n-1;
while((1) )
if(a[i]<0)
{
t=a[i];a[i]=a[low];a[low]=t;
(2) ;i++;
}
else if((3) )
{t=a[i];a[i]=a[high];a[high]=t;
(4) ;
}
else (5) ;
}
第4题
●试题八
阅读以下说明和C++代码,将解答写入答题纸的对应栏内。
【说明】
源程序文件vectorClass.cpp,其中定义了用于表示向量的类vector,但类vector的定义并不完整。请按要求完成下列操作,将类vector的定义补充完整,并给出输出结果。
1.补充类vector的构造函数,该函数有参数x和y,它们都是int型的数据,默认值都为0。请使用参数列表的形式分别将类的数据成员a和b分别初始化为参数x和y的值。
2.完成类vector的成员函数input(int x,int y)的定义,将int型的参数x和y分别赋值给数据成员b和a。
3.完成类vector的友元函数friend double Multiply(vector &x,vector &y)的定义,先定义double型的临时变量c,然后将参数对象x和对象y的数据成员a与b分别相乘再相加后赋值给c,最后返回c的值。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件vectorClass.cpp清单如下:
#include <iostream.h>
class vector
{
int a;
int b;
public:
vector((1) ): (2)
{
}
void input(int x,int y)
{ (3)
}
void output()
{
cout<<'('<<a<<','<<b<<")"<<endl;
}
friend double Multiply(vector &x,vector &y);
};
double Multiply(vector &x,vector &y)
{
double c;
(4)
return c;
}
void main()
{
vector x(10,20),y;
double d;
y.input(2,3);
d=Multiply(x,y);
cout<<d<<endl;
}
程序输出结果是: (5) 。
第5题
●试题一
阅读下列函数说明和C代码,把应填入其中n处的字句写在答卷的对应栏内。
【函数1.1说明】
函数strcpy(char*to,char*from)将字符串from复制到字符串to。
【函数1.1】
void strcpy(char*to,char*from)
{while((1 ) );}
【函数1.2说明】
函数merge(int a[ ],int n,int b[ ],int m,int *c)是将两个从小到大有序数组a和b复制合并出一个有序整数序列c,其中形参n和m分别是数组a和b的元素个数。
【函数1.2】
void merge(int a[ ],int n,int b[ ],int m,int *c)
{ int i,j;
for(i=j=0;i<n && j<m;)
*c++=a[i]<b[j]? a[i++]:b[j++];
while((2) )*c++=a[i++];
while((3) )*c++=b[j++];
}
【函数1.3说明】
递归函数sum(int a[ ],int n)的返回值是数组a[ ]的前n个元素之和。
【函数1.3】
int sum(int a[ ],int n)
{ if(n>0)return (4) ;
else (5) ;
}
第6题
●试题二
阅读下列程序或函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【函数2.1说明】
函数strcmp()是比较两个字符串s和t的大小。若s<t函数返回负数;若s=t函数返回0;若s>t,函数返回正数。
【函数2.1】
int strcmp(char *s,char *t)
{ while(*s && *t && (1) ){
s++;t++;
}
return (2) ;
}
【程序2.2说明】
在n行n列的矩阵中,每行都有最大的数,本程序求这n个最大数中的最小一个。
【程序2.2】
#includestdio.h
#define N 100
int a[N][N];
void main()
{ int row ,col,max,min,n;
/*输入合法n(<100),和输入n×n个整数到数组a的代码略*/
for (row=0;row<n;row++){
for(max=a[row][0],col=1;col<n;col++)
if((3) )max=a[row][col];
if((4) )min=max;
else if((5) )min=max;
}
printf ("The min of max numbers is %d\n",min);
}
第7题
●试题二
阅读下列函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明2.1】
L为一个带头结点的循环链表。函数deletenode(LinkList L,int c)的功能是删除L中数据域data的值大于c的所有结点,并由这些结点组建成一个新的带头结点的循环链表,其头指针作为函数的返回值。
【函数2.1】
LinkList deletenode(LinkList L,int c)
{
LinkList Lc,p,pre;
pre=L;
p= (1) ;
Lc=(LinkList)malloc(sizeof(ListNode));
Lc->next=Lc;
while(p!=L)
if(p->data>c)
{
(2) ;
(3) ;
Lc->next=p;
p=pre->next;
}
else
{
pre=p;
p=pre->next;
}
return Lc;
}
【说明2.2】
递归函数dec_to_k_2(int n,int k)的功能是将十进制正整数n转换成k(2≤k≤9)进制数,并打印。
【函数2.2】
dec_to_k_2(int n,int k)
{∥将十进制正整数n转换成k(2≤k≤9)进制数
if(n!=0)
{
dec_to_k_2((4) ,k);
printf("%d", (5) );
}
}
第8题
●试题二
阅读下列函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
函数print(BinTreeNode*t;DateType &x)的功能是在二叉树中查找值为x的结点,并打印该结点所有祖先结点。在此算法中,假设值为x的结点不多于一个。此算法采用后序的非递归遍历形式。因为退栈时需要区分右子树。函数中使用栈ST保存结点指针ptr以及标志tag,Top是栈顶指针。
【函数】
void print(BinTreeNode*t;DateType &x){
stack ST;int i,top;top=0;∥置空栈
while(t!=NULL &&t->data!=x‖top!=0)
{while(t!=NULL && t->data!=x)
{
∥寻找值为x的结点
(1) ;
ST[top].ptr=t;
ST[top].tag=0;
(2) ;
}
if(t!=Null && t->data==x){∥找到值为x的结点
for(i=1; (3) ;i++)
printf("%d",ST[top].ptr->data);}
else{
while((4) )
top--;
if(top>0)
{
ST[top].tag=1;
(5) ;
}
}
}
第9题
●试题三
阅读下列函数说明和C代码,将应填入(n)处的字句写在答题纸的对应栏内。
【说明】
函数diff的功能是:根据两个由整数(都大于-32768)按升序构成的单链表L1和L2(分别由A,B指向)构造一个单链表L3(由*r指向),要求L3中的所有整数都是L1,并且不是L2中的整数,还要求L3中的所有整数都两两不等。
【函数】
#include<mallo
C.h>
typedef struct node{
int d;
struct node *next
}Node;
void diff(Node *A,Node *B,Node **r)
{
int lastnum;
Node*p;
*r=NULL;
if(!A)return;
while((1) )
if(A->d<B->d)
{
lastnum=A->d;
p=(Node*)malloc(sizeof(Node));
p->d=lastnum;
p->next=*r; (2) ;
do
A=A->next;
while((3) );
}
else if(A->d>B->d)
B=B->next;
else{
(4) ;
lastnum=A->d;
while (A && A->d==lastnum)A=A->next;
}
while(A)
{
lastnum=A->d;
p=(Node*)malloc(sizeof(Node));
p->d=lastnum;
(5) ;
*r=p;
while (A && A->d==lastnum) A=A->next;
}
}
第10题
使用VC++6.0打开考生文件夹下的源程序文件2.cpp。阅读下列函数说明和代码。函数num(ehar*str)用于返回字符串中非数字的个数。 例如:abcl23abc45 返回值为:6 将函数num补充完整。 注意:请勿改动主函数main。 试题程序: include<iostream.h> intnum(char*str) { } intmain { charstr[1024]; cout<<"pleaseinputastring:"<<endl; cin.getline(str,1024); cout<<"charnumberis"<<num(str)<<endl: return0; }
为了保护您的账号安全,请在“赏学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!