1 #include<iostream>
2 #include<iomanip>
3 using namespace std;
4
5 enum error{overflow,underflow,success};
6 const int maxlen=100;
7
8 class queue{
9 public:
10 queue();//初始化
11 bool empty()const;//判断为空
12 bool full()const;//判断为满
13 int get_front(int &x)const;//取队头元素
14 error append(const int x);//入队
15 error serve();//出队
16 private:
17 int count;//队列中元素计数
18 int rear,front;//队头,队尾
19 int data[maxlen];//存储队列中的数据
20 };
21 queue::queue(){//初始化
22 count=0;
23 rear=front=0;
24 }
25
26 bool queue::empty()const{//判断为空
27 if(count==0)return true;
28 return false;
29 }
30
31 bool queue::full()const{//判断为满
32 if(count==maxlen-1)return true;
33 return false;
34 }
35
36 int queue::get_front(int &x)const{//取队头元素
37 if(empty())return underflow;
38 x=data[(front+1)%maxlen];
39 return success;
40 }
41 error queue::append(const int x){//入队
42 if(full())return overflow;
43 rear=(rear+1)%maxlen;
44 data[rear]=x;
45 count++;
46 return success;
47 }
48
49 error queue::serve(){//出队
50 if(empty())return underflow;
51 front=(front+1)%maxlen;
52 count--;
53 return success;
54 }
55
56 int main(){
57 queue q;
58 int n;
59 cout<<"please input 杨辉三角要打印的行数:";
60 cin>>n;
61 int s1,s2;
62 for(int i=1;i<n;i++)cout<<" ";
63 cout<<1<<endl;//输出第一行上的1
64 q.append(1);//所输出1入队
65 for(int i=2;i<=n;i++){//逐行计算并输出2~N行上的数据
66 s1=0;//存放前一个入队数
67 for(int k=1;k<=n-i;k++ )cout<<" ";
68 for(int j=1;j<=i-1;j++){//先计算并输出n-1个数
69 q.get_front(s2);//取队头元素并出队
70 q.serve();
71 cout<<s1+s2<<setw(4);
72 q.append(s1+s2);//所输出的当行中的元素入队
73 s1=s2;
74 }
75 cout<<1<<endl;//输出当行中的子最后一个元素1并换行
76 q.append(1);
77 }
78 return 0;
79 }
![](https://images2015.cnblogs.com/blog/836979/201512/836979-20151205172456627-604427075.jpg)