#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using std::cout;
using std::cin;
using std::endl;
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define true 1
#define false 0
typedef float ElemType;
struct StackNode {
ElemType dataltem;
struct StackNode *next;
};
struct Stack {
int Size;
struct StackNode *top;
};
void InitStack( struct Stack &S);
void DestroyStack( struct Stack &S );
void Push(Stack &S, ElemType &newDataItem );
// void Pop( struct Stack &S );
ElemType Pop(struct Stack &S);
void Clear( struct Stack &S );
bool isEmpty( struct Stack &S );
bool isFull( struct Stack &S );
void showStructure( struct Stack &S );
void InitStack( struct Stack &S)
{
StackNode *p;
p=new StackNode;
p->next=NULL;
p->dataltem=0;
S.top=p;
S.Size=0;
}
void DestroyStack(Stack &S )
{
Clear(S);
}
void Push(Stack &S, ElemType newDataItem )
{
if(isFull(S)){cout <<"the stack is full"<
StackNode *p;
p=new StackNode;
p->next=S.top;
p->dataltem=newDataItem;
S.top=p;
S.Size++;
}
ElemType Pop( struct Stack &S )
{
if(isEmpty(S)){cout <<"the stack is empty"<
else
{
StackNode *p;
p=new StackNode;
p=S.top;
S.top=S.top->next;
// cout<dataltem<
return p->dataltem;
}
}
void Clear( struct Stack &S )
{
StackNode *p,*q;
p=new StackNode;
q=new StackNode;
p=S.top;
while(p->next)
{
q=p->next;
free(p);
p=q;
S.top=p;
}
cout<<"the stack has been cleared"<<endl;
}
bool isEmpty(Stack &S)
{
if(S.top->next)
{
return false;
}
else return true;
}
bool isFull( struct Stack &S )
{
if(S.Size==STACK_INIT_SIZE-1)
return true;
else return false;
}
void showStructure( struct Stack &S )
{
if(isEmpty(S)){cout<<"empty stack"<
StackNode *p;
p=new StackNode;
p=S.top;
while(p->next)
{
cout <dataltem<<"\t";
p=p->next;
}
cout <<endl;
}
/*void menu()
{
cout <<endl<<" Stack"<<endl;
cout <<"===================================="<<endl;
cout <<" +: push elem"<<endl;
cout <<" -: pop elem "<<endl;
cout <<" E: if the stack is empty"<<endl;
cout <<" F: if the stack is full"<<endl;
cout <<" O: output the stack"<<endl;
cout <<" C: clear the stack"<<endl;
cout <<" Q: leave the system"<<endl<<endl;
cout <<"--------------------------------------"<<endl;
}*/
int main()
{
Stack S;
InitStack(S);
while(1)
{
cout<<"Please input a postfix express: "<
char *s;
cin>>s;
int l=strlen(s);
for(int i=0;i<l;i++)
{
switch (*(s+i))
{
case '+':
float q1,q2;
q1=Pop(S);
q2=Pop(S);
Push(S,q2+q1);
break;
case '-':
q1=Pop(S);
q2=Pop(S);
Push(S,q2-q1);
break;
case '*':
q1=Pop(S);
q2=Pop(S);
Push(S,q2*q1);
break;
case '/':
{
q1=Pop(S);
q2=Pop(S);
Push(S,q2/q1);
break;
}
default:
Push(S,(*(s+i))-'0');
break;
}
}
showStructure(S);
}
}