为什么我的程序不对(stack.cpp)
XBug 2003-09-13 09:31:52 //stack.h
// A traditional c implementation of type stack
const int max_len=1000;
enum boolean {false,true};
enum {EMPTY=-1,FULL=max_len-1};
struct stack
{
char s[max_len];
int top;
};
void reset(stack *stk)
{
stk->top=EMPTY;
}
void push(char c,stack* stk)
{
stk->top++;
stk->s[stk->top]=c;
}
char pop(stack* stk)
{
return(stk->s[stk->top--]);
}
char top(stack* stk)
{
return(stk->s[stk->top]);
}
boolean empty(const stack* stk)
{
return (boolean)(stk->top==EMPTY);
}
boolean full(const stack* stk)
{
return (boolean)(stk->top==FULL);
}
//stack.cpp
//Test of sack implementation by reversing a string
#include <iostream>
#include "stack.h" //stack implementation imported
main()
{
stack s;
char str[40]={"My name is Betty Dolsberry!"};
int i=0;
std::cout<<str<<'\n'; //print the string
reset(&s);
while(str[i]) //push onto stack
if(!full(&s))
push(str[i++],&s);
while(!empty(&s))
std::cout<<pop(&s);
std::cout<<'\n';
}