{
 ##################################
 # Tkachenko Max, 112             #
 #                  22 var.       #
 #                     2006 year  #
 ##################################
}

program Task2_DynamicDate;
 uses crt;

 const
   BEGINX = 40;
   BEGINY = 10;

 type
   PTreeElem = ^TTreeElem;
   TTreeElem = record
     k : char;
     l,r : PTreeElem;
 end;

 {--- Helper type ---}
 type TError = record
  num : integer;
 end;

 {----------- New Tree (+Constructor) -----------}
 procedure NewTree(var tree : PTreeElem);
 begin
  new(tree);
  tree^.l := nil;
  tree^.r := nil;
  tree^.k := '?';
 end;

 {----------- Error Message -----------}
 procedure ShowError(s:string; var err:TError);
  var c : char;
  begin
    err.num := err.num + 1;
     textcolor(LIGHTRED);
      writeln('    Ç  Error   :: ', err.num,' :: ', s);
  end;


 {-----------  GET FORMULA ------------}
 procedure GetForm(var tree : PTreeElem; var err : TError);

  {--- Subprocedure :: Test on error with operation sign ---}
  procedure TestError_Sing(var tree : PTreeElem; var err : TError);
   begin
    read(tree^.k);
      if (tree^.k <>'+') and (tree^.k <>'-') and (tree^.k <>'*') then
        ShowError('+,- or *   was missing, but found '+tree^.k+'.',err);
   end;

  {--- Subprocedure :: Test on error with ')' ---}
  procedure TestError_Bracket(var tree : PTreeElem; var err : TError);
  var c2 : char;
   begin
    read(c2);
      if c2 = #13 then
       ShowError(')   was missing after '+tree^.r^.k+', but found RETURN(ENTER).', err)
        else
         if c2 <> ')' then
          ShowError(')   was missing after '+tree^.r^.k+', but found '+c2+'.', err);
   end;

   var c : char;

  begin
   read(c);
    case c of
     '0'..'9', 'x','X' : tree^.k := c;

     '(' : begin  { (<formula> znak <formula>) }
             NewTree(tree^.l); GetForm(tree^.l, err);
              TestError_Sing(tree, err);
             NewTree(tree^.r); GetForm(tree^.r, err);
              TestError_Bracket(tree, err);
           end;
     ')' : ;

      else ShowError('Unrecognized symbol or sign: '+c,err);
    end;
  end;


  {----------- POSTFIX FORMUL -------------}
  procedure PostForm(var tree : PTreeElem);
   begin
    case tree^.k of
     '0'..'9', 'x','X' : write(tree^.k);
     '+', '-', '*' : begin
                      write('(');
                       PostForm(tree^.l);
                       PostForm(tree^.r);
                      write(tree^.k);
                      write(')');
                     end;
    end;
   end;


 {----------- Free Memory -------------}
 procedure FreeForm (var tree : PTreeElem);
  begin
   if (tree^.l = nil) and (tree^.r = nil) then
     dispose(tree)
      else
       begin
        if tree^.l <> nil then FreeForm(tree^.l);
        if tree^.r <> nil then FreeForm(tree^.r);
       end;
  end;

 {----------- DRAW TREE ---------------}
 procedure DrawTree(var tree : PTreeElem);
 var i : integer;
  begin
      i:= (10-round((wherey-BEGINY)*1.5));
        if i<1 then i:=1;

     write(tree^.k);
        if tree^.l <> nil then begin
                               gotoxy(wherex-i, wherey+2);
                                DrawTree(tree^.l);
                               gotoxy(wherex+i+1, wherey-2);
                               end;
        if tree^.r <> nil then begin
                               gotoxy(wherex+i, wherey+2);
                                DrawTree(tree^.r);
                               gotoxy(wherex-i+1, wherey-2);
                               end;
  end;



 {=========== MAIN PROGRAM ============}

var
  tree : PTreeElem;
  err : TError;

begin
  clrscr;
     err.num := 0;
 NewTree(tree);

  textcolor(LIGHTGRAY);  write('  Input form: ');
 GetForm(tree, err); writeln; writeln;

  textcolor(BROWN);  write('  Postfix form: ');
   if err.num <> 0 then
    begin
     writeln(' input was incorrect; postfix may be wrong or can not be. ');
     write('     ');
    end;
  PostForm(tree); writeln; writeln; writeln;

  textcolor(GREEN);  write('  Tree view (for simple formulas): ');  gotoxy(BEGINX, BEGINY+err.num);
 DrawTree(tree);

  readln;
 FreeForm(tree);
end.