比如【】正确,【{】}错误
每次无论输入啥都是输出“bye”是为啥。
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Node;
typedef struct Node *Stack;
typedef struct Node *PtrToNode;
bool IsEmpty(Stack);
void Push(Stack S,char value);
void Pop(Stack S);
int Top(Stack S);
Stack CreateStack(); //“格式化”一个栈
void MakeEmpty(Stack S);
struct Node
{
char ch;
PtrToNode next;
};
#include "Stack-link.h"
bool IsEmpty(Stack S){
return S->next == NULL;
}
void Push(Stack S,char value){
PtrToNode new_cell;
new_cell = malloc(sizeof(struct Node));
if (IsEmpty(S))
{
S->next = new_cell;
new_cell->next = NULL;
new_cell->ch = value;
}
else
{
new_cell->next = S->next;
S->next = new_cell;
new_cell->ch = value;
}
}
void Pop(Stack S){
PtrToNode temp;
if (IsEmpty(S))
printf("Empty stack!!!");
else
{
temp = S->next;
S->next = temp->next;
free(temp);
}
}
int Top(Stack S){
if (!IsEmpty(S))
return S->next->ch;
printf("Empty stack!!!");
return 0;
}
Stack CreateStack(){
Stack S;
S=malloc(sizeof(struct Node));
S->ch = 'h';
if (S == NULL)
printf("Out of space!!!");
else
S->next = NULL;
MakeEmpty(S);
return S;
} //初始化一个栈
void MakeEmpty(Stack S){
if (S == NULL)
printf("You must creat a stack first!");
else
{
while (!IsEmpty(S))
{
Pop(S);
}
}
}
int main()
{
char ch;
bool judge = true;
Stack s = CreateStack();
fputs("Please enter the test line:",stdout);
while ((ch = getchar()) != EOF)
{
if (ch == '{' || ch == '[' || ch == '(')
Push(s,ch);
else
{
switch (ch)
{
case '}':
if (Top(s) != '{') judge = false;
break;
case ']':
if (Top(s) != '(') judge = false;
break;
case ')':
if (Top(s) != '(') judge = false;
break;
default:
break;
}
if (judge != false) Pop(s);
}
if (judge == false) fputs("false!",stdout); break;
}
fputs("bye!",stdout);
return 0;
}