岳超华 2018-12-16 13:58 采纳率: 100%
浏览 844

请教大神,c++输入字符串反序输出,为什么我的程序总是显示为空呢。

请教大神,c++输入字符串反序输出,为什么我的程序总是显示为空呢(stark is empty)。
感觉是Push和Pop的问题。请指导。

#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;

typedef int ElemType;
struct LSNodeType
{
    ElemType data;
    LSNodeType *next;
};
const int MAXSIZE=100;

class SqStack
{
private :
    ElemType elem[MAXSIZE];
    int top;
public:
    SqStack(){top=-1;}
    ~SqStack(){}
    void Push (ElemType e );
    ElemType Pop();
    int IsEmpty();
    ElemType GetPop() {return elem[top];}
    void Display();
    void SetEmpty() {top=-1;}   
};

void SqStack::Push(ElemType e)
{
    if(top==MAXSIZE-1)
        cout <<"stack overflow,cannot push"<<endl;
    else
    {
        top++;
        elem[top]=e;
    }
}

ElemType SqStack::Pop()
{
    top--;
    return elem[top+1];
}

int SqStack::IsEmpty()
{
    if(top==-1)
        return 1;
    else 
        return 0;
}

void SqStack::Display ()
{
    int k;
    if (top==-1)
        cout<<"stack is empty"<<endl;
    else
    {
        cout<<"data elem:";
        for(k=top;k>=0;k--)
            cout<<setw(20)<<elem[k];
        cout<<endl;
    }
}


void main()
{
    SqStack sq;
    char e;
    cout<<"put in 字符串e,反向输出"<<endl;
    cin>>e;
    sq.Push (e);
    sq.Pop();
    sq.IsEmpty();
    sq.GetPop() ;
    sq.Display();
    sq.SetEmpty() ;

    system("pause");
}
  • 写回答

1条回答 默认 最新

  • threenewbee 2018-12-16 14:01
    关注

    之前的问题采纳了,再回答你新的问题,谢谢

    评论

报告相同问题?

悬赏问题

  • ¥30 win c++ socket
  • ¥15 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的