22081-11-10 c++作业

1、

 

#include <iostream>
#include <string>
using namespace std;
class stu
{
private:
    string name;
    int age;
    int score;
public:
    stu(){} //无参构造
    stu(string n,int a,int s):name(n),age(a),score(s){}//有参构造
    ~stu(){}//析构函数
    void show()
    {
        cout<<name<<":"<<age<<":"<<score<<endl;
    }
};
class party
{
private:
    string activity;
    string organize;
public:
    party(){}//无参构造
    party(string a,string o):activity(a),organize(o){}//有参构造
    ~party(){}//析构函数
    void show()
    {
        cout<<activity<<":"<<organize<<endl;
    }
};
class cstu:public stu,public party
{
private:
    string ocupation;
public:
    cstu(){}//无参构造
    cstu(string n,int a,int s,string b,string o,string m):stu(n,a,s),party(b,o),ocupation(m){}//有参构造
    ~cstu(){}//析构函数
    void show()
    {
        cout<<ocupation<<endl;
    }
};

int main()
{
    cstu c1("zhangsan",22,99,"study","tuanyuan","student");
    c1.stu::show();
    c1.party::show();
    c1.show();
    return 0;
}

2、 

#include <iostream>
#define MAX 8
typedef int datatype;
using namespace std;
class queue
{
private:
    datatype data[MAX];
    int front;//队头
    int tail;//队尾
public:
    void init()
    {
        front=0;
        tail=0;
    }
    bool empty()
    {
        return front==tail?1:0;
    }
    bool full()
    {
        return front==(tail+1)%MAX?1:0;
    }
    int size()
    {
        cout<<((tail+MAX-front)%MAX)<<endl;
        return 0;
    }
    void push(datatype e)
    {
        if(full()==1)
        {
            cout<<"queue full"<<endl;
            return ;
        }
        data[tail]=e;
        tail=(tail+1)%MAX;
    }
    void pop()
    {
        if(empty()==1)
        {
            cout<<"queue empty"<<endl;
            return ;
        }
        front=(front+1)%MAX;
    }
    void show()
    {
        int i;
        for(i=front;i!=tail;i=(i+1)%MAX)
        {
            cout<<data[i]<<" ";
        }
        cout<<endl;
    }

};

int main()
{
    queue c1;
    c1.init();
    if(c1.empty()==1)
    {
        cout<<"empty"<<endl;
    }
    else
    {
        cout<<"no empty"<<endl;
    }
    if(c1.full()==1)
    {
        cout<<"full"<<endl;
    }
    else
    {
        cout<<"no full"<<endl;
    }
    c1.push(3);
    c1.push(6);
    c1.push(9);
    c1.push(8);
    c1.push(3);
    c1.push(6);
    c1.push(9);
    c1.push(8);
    c1.show();
    c1.size();
    c1.pop();
    c1.show();
    c1.size();
    cout << "Hello World!" << endl;
    return 0;
}

 

3、仿照上图循环顺序队列,写出栈

#include <iostream>
typedef int datatype;
#define MAX 20
using namespace std;
class stack
{
private:
    datatype data[MAX];
    int top;
public:
    void init()
    {
        top=-1;
    }
    bool empty()
    {
        return (top==-1)?1:0;
    }
    bool full()
    {
        return (top==MAX-1)?1:0;
    }
    void size()
    {
        int s=0;
        s=top+1;
        cout<<s<<endl;
    }
    void push(datatype e)
    {
        if(full()==1)
        {
            cout<<"stack full"<<endl;
            return ;
        }
        top++;
        data[top]=e;
    }
    void pop()
    {
        if(empty()==1)
        {
            cout<<"stack empty"<<endl;
            return;
        }
        top--;
    }
    void show()
    {
        int i;
        for(i=0;i<=top;i++)
        {
            cout<<data[i]<<" ";
        }
        cout<<endl;
    }
};

int main()
{
    stack s1;
    s1.init();
    if(s1.empty()==1)
    {
        cout<<"empty"<<endl;
    }
    else
    {
        cout<<"no empty"<<endl;
    }
    if(s1.full()==1)
    {
        cout<<"full"<<endl;
    }
    else
    {
        cout<<"no full"<<endl;
    }
    s1.empty();
    s1.full();
    s1.push(6);
    s1.push(9);
    s1.push(5);
    s1.show();
    s1.size();
    s1.pop();
    s1.show();
    s1.size();
    s1.pop();
    s1.pop();
    s1.pop();
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值