Parentheses checker logical error

This below, is an implementation of stack method for parentheses checker, this code works extremely fine except for 1 case.. where i input
(a+b)) or ())
just any type of such input where a closing bracket is left alone, even if the rest is balanced it counts it as balanced..why does that happen ? I couldn't find where the error is, maybe someone can help ? ^-^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <bits/stdc++.h>


#define fl(i,n)    for(int i = 0; i < n; i++)

#define ll   long long
#define nl   endl
#define pb   push_back
#define mp   make_pair
#define PII  pair<int,int>

#define EPS  1e-9
#define INF  1e9


using namespace std;

class Stack{
private:
    char data;
    Stack *next;
public:
    void push(Stack *&top, char x){
        Stack *temp = new Stack;
        temp->data = x;
        temp->next = top;
        top = temp;
    }

    void pop(Stack *&top){
        if(top == nullptr) return;
        Stack *temp = top;
        temp = top->next;
        delete top;
        top = temp;
    }
    void Print(Stack *top)
    {
        Stack* temp = top;
        while(temp!=nullptr)
        {
            cout << temp->data<<" ";
            temp = temp->next;
        }
        cout << nl;
    }

    bool isEmpty(Stack *top){
        if(top == nullptr) return true;
        return false;
    }

    int top_element(Stack *top){
        if(isEmpty(top)) return -1;
        return (top->data);
    }
    void free_stack(Stack *&top){
        if(top == nullptr) return;
        free_stack(top->next);
        pop(top);
    }

};


int main()
{
    //Stack implemented using linked list to check for parentheses balance
    Stack *top = nullptr;
    Stack checker;
    string s;
    getline(cin,s);
    cout << "Checking.." << nl;
    bool flag = 1;
    for(unsigned int i = 0; i < s.length(); i++){
        if(s[i] == '(' || s[i] == '[' || s[i] == '{')
            flag = 0, checker.push(top, s[i]);
        if(s[i] == ')'){
            if(checker.top_element(top) == '(') checker.pop(top);
        }
        else if(s[i] == ']'){
            if(checker.top_element(top) == '[') checker.pop(top);
        }
        else if(s[i] == '}'){
            if(checker.top_element(top) == '{') checker.pop(top);
        }
    }

    if(flag){
        cout << "No parentheses found" << nl;
        return 1;
    }
    if(checker.isEmpty(top)) cout << "Parentheses Balanced" << nl;
    else cout << "Parentheses not Balanced" << nl;

    checker.free_stack(top);

   
    return 0;
}


Thanks !
Last edited on
You don't need all that, its just a counting problem.

int ct = 0;
if (input == '(' )
ct++
if (input == ')' )
ct--;

... after all input,
if(ct!= 0) its mismatched. you can add more logic to check for weird cases, for example, if ct is ever negative, its wrong, even if it balances. That will handle )))((( type mistakes that balance but are wrong. If being really smart, you will detect // and /* */ comment blocks and ignore inside (assuming c++ code being checked).

-- if I understood your code, I think your error is here.
if(top == nullptr) return;
this leaves the last value that was there in all the variables. It seems like something should be set so you can detect this?



Last edited on
1
2
3
4
        if(s[i] == ')'){
            if(checker.top_element(top) == '(') checker.pop(top);
            //else not_balanced();
        }



By the way, your class has its organs outside.
`Stack' is `Node', but the members functions do seem to correspond to `Stack' and no one uses its state.
so you end with this abomination checker.isEmpty(top)
Last edited on
Topic archived. No new replies allowed.