Print? # Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
Typedef struct Mystack * Stack;
Struct Mystack {
Int Capacity;/* stack Capacity */
Int Top_of_stack;/* stack top/bottom mark */
Int * Array;/* Array of elements in the stack */
};
/* Stack creation */
Stack CreateStack (int Max)
{
Stack S;
S = malloc (sizeof (struct Mystack ));
If (S = NULL)
Printf ("Create stack error! \ N ");
S-> Array = malloc (sizeof (char) * Max );
If (S-> Array = NULL)
Printf ("Create stack error! \ N ");
S-> Capacity = Max;
S-> Top_of_stack = 0;
Return S;
}
/* Release stack */
Void DisposeStack (Stack S)
{
If (S! = NULL)
{
Free (S-> Array );
Free (S );
}
}
/* Determine whether a stack is empty */
Int IsEmpty (Stack S)
{
Return! S-> Top_of_stack;
}
/* Determine whether a stack is full */
Int IsFull (Stack S)
{
If (S-> Top_of_stack = S-> Capacity-1)
Return 1;
Else
Return 0;
}
/* Data stack */
Int Push (int x, Stack S)
{
If (IsFull (S ))
Printf ("The Stack is full! \ N ");
Else
S-> Array [S-> Top_of_stack ++] = x;
}
/* Data output stack */
Int Pop (Stack S)
{
If (IsEmpty (S ))
Printf ("The Stack is empty! \ N ");
Else
S-> Top_of_stack --;
}
/* Return the stack top */
Int Top (Stack S)
{
If (! IsEmpty (S ))
Return S-> Array [S-> Top_of_stack-1];
Printf ("The Stack is empty! \ N ");
Return 0;
}
Int main ()
{
Int I, len, result;
Char str [100];
Printf ("Please input the postfix that you want to compute: \ n ");
Scanf ("% s", str );
Len = strlen (str );
/* Create a stack based on the sequence length */
Struct Mystack * my_stack = CreateStack (len + 1 );
For (I = 0; I <len; I ++)
{
If (str [I]> = '0' & str [I] <= '9 ')
Push (int) str [I]-48, my_stack );
Else if (str [I] = '+ ')
{
Int x = Top (my_stack );
Pop (my_stack );
Int y = Top (my_stack );
Pop (my_stack );
Push (x + y, my_stack );
// Printf ("% d \ n", Top (my_stack ));
}
Else if (str [I] = '-')
{
Int x = Top (my_stack );
Pop (my_stack );
Int y = Top (my_stack );
Pop (my_stack );
Push (x-y, my_stack );
// Printf ("% d \ n", Top (my_stack ));
}
Else if (str [I] = '*')
{
Int x = Top (my_stack );
Pop (my_stack );
Int y = Top (my_stack );
Pop (my_stack );
Push (x * y, my_stack );
// Printf ("% d \ n", Top (my_stack ));
}
Else if (str [I] = '/')
{
Int x = Top (my_stack );
Pop (my_stack );
Int y = Top (my_stack );
Pop (my_stack );
Push (x/y, my_stack );
// Printf ("% d \ n", Top (my_stack ));
}
}
Printf ("The result is: % d \ n", Top (my_stack ));
Pop (my_stack );
/* A bug */
// DisposeStack (my_stack );
Return 0;
}
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
Typedef struct Mystack * Stack;
Struct Mystack {
Int Capacity;/* stack Capacity */
Int Top_of_stack;/* stack top/bottom mark */
Int * Array;/* Array of elements in the stack */
};
/* Stack creation */
Stack CreateStack (int Max)
{
Stack S;
S = malloc (sizeof (struct Mystack ));
If (S = NULL)
Printf ("Create stack error! \ N ");
S-> Array = malloc (sizeof (char) * Max );
If (S-> Array = NULL)
Printf ("Create stack error! \ N ");
S-> Capacity = Max;
S-> Top_of_stack = 0;
Return S;
}
/* Release stack */
Void DisposeStack (Stack S)
{
If (S! = NULL)
{
Free (S-> Array );
Free (S );
}
}
/* Determine whether a stack is empty */
Int IsEmpty (Stack S)
{
Return! S-> Top_of_stack;
}
/* Determine whether a stack is full */
Int IsFull (Stack S)
{
If (S-> Top_of_stack = S-> Capacity-1)
Return 1;
Else
Return 0;
}
/* Data stack */
Int Push (int x, Stack S)
{
If (IsFull (S ))
Printf ("The Stack is full! \ N ");
Else
S-> Array [S-> Top_of_stack ++] = x;
}
/* Data output stack */
Int Pop (Stack S)
{
If (IsEmpty (S ))
Printf ("The Stack is empty! \ N ");
Else
S-> Top_of_stack --;
}
/* Return the stack top */
Int Top (Stack S)
{
If (! IsEmpty (S ))
Return S-> Array [S-> Top_of_stack-1];
Printf ("The Stack is empty! \ N ");
Return 0;
}
Int main ()
{
Int I, len, result;
Char str [100];
Printf ("Please input the postfix that you want to compute: \ n ");
Scanf ("% s", str );
Len = strlen (str );
/* Create a stack based on the sequence length */
Struct Mystack * my_stack = CreateStack (len + 1 );
For (I = 0; I <len; I ++)
{
If (str [I]> = '0' & str [I] <= '9 ')
Push (int) str [I]-48, my_stack );
Else if (str [I] = '+ ')
{
Int x = Top (my_stack );
Pop (my_stack );
Int y = Top (my_stack );
Pop (my_stack );
Push (x + y, my_stack );
// Printf ("% d \ n", Top (my_stack ));
}
Else if (str [I] = '-')
{
Int x = Top (my_stack );
Pop (my_stack );
Int y = Top (my_stack );
Pop (my_stack );
Push (x-y, my_stack );
// Printf ("% d \ n", Top (my_stack ));
}
Else if (str [I] = '*')
{
Int x = Top (my_stack );
Pop (my_stack );
Int y = Top (my_stack );
Pop (my_stack );
Push (x * y, my_stack );
// Printf ("% d \ n", Top (my_stack ));
}
Else if (str [I] = '/')
{
Int x = Top (my_stack );
Pop (my_stack );
Int y = Top (my_stack );
Pop (my_stack );
Push (x/y, my_stack );
// Printf ("% d \ n", Top (my_stack ));
}
}
Printf ("The result is: % d \ n", Top (my_stack ));
Pop (my_stack );
/* A bug */
// DisposeStack (my_stack );
Return 0;
}