7-3 符號配對 (20 分)
請編寫程序檢查C語言源程序中下列符號是否配對:/*
與*/
、(
與)
、[
與]
、{
與}
。
輸入格式:
輸入為一個C語言源程序。當讀到某一行中只有一個句點.
和一個回車的時候,標志着輸入結束。程序中需要檢查配對的符號不超過100個。
輸出格式:
首先,如果所有符號配對正確,則在第一行中輸出YES
,否則輸出NO
。然后在第二行中指出第一個不配對的符號:如果缺少左符號,則輸出?-右符號
;如果缺少右符號,則輸出左符號-?
。
輸入樣例1:
void test()
{
int i, A[10];
for (i=0; i<10; i++) /*/
A[i] = i;
}
.
輸出樣例1:
NO
/*-?
輸入樣例2:
void test()
{
int i, A[10];
for (i=0; i<10; i++) /**/
A[i] = i;
}]
.
輸出樣例2:
NO
?-]
輸入樣例3:
void test()
{
int i
double A[10];
for (i=0; i<10; i++) /**/
A[i] = 0.1*i;
}
.
輸出樣例3:
YES
#include<bits/stdc++.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
using namespace std;
typedef long long ll;
typedef int Status;
typedef struct{
char *base;
char *top;
int StackSize;
}SqStack;
Status InitStack(SqStack&S){
S.base = (char*)malloc(110*sizeof(char));
if(!S.base) return OVERFLOW;
S.top = S.base;
S.StackSize = 110;
return OK;
}
Status push(SqStack &S,char e){
if(S.top - S.base==S.StackSize) return ERROR;
*S.top++ = e;
return OK;
}
Status pop(SqStack&S,char e){
if(S.top==S.base)
return ERROR;
e = *--S.top;
return OK;
}
char GetTop(SqStack S){
if(S.top!=S.base) return *(S.top-1);
}
bool StackEmpty(SqStack&S){
if(S.top==S.base) return true;
return false;
}
int main()
{
//freopen("i.txt","r",stdin);
char s[110],str[110];
char c;
int cnt = 0;
while(scanf("%s",str)!=EOF&&str[0]!='.'){
for(int i=0;str[i];i++){
if(str[i]=='('||str[i]=='{'||str[i]=='['||str[i]==')'||str[i]=='}'||str[i]==']')
s[cnt++] = str[i];
else if(str[i]=='/'&&str[i+1]=='*'){
s[cnt++] = str[i];
s[cnt++] = str[i+1];
i++;
}else if(str[i]=='*'&&str[i+1]=='/'){
s[cnt++] = str[i];
s[cnt++] = str[i+1];
i++;
}
}
}
s[cnt++]='\0';
SqStack S;
InitStack(S);
bool flag = true;
for(int i=0;s[i];i++){
if(s[i]=='('||s[i]=='{'||s[i]=='[')
push(S,s[i]);
else if(s[i]=='/'&&s[i+1]=='*'){
push(S,s[i]);
push(S,s[i+1]);
i++;
}
else if(s[i]==')'){
if(!StackEmpty(S)&&GetTop(S)=='('){
pop(S,'(');
}else{
c = s[i];
flag = false;
break;
}
}
else if(s[i]==']'){
if(!StackEmpty(S)&&GetTop(S)=='['){
pop(S,'[');
}else{
c = s[i];
flag = false;
break;
}
}
else if(s[i]=='}'){
if(!StackEmpty(S)&&GetTop(S)=='{'){
pop(S,'{');
}else{
c = s[i];
flag = false;
break;
}
}
else if(s[i]=='*'&&s[i+1]=='/'){
if(!StackEmpty(S)&&GetTop(S)=='*'){
pop(S,'*');
if(!StackEmpty(S)&&GetTop(S)=='/')
pop(S,'/');
else{
c = s[i];
flag = false;
break;
}
}else{
c = s[i];
flag = false;
break;
}
i++;
}
}
if(flag&&StackEmpty(S)) printf("YES\n");
else{
printf("NO\n");
if(!StackEmpty(S)){
char x = GetTop(S);
if(x=='(')
cout<<'('<<'-'<<'?'<<endl;
else if(x=='[')
cout<<'['<<'-'<<'?'<<endl;
else if(x=='{')
cout<<'{'<<'-'<<'?'<<endl;
else if(x=='*')
cout<<'/'<<'*'<<'-'<<'?'<<endl;
}else{
if(c==')')
cout<<'?'<<'-'<<')'<<endl;
else if(c==']')
cout<<'?'<<'-'<<']'<<endl;
else if(c=='}')
cout<<'?'<<'-'<<'}'<<endl;
else if(c=='*')
cout<<'?'<<'-'<<'*'<<'/'<<endl;
}
}
}