谢谢啦,俺以后会努力的!!
给个栈的程序参考,楼主自己看看.
#include<stdio.h>
#define MAXSIZE 100
typedef int datatype ;
typedef struct
{
datatype a[MAXSIZE];
int top ;
}
sequence_stack ;
void init_seqstack(sequence_stack*st)
{
st->top=0 ;
}
void printseqstack(sequence_stack*st,int m)
{
int i ;
i=st->top-1 ;
while(i>=0)
{
/*十进制转换为十六进制*/
if(m==16)
{
switch(st->a[i])
{
case 10 :
printf("A");
break ;
case 11 :
printf("B");
break ;
case 12 :
printf("C");
break ;
case 13 :
printf("D");
break ;
case 14 :
printf("E");
break ;
case 15 :
printf("F");
break ;
case 9 :
case 8 :
case 7 :
case 6 :
case 5 :
case 4 :
case 3 :
case 2 :
case 1 :
case 0 :
printf("%d",st->a[i]);//出栈
break ;
}
}
else printf("%d",st->a[i]);
i--;
}
}
/*堆栈存放各个数*/
void jzhzhh(sequence_stack*st,int n,int m)
{
int i=0 ;
while(n!=0)
{
st->a[i]=n%m ;//进栈
n=n/m ;
st->top++;
i++;
}
}
int main()
{
int n,m ;
sequence_stack p ;
printf("十进制数n=");
scanf("%d",&n);
printf("进制m=");
scanf("%d",&m);
init_seqstack(&p);
jzhzhh(&p,n,m);
printseqstack(&p,m);
printf("\n");
return (0);
}
#include<stdio.h>
#define MAXSIZE 100
typedef int datatype ;
typedef struct
{
datatype a[MAXSIZE];
int top ;
}
sequence_stack ;
init_sequence_stack(sequence_stack*st)
{
st->top=0 ;
}
int is_empty_stack(sequence_stack st)
{
return(st.top==0?0:1);
/*为0时堆栈为空*/
}
void pop(sequence_stack*st)
{
if(st->top==0)
{
printf("The list is empty!");
exit(1);
}
st->top--;
}
/*左括号进栈*/
void push(sequence_stack*st,datatype x)
{
if(st->top==MAXSIZE)
{
printf("\nThe sequence stack is full !");
exit(1);
}
st->a[st->top]=x ;
st->top++;
}
/*得到头节点*/
int get_top(sequence_stack st)
{
if(!is_empty_stack(st))
{
printf("The stack is empty!");
exit(1);
}
else
return st.a[st.top-1];
}
int match_kuohao(char c[])
{
int i ;
sequence_stack s ;
i=0 ;
init_sequence_stack(&s);
while(c[i]!='#')
{
switch(c[i])
{
case '{' :
case '[' :
case '(' :
push(&s,c[i]);
break ;
case '}' :
if(is_empty_stack(s)&&get_top(s)=='{')
{
pop(&s);
break ;
}
else return 1 ;
/*还回1则栈不为空,即括号不匹配*/
case ')' :
if(is_empty_stack(s)&&get_top(s)=='(')
{
pop(&s);
break ;
}
else return 1 ;
case ']' :
if(is_empty_stack(s)&&get_top(s)=='[')
{
pop(&s);
break ;
}
else return 1 ;
}
i++;
}
return(is_empty_stack(s));
}
int main()
{
int leap ;
char str[MAXSIZE];
scanf("%s",str);
leap=match_kuohao(str);
if(leap==0)
printf("yes! \n");
else
printf("no! \n");
return (0);
}