主题:求救!!!!括号匹配问题!
各位大虾求助啊!!!括号匹配问题。
#include "stdio.h"
#include "malloc.h"
typedef char ElementType;
typedef struct linknode
{ ElementType data;
struct linknode *next;
}LinkNode;
typedef LinkNode* LinkStack;
void InitStack(LinkStack *s)
{
*s = NULL;
}
int StackEmpty(LinkStack s)
{
return s == NULL;
}
void Push(LinkStack *s,ElementType x)
{
LinkNode *q;
q = (LinkNode *)malloc(sizeof(LinkNode));
q->data = x;
q->next = *s;
*s = q;
}
ElementType pop(LinkStack *s)
{
LinkNode *k;
ElementType v;
if (StackEmpty(*s))
return '\0';
v = (*s)->data;
k = *s;
*s = k->next;
free(k);
return v;
}
int match(char *str)
{
char ch;
ElementType x;
LinkStack s;
InitStack(&s);
while ((ch = *str) != '\0') {
if (ch == '(' || ch == '[' || ch == '{')
Push (&s,ch );
else if (ch == ')' || ch == ']' || ch == '}') {
x = pop(&s);
if (x == '\0') return 0;
if (x == '(' && ch != ')')
return 0;
if (x == '[' && ch != ']')
return 0;
if (x == '{' && ch != '}')
return 0;
}
str ++;
}
if (StackEmpty(s))
return 1;
return 0;
}
int main()
{
char str[100];
printf("input a expression:");
gets(str);
if (match(str))
printf( "match!\n");
else
printf( "not match!\n");
return 0;
}
运行的时候只显示printf("input a expression:");这条语句,后面结果不出现,屏幕直接跳出来,听朋友说是要用getch()。可我用了也不行啊
帮忙看看哪个地方有错误!!谢谢谢谢谢谢!!![em7]
#include "stdio.h"
#include "malloc.h"
typedef char ElementType;
typedef struct linknode
{ ElementType data;
struct linknode *next;
}LinkNode;
typedef LinkNode* LinkStack;
void InitStack(LinkStack *s)
{
*s = NULL;
}
int StackEmpty(LinkStack s)
{
return s == NULL;
}
void Push(LinkStack *s,ElementType x)
{
LinkNode *q;
q = (LinkNode *)malloc(sizeof(LinkNode));
q->data = x;
q->next = *s;
*s = q;
}
ElementType pop(LinkStack *s)
{
LinkNode *k;
ElementType v;
if (StackEmpty(*s))
return '\0';
v = (*s)->data;
k = *s;
*s = k->next;
free(k);
return v;
}
int match(char *str)
{
char ch;
ElementType x;
LinkStack s;
InitStack(&s);
while ((ch = *str) != '\0') {
if (ch == '(' || ch == '[' || ch == '{')
Push (&s,ch );
else if (ch == ')' || ch == ']' || ch == '}') {
x = pop(&s);
if (x == '\0') return 0;
if (x == '(' && ch != ')')
return 0;
if (x == '[' && ch != ']')
return 0;
if (x == '{' && ch != '}')
return 0;
}
str ++;
}
if (StackEmpty(s))
return 1;
return 0;
}
int main()
{
char str[100];
printf("input a expression:");
gets(str);
if (match(str))
printf( "match!\n");
else
printf( "not match!\n");
return 0;
}
运行的时候只显示printf("input a expression:");这条语句,后面结果不出现,屏幕直接跳出来,听朋友说是要用getch()。可我用了也不行啊
帮忙看看哪个地方有错误!!谢谢谢谢谢谢!!![em7]