该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
#pragma warning (disable: 4996)
typedef struct St
{
char data;
struct St *pNext;
}NODE, *PNODE;
typedef struct stack
{
PNODE pTop;
PNODE pBottom;
}Stack;
void creat_Stack(Stack* s);
void push_Stack(char ch, Stack* s);
void push_Pop(Stack* s);
void creat_Stack(Stack* s)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
s->pBottom = s->pTop = pNew;
pNew->pNext = NULL;
}
void push_Stack(char ch, Stack* s)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
pNew->pNext = s->pTop;
pNew->data = ch;
s->pTop = pNew;
}
void push_Pop(Stack* s)
{
PNODE p = s->pTop;
s->pTop = p->pNext;
free(p);
}
int main(void)
{
for (;;)
{
Stack s;
char c[100];
int i = 0;
int k = 0;
int b = 0;
int max = 0;
int left = 0;
int right = 0;
int flag = 0;
printf("请输入括号深度(ps:如要退出程序请输入“0”) :");
scanf("%d", &k);
if (k != 0)
{
creat_Stack(&s);
printf("请输入括号 : ");
scanf("%s", &c);
for (i = 0; c[i] != '\0'; i++)
{
switch (c[i])
{
case'(':
{
push_Stack(c[i], &s);
left = left + 1;
b = b + 1;
flag++;
break;
}
case')':
{
push_Pop(&s);
right = right + 1;
b = b - 1;
flag--;
if (flag < 0)
{
printf("xxxxxxxxx该括号序列不合法xxxxxxxxx\n");
goto NEXT;
}
break;
}
default:break;
}
if (max < b)
{
max = b;
}
}
if (left == right)
{
printf("该括号深度为 : %d\n", max);
printf("该括号对数为:%d\n", right);
if (k == max)
{
printf("#####################################\n");
printf("########### ###########\n");
printf("######## 该括号嵌套深度匹配 ########\n");
printf("########### ############\n");
printf("#####################################\n\n\n");
}
else
{
printf("#####################################\n");
printf("########### ############\n");
printf("####### 该括号嵌套深度BU匹配 ########\n");
printf("########### ############\n");
printf("#####################################\n\n\n");
}
}
else
{
printf("xxxxxxxxx该括号序列不合法xxxxxxxxx\n");
left - right > 0 ? printf("xxxxxxxxxxx缺少%d个‘)’xxxxxxxxxxx\n", left - right) : printf("xxxxxxxxxxx缺少%d个‘(’\nxxxxxxxxxxxx", (left - right)*(-1));
NEXT:printf("xxxxxxxxxxxx请细心输入xxxxxxxxxxxx\n\n\n");
}
}
else
{
printf("感谢使用!\n\n\n\n");
}
}
}