yiteeee 2017-04-19 12:14 采纳率: 33.3%
浏览 802
已结题

C++代码练习自写栈类,是我自己写的,出问题解决不了了

#include

#include
using namespace std;
const int Max = 20;
template
class Stack {

public:
T s[Max];
int top;
Stack();
Stack(int)
{

cout << "Constructing" << endl;
top = -1;
}
~Stack()
{
cout << "Destructing" << endl;
delete[]s;
}
void Push(T x);
void Pop(T& x);
bool stackempty();
};

template
bool Stack::stackempty()
{
return top == -1;
}

template
void Stack::Pop(T& x)
{
if (stackempty())
{
cout << "错误!栈空" << endl;
}
else
{
x = s[top];
top--;
}
}

template
void Stack::Push(T x)
{
if (top < Max)
{
top++;
s[top] = x;
}
else
{
cout << "满栈!" << endl;
}
}

int main()
{
Stack s1(4);
memset(s1.s, 0, sizeof(s1.s));
int a, b, c, d;
for(int i=0;i cin >> a >> b >> c >> d;
s1.Push(a); s1.Push(b);
s1.Push(c); s1.Push(d);
int x = 0;
s1.Pop(x);
cout << x << " " << endl;

Stack<char> s2(4);
memset(s2.s, 'a', sizeof(s2.s));
char e, f, g, h;
cin >> e >> f >> g >> h;
s2.Push(e); 
s2.Push(f);
s2.Push(g);
s2.Push(h);
char y = 'a';
s2.Pop(y);
cout << y << " " << endl;
return 0;

}
哪里有问题啊!怎么解决问题啊?

  • 写回答

2条回答 默认 最新

  • Q_C 2017-04-19 12:46
    关注

    #include

    using namespace std;
    const int Max = 20;
    template
    class Stack {

    public:
    T s[Max];
    int top;
    Stack();
    Stack(int)
    {

        cout << "Constructing" << endl;
        top = -1;
    }
    ~Stack()
    {
        cout << "Destructing" << endl;
    }
    void Push(T x);
    void Pop(T& x);
    bool stackempty();
    

    };
    template
    bool Stack::stackempty()
    {
    return top == -1;
    }
    template
    void Stack::Pop(T& x)
    {
    if (stackempty())
    {
    cout << "错误!栈空" << endl;
    }
    else
    {
    x = s[top];
    top--;
    }
    }
    template
    void Stack::Push(T x)
    {
    if (top < Max)
    {
    top++;
    s[top] = x;
    }
    else
    {
    cout << "满栈!" << endl;
    }
    }

    int main()
    {
    Stack s1(4);
    memset(s1.s, 0, sizeof(s1.s));
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    s1.Push(a);
    s1.Push(b);
    s1.Push(c);
    s1.Push(d);
    int x = 0;
    s1.Pop(x);
    cout << x << " " << endl;
    Stack s2(4);
    memset(s2.s, 'a', sizeof(s2.s));
    char e, f, g, h;
    cin >> e >> f >> g >> h;
    s2.Push(e);
    s2.Push(f);
    s2.Push(g);
    s2.Push(h);
    char y = 'a';
    s2.Pop(y);
    cout << y << " " << endl;
    return 0;
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法