万能百科  > 所属分类  > 

阅读以下说明C++代码,将应填入(n)处的字句写在对应栏内。[说明] 以下程序的功能是实现堆栈的一些

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

[说明]

以下程序的功能是实现堆栈的一些基本操作。堆栈类stack共有三个成员函数:empty判断堆栈是否为空;push进行人栈操作;pop进行出栈操作。

[C++程序]

include "stdafx. h"

include <iostream, h>

eonst int maxsize = 6;

class stack {

float data[ maxsize];

int top;

public:

stuck(void);

~ stack(void);

bool empty(void);

void push(float a);

float pop(void);

};

stack: :stack(void)

{ top =0;

cout < < "stack initialized." < < endl;

}

stack:: ~stack(void) {

cout < <" stack destoryed." < < endl;

bool stack:: empty (void) {

return (1);

void stack: :push(float a)

if(top= =maxsize) {

cout < < "Stack is full!" < < endl;

return;

data[top] =a;

(2);

}

float stack:: pop (void)

{ if((3)){

cout< < "Stack is undcrflow !" < < endl;

return 0;

(4);

return (5);

}

void main( )

{ stack s;

coat < < "now push the data:";

for(inti=l;i< =maxsize;i+ +) {

cout< <i< <" ";

s. push(i);

}

coat < < endl;

cout< < "now pop the data:";

for(i = 1 ;i < = maxsize ;i + + )

cout< <s. pop()< <" ";

}

正确答案:

(1)top==0? true:false (2)top++(或者top =top+1)(3)top==0 (4)top--(或者top =top-1)(5)data[top](1)top==0? true:false (2)top++(或者top =top+1)(3)top==0 (4)top--(或者top =top-1)(5)data[top] 解析:(1)判断堆栈是否为空的条件是top为0,而且本函数的返回值为布尔类型,故此处应该填写top==0? true:false;(2)数据入栈后,

词条内容仅供参考,如果您需要解决具体问题
(尤其在法律、医学等领域),建议您咨询相关领域专业人士。

标签