Stack-hexadecimal conversion
# Include <iostream. h>
# Include <malloc. h>
# Include <stdlib. h>
Typedef Struct {
Int * Base ;
Int * Top;
Int Stacksize;
} Stack;
Int Initstack (stack & S)
{
S. Base = ( Int *) Malloc (stack_init_size * Sizeof ( Int ));
If (! S. Base ) Exit (overflow );
S. Top = S. Base ;
S. stacksize = stack_init_size;
Return 1 ;
}
Int Push (stack & S, Int E)
{
If (S. Top-s. Base > = S. stacksize)
{
S. Base = ( Int *) Realloc (S. Base , (S. stacksize + T )* Sizeof ( Int ));
If (! S. Base ) Exit (overflow );
S. Top = S. Base + S. stacksize;
S. stacksize + = T;
}
* S. Top ++ = E;
Return 1 ;
}
Int Pop (stack & S, Int & E)
{
If (S. Top = S. Base ) Return 0 ;
E = * -- S. Top;
Return 1 ;
}
Int Stackempty (stack S)
{
If (S. Top = S. Base )
Return 1 ;
Else Return 0 ;
}
Void Conversion ( Int N, Int R)
{
Stack S;
Int E;
Initstack (s );
While (N)
{
Push (S, N % R );
N = N/R;
}
}
While (! Stackempty (s )){
Pop (S, e );
Cout <E;
}
Void Main ()
{
Int N, R;
Cin> N;
Cin> r;
Cout <n < " Convert from decimal " <R < " The hexadecimal value is: " ;
Conversion (n, R );
Cout <Endl;
}
Bracket Matching Test
# Include <iostream. h>
# Include < String . H>
Typedef Struct Node {
Elemtype data;
Struct Node * next;
} Stack;
Stack * top;
Void Push (elemtype N ){
Stack * P;
P = New Stack;
P-> DATA = N;
P-> next = Top-> next;
Top-> next = P;
}
Void Pop (){
Stack * q;
Q = Top-> next;
Top-> next = Top-> next;
Delete Q;
}
Elemtype gettop (){
Return Top-> next-> data;
}
Void Judge (Char A []) {
Int I, tag = 0 , Len = strlen (); // Tag = 1
For (I = 0 ; I <Len; I ++)
If (A [I] = ' ( ' | A [I] =' [ ' | A [I] = ' { ' )
{Push (A [I]); Continue ;}
If (Top-> next = NULL)
{
Cout < " No " <Endl;
Break ;
Tag = 1 ;
}
If (A [I]! = ' ) ' & A [I]! = ' ] ' & A [I]! = ' } ' )
Continue ;
If (A [I]-gettop () = 1 | A [I]-gettop () = 2 )
Pop ();
Else
{
Cout < " No " <Endl;
Break ;
Tag = 1 ;
}
}
If (TAG =0 & I = Len)
{
If (Top-> next = NULL)
Cout < " Yes " <Endl;
Else
Cout < " No " <Endl;
}
Top-> next = NULL;
}
Int Main ()
{
Top = New Stack;
Top-> next = NULL;
Char A [ 100 ];
Int N;
Cin> N;
While (N --){
Cin>;
Judge ();
}
Return 0 ;