阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。【说明】以下程序的功能是设计一个栈类stack<T>,并建立一个整数栈。【程序】include < iostream. h >include < stdlib. h >const int Max =20; //栈大小template < class T >class stack{ //栈元素数组T s[Max]; //栈顶下标int top;public:stack( ){top =-1; //栈顶初始化为-1}void push( const

题目

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

【说明】

以下程序的功能是设计一个栈类stack<T>,并建立一个整数栈。

【程序】

include < iostream. h >

include < stdlib. h >

const int Max =20; //栈大小

template < class T >

class stack{ //栈元素数组

T s[Max]; //栈顶下标

int top;

public:

stack( )

{

top =-1; //栈顶初始化为-1

}

void push( const T &item); //item入栈

T pop( ); //出栈

int stackempty( ) const; //判断栈是否为

};

template < class T >

void stack <T >::push(const T &item)

{

if(top==(1))

{

cout <<"栈满溢出" <<endl;

exit(1);

}

top ++

s[top] = item;

}

template < class T >

T stack<T> ::pop()

{

T temp;

if(top==(2))

{

cout <<"栈为空,不能出栈操作" < < endl;

exit(1);

}

temp =s[top];

top --;

return temp;

}

template < class T >

int stack < T >:: stackempty( ) const

{ return top == -1;

{

void main( )

{

stack <int> st;

int a[] ={1,2,3,4,5};

cout <<"整数栈" <<endl;

cout <<"入栈序列:" <<endl;

for(int i=0;i<4;i ++)

{

cout <<a[i] <<" ";

(3);

}

cout << endl <<"出栈序列";

while((4))

tout<<(5)<<" ";

cout< < endl;

}


相似考题
更多“阅读以下说明和C++程序,将应填入(n)处的字句写在对应栏内。【说明】 以下程序的功能是设计一个栈类s ”相关问题
  • 第1题:

    阅读以下说明和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);

    }


    正确答案:float t =1.0; i< =100 - sign * i/( t + sign* i) return s
    float t =1.0; i< =100 - sign * i/( t + sign* i) return s

  • 第2题:

    试题三(共 15 分)

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


    正确答案:

  • 第3题:

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






    答案:
    解析:

  • 第4题:

    阅读下列程序说明和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

    }


    正确答案:程序1优点:程序简洁;缺点:多执行了N-1次逻辑判断并且程序无法循环“流水”作业使得编译器无法对循环进行优化处理降低了效率。 程序2优点:循环的效率高;缺点:程序不简洁。
    程序1优点:程序简洁;缺点:多执行了N-1次逻辑判断,并且程序无法循环“流水”作业,使得编译器无法对循环进行优化处理,降低了效率。 程序2优点:循环的效率高;缺点:程序不简洁。

  • 第5题:

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

    【C++代码】 #include using namespace std; class invoice{ public: (1){ cout<<"This is the content of the invoice!"<

    答案:
    解析:
    (1) virtual void printInvoice() (2) ticket->printInvoice() (3) Decorator::printInvoice() (4) Decorator::printInvoice() (5) &a
    【解析】

    试题分析
    1.Invoice类下,义虛函数,按类图,函数名是printInvoice
    2.前面定义对象名是ticket,那么在ticket不为空的时候调用函数printInvoice
    3.这部分填写发票的抬头,看类图应该实现函数printInvoice ,Decorator装饰模式使用该方法
    4.这部分是发票的脚注,看类图应该实现函数printlnvoice,Decorator装饰模式使用该方法
    5.FootDecorator a(NULL) ;脚步的装饰参数是a,调用a参数,