程序如何检查堆栈溢出_通过使用堆栈检查平衡的括号(C ++程序)

程序如何检查堆栈溢出

Problem Statement:

问题陈述:

Mathematical calculations can sometimes give incorrect and varied results. To overcome this problem we should use balanced brackets.

数学计算有时会得出错误且变化的结果。 为了克服这个问题,我们应该使用平衡括号。

Balanced brackets are those who have a closing bracket corresponding to each of its opening bracket and in respective order. Here, I am considering square brackets [ ], circular brackets ( ) and curly braces { } as parentheses.

平衡括号是指具有对应于每个开头括号并按顺序排列的结尾括号的那些括号 。 在此,我考虑将方括号[] ,圆括号()和花括号{}作为括号。

The stack is a type of data structure in which any data inserted is considered to be added on top of first entered data and any data deleted or removed from the data layer structure is deleted from the top only; thus this data structure works on the principle of LIFO (Last In First Out).

堆栈是一种数据结构,其中任何插入的数据都被视为添加在第一个输入数据的顶部,而从数据层结构中删除或删除的任何数据仅从顶部删除。 因此,此数据结构基于LIFO ( 后进先出)原理工作。

First, we make the user enter the number of test cases.Then for each corresponding test case we, call a function named balanced parentheses(). This function allows declaring a stack which can store datatype char. Then, the user is made to enter a string, and then it iterates by the length of string and whenever it approaches an opening bracket, that bracket is inserted (pushed i.e. push function)to the top of the stack. Whenever it comes across a closing bracket, the top element of the stack is deleted( or popped i.e pop function) with a corresponding opening bracket(as one opening and one corresponding closing bracket give empty Stack). While iterating through the length of the string, if it encounters any character other than the brackets(either opening or closing), then it won't affect our stack in any way.

首先,让用户输入测试用例的数量,然后为每个对应的测试用例调用一个名为Balanced Parentheses()的函数。 此函数允许声明一个可以存储数据类型char的堆栈 。 然后,使用户输入一个字符串,然后以字符串的长度进行迭代,并且每当它接近一个打开的括号时,都会将该括号插入(推入即推入功能)到堆栈的顶部。 只要碰到一个右括号,就会用相应的右括号删除堆栈顶部的元素(或弹出即弹出功能)(因为一个开口和一个相应的右括号给出了空的堆栈)。 在遍历字符串的长度时,如果遇到除方括号以外的其他任何字符(打开或关闭),那么它将不会以任何方式影响我们的堆栈。

In the end, the whole string has been iterated by its length times, then for the correct case our stack must be empty if it is not, then the brackets in the string entered are not balanced.

最后,整个字符串已按其长度倍数进行了迭代,然后在正确的情况下我们的堆栈必须为空(如果不是),那么输入的字符串中的括号将不平衡。

Remeber the following examples:

请记住以下示例:

    ( ) : balanced brackets
    ) ( : unbalanced brackets
    { ( ) ( ) } : balanced brackets

See, in expression [(3+5)(12)] the brackets are correctly placed and give the result as 96.

请参见表达式[(3 + 5)(12)]中的括号正确放置,结果为96

Whereas in {[65-1))]2} the two closing brackets aren't balanced with corresponding opening circular brackets and use of such terms should be taken care of while solving complex mathematical equations.

而在[[[65-1))] 2}中 ,两个闭合括号不与相应的打开圆括号保持平衡,因此在解决复杂的数学方程式时应注意使用此类术语。

C ++程序检查平衡括号 (C++ program to check balanced parentheses)

//Updated by Anshuman Singh

#include <iostream> //main header file
#include <stack>
using namespace std;

void balance_parentheses();

int main()
{
    int t;
    cout << "Enter number of test cases:";
    cin >> t;

    for (int i = 0; i < t; i++) {
        //calling of function for checking of brackets
        balance_parentheses();
    }

    return 0;
}

void balance_parentheses()
{
    stack<char> a;
    string s;
    cout << "Enter string may or may not containing parentheses:";
    cin >> s;

    int flag = 0; //flag is an arbitrary variable

    for (int i = 0; i < s.length(); i++)
    //for length of the string calculated by number of letters
    {
        if (s[i] == '{' || s[i] == '[' || s[i] == '(') {
            //push function to enter terms in a stack
            a.push(s[i]);
            flag = 1;
        }
        if (!a.empty()) {
            if (s[i] == '}') {
                if (a.top() == '{')
                // top of the stack
                {
                    a.pop();
                    //pop function to delete terms from the top of array
                    continue;
                }
                else
                    break;
            }
            if (s[i] == ']') {
                if (a.top() == '[') {
                    a.pop();
                    continue;
                }
                else
                    break;
            }
            if (s[i] == ')') {
                if (a.top() == '(') {
                    a.pop();
                    continue;
                }
                else
                    break;
            }
        }
        else {
            break;
        }
    }

    if ((a.empty()) && (flag == 1))
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}

Output

输出量

Enter number of test cases:2 
Enter string may or may not containing parentheses:}[])] 
NO 
Enter string may or may not containing parentheses:[]{}()
YES


翻译自: https://www.includehelp.com/data-structure-tutorial/check-for-balanced-parentheses-by-using-stacks-cpp-program.aspx

程序如何检查堆栈溢出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值