#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct stack
{
char data;
struct stack *next;
}ListStack;
ListStack *Init_Stack()
{
ListStack *top;
top=(ListStack *)malloc(sizeof(ListStack));
top->data='#';
top->next=NULL;
return top;
}/*Init_Stack*/
ListStack * Push_Stack(ListStack *top,char c)
{
ListStack *p;
p=(ListStack *)malloc(sizeof(ListStack));
p->data=c;
p->next=top->next;
top->next=p;
top=top->next;
return top;
}/*Push_Stack*/
ListStack * Pop_Stack(ListStack *top)
{
char c;
ListStack *p;
if(top->next==NULL)
return 0;
c=p->data;
top->next=p->next;
free(p);
return top;
}/*Pop_Stack*/
void main()
{
char a[50];
char c;
int i;
printf("请输入你的表达式: ");
gets(a);
ListStack *stack;
stack=Init_Stack();
for(i=0;(c=a[i])!='\0';i++)
{
if(c=='(')
{
Push_Stack(stack,c);
continue;
}
if(c==')')
{
if(stack->next==NULL)
printf("缺少左括号\n");
else
Pop_Stack(stack);
}
}
if(stack->next!=NULL)
printf("缺少右号\n");
else
printf("正确\n");
}
[此贴子已经被作者于2007-10-13 18:36:41编辑过]