?? p2-107.cpp
字號(hào):
#include<iostream.h>
const int MAX=5; //假定棧中最多保存5個(gè)數(shù)據(jù)
//定義名為stack的具有棧功能的類
class stack {
//數(shù)據(jù)成員
double num[MAX]; //存放棧數(shù)據(jù)的數(shù)組
int top; //指示棧頂位置的變量
public:
//成員函數(shù)
stack(char *name) //構(gòu)造函數(shù)
{
top=0;
cout<<"Stack "<<name<<" initialized."<<endl;
}
~stack(void) //析構(gòu)函數(shù)
{
cout << "Stack destroyed." << endl; //顯示信息
}
void push(double x) //入棧函數(shù)
{
if (top==MAX){
cout<<"Stack is full !"<<endl;
return;
};
num[top]=x;
top++;
}
double pop(void) //出棧函數(shù)
{
top--;
if (top<0){
cout<<"Stack is underflow !"<<endl;
return 0;
};
return num[top];
}
}
//以下是main()函數(shù),其用stack類創(chuàng)建棧對象,并使用了這些對象
main(void)
{
double x;
//聲明(創(chuàng)建)棧對象并初始化
stack a("a"),b("b");
//以下利用循環(huán)和push()成員函數(shù)將2,4,6,8,10依次入a棧
for (x=1; x<=MAX; x++)
a.push(2.0*x);
//以下利用循環(huán)和pop()成員函數(shù)依次彈出a棧中的數(shù)據(jù)并顯示
cout<<"a: ";
for (int i=1; i<=MAX; i++)
cout<<a.pop()<<" ";
cout<<endl;
//從鍵盤上為b棧輸入數(shù)據(jù),并顯示
for(i=1;i<=MAX;i++) {
cout<<i<<" b:";
cin>>x;
b.push(x);
}
cout<<"b: ";
for(i=1;i<=MAX;i++)
cout<<b.pop()<<" ";
cout<<endl;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -