//杨辉三角
#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
c++中 用队列写杨辉三角
答案:1 悬赏:40 手机版
解决时间 2021-01-28 12:00
- 提问者网友:爱了却不能说
- 2021-01-27 21:41
最佳答案
- 五星知识达人网友:骨子里都是戏
- 2021-01-27 22:56
//杨辉三角
#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;
}
#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;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