数据结构-符号配对

请编写程序检查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 <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <string>
using namespace std;
vector<char> kh;

void print(char c)
{
    cout << "NO" << endl;
    if (c == '(') cout << "(-?";
    else if(c=='[') cout << "[-?";
    else if (c == '{') cout << "{-?";
    else if (c == '*') cout << "/*-?";
}


bool isOK(string str)
{
    int len = str.length();
    for (int i = 0; i < len; i++)
    {
        if (str[i] == '(' || str[i] == '[' || str[i] == '{')
        {  //如果有左括号,则存入栈中 
            kh.push_back(str[i]);
        }
        else if (i < len - 1 && str[i] == '/' && str[i + 1] == '*')
        {
            kh.push_back(str[i]);
            kh.push_back(str[i + 1]);
            i++;
        }
        else if (str[i] == ')')
        {  //若出现了右括号,则top应该有和它对应得左括号 
            if (kh.empty())
            {
                cout << "NO\n?-)";
                return false;
            }
            else if (kh.back() != '(')
            {
                print(kh.back());
                return false;
            }
            kh.pop_back();   //取出对应的左括号 
        }
        // 一下重复[ , {;
        else if (str[i] == ']')
        {
            if (kh.empty())
            {
                cout << "NO\n?-]";
                return false;
            }
            else if (kh.back() != '[')
            {
                print(kh.back());
                return false;
            }
            kh.pop_back();
        }
        else if (str[i] == '}')
        {
            if (kh.empty())
            {
                cout << "NO\n?-}";
                return false;
            }
            else if ( kh.back() != '{')
            {
                print(kh.back());
                return false;
            }
            kh.pop_back();
        }
        else if (i < len - 1 && str[i] == '*' && str[i + 1] == '/')
        {
            i++;
            if (kh.empty())
            {
                cout << "NO\n?-*/";
                return false;
            }
            else if (kh.back() != '*')
            {
                print(kh.back());
                return false;
            }
            kh.pop_back();
            kh.pop_back();
        }
    }
        return true;
}


int main()
{
    string str;
    while (true)
    {
        getline(cin, str);
        if (str == ".") break;
        if (!isOK(str)) return 0;
    }
    if (kh.empty()) cout << "YES";
    else
    {
        cout<<"NO"<<endl;
        if(kh[0]=='/') cout<<"/*-?";
        else cout<<kh[0]<<"-?";
    }
    return 0;
}
做题时出现的问题:
1 、有且只有将字符串全部遍历一遍且栈为空,输出 YES
2 NO 的情况
(1) 当出现右括号且栈为空的情况下,输出 ”?-(str[ i ])”
(2) 当出现右括号且栈不为空的情况 , 则对应的是栈顶为第一个出现不匹配的符号,输出 ”(str[ i ])-?”
(3) 当字符串遍历完了,但栈不为空的情况, ( 因为入栈了,所以为左括号 ) ,输出 ”( 栈底 )-?”
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值