#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;
#define FALSE 1
typedef struct StackNode{
char op;
StackNode * next;
}StackNode,*StackTop;
char operation[50];int length;
//鍊式棧棧頂
StackTop top=(StackNode *)malloc(sizeof(StackNode));
//初始化鍊式棧
void InitialStack()
{
top->next=NULL;
}
//判斷棧是否為空
bool IsEmpty()
{
bool flag_bool;
top->next==NULL?flag_bool=true:flag_bool=false;
return flag_bool;
}
//将棧頂元素Pop,并傳回棧頂元素值
char Pop()
{
StackNode * temp=(StackNode *)malloc(sizeof(StackNode));
temp=top->next;
if(temp==NULL)return FALSE;
char op=temp->op;
top->next=temp->next;
free(temp);
return op;
}
//取棧頂元素值
char GetTop()
{
return top->next==NULL?FALSE:top->next->op;
}
//将新元素壓入棧
void Push(char char_op)
{
StackNode * temp=(StackNode *)malloc(sizeof(StackNode));
temp->op=char_op;
temp->next=top->next;
top->next=temp;
}
//判斷括号與棧頂的左括号是否比對
bool Match(char op1,char op2)
{
if((op1=='('&&op2==')')||(op1=='{'&&op2=='}')||(op1=='['&&op2==']'))
return true;
else
return false;
}
void procExecute()
{
length=strlen(operation);
for(int i=0;i<length;i++)
{
switch(operation[i]){
case '(':
case '{':
case '[':
Push(operation[i]);
break;
case ')':
case '}':
case ']':
if(IsEmpty()){
cout<<"右括号多餘\n";
return;
}else{
if(Match(GetTop(),operation[i])){
Pop();break;
}
else{
cout<<"左右括号不比對\n";
return;
}
}
}
}
if(IsEmpty())
cout<<"括号比對成功!\n";
else
cout<<"左括号多餘\n";
}
int main()
{
while(cin>>operation)
{
InitialStack();
procExecute();
}
return 0;
}