變量的名字應當使用「名詞」或者「形容詞+名詞」

變量的名字應當使用「名詞」或者「形容詞+名詞」。 例如: float value; float oldValue; float newValue;ios

 

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 #define MAX 5
 5 //定義stack類接口
 6 using namespace std;
 7 class stack{
 8     int num[MAX];
 9     int top;
10 public:
11      stack(char *name);   //構造函數原型
12     ~stack(void);         //析構函數原型
13     void push(int n);
14     int pop(void);
15 };
16 int main(int argc, char** argv) {
17         int i,n;
18     //聲明對象
19     stack a("a"),b("b");    
20 
21     //如下利用循環和push()成員函數將2,4,6,8,10依次入a棧
22     for (i=1; i<=MAX; i++)
23         a.push(2*i);
24 
25     //如下利用循環和pop()成員函數依次彈出a棧中的數據,並顯示
26     cout<<"a: ";
27     for (i=1; i<=MAX; i++)
28        cout<<a.pop()<<"  ";
29     cout<<endl;
30 
31     //從鍵盤上爲b棧輸入數據,並顯示
32     for(i=1;i<=MAX;i++) {
33         cout<<i<<" b:";
34         cin>>n;
35         b.push(n);
36     }
37     cout<<"b: ";
38     for(i=1;i<=MAX;i++) 
39        cout<<b.pop()<<" ";
40     cout<<endl;
41 
42     return 0;
43     
44     return 0;
45 }
46 
47 //-------------------------
48 //   stack成員函數的定義
49 //-------------------------
50 //定義構造函數
51 stack::stack(char *name)
52 {
53     top=0;
54     cout << "Stack "<<name<<" initialized." << endl;
55 }
56 //定義析構函數
57 stack::~stack(void)
58 {
59     cout << "stack destroyed." << endl;  //顯示信息
60 }
61 //入棧成員函數
62 void stack::push(int n)
63 {
64     if (top==MAX){
65         cout<<"Stack is full !"<<endl;
66         return;
67     };
68     num[top]=n;
69     top++;
70 }
71 //出棧成員函數
72 int stack::pop(void)
73 {
74     top--;
75     if (top<0){
76         cout<<"Stack is underflow !"<<endl;
77         return 0;
78     };
79     return num[top];
80 }
相關文章
相關標籤/搜索