#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXSIZE 100
typedef struct SeqStack
{
int data[MAXSIZE];
int top;
}SeqStack,*StackP;
/**判定栈空*/
bool isEmpty(StackP sp)
{
if(sp->top==-1)
return true;
else
return false;
}
/**初始化栈*/
StackP initSeqStack()
{
StackP sp=(StackP)malloc(sizeof(SeqStack));
sp->top=-1;
return sp;
}
/**压栈*/
bool PushToStack(int x,StackP sp)//根据题干,必然不会超出100个,一定为真
{
if(sp->top==MAXSIZE-1)
return false;//栈满,入栈失败
sp->top++;
sp->data[sp->top]=x;
return true;
}
/**出栈*/
bool outToStack(int *x,StackP sp)
{
if(sp->top==-1)
return false;//空栈,出栈失败
*x=sp->data[sp->top]; //用于更新数据,而'x='则是修改地址,请一定注意
sp->top--;
return true;
}
/**弹栈并判断*/
int PushAndJudge(StackP sp,int x,int *y)//x是右符号,y是左符号
{
bool is=outToStack(y,sp);//y就是地址
if(!is) //空栈 则第一个不配对的符号是右符号
return -1;
if(x+*y==10)
{
return 1;
}
else //不匹配 则第一个不匹配的是左符号
{
//printf("不匹配");
return 0;
}
}
char turnToChar(int y)
{
char temp='&';
if(y==2)
temp='(';
else if(y==3)
temp='[';
else if(y==4)
temp='{';
return temp;
}
/**一般右符号转义为int*/
int turnToInt(char temp)
{
int x=0;
if(temp==')')
x=8;
else if(temp==']')
x=7;
else if(temp=='}')
x=6;
return x;
}
/**最终判断*/
void endJudge(StackP sp)
{
if(isEmpty(sp))
{
//printf("当然");
printf("YES");
}
else
{
int tempInt=sp->data[0];
if(tempInt==1)
{
printf("NO\n/*-?");
return ;
}
char temp = turnToChar(tempInt);
printf("NO\n%c-?",temp);
}
return ;
}
int main()
{
StackP sp=initSeqStack();
char temp;
int judgeInt;//判断常量
int y,tempInt;//y 取值常量
while((temp=getchar()))
{
if(temp=='.')
{
temp=getchar();
if(temp=='\n')
break;
}
//对/**/进行特殊处理
if(temp=='/')//判断/*
{
scanf("%c",&temp);
if(temp=='*')
PushToStack(1,sp);
else if(temp=='.')//说明结束
{
//补充结束代码
endJudge(sp);
return 0;
}
}//若是则压栈,是或否都会更新temp值
else if(temp=='*')//判断*/
{
scanf("%c",&temp);
if(temp=='/')//是*/
{
//弹栈并判断
judgeInt = PushAndJudge(sp,9,&y);
if(judgeInt==-1)
{
printf("NO\n?-*/");
return 0;
}
else if(judgeInt==0)
{
temp=turnToChar(y);
printf("NO\n%c-?",y);
return 0;
}
}
else if(temp=='.')//说明结束
{
//补充结束代码
endJudge(sp);
return 0;
}
}
//对一般符号进行一般处理
if(temp=='(') /**一般压栈*/
PushToStack(2,sp);
else if(temp=='[')
PushToStack(3,sp);
else if(temp=='{')
PushToStack(4,sp);
else if(temp==')'||temp==']'||temp=='}')/**一般判断*/
{
//弹栈并判断
tempInt = turnToInt(temp);
//printf("tempInt=%d\n",tempInt); /**测试点*/
judgeInt = PushAndJudge(sp,tempInt,&y);
// printf("judgeInt=%d\n",judgeInt);
if(judgeInt==-1)
{
printf("NO\n?-%c",temp);
return 0;
}
else if(judgeInt==0)
{
temp=turnToChar(y);
if(temp=='('||temp=='['||temp=='{')
printf("NO\n%c-?",temp);
else
printf("NO\n/*-?");
return 0;
}
}
}
endJudge(sp);
return 0;
}