0

I ran into this problem when solving a math problem. Here's the code:

// program to check if parentheses expression is correct
// {()}[{()}] = true, ()(} = false
#include <string.h>

#include <iostream>
#include <stack>

using namespace std;

int par(string str) {
  int a = str.length();
  stack<char> S;
  char x;
  for (int i = 0; i < a; i++) {
    x = str[i];
    if (x == '(' || x == '[' || x == '{') {
      S.push(x);
    } else {
      if (x == ')') {
        if (S.top() == '(') {
          S.pop();
        } else
          return 0;
      } else if (x == ']') {
        if (S.top() == '[') {
          S.pop();
        } else
          return 0;
      } else if (x == '}') {
        if (S.top() == '{') {
          S.pop();
        } else
          return 0;
      }
    }
  }
  if (!S.empty()) {
    return 0;
  } else
    return 1;
}

int main() {
  int n;
  string str;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> str;
    cout << par(str) << endl;
  }
  return 0;
}

Ignore the bad coding, when compile this code using this command:

clang++ -std=c++20 -Wall -Wextra -pedantic -g par.cpp -o par.exe

The code compiled, executable generated, but when I open par.exe it terminates immediately.

But when using g++, it worked well:

g++ -std=c++20 -Wall -Wextra -pedantic -g par.cpp -o par.exe

Here's the clang++ version:

>clang++ --version
clang version 15.0.5
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:/msys64/ucrt64/bin

and g++:

>g++ --version
g++.exe (tdm64-1) 10.3.0
Copyright (C) 2020 Free Software Foundation, Inc.

I'm assumimg this was because of the <stack> and <queue> library, as I used clang without these libraries and it worked well.

Any ideas ?

Trung Kiên
  • 135
  • 1
  • 8

0 Answers0