标识符应当直观且可以拼读,可望文知意,不必进行“解码”

标识符应当直观且可以拼读,可望文知意,不必进行“解码”。 标识符最好采用英文单词或其组合,便于记忆和阅读。切忌使用汉语拼音来命名。 程序中的英文单词一般不会太复杂,用词应当准确。例如不要把 CurrentValue 写成 NowValue。

 1 #include <iostream>
 2 
 3 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 4 const int MAX=5;    //假定栈中最多保存5个数据
 5 using namespace std;
 6 //定义名为stack的具有栈功能的类
 7 class  stack {
 8     //数据成员
 9     float  num[MAX];   //存放栈数据的数组
10     int  top;           //指示栈顶位置的变量
11 public:
12     //成员函数
13     stack(void)  //初始化函数
14     {
15         top=0; 
16         cout<<"Stack initialized."<<endl;
17     }     
18     void push(float x)     //入栈函数
19     {
20         if (top==MAX){
21             cout<<"Stack is full !"<<endl;
22             return;
23         };
24         num[top]=x;
25         top++;
26     }
27     float pop(void)      //出栈函数
28     {
29         top--;
30         if (top<0){
31         cout<<"Stack is underflow !"<<endl;
32         return 0;
33         };
34         return num[top];
35     }
36 };
37 
38 int main(int argc, char** argv) {
39         //声明变量和对象
40     int i;
41     float x;
42     stack a,b;    //声明(创建)栈对象并初始化
43 
44     //以下利用循环和push()成员函数将2,4,6,8,10依次入a栈
45     for (i=1; i<=MAX; i++)
46         a.push(2.0*i);
47 
48     //以下利用循环和pop()成员函数依次弹出a栈中的数据并显示
49     for (i=1; i<=MAX; i++)
50        cout<<a.pop()<<"  ";
51     cout<<endl;
52 
53     //以下利用循环和push()成员函数将键盘输入的数据依次入b栈
54     cout<<"Please input five numbers."<<endl;
55     for (i=1; i<=MAX; i++) {
56          cin>>x;
57          b.push(x);
58     }
59  
60     //以下利用循环和pop()成员函数依次弹出b栈中的数据并显示
61     for (i=1; i<=MAX; i++)
62        cout<<b.pop()<<"  ";
63     cout<<endl;
64     return 0;
65 }
原文地址:https://www.cnblogs.com/borter/p/9413386.html