7-2 符號配對(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<cstdio>
#include<iostream>
#include<cmath>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
#include<cstring>
#define STACK_INIT_SIZE 10000
#define STACKINCREMENT 10
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
using namespace std;
typedef char SElemType,Status;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
} Stack;
Status InitStack(Stack &S)
{
S.base=(SElemType *)malloc(sizeof(SElemType)*STACK_INIT_SIZE);
if(!S.base)
exit(OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK;
}
Status Push(Stack &S,SElemType e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType*)malloc(sizeof(SElemType)*(S.stacksize+STACKINCREMENT));
if(!S.base)
exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}
Status Pop(Stack &S)
{
if(S.top==S.base)
return ERROR;
S.top--;
return OK;
}
Status GetTop(Stack &S,SElemType &e)
{
if(S.base==S.top)
return ERROR;
e=*(S.top-1);
return OK;
}
const int maxn=1000+5;
char s[maxn];
bool Find(Stack &S,char ch)
{
char tmp[maxn];
memset(tmp,'\n',sizeof(tmp));
int num=0;
while(S.top!=S.base)
{
SElemType e2;
GetTop(S,e2);
if(e2==ch)
{
Pop(S);
for(int i=num-1; i>=0; i--)
Push(S,tmp[i]);
return true;
}
else
{
tmp[num++]=e2;
}
Pop(S);
}
for(int i=num-1; i>=0; i--)
Push(S,tmp[i]);
return false;
}
void judge(char ch)
{
if(ch=='(')
printf("(-?\n");
else if(ch=='[')
printf("[-?\n");
else if(ch=='{')
printf("{-?\n");
else if(ch=='<')
printf("/*-?\n");
}
int main()
{
Stack Sta;
InitStack(Sta);
int flag=1;
while(gets(s))
{
if(s[0]=='.') break;
int len=strlen(s);
for(int i=0;i<strlen(s);i++)
{
if(s[i]=='('||s[i]=='['||s[i]=='{')
Push(Sta,s[i]);
else if(s[i]=='/'&&s[i+1]=='*'&&i+1<len)
{
++i;
Push(Sta,'<');
}
else if(s[i]==')')
{
if(Sta.top!=Sta.base)
{
SElemType e;
GetTop(Sta,e);
if(e=='(')
Pop(Sta);
else if(flag)
{
printf("NO\n");
flag=0;
judge(e);
}
}
else if(flag)
{
flag=0;
printf("NO\n");
printf("?-)\n");
}
}
else if(s[i]==']')
{
if(Sta.top!=Sta.base)
{
SElemType e;
GetTop(Sta,e);
if(e=='[')
Pop(Sta);
else if(flag)
{
printf("NO\n");
flag=0;
judge(e);
}
}
else if(flag)
{
flag=0;
printf("NO\n");
printf("?-]\n");
}
}
else if(s[i]=='}')
{
if(Sta.top!=Sta.base)
{
SElemType e;
GetTop(Sta,e);
if(e=='{')
Pop(Sta);
else if(flag)
{
printf("NO\n");
flag=0;
judge(e);
}
}
else if(flag)
{
flag=0;
printf("NO\n");
printf("?-}\n");
}
}
else if(s[i]=='*'&&s[i+1]=='/'&&i+1<len)
{
++i;
if(Sta.top!=Sta.base)
{
SElemType e;
GetTop(Sta,e);
if(e=='<')
Pop(Sta);
else if(flag)
{
printf("NO\n");
flag=0;
judge(e);
}
}
else if(flag)
{
flag=0;
printf("NO\n");
printf("?-*/\n");
}
}
}
}
if(flag)
{
if(Sta.base==Sta.top)
printf("YES\n");
else
{
SElemType e;
GetTop(Sta,e);
printf("NO\n");
judge(e);
}
}
}
