网友您好, 请在下方输入框内输入要搜索的题目:

题目内容 (请给出正确答案)

阅读下列C++程序和程序说明,将应填入(n)处的字句写在对应栏内。

【说明】

C++语言本身不提供对数组下标越界的判断。为了解决这一问题,在程序6中定义了相应的类模板,使得对厂任意类型的二维数组,可以在访问数组元素的同时,对行下标和列下标进行越界判断,并给出相应的提示信息。

include<iostream.h>

template <class T> class Array;

template <class T> class ArrayBody {

friend (1)

T* tpBody;

int iRows, iColumns, iCurrentRow;

ArrayBody (int iRsz, int iCsz) {

tpBody =(2)

iRows = iRsz; iColumns =iCsz; iCurrentRow =-1;

}

public:

T& operator[] (int j) {

bool row_error, column_error;

row_error=column_error=false;

try{

if (iCurrentRow < 0 || iCurrentRow >=iRows)

row_error=true;

if (j < 0 || j >=iColumns)

column_error=true;

if ( row_error==true || column_error == true)

(3)

}

catch (char) {

if (row_error==true)

cerr << "行下标越界[" << iCurrentRow << "] ";

if (column_error== true )

cerr << "列下标越界[" <<j << "]";

cout << "\n";

}

return tpBody[iCurrentRow * iColumns +j];

};

~ArrayBody ( ) { delete[] tpBody; }

};

template <class T> class Array {

ArrayBody<T> tBody;

public:

ArrayBody<T> & operator[] (int i) {

(4)

return tBody;

}

Array (int iRsz, int iCsz) :(5) {}

};

void main()

{ Array<int>a1(10,20);

Array<double>a2(3,5);

int b1;

double b2;

b1=a1[-5][10]; //有越界提示:行下标越界[-5]

b1=a1[10][15]; //有越界提示:行下标越界[10]

b1=a1[1][4]; //没有越界提示

b2=a2[2][6]; //有越界提示:列下标越界[6]

b2=s2[10][20]; //有越界提示:行下标越界[10]列下标越界[20]

b2=a2[1][4]; //没有越界提示

}


参考答案

