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