永发信息网

c++中 用队列写杨辉三角

答案:1  悬赏:40  手机版
解决时间 2021-01-28 12:00
//杨辉三角
#include<iostream>
#include <iomanip>
using namespace std;
enum error_code {success,overflow,underflow};
class queue
{
public:
queue();
bool empty()const;
bool full()const;
error_code get_front(int & x)const;
error_code append(const int x);
error_code serve();
private:
int count;
int front,rear;
int data[50];
};
queue::queue()
{
count=0;
front=0;
rear=0;
}
bool queue::empty()const
{
if(count==0) return true;
else return false;
}
bool queue::full()const
{
if(count==49) return true;
else return false;
}
error_code queue::get_front(int & x)const{
if(empty()) return underflow;
else{
x=data[(front+1)%50];
return success;
}
}
error_code queue::append(const x){
if(full()) return overflow;
else{
rear=(rear+1)%50;
data[rear]=x;
count++;
return success;
}
}
error_code queue::serve(){
if(empty()) return underflow;
else{
front=(front+1)%50;
count--;
return success;
}
}
void out_number(int n)
{
int s1,s2,i,j,x;
queue q;
cout<<setw(5)<<1<<endl;
q.append(1);
for(i=2;i<=n;i++){
cout<<setw(n-i)<<" ";
for(i=2;i<=n;i++)
{
s1=0;
for(j=1;j<=i-1;j++)
{
q.get_front(s2);
q.serve();
cout<<setw(5)<<s1+s2;
q.append(s1+s2);
s1=s2;
}
cout<<setw(5)<<1<<endl;
q.append(1);
}
}
int main()
{
int m;
cout<<"请输入要打印的行数:";
cin>>m;
out_number(m);
cout<<endl;
return 0;
}
E:\y668787\e.cpp(84) : error C2601: 'main' : local function definitions are illegal
E:\y668787\e.cpp(92) : fatal error C1004: unexpected end of file found
最佳答案
//杨辉三角
#include<iostream>
#include <iomanip>
using namespace std;
enum error_code {success,overflow,underflow};
class queue
{
public:
    queue();
    bool empty()const;
    bool full()const;
    error_code get_front(int & x)const;
    error_code append(const int x);
    error_code serve();
private:
    int count;
    int front,rear;
    int data[50];
};
queue::queue()
{
    count=0;
    front=0;
    rear=0;
}
bool queue::empty()const
{
    if(count==0) return true;
    else return false;
}
bool queue::full()const
{
    if(count==49) return true;
    else return false;
}
error_code queue::get_front(int & x)const{
    if(empty()) return underflow;
    else{
        x=data[(front+1)%50];
        return success;
    }
}
error_code queue::append(const int  x){
    if(full()) return overflow;
    else{
        rear=(rear+1)%50;
        data[rear]=x;
        count++;
        return success;
    }
}
error_code queue::serve(){
    if(empty()) return underflow;
    else{
        front=(front+1)%50;
        count--;
        return success;
    }
}
void out_number(int n)
{
    int s1,s2,i,j,x;
    queue q;
    cout<<setw(5)<<1<<endl;
    q.append(1);
    for(i=2;i<=n;i++){
        cout<<setw(n-i)<<" ";
        for(i=2;i<=n;i++)
        {
            s1=0;
            for(j=1;j<=i-1;j++)
            {
                q.get_front(s2);
                q.serve(); 
                cout<<setw(5)<<s1+s2;
                q.append(s1+s2);
                s1=s2;
            }
            cout<<setw(5)<<1<<endl;
            q.append(1);
        }
    }
}
int main()
{
    int m;
    cout<<"请输入要打印的行数:";
    cin>>m;
    out_number(m);
    cout<<endl;
    return 0;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
四个表现美好品质的成语
赵后的意思是什么啊?知道的请说下!
固体氢氧化钠和氧化钙的混合物叫做碱石灰,常
汽车火花塞陶瓷部分为什么不能摸
德国水动力吸脂有几代
奔驰13年e260油泵保险丝在哪?
吉利自由舰满满的动力不足咋回事呢?
2011年君越加速不给力
谁用过唯米手机,怎么样,为什么我的手机不能
为什么太阳系现在只有八大行星了?
翻译expansion of
动车票定了无票,等坐车的时候会有位置坐吗
壮厉的意思是什么啊?知道的请说下!
基业箱和户外不锈钢配电箱优什么区别
骐騄的意思是什么啊?知道的请说下!
推荐资讯
侵害的意思是什么啊?知道的请说下!
杀毒软件排名都有什么作用那个比较好?
魅族mx3充不上电,要怎样维修
阃命的意思是什么啊?知道的请说下!
生物,火山爆发后所有物种灭绝,经过群落演替
中道造型(武商黄石购物中心店)地址有知道的么
领取的意思是什么啊?知道的请说下!
广驰汽贸有限公司地址在什么地方,想过去办事
我想开店,但不知道开什么电最正挣钱?
Aufgang是什么意思 《德语助手》德汉
施华洛国际婚纱影楼地址在什么地方,想过去办
香招火锅城地址在什么地方,想过去办事
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?