M
M
mihailos2020-12-07 18:13:42
C++ / C#
mihailos, 2020-12-07 18:13:42

How to complete this code?

#include <iostream>
#include <vector>

using namespace std;

int main(){
  string a;
  cin>>a;
  int c;
  vector<char> b;
  for(int i = 0; i < a.length(); i++){
    if(a[i] == '(') b.push_back(a[i]);
    else if(a[i] == ')') b.pop_back();
  }
  if(b.size() > 0) cout<<"Неправильная скобочная последовательность!!!"<<endl;
  else cout<<"Правильная скобочная последовательность"<<endl;
}

When you enter the string "()())" gives an error: Segmentation error (memory stack flushed to disk).
But I seem to understand why.
And there is a question: how to display "Incorrect bracket sequence!!!" instead of this error! Or how can I track this error?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
Rsa97, 2020-12-07
@mihailos

Because you are doing pop_back on an empty vector.

PS
А когда-то для этих целей хватало одной целочисленной переменной. Теперь, смотрю, векторы используют. Скоро, наверное, bigdata и искусственный интеллект подтянут...

D
DTX, 2016-12-06
@DirecTwiX

/([^\d.-]|(?<=\.\d\d).+)/g
https://regex101.com/r/Ypju79/1
Upd:
Hmm. It turned out that there is no positive lookbehind in js.
In any case, it will not be possible to do this with the help of one regular expression of adequate size.
It's easier to do post processing in js. Here is an example:
codepen.io/anon/pen/QGrYrw

D
datasheet, 2016-12-07
@datasheet

^[\d-]*(?=(\.\d{0,2})?)(\.\d{0,2})?$
https://regex101.com/r/rrwfHP/2
under did the signs after the dot imply numbers?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question