#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACK_INIT_SIZE 20 //栈的容量
typedef int Status;
typedef char ElemType;
typedef struct
{
int base;
int top;
int stacksize;
char data[STACK_INIT_SIZE];
}SqStack;
//初始化
void InitStack(SqStack& S)
{
S.top = -1; //初始化栈顶
}
//创建
Status Create(SqStack& L)
{
L.base = (int)malloc(STACK_INIT_SIZE * sizeof(int));
if (!L.base)
{
printf("存储分配失败\n");
return 0;
}
L.top = L.base;
L.stacksize = STACK_INIT_SIZE;
return 1;
}
//判断栈是否为空
Status StackEmpty(SqStack& S)
{
if (S.top == -1) //栈空
{
return 1;
}
else
{
return 0;
}
}
//入栈
Status Push(SqStack& S, ElemType x)
{
if (S.top == STACK_INIT_SIZE - 1)
{
return 0;
}
S.top = S.top + 1;
S.data[S.top] = x;
return 1;
}
//出栈
Status Pop(SqStack& S, ElemType x)
{
if (S.top == -1) //栈空
{
return 0;
}
x = S.data[S.top]; //栈顶元素出栈
S.top = S.top - 1;
return 1;
}
//判断括号
Status Check(char str[], int length)
{
SqStack S;
InitStack(S); //初始化
for (int i = 0; i < length; i++)
{
if ((str[i] == '(') || (str[i] == '[')) //为左括号则入栈
{
Push(S, str[i]);
}
else
{
if (StackEmpty(S))
{
return 0;
}
char topElem = 0;
Pop(S, topElem);
//匹配出现错误的情况
if ((str[i] == ')') && (topElem != '('))
{
return 0;
}
if ((str[i] == ']') && (topElem != '['))
{
return 0;
}
}
}
return StackEmpty(S);
}
int main()
{
int i = 0;
char str[19],ch;
int len = strlen(str);
//输入算式,输入#结束
while ((ch = getchar()) != '#')
{
str[i++] = ch;
}
str[i] = '\0';
if (Check(str, len))
{
printf("ok");
}
else
{
printf("error");
}
}