小狸猫--陈乘 的个人资料chencheng照片日志列表 工具 帮助

日志


用队列输出杨晖三角

//本程序使用C++编写//
 
 
// sequeue.h//
// sequeue.h: interface for the sequeue class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_SEQUEUE_H__29A28262_0EBE_4813_BAAF_FDA1156DFABD__INCLUDED_)
#define AFX_SEQUEUE_H__29A28262_0EBE_4813_BAAF_FDA1156DFABD__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class sequeue 
{
public:
 sequeue                         ();
 virtual ~sequeue                ();
 void SetNullSequeue             ();
 bool ChargeEmpty                ();
 bool ChargeFull                 ();
 int& GetFrontELe                ();
 int& InsertSequeue              ();
 int& InsertZero                 ();
 int& InsertZero                 (int meaningless);
 int& JumpOut                    ();
private:
 int data[10];
 int front;
 int rear;
};
#endif // !defined(AFX_SEQUEUE_H__29A28262_0EBE_4813_BAAF_FDA1156DFABD__INCLUDED_)
 
 
// sequeue.cpp// 
//////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 
// sequeue.cpp: implementation of the sequeue class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "sequeue.h"
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
 
sequeue::sequeue()                      //构造队列//
{
 front = 10 - 1;
 data[0] = 0;
 data[1] = 1;
    data[2] = 1;
 data[3] = 0;
 rear = 3;
 cout << "1" << endl;
 cout << "1" << " " << "1" << endl;
}
sequeue::~sequeue()
{
}
bool sequeue::ChargeEmpty()            //判断队列是否为空//
{
 if(rear==front)
  return true;
 else
  return false;
}
bool sequeue::ChargeFull()                 //判断队列是否为满//
{
 if((rear+1)%10 == front)
  return true;
 else
  return false;
}
int& sequeue::GetFrontELe()               //取出队列头元素//
{
 if(data[front]!=0)
  cout << data[front] << " ";
 return data[front];
}
int& sequeue::InsertSequeue()                      //插入队列//
{
 int x;
 if(ChargeFull())
 {
  cout << "数组溢出" << endl;
  exit(0);
 }
 else
 {
  x = GetFrontELe() + JumpOut();
  rear = (rear+1)%10;
  data[rear] = x;
  return data[rear];
 }
}
int& sequeue::InsertZero()          //插入队列前的零//
{
 if(ChargeFull())
 {
  cout << "数组溢出" << " ";
  exit(0);
 }
 else
 {
  JumpOut();
  rear = (rear+1)%10;
  data[rear] = 0;
  return data[rear];
 }
}
int& sequeue::InsertZero(int meaningless)                  //插入队列后的零//
{
 if(ChargeFull())
 {
  cout << "数组溢出" << " ";
  exit(0);
 }
 else
 {
  rear = (rear+1)%10;
  data[rear] = 0;
  return data[rear];
 }
}
int& sequeue::JumpOut()                     //跳出队列//
{
 int x;
 if(ChargeEmpty())
 {
  cout << "数组为空" << endl;
  exit(0);
 }
 else
 {
  x = front;
  front = (front+1)%10;
     return data[front];
 }
}
void sequeue::SetNullSequeue()                 //置空队列//
{
 rear = front;
}
 
 
//main//
/////////////////////////////////////////////////////////////////////////////////////////////////////////
 
 
// chencheng_数组队列实现杨晖三角.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "sequeue.h"
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
 sequeue Queue;
 for(int i=2; i<7; i++)
 {
  Queue.InsertZero();
  for(int j=1; j<=(i+1); j++)
   Queue.InsertSequeue();
  Queue.InsertZero(1);
  cout << endl;
 }
 return 0;
}