V
V
valent_in_habrahabr2016-05-11 20:14:42
C++ / C#
valent_in_habrahabr, 2016-05-11 20:14:42

[C++] Set begin - set end as well. How to recognize attachments and correctly put end in the code?

Good afternoon. I have a task for the KP to write a program on the pluses: "Set begin - set end." An array of strings is given, which is a fragment of a Pascal program. Write a program that checks the correct nesting of operator brackets begin ... end - each begin must correspond to end. "
Everything is very clear to me, no problem. It
's just not clear how to recognize nestings. More precisely, how to find out where to insert end, in the comments I highlighted end , which was "forgotten" to put and I need to add it. The code should work correctly, after checking, and not deteriorate.
For example:

var a,n,c,d:word;
begin 
    readln( a );
    n:=1;
    while ( n <= sqrt(a) ) do begin
       c:=a mod n;
       d:=a div n;
       if c = 0 then begin
          writeln( n );
          if n <> d then writeln( d );
       {end; }
       inc( n ); { вот как понять, что end;должен стоять до а не после? }
    end;
end.

I would have solved this problem with indentation without any problems. But I'm sure my teacher will copy unformatted code into the textBox. Please tell me how to solve this problem? I'm sure there is a good way. I don't want to coddle.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2016-05-11
@valent_in_habrahabr

No way.
If it were possible to easily predict where to insert end, then all compilers would have been doing this for a long time, but for some reason they don’t do it, but only give an error message, as a rule, a line is indicated that doesn’t even close pointing to the place where end is missing .
And read the assignment carefully - you don't need to insert end. Do as the compiler - count the number of begin and end, if not equal - an error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question