关于栈的简单程序,初学栈,觉得函数都没错,题目意思也表达清楚了,为什么运行结果不对呢?求解!!!
//程序中可以使用一个栈来逐行处理从终端输入的字符。
//每次从终端接收一个字符后先作如下判断,如果它既不是"#"也不是"@",则将该字符压入栈顶;
//如果是"#"则从栈顶删去一个字符;如果是"@",则把字符栈清空。
#include <iostream>
#define MaxSize 50
using namespace std;
typedef struct sqstack{
char data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack *&s)//初始化栈
{
s = new SqStack;
s->top = -1;
}
int EmptyStack(SqStack *s)//判空
{
return(s->top == -1);
}
int FullStack(SqStack *s)//判满
{
return(s->top == MaxSize - 1);
}
void PopStack(SqStack *s)//删除栈顶元素
{
s->top --;
}
int Push(SqStack *s, char e)//将元素压入栈中
{
if(FullStack(s))
{
return 0;
}
s->top ++;
s->data[s->top] = e;
return 1;
}
void ClearStack(SqStack *s)//将栈清空
{
s->top = -1;
}
void DispStack(SqStack *s)//输出栈
{
int i;
if(EmptyStack(s))
cout<<"Stack is empty!"<<endl;
else
{
cout<<"Stack is:"<<endl;
for(i=s->top;i>=0;i--)
cout<<s->data[i]<<' ';
cout<<endl;
}
}
void main()
{
SqStack *s = NULL;
InitStack(s);
char a;
while(getchar()!='\n')
{
cin>>a;
if(a == '#')
{
PopStack(s);
}
else if(a == '@')
{
ClearStack(s);
}
else
{
Push(s, a);
}
}
DispStack(s);
}
//...
while ((a = getchar()) != '\n')
{
//cin >> a;
if (a == '#')
{
PopStack(s);
}
//...