```c
# include <stdio.h>
# include <stdlib.h>
# include "stack.h"
# include "stacklist.h"
int main ( )
{
int n;
int i;
int x;
char test[ 100 ] ;
do
{
printf ( "请选择你想要实现的功能\n" ) ;
printf ( "------1.利用顺序栈将十进制转换为十六进制---------------\n" ) ;
printf ( "------2.利用顺序栈设计算法对以字符串形式读入的表达式S,判断其中的各括号是否是匹配的。---\n" ) ;
printf ( "------3.利用链栈将十进制转换为十六进制----\n" ) ;
printf ( "------4.利用链栈将设计算法对以字符串形式读入的表达式S,判断其中的各括号是否是匹配的----\n" ) ;
scanf ( "%d" , & n) ;
switch ( n)
{
case 1 :
system ( "CLS" ) ;
printf ( "请输入你想要转换为十六进制的十进制数字" ) ;
scanf ( "%d" , & x) ;
change ( x) ;
system ( "Pause" ) ;
break ;
case 2 :
system ( "CLS" ) ;
printf ( "请输入一串字符" ) ;
scanf ( "%s" , test) ;
Match_SeqStack ( test) ;
system ( "Pause" ) ;
break ;
case 3 :
system ( "CLS" ) ;
printf ( "请输入你想要转换为十六进制的十进制数字" ) ;
scanf ( "%d" , & x) ;
_Decimal_to_Hexadecimal ( x) ;
system ( "Pause" ) ;
break ;
case 4 :
system ( "CLS" ) ;
printf ( "请输入一串字符" ) ;
scanf ( "%s" , test) ;
_CheckMatch ( test) ; 在这里插入代码片
system ( "Pause" ) ;
default :
printf ( "Selection is error!\n" ) ;
system ( "Pause" ) ;
}
} while ( 1 ) ;
return 0 ;
}
```c
# include <stdio.h>
# define OK 1
# define ERROR 0
# define TRUE 1
# define FALSE 0
# define MAXSIZE 100
typedef int Status;
typedef int SElemType;
typedef struct
{
int data[ MAXSIZE] ;
int top;
} SqStack;
typedef struct
{
char data[ MAXSIZE] ;
int top;
} SqStack1;
Status visit ( SElemType c)
{
printf ( "%d " , c) ;
return OK;
}
Status InitStack ( SqStack * S)
{
S-> top= - 1 ;
return OK;
}
Status InitStack1 ( SqStack1 * S)
{
S-> top= - 1 ;
return OK;
}
Status ClearStack ( SqStack * S)
{
S-> top= - 1 ;
return OK;
}
Status StackEmpty ( SqStack * S)
{
if ( S-> top== - 1 )
return TRUE;
else
return FALSE;
}
int StackLength ( SqStack * S)
{
return S-> top+ 1 ;
}
Status GetTop ( SqStack * S)
{
int x;
if ( S-> top== - 1 )
return ERROR;
else
x= S-> data[ S-> top] ;
return x;
}
Status Push ( SqStack * S, SElemType e)
{
if ( S-> top == MAXSIZE - 1 )
{
return ERROR;
}
S-> top++ ;
S-> data[ S-> top] = e;
return OK;
}
Status Push1 ( SqStack1 * S, SElemType e)
{
if ( S-> top == MAXSIZE - 1 )
{
return ERROR;
}
S-> top++ ;
S-> data[ S-> top] = e;
return OK;
}
int pop ( SqStack * s)
{
if ( s-> top < 0 )
{
printf ( "栈为空!\n" ) ;
exit ( - 1 ) ;
}
s-> top-- ;
return s-> top;
}
int pop1 ( SqStack1 * s)
{
if ( s-> top < 0 )
{
printf ( "栈为空!\n" ) ;
exit ( - 1 ) ;
}
s-> top-- ;
return s-> top;
}
Status StackTraverse ( SqStack * S)
{
int i;
i= 0 ;
while ( i<= S-> top)
{
visit ( S-> data[ i++ ] ) ;
}
printf ( "\n" ) ;
return OK;
}
void change ( int x)
{
SqStack * s;
int mod;
int top;
InitStack ( s) ;
while ( x> 0 )
{
mod= x% 16 ;
Push ( s, mod) ;
x= x/ 16 ;
}
top= s-> top;
printf ( "转换为十六进制为" ) ;
while ( top!= - 1 )
{
if ( GetTop ( s) < 10 )
printf ( "%d" , GetTop ( s) ) ;
else
printf ( "%c" , GetTop ( s) - 10 + 'A' ) ;
top= pop ( s) ;
}
putchar ( '\n' ) ;
}
void Match_SeqStack ( char test[ ] )
{
SqStack1 * s;
InitStack1 ( s) ;
int i= 0 ;
int k= 0 , m= 0 ;
while ( test[ i] != '\0' )
{
if ( test[ i] == '(' || test[ i] == '[' || test[ i] == '{' )
{
Push1 ( s, test[ i] ) ;
i++ ;
continue ;
}
if ( test[ i] == ')' && s-> data[ s-> top] == '(' )
{
pop1 ( s) ;
i++ ;
continue ;
}
else if ( test[ i] == ']' && s-> data[ s-> top] == '[' )
{
pop1 ( s) ;
i++ ;
continue ;
}
else if ( test[ i] == '}' && s-> data[ s-> top] == '{' )
{
pop1 ( s) ;
i++ ;
continue ;
}
i++ ;
}
if ( s-> top== - 1 && test[ i] == '\0' && k== m)
printf ( "match!\n" ) ;
else
printf ( "don't match!\n" ) ;
}
```c
# include <stdio.h>
# include <stdlib.h>
# include <stdbool.h>
typedef struct lsNode
{
int data;
struct lsNode * next;
} sNode;
void InitialStack ( sNode * top)
{
top-> next = NULL ;
}
bool StackEmpty1 ( sNode * top)
{
if ( top-> next == NULL )
return true;
else
return false;
}
bool StackTop ( sNode * top, int * x)
{
if ( StackEmpty1 ( top) )
return false;
else
{
* x = top-> data;
return true;
}
}
sNode * PushStack ( sNode * top, int x)
{
sNode * s;
s = ( sNode * ) malloc ( sizeof ( sNode) ) ;
s-> data = x;
s-> next = top;
top = s;
return top;
}
sNode * PopStack ( sNode * top, int * x)
{
sNode * u;
if ( StackEmpty1 ( top) )
return top;
else
{
* x = top-> data;
u = top;
top = top-> next;
free ( u) ;
return top;
}
}
void _Decimal_to_Hexadecimal ( int n)
{
sNode * S;
int mod, x;
S = ( sNode * ) malloc ( sizeof ( sNode) ) ;
InitialStack ( S) ;
printf ( "十进制数为:%d\n" , n) ;
while ( n != 0 )
{
mod = n % 16 ;
S = PushStack ( S, mod) ;
n = n/ 16 ;
}
printf ( "十六进制数为:" ) ;
while ( ! StackEmpty1 ( S) )
{
S = PopStack ( S, & x) ;
if ( x >= 0 && x < 10 )
printf ( "%d" , x) ;
else
printf ( "%c" , x% 10 + 'a' ) ;
}
}
void _CheckMatch ( char * str)
{
sNode * S;
int i, x, iFlag;
S = ( sNode * ) malloc ( sizeof ( sNode) ) ;
InitialStack ( S) ;
i = 0 , iFlag = 1 ;
while ( str[ i] != '\0' && iFlag == 1 )
{
switch ( str[ i] )
{
case '(' :
S = PushStack ( S, 1 ) ;
break ;
case '[' :
S = PushStack ( S, 2 ) ;
break ;
case '{' :
S = PushStack ( S, 3 ) ;
break ;
case ')' :
if ( StackEmpty1 ( S) )
{
printf ( "左右括号不匹配。\n" ) ;
iFlag = 0 ;
break ;
}
else
{
S = PopStack ( S, & x) ;
if ( x != 1 )
{
printf ( "左右括号不匹配。\n" ) ;
iFlag = 0 ;
break ;
}
break ;
}
case ']' :
if ( StackEmpty1 ( S) )
{
printf ( "左右括号不匹配。\n" ) ;
iFlag = 0 ;
break ;
}
else
{
S = PopStack ( S, & x) ;
if ( x != 2 )
{
printf ( "左右括号不匹配。\n" ) ;
iFlag = 0 ;
break ;
}
break ;
}
case '}' :
if ( StackEmpty1 ( S) )
{
printf ( "左右括号不匹配。\n" ) ;
iFlag = 0 ;
break ;
}
else
{
S = PopStack ( S, & x) ;
if ( x != 3 )
{
printf ( "左右括号不匹配。\n" ) ;
iFlag = 0 ;
break ;
}
break ;
}
default :
break ;
}
i++ ;
}
if ( ! ( ! StackEmpty1 ( S) || iFlag == 0 ) )
printf ( "左右括号匹配。\n" ) ;
else
{
printf ( "左右括号不匹配\n" ) ;
}
}