查找对应的括号

c/c++练习题:查找对应的括号。如:((()(()))()), 在位置3出的左括号其右括号所在的位置为4


// 查找对应的括号。如:
// ((()(()))())
// 在位置3出的左括号其右括号所在的位置为4


#include <iostream>
#include <stack>
#include <string>
#include <conio.h>

using std::cin;
using std::cout;
using std::endl;

enum Status
{
    s_ok,
    s_err,
    s_invalidstring,
    s_invalidchar,
    s_outofrange,
};

std::string PrintStatus( Status s )
{
    switch(s)
    {
    case s_ok: return "s_ok";
    case s_err: return "s_err";
    case s_invalidstring: return "s_invalidstring";
    case s_invalidchar: return "s_invalidchar";
    case s_outofrange: return "s_outofrange";
    default: return "";
    }
}

typedef std::string::const_iterator ParenthesesCIter;

Status IsParentheses( const std::string & inputsrc )
{


    for ( ParenthesesCIter it(inputsrc.begin());
        it != inputsrc.end(); ++it )
    {
        const char & ch = *it;
        if ( ch != '(' && ch != ')')
        {
            return s_err;
        }

    }
    return s_ok;
}


Status  FindParentheses(const std::string & inputstr, const char & inputsrc, std::size_t inputindex, int & outputindex )
{
#pragma region check input source
    if ( IsParentheses(inputstr) != s_ok )
    {
        return s_invalidstring;
    }
    if ( inputsrc != '(' && inputsrc != ')')
    {
        return s_invalidchar;
    }
    if (inputindex < 0 || inputindex > inputstr.size() )
    {
        return s_outofrange;
    }
    if (inputstr[inputindex] != inputsrc )
    {
        return s_err;
    }

    size_t nleft, nright;
    nleft = nright = 0;

    for ( ParenthesesCIter it(inputstr.begin()); 
        it != inputstr.end(); ++it )
    {
        const char & ch = *it;
        if ( ch == '(' )
        {
            ++nleft;
        }
        else
        {
            ++nright;
        }
    }
    if ( nleft != nright )
    {
        return s_err;
    }

#pragma endregion check input source

    std::stack<char> chStk;

    if (inputsrc == '(')
    {
        for ( std::size_t i = inputindex+1; i < inputstr.size(); ++i )
        {
            if (inputstr[i] == ')')
            {

                if (chStk.empty())
                {
                    outputindex = i;
                    break;
                }
                chStk.pop();
            }else
            {
                chStk.push(inputstr[i]);
            }
        }
    }
    else
    {
        for ( std::size_t i = inputindex-1; i >= 0; --i )
        {
            if (inputstr[i] == '(')
            {

                if (chStk.empty())
                {
                    outputindex = i;
                    break;
                }
                chStk.pop();
            }else
            {
                chStk.push(inputstr[i]);
            }
        }
    }




    return s_ok;

}
int _tmain(int argc, _TCHAR* argv[])
{
    std::string inputstr;
    char ch;
    int inputindex;
    int outputindex;
    inputindex = outputindex = -1;

    while(1)
    {
        cout << "请输入几组左右括号,如: \"((()(()))())\"\t:";
        cin >> inputstr;
        cout << endl << "请输入一个左括号或右括号: ";
        cin >> ch;
        cout << endl << "请输入其在括号组里面的位置(从1数起):" ;
        cin >> inputindex;
        cout << endl;
        Status s;
        s = FindParentheses(inputstr, ch, inputindex-1, outputindex);
        if ( s == s_ok )
        {
            cout << "\"" << inputstr <<"\"" << "位置"<< inputindex << "出" <<"'"<< ch <<"'" <<"所对应的括号其所在的位置为(从1数起): " << outputindex+1 << endl;
            break;
        }
        else
        {
           cout << "error code is "  << PrintStatus(s);
        }

        getch();

#if _MSC_VER
        system("cls");
#endif
        cin.clear();
        cin.sync();
    }

#if _MSC_VER
    system("pause");
#endif

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值