-4

here is my code it is working fine now but not giving the desire output ex for eq {a+(b-c)*d} the output should be
"bracket matched popping )
bracket matched popping }"
but output is
"bracket dose not matched"

#include <bits/stdc++.h>
using namespace std;
main() {    
    string exp;
    cin >> exp;
//  cin.getline(exp);
    stack <char> s;
    for(int i = 0; exp[i] != '\0'; i++) {
        if(exp[i] == '{' || exp[i] == '(' || exp[i] == '[') {
            s.push(exp[i]);
            continue;
        } else if(exp[i] == '}' || exp[i] == ')' || exp[i] == ']') {
            if(s.top() == exp[i]) {
                cout << "bracket matched popping " << s.top() << endl;
                s.pop();
                continue;
            } else {
                cout << "bracket dose not matched" << endl;
                break;
            }
        } else {
            continue;
        }
    }
}
  • 3
    First of all please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) Secondly, please edit your question to include a comment on the line you're getting the error (and also to include the *full* and *complete* copy-pasted error output). – Some programmer dude Dec 17 '18 at 09:20
  • 2
    Also you don't need those `continue` statements, the loop will continue anyway. Also don't loop until the null terminator, a `std::string` might have it embedded anywhere (and old versions of the C++ standard didn't require it). Either loop over the length, use iterators of [range-based `for`](https://en.cppreference.com/w/cpp/language/range-for). – Some programmer dude Dec 17 '18 at 09:25
  • If you want help with a compiler error you really need to say which line has the error. – john Dec 17 '18 at 09:27
  • and your code do not check the missing ])} at the end – bruno Dec 17 '18 at 09:28
  • thanks @Someprogrammerdude I have numeric 1 at the end of the code but I want to check that the brackets are in right position or not in a given equation but where i am getting wrong any help is appreciated – Parikshit Singh Dec 17 '18 at 09:46
  • I got what was wrong in my code after dry run thanks for your time – Parikshit Singh Dec 17 '18 at 09:51

3 Answers3

4

because ( is not ) not [ is not ] nor { is not }

when you read a ( you have to push a ) rather than a ( etc, or you have to change the test (s.top() == exp[i])

bruno
  • 31,755
  • 7
  • 21
  • 36
0

This line is wrong, C++ strings are not null terminated

for(int i = 0; exp[i] != '\0'; i++) {

Do this

for(size_t i = 0; i < exp.size(); i++) {
john
  • 71,156
  • 4
  • 49
  • 68
  • They're guaranteed to have `\0` at the end since at least C++11, aren't they?. Such loop condition would give incorrect results if you have `\0` in the middle of your string, yes; but that's not what happens in OP's case. – HolyBlackCat Dec 17 '18 at 09:47
  • @HolyBlackCat, Good to know, yet another case of my C++ being out of date. Would still prefer to see my version of the loop though. – john Dec 17 '18 at 09:49
  • I have done this and now changed my code also but now also it is changing my output – Parikshit Singh Dec 17 '18 at 10:15
0

Your comparison for the parenthesis match is wrong:

  if(s.top() == exp[i]) {

That would mean that you expect '(' to close '('.

You need to expand that comparison to the respective expected opening parenthesis - i.e., if you currently encounter ']', you need to check for '[' on the stack; don't check for equality, but check explicitly both s.top() and exp[i] values; just to give you an idea:

} else if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']') {
       if ((s.top() == '(' && exp[i] == ')') ||
           //... check for '[' matching ']' and '(' matching ')'...
codeling
  • 9,289
  • 4
  • 36
  • 61