更多 “ 阅读下列C++程序和程序说明,将应填入(n)处的字句写在对应栏内。【说明】C++语言本身不提供对数组下标越界的判断。为了解决这一问题,在程序6中定义了相应的类模板,使得对厂任意类型的二维数组,可以在访问数组元素的同时,对行下标和列下标进行越界判断,并给出相应的提示信息。include<iostream.h>template <class T> class Array;template <class T> class ArrayBody {friend (1)T* tpBody;int iRows, iColumns, iCurrentRow;ArrayBody (int iRsz, int iCsz) {tpBody =(2)iRows = iRsz; iColumns =iCsz; iCurrentRow =-1;}public:T operator[] (int j) {bool row_error, column_error;row_error=column_error=false;try{if (iCurrentRow < 0 || iCurrentRow >=iRows)row_error=true;if (j < 0 || j >=iColumns)column_error=true;if ( row_error==true || column_error == true)(3)}catch (char) {if (row_error==true)cerr << "行下标越界[" << iCurrentRow << "] ";if (column_error== true )cerr << "列下标越界[" <<j << "]";cout << "\n";}return tpBody[iCurrentRow * iColumns +j];};~ArrayBody ( ) { delete[] tpBody; }};template <class T> class Array {ArrayBody<T> tBody;public:ArrayBody<T> operator[] (int i) {(4)return tBody;}Array (int iRsz, int iCsz) :(5) {}};void main(){ Array<int>a1(10,20);Array<double>a2(3,5);int b1;double b2;b1=a1[-5][10]; //有越界提示:行下标越界[-5]b1=a1[10][15]; //有越界提示:行下标越界[10]b1=a1[1][4]; //没有越界提示b2=a2[2][6]; //有越界提示:列下标越界[6]b2=s2[10][20]; //有越界提示:行下标越界[10]列下标越界[20]b2=a2[1][4]; //没有越界提示} ” 相关考题
考题 阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。【C++程序】include include 阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。【C++程序】include < stdio. h >include < string. h >define Max 1000class Bank{int index;char date [Max] [10]; // 记录交易日iht amount[Max]; // 记录每次交易金额,以符号区分存钱和取钱int rest[ Max]; // 记录每次交易后余额static iht sum; // 账户累计余额public:Bank( ) {index =0;}void deposit( char d[ ] , int m) //存入交易{strcpy ( date [ index ], d);amount[ index] = m;(1);rest[ index] = sum;index++;}void withdraw (char d[ ], int m) //取出交易{strcpy( date[ index] ,d);(2);(3);rest[ index] = sum;index++;}void display( );};int Bank:: sum = 0;void Bank:: display ( ) //输出流水{int i;printf("日期 存入 取出 余额\n");for (4){printf(" %8s" ,date[i] );if (5)printf(" %6d" , -amount[i] );elseprintf( "%6d ",amount[i] );printf( "% 6d\n" ,rest[i] );} }void main( ){Bank object;object. deposit ( "2006.2.5", 1 00 );object. deposit( "2006.3.2" , 200);object. withdraw( "2006.4.1", 50);object. withdraw( "2006.4.5", 80);object. display ( );}本程序的执行结果如下:日期 存入 取出 余额 2006.2.5 100 1002006.3.2 200 3002006.4.1 50 2502006.4.5 80 170

考题 阅读以下说明和C++ 程序,将应填入(n)处的字句写在对应栏内。[说明]试从含有n个int 型数的数组中删去若干个成分,使剩下的全部成分构成一个不减的子序列。设计算法和编写程序求出数组的不减子序列的长。[C++ 程序]include<stdio.h>define N 100int b[]={9,8,5,4,3,2,7,6,8,7,5,3,4,5,9,1};int a [N];define n sizeofb/sizeofb[0]void main ( ){kit k,i,j;(1)(2)for (i=1;i<n; i++ ){for ( j=k;(3); j--);(4); /*长为 j+1 的子序列的终元素存储在 a[j+1]*/if ((5)k++; /*最长不减子序列长 k 增1*/}printf ( "K = %d\n ",k );}

考题 阅读以下说明和c++码,将应填入(n)处的字名写在的对应栏内。[说明] 以下函数完成求表达式的值,请填空使之完成此功能。float sum ( float x ){ float s=0.0;int sign = 1;(1);for(inti=1;(2); i+ +){t=t*x;s=s+(3);sign = - sign;(4);}

考题 阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。[说明]下面程序实现十进制向其它进制的转换。[C++程序]include"ioStream.h"include"math.h"includetypedef struct node {int data;node*next;}Node;Class Transform.{DUDlic:void Trans(int d,int i); //d为数字;i为进制void print();private:Node*top;};void Transform.:Trans(int d,int i){int m,n=0;Node*P;while(d>0){(1);d=d/i;p=new Node;if(!n){p->data=m;(2);(3);n++;}else{p->data=m;(4);(5);}}}void Transform.:print(){Node*P;while(top!=NULL){p=top;if(p->data>9)cout<<data+55;elsecout<<data;top=p->next;delete p;}}

考题 阅读以下说明,以及用C++在开发过程中所编写的程序代码,将应填入(n)处的字句写在对应栏内。【说明】在下面函数横线处填上适当的字句,使其输出结果为:构造函数.构造函数.1,25,6析构函数析构函数.【C++代码】include "iostream.h"class AA{ public;AA(int i,int j){A=i; B=j;cout<<"构造函数.\n";}~AA(){(1);}void print();private:int A, B;};void AA∷print(){cout<<A<<","<<B<<endl;}void main(){AA *a1, *a2;(2)=new AA(1, 2);a2=new AA(5, 6);(3);a2->print();(4) a1;(5) a2;}

考题 阅读下列程序说明和C++程序,把应填入其中(n)处的字句,写在对应栏内。【说明】阅读下面几段C++程序回答相应问题。比较下面两段程序的优缺点。①for (i=0; i<N; i++ ){if (condition)//DoSomething…else//DoOtherthing…}②if (condition) {for (i =0; i<N; i++ )//DoSomething}else {for (i=0; i <N; i++ )//DoOtherthing…}

考题 试题三(共 15 分)阅读以下说明和 C 程序,将应填入 (n) 处的字句写在答题纸的对应栏内。

考题 阅读下列说明和C++-代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 某发票(lnvoice)由抬头(Head)部分、正文部分和脚注(Foot)部分构成。现采用装饰(Decorator)模式实现打印发票的功能,得到如图5-1所示的类图。 【C++代码】 #include using namespace std; class invoice{ public: (1){ cout

考题 阅读下列说明和C++代码,回答问题,将解答填入答题纸的对应栏内。 【说明】某航空公司的会员积分系统将其会员划分为:普卡 (Basic)、银卡(Silver)和金卡 (Gold) 三个等级。非会员 (NonMember) 可以申请成为普卡会员。会员的等级根据其一年内累积 的里程数进行调整。描述会员等级调整的状态图如图 5-1 所示。现采用状态 (State) 模式实现上述场景,得到如图 5-2 所示的类图。 【问题1】(15分)阅读上述说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。

考题 阅读下列说明和?C++代码,将应填入(n)处的字句写在答题纸的对应栏内。 【说明】 阅读下列说明和?Java代码,将应填入?(n)?处的字句写在答题纸的对应栏内。 【说明】 某快餐厅主要制作并出售儿童套餐,一般包括主餐(各类比萨)、饮料和玩具,其餐品种 类可能不同,但其制作过程相同。前台服务员?(Waiter)?调度厨师制作套餐。现采用生成器?(Builder)?模式实现制作过程,得到如图?6-1?所示的类图。