C++day3

文章描述了如何自定义一个包含栈和循环顺序队列功能的类,包括构造函数、析构函数、拷贝构造函数等方法的实现,以及判断栈/队列状态和操作元素的基本功能。
摘要由CSDN通过智能技术生成

1> 自行封装一个栈的类,包含私有成员属性:栈的数组、记录栈顶的变量成员函数完成:构造函数、析构函数、拷贝构造函数、入栈、出栈、清空栈、判空、判满、获取栈顶元素、求栈的大小

代码:

头文件

#ifndef TEST_H
#define TEST_H
#include <iostream>
#include <cstring>
#include <cstdlib>
#define MAX 5
using namespace std;
class Stu
{
public:
    //构造函数
    Stu();
    //析构函数
    ~Stu();
    //拷贝构造函数
    Stu(const Stu &other);
    //判空
    bool empty();
    //判满
    bool full();
    //入栈
    void init();
    //出栈
    void out();
    //获取栈顶元素
    int get_h();
    //清空栈
    void dele();
    //栈的大小
    long int big();
private:
    int *score;
    int top;
};
#endif // TEST_H

源代码

#include"test.h"
//构造函数
Stu::Stu():score(new int[MAX]),top(-1){}
//析构函数
Stu::~Stu()
{
    delete []score;
}
//拷贝构造函数
Stu::Stu(const Stu &other):score(new int (*other.score)){}
//入栈
void Stu::init()
{
    char q;
    if(Stu::full())
    {
        cout << "栈已满!" << endl;
        return;
    }
    else
    {
        while(top<MAX-1)
        {
            top++;
            cout << "请输入学生成绩:";
            cin >> score[top];
            while(getchar()!=10);
            cout << "入栈成功!" << endl;
            cout << "是否退出(q):" << endl;
            cin >> q;
            while(getchar()!=10);
            if(q == 'q')
            {
                break;
            }
        }
    }
}
//出栈
void Stu::out()
{
    if(Stu::empty())
    {
        cout << "栈为空!" << endl;
        return;
    }
    else
    {
        cout << "当前栈顶元素:" << score[top] << endl;
        cout << "出栈成功!" << endl;
        top--;
    }
}
//获取栈顶元素
int Stu::get_h()
{
    return score[top];
}
//判空
bool Stu::empty()
{
    if(top == -1)
    {
        return true;
    }
    else
    {
        return false;
    }
}
//判满
bool Stu::full()
{
    if(top == MAX-1)
    {
        return true;
    }
    else
    {
        return false;
    }
}
//清空栈
void Stu::dele()
{
    if(Stu::empty())
    {
        cout << "栈为空!" << endl;
        return;
    }
    else
    {
        memset(score,0,sizeof(score[0])*MAX);
        top = -1;
        cout << "栈清空成功!" << endl;
    }
}
//栈的大小
long int Stu::big()
{
    if(Stu::empty())
    {
        cout << "栈为空!" << endl;
        return 0;
    }
    else
    {
        return sizeof(score[0])*MAX;
    }
}

测试函数

#include"test.h"
int main()
{
    Stu s0,s1;
    //调用入栈函数
    s0.init();
    //调用拷贝构造
    s1 = s0;
    //调用出栈函数
    s1.out();
    //获取栈顶元素
    cout << "s0当前栈顶元素:" << s0.get_h() << endl;
    cout << "s1当前栈顶元素:" << s1.get_h() << endl;
    //栈的大小
    cout << "s0当前栈的大小:" << s0.big() << endl;
    cout << "s1当前栈的大小:" << s1.big() << endl;
    //清空栈
    s0.dele();
    s1.dele();
    return 0;
}

运行结果:

 

2>自行封装一 个循环顺序队列的类,包含私有成员属性:存放队列的数组、队头位置、队尾位置成员函数完成:构造函数、析构函数、拷贝构造函数、入队、出队、清空队列、判空、判满、求队列大小

代码:

头文件

#ifndef TEST_H
#define TEST_H
#include <iostream>
#include <cstring>
#define MAX 5
using namespace std;
class Stu
{
public:
    //构造函数
    Stu();
    //析构函数
    ~Stu();
    //拷贝构造函数
    Stu(const Stu &other);
    //判空
    bool empty();
    //判满
    bool full();
    //获取队头元素
    int get_head();
    //入队函数
    void init();
    //出队函数
    void out();
    //清空队列
    void dele();
    //求队列大小
    void get_h();
private:
    int *data;
    int head;
    int tall;
};

#endif // TEST_H

源代码

#include "test.h"
//构造函数
Stu::Stu():data(new int[MAX]),head(0),tall(0){}
//析构函数
Stu::~Stu()
{
    delete []data;
}
//拷贝构造函数
Stu::Stu(const Stu &other):data(new int(*other.data)){}
//判空
bool Stu::empty()
{
    if(head%MAX == tall%MAX)
    {
        return true;
    }
    else
    {
        return false;
    }
}
//判满
bool Stu::full()
{
    if(head%MAX == (tall+1)%MAX)
    {
        return true;
    }
    else
    {
        return false;
    }
}
//入队函数
void Stu::init()
{
    char q;
    if(Stu::full())
    {
        cout << "队列已满!" << endl;
        return;
    }
    else
    {
        while((tall+1)%MAX < MAX-1)
        {
            cout << "请输入学生成绩:";
            cin >> data[tall];
            while(getchar()!=10);
            cout << "入队成功!" << endl;
            tall = (tall+1)%MAX;
            cout << "是否退出(q):" << endl;
            cin >> q;
            while(getchar()!=10);
            if(q == 'q')
            {
                break;
            }
        }
    }
}
//出队函数
void Stu::out()
{
    if(Stu::empty())
    {
        cout << "队列为空!" << endl;
        return;
    }
    else
    {
        cout << "当前队头元素:" << get_head() << endl;
        head = (head+1)%MAX;
        cout << "出队成功" << endl;
    }
}
//清空队列
void Stu::dele()
{
    if(Stu::empty())
    {
        cout << "队列为空!" << endl;
        return;
    }
    else
    {
        memset(data,0,sizeof (data[0])*MAX);
        head = tall = 0;
    }
}
//求队列大小
void Stu::get_h()
{
    cout << "当前队列大小:" << sizeof (data[0])*MAX << endl;
}
//获取队头元素
int Stu::get_head()
{
    if(Stu::empty())
    {
        cout << "队列为空!" << endl;
        return 0;
    }
    else
    {
        return data[head];
    }
}

测试函数

#include "test.h"
int main()
{
    Stu s0,s1;
    //入队函数
    s0.init();
    //拷贝构造
    s1 = s0;
    //出队函数
    s1.out();
    //获取队头元素
    cout << "当前对头元素:" <<s1.get_head() << endl;
    //清空队列
    s1.dele();
    //求队列大小
    s1.get_h();
    return 0;
}

 运行结果:

思维导图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值