问答题

[说明] 以下程序的功能是实现堆栈的一些基本操作。堆栈类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-......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
[说明]本程序实现的功能是判断指定的文本文件中是否包含指定的字符串,并且如果包含则返回其第一次出现的位置(行号、列号)。程序运行界面如下图所示,用户只要通过驱动器列表框、目录列表框选定文件夹,文件列表框会自动列出该文件夹下所有文本文件名称,选中其中某个文件,再输入目标字符串并且单击“查找”按钮,就能看到查找的结果。在开发过程中,驱动器列表框名为Drive1,目录列表框名为Dirl,文件列表框名为File1,目标字符串输入框名为Text1,“查找”按钮名为Command1。[Visual Basic 代码]Private Sub Drivel_Change()Dirl. Path= (1) ’更新目录列表框的路径End SubPrivate Sub Dirl_Change( )(2) = Dirl. Path ’更新文件列表框的路径 End SubPrivate Sub Commandl_Click( )Dim fso, a As ObjectDim i, j As IntegerIf (3) = Then ’判断是否已经选中某个文件MsgBox 请选择一个文件文件! (4) ’退出事件过程End If’创建文件系统对象以及文本流对象Set fso = CreateOhjecl( Scripting. FileSystemOhject )Set a = fso. OpenTextFile( Dirl. Path & & (3) )i=1Do While (5) <> True ’逐行读取文件,直至文件末尾strLine = a. ReadLinej = InStr(1, (6) )’返回目标字符串在文件这一行中的位置If j > 0 Then ’如果目标字符串在这一行出现,则返回其位置MsgBox 字符串第一次出现的位置是 & ( & i & , &j & ),, (4) ’退出事件过程End Ifi:i+lLoopMsgBox 字符串在文件中不出现, End Sub