请编写程序检查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
限制条件
限制条件 | 数量 |
---|---|
代码长度限制 | 16 KB |
时间限制 | 400 ms |
内存限制 | 64 MB |
C++代码
//replace、length、sizeof好像都是stl,改掉了
#include <iostream>
using namespace std;
int chang(string str) //替换length函数
{
int i = 0;
while(str[i] != '\0')
{
i++;
}
return i;
}
string rep(string str) //替换并提取
{
//int len = str.length();
/*
for(int i = 0; i < len - 1; i++)
{
if(str[i] == '<' || str[i] == '>')
{
str.replace(i, 1, "$");
}
}
*/
int i = 0;
while(str[i] != '\0')
{
if(str[i] == '<' || str[i] == '>')
{
str[i] = '$';
}
i++;
}
/*
for(int i = 0; i < len - 1; i++)
{
if(str[i] == '/' && str[i+1] == '*')
{
str.replace(i, 1, "<");
str.replace(i + 1, 1, "#");
}
else if(str[i] == '*' && str[i+1] == '/')
{
str.replace(i, 1, ">");
str.replace(i + 1, 1, "#");
}
}
*/
int j = 0;
while(str[j] != '\0')
{
if(str[j] == '/' && str[j+1] == '*')
{
str[j] = '<';
str[j + 1] = '#';
}
if(str[j] == '*' && str[j+1] == '/')
{
str[j] = '>';
str[j + 1] = '#';
}
j++;
}
/*
for(int i = 0; i < len; i++)
{
if(str[i] == '#'){
str.replace(i, 1, "");
}
}
*/
char tmp[] = {'(', ')', '[', ']', '{', '}', '<', '>'};
string str1;
int lenstr = chang(str);
for(int i = 0; i < lenstr; i++)
{
for(int j = 0; j < 8; j++)
{
if(str[i] == tmp[j])
{
str1 += str[i];
break;
}
}
}
return str1;
}
bool judge(string str)
{
char sta[100] = {};
int len = chang(str);
for(int i = 0; i < len; i++){
if(str[i] == '(' || str[i] == '[' || str[i] == '{' || str[i] == '<') //左符号
{
//int len1 = sizeof(sta);
for(int j = 0; j < 100; j++)
{
if(sta[j] == '\0')
{
sta[j] = str[i]; //存入sta中
break;
}
}
}
else if(str[i] == ')' || str[i] == ']' || str[i] == '}' || str[i] == '>') //右符号
{
if(sta[0] == '\0') //sta为空
{
if(str[i] == '>')
{
cout << "NO" << endl;
cout << "?-*/" << endl;
}
else
{
cout << "NO" << endl;
cout << "?-" << str[i] << endl;
}
break;
}
else
{
char endele;
int k;
for(k = 0; k < 100; k++) //取sta最后一个元素
{
if(sta[k] == '\0')
{
endele = sta[k - 1];
break;
}
}
if( (str[i] == ')' && endele == '(') || (str[i] == ']' && endele == '[') ||(str[i] == '}' && endele == '{') ||(str[i] == '>' && endele == '<') )
{ //若匹配,删除sta最后一个元素
sta[k - 1] = '\0';
}
else //若不匹配,结束
{
if(endele == '<')
{
cout << "NO" << endl;
cout << "/*-?" << endl;
}
else
{
cout << "NO" << endl;
cout << endele << "-?" << endl;
}
break;
}
}
}
if(sta[0] == '\0' && i == len - 1)
{
cout << "YES" <<endl;
break;
}
else if(sta[0] != '\0' && i == len - 1)
{
if(sta[0] == '<')
{
cout << "NO" <<endl;
cout <<"/*-?";
}
else
{
cout << "NO" <<endl;
cout << sta[0] << "-?";
}
break;
}
}
}
int main()
{
string s;
string res;
while(1)
{
getline(cin, s);
if(s == ".")
break;
res = res + s;
}
judge(rep(res));
return 0;
}