2)杨辉三角[1]队列实现
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)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 cin>>n; 60 int s1,s2; 61 for(int i=1;i<n;i++)cout<<" "; 62 cout<<1<<endl; 63 q.append(1); 64 for(int i=2;i<=n;i++){ 65 s1=0; 66 for(int k=1;k<=n-i;k++ )cout<<" "; 67 for(int j=1;j<=i-1;j++){ 68 q.get_front(s2); 69 q.serve(); 70 cout<<s1+s2<<setw(4); 71 q.append(s1+s2); 72 s1=s2; 73 } 74 cout<<1<<endl; 75 q.append(1); 76 } 77 return 0; 78 79 }