Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C+中实现int堆栈+;_C++_Oop_Stack_Operator Overloading - Fatal编程技术网

C++ 在C+中实现int堆栈+;

C++ 在C+中实现int堆栈+;,c++,oop,stack,operator-overloading,C++,Oop,Stack,Operator Overloading,我在网上看到一个练习,以下是课文: 编写一个类int_堆栈来管理一个整数堆栈。这个 整数值将存储在动态分配的数组中 本课程将提出以下成员函数: int_堆栈(int n)构造函数,它将动态分配n 整数 int_stack()构造函数分配20个整数 ~int_stack()析构函数 int empty()如果堆栈为空,则返回值为1,0 否则, int full()如果堆栈已满,则返回值为1,否则返回值为0 void操作符(int p)返回(并删除)函数顶部的值 堆栈 我已尝试实现它,但>(拉)操作

我在网上看到一个练习,以下是课文:

编写一个类int_堆栈来管理一个整数堆栈。这个 整数值将存储在动态分配的数组中

本课程将提出以下成员函数:

int_堆栈(int n)构造函数,它将动态分配n 整数

int_stack()构造函数分配20个整数

~int_stack()析构函数

int empty()如果堆栈为空,则返回值为1,0 否则,

int full()如果堆栈已满,则返回值为1,否则返回值为0

void操作符<(int p)在堆栈上推送(添加)p值

int运算符>(int p)返回(并删除)函数顶部的值 堆栈

我已尝试实现它,但>(拉)操作符不起作用

这是我的密码:

int_stack.h

class int_stack
{
private:
    int* stack;
    unsigned int n, p;
    void init(unsigned int n);

public:
    int_stack(unsigned int n);
    int_stack();
    ~int_stack();
    int empty();
    int full();
    void operator <(int i);
    int operator >(int i);
};
#include <exception>

class int_stack
{
private:
    int* stack;
    unsigned int n, p;
    void init(unsigned int n);
    void copy(int_stack& other);

public:
    int_stack(unsigned int n);
    int_stack();
    int_stack(int_stack& other);
    int_stack& operator=(int_stack& other);
    ~int_stack();
    int empty();
    int full();
    void push(int i);
    int pop();
    class OutOfBoundariesException: public std::exception {};
};
class int\u堆栈
{
私人:
int*堆栈;
无符号整数n,p;
void init(无符号整数n);
公众:
int_堆栈(无符号int n);
int_stack();
~int_stack();
int empty();
int full();
void算子(inti);
};
int_stack.cpp

#include "int_stack.h"

void int_stack::init(unsigned int n)
{
    this->stack = new int[n];
    this->p = 0;
}

int_stack::int_stack(unsigned int n)
{
    this->init(n);
}

int_stack::int_stack()
{
    this->init(20);
}

int_stack::~int_stack()
{
    delete this->stack;
}


int int_stack::empty()
{
    return (this->p == 0 ? 1 : 0);
}

int int_stack::full()
{
    return (this->p == n-1 ? 1 : 0);
}

void int_stack::operator <(int i)
{
    if (!this->full())
        this->stack[p++] = i;
}

int int_stack::operator >(int i)
{
    if(!this->empty())
        return this->stack[p--];
    return 0;
}
#include "int_stack.h"

void int_stack::init(unsigned int _n)
{
    n = _n;
    stack = new int[n];
    p = 0;
}

int_stack::int_stack(unsigned int n)
{
    init(n);
}

int_stack::int_stack()
{
    init(20);
}

int_stack::int_stack(int_stack& other)
{
    copy(other);
}

int_stack& int_stack::operator=(int_stack& other)
{
    copy(other);
    return *this;
}

void int_stack::copy(int_stack& other)
{
    n = other.n;
    p = other.p;
    stack = new int[n];
    for (unsigned int i = 0; i < n; i++)
        stack[i] = other.stack[i];
}

int_stack::~int_stack()
{
    delete[] stack;
}

int int_stack::empty()
{
    return (p == 0 ? 1 : 0);
}

int int_stack::full()
{
    return (p == n ? 1 : 0);
}

void int_stack::push(int i)
{
    if (!full())
        stack[(++p)-1] = i;
    else
        throw new OutOfBoundariesException;
}

int int_stack::pop()
{
    if (!empty())
        return stack[(p--)-1];
    else
        throw new OutOfBoundariesException;

    return 0;
}
#包括“int_stack.h”
void int_stack::init(无符号int n)
{
此->堆栈=新整数[n];
这->p=0;
}
int_堆栈::int_堆栈(无符号int n)
{
这->初始化(n);
}
int_stack::int_stack()
{
这->初始化(20);
}
int_stack::~int_stack()
{
删除此->堆栈;
}
int_堆栈::empty()
{
返回(此->p==0?1:0);
}
int_堆栈::full()
{
返回(this->p==n-1?1:0);
}
void int_stack::operator full())
此->堆栈[p++]=i;
}
int_堆栈::运算符>(int i)
{
如果(!this->empty())
返回此->堆栈[p--];
返回0;
}

<>我做错了什么?

< P>接口的选择很不好,但是忽略这个事实,考虑你的成员的意思,特别是P。索引
p
指最后添加的元素上方的位置。当您在
pop
操作中返回值时,您正在从该位置读取值,但该位置没有值:

int int_stack::operator >(int i)
{
    if(!this->empty())
        return this->stack[p--];   // <-- predecrement!
    return 0;
}

这看起来像是将
s
与5进行比较,然后将结果存储到
值中。此外,实际行为完全独立于参数5,相同的操作可以拼写为
s>-1
甚至
s>5.3

除了正确索引之外,类还需要一个复制构造函数和赋值操作符。如前所述,您将获得对同一数据块的多次删除:

int_stack s0;
int_stack s1(s0); // uh-oh

两个析构函数都将删除构造函数为
s0

分配的数组。您的代码有几个主要缺陷:

除非您想在每次推送或弹出堆栈时分别调整堆栈大小,否则您可能希望使用链表或deque样式的存储结构,而不是向量/数组样式

重载
操作符
执行相当于提取和插入的操作是一个糟糕的接口选择。我强烈反对使用操作员进行这些操作:

void int_stack::push(int i)
{
    // push an element onto the stack
}

int int_stack::pop()
{
    // pop an element off of the stack
}
因为您没有将其实现为链表或deque,所以当您转到push元素时,您可以(并且最终将)尝试在分配的内存边界之外进行写入


最后,您没有正确删除堆栈。如果使用
new[]
,还必须使用
delete[]

以下是我提出的工作实现

它实现了一个复制构造函数和赋值运算符

此外,索引工作正常,界面已从
操作符更改为两个简单的
push(int)
intpop()
函数

当您试图推/弹出边界时,它会抛出异常

int_stack.h

class int_stack
{
private:
    int* stack;
    unsigned int n, p;
    void init(unsigned int n);

public:
    int_stack(unsigned int n);
    int_stack();
    ~int_stack();
    int empty();
    int full();
    void operator <(int i);
    int operator >(int i);
};
#include <exception>

class int_stack
{
private:
    int* stack;
    unsigned int n, p;
    void init(unsigned int n);
    void copy(int_stack& other);

public:
    int_stack(unsigned int n);
    int_stack();
    int_stack(int_stack& other);
    int_stack& operator=(int_stack& other);
    ~int_stack();
    int empty();
    int full();
    void push(int i);
    int pop();
    class OutOfBoundariesException: public std::exception {};
};
#包括
类int_堆栈
{
私人:
int*堆栈;
无符号整数n,p;
void init(无符号整数n);
无效副本(内部堆栈和其他);
公众:
int_堆栈(无符号int n);
int_stack();
int_堆栈(int_堆栈和其他);
int_堆栈和运算符=(int_堆栈和其他);
~int_stack();
int empty();
int full();
无效推力(int i);
int-pop();
类OutOfBoundariesException:public std::exception{};
};
int_stack.cpp

#include "int_stack.h"

void int_stack::init(unsigned int n)
{
    this->stack = new int[n];
    this->p = 0;
}

int_stack::int_stack(unsigned int n)
{
    this->init(n);
}

int_stack::int_stack()
{
    this->init(20);
}

int_stack::~int_stack()
{
    delete this->stack;
}


int int_stack::empty()
{
    return (this->p == 0 ? 1 : 0);
}

int int_stack::full()
{
    return (this->p == n-1 ? 1 : 0);
}

void int_stack::operator <(int i)
{
    if (!this->full())
        this->stack[p++] = i;
}

int int_stack::operator >(int i)
{
    if(!this->empty())
        return this->stack[p--];
    return 0;
}
#include "int_stack.h"

void int_stack::init(unsigned int _n)
{
    n = _n;
    stack = new int[n];
    p = 0;
}

int_stack::int_stack(unsigned int n)
{
    init(n);
}

int_stack::int_stack()
{
    init(20);
}

int_stack::int_stack(int_stack& other)
{
    copy(other);
}

int_stack& int_stack::operator=(int_stack& other)
{
    copy(other);
    return *this;
}

void int_stack::copy(int_stack& other)
{
    n = other.n;
    p = other.p;
    stack = new int[n];
    for (unsigned int i = 0; i < n; i++)
        stack[i] = other.stack[i];
}

int_stack::~int_stack()
{
    delete[] stack;
}

int int_stack::empty()
{
    return (p == 0 ? 1 : 0);
}

int int_stack::full()
{
    return (p == n ? 1 : 0);
}

void int_stack::push(int i)
{
    if (!full())
        stack[(++p)-1] = i;
    else
        throw new OutOfBoundariesException;
}

int int_stack::pop()
{
    if (!empty())
        return stack[(p--)-1];
    else
        throw new OutOfBoundariesException;

    return 0;
}
#包括“int_stack.h”
void int_stack::init(无符号int_n)
{
n=\u n;
堆栈=新整数[n];
p=0;
}
int_堆栈::int_堆栈(无符号int n)
{
init(n);
}
int_stack::int_stack()
{
init(20);
}
int_堆栈::int_堆栈(int_堆栈和其他)
{
副本(其他);
}
int_stack&int_stack::operator=(int_stack&other)
{
副本(其他);
归还*这个;
}
void int_stack::copy(int_stack和其他)
{
n=其他。n;
p=其他。p;
堆栈=新整数[n];
for(无符号整数i=0;i
p
是项数,但数组索引是
0-(p-1)
。这是一个相当糟糕的设计,所以不要在实现它上花费太多精力。使用
推送操作和
弹出操作使用操作符是不自然的,更重要的是
int操作符>(int)
…关于样式的建议:不需要使用
这个->
中的任何一个。如果您要检查边界(如果您的设计是固定长度的话,您应该这样做),在异常情况下抛出异常(例如超出限制的op);不要什么都不做。我怎样才能实现