10.PAS 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. USES Graph;
  2. Type
  3. PBTree = ^TBTree;
  4. TBTree = record
  5. info : string[30];
  6. Left : PBTree;
  7. Right: PBTree;
  8. end;
  9. ps = ^el;
  10. el = record
  11. data : integer;
  12. prev : ps;
  13. end;
  14. Var
  15. inp : string;
  16. p : byte;
  17. mh : byte;
  18. {---------------------------------}
  19. Procedure DelBranch(br:PBTree);
  20. begin
  21. if br^.left <> nil then DelBranch(br^.left);
  22. if br^.right <> nil then DelBranch(br^.right);
  23. Dispose(br);
  24. end;{DelBranch}
  25. {-=-==-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-=-}
  26. Function GetToken:string;
  27. var
  28. ret : string[30];
  29. begin
  30. ret :='';
  31. while inp[p] = ' ' do inc(p);
  32. while (not (inp[p] in [' ','(',')'])) and (p <= ord(inp[0])) do
  33. begin
  34. ret := ret+inp[p];
  35. inc(p);
  36. end;
  37. While (inp[p] = ' ') and (p <= ord(inp[0])) do inc(p);
  38. GetToken := ret;
  39. end;
  40. {--------------------}
  41. Procedure Create(TR:PBTree);
  42. var
  43. aT : string[20];
  44. begin
  45. tr^.left:=nil;
  46. tr^.right:=nil;
  47. while inp[p] = ' ' do inc(p);
  48. if (copy(inp,p,3)<>'NOT') or
  49. (copy(inp,p,3)<>'not') then
  50. begin
  51. New(Tr^.left);
  52. Tr^.left^.right:=nil;
  53. Tr^.left^.left:=nil;
  54. if inp[p] = '(' then
  55. begin
  56. inc(p);
  57. Create(tr^.left);
  58. end
  59. else
  60. begin
  61. aT:=GetToken;
  62. if (aT<>'NOT') and (aT<>'AND') and (aT <> 'OR') and
  63. (aT<>'not') and (aT<>'and') and (aT <> 'or') then
  64. tr^.left^.info := aT
  65. else
  66. begin
  67. WriteLn('Get operation, when expected operand: ', aT);
  68. Halt(255);
  69. end;
  70. end;
  71. end;
  72. tr^.info := GetToken;
  73. if (tr^.info <> 'AND') and (tr^.info <> 'and') and
  74. (tr^.info <> 'NOT') and (tr^.info <> 'not') and
  75. (tr^.info <> 'OR') and (tr^.info <> 'or') then
  76. begin
  77. WriteLn('Error at pos ',p - Length(tr^.info));
  78. Halt(255);
  79. end;
  80. new(tr^.right);
  81. Tr^.right^.right:=nil;
  82. Tr^.right^.left:=nil;
  83. if (inp[p]='(') then
  84. begin
  85. inc(p);
  86. Create(tr^.right)
  87. end
  88. else
  89. begin
  90. aT:=GetToken;
  91. if aT = '' then
  92. begin
  93. WriteLn('No operand, when expected, pos ',p);
  94. Halt(255);
  95. end;
  96. if ((aT<>'NOT') and (aT<>'AND') and (aT <> 'OR') and
  97. (aT<>'not') and (aT<>'and') and (aT <> 'or')) then
  98. tr^.right^.info := aT
  99. else
  100. begin
  101. WriteLn('Get operation, when expected operand: ', aT);
  102. Halt(255);
  103. end;
  104. end;
  105. if (inp[p]=')') then inc(p);
  106. end;
  107. {---=--=--=-=-=-=-=-=---=-=-=-=-=--}
  108. Procedure DrawTree(aT:PBTree;x,y,h,dy:word);
  109. begin
  110. if aT = nil then exit;
  111. SetTextJustify(CenterText,BottomText);
  112. OutTextXY(x,y,aT^.info);
  113. if aT^.left <> nil then
  114. begin
  115. Line(x,y+1,x-(GetMaxX shr h),y+dy-1-TextHeight(at^.left^.info));
  116. DrawTree(at^.left, x-(GetMaxX shr h),y+dy,h+1,dy);
  117. end;
  118. if aT^.right <> nil then
  119. begin
  120. Line(x,y+1,x+(GetMaxX shr h),y+dy-1-TextHeight(at^.right^.info));
  121. DrawTree(at^.right,x+(GetMaxX shr h),y+dy,h+1,dy);
  122. end;
  123. end;
  124. {=======================}
  125. Function Height(aT:PBTree;ch:byte):byte;
  126. begin
  127. if aT = nil then exit;
  128. Height := Height(at^.left,ch+1);
  129. Height := height(at^.right,ch+1);
  130. if ch > mh then mh := ch;
  131. Height := mh;
  132. end;
  133. {--------------}
  134. var
  135. tree: PBTree;
  136. grD,grM : integer;
  137. Begin
  138. New(Tree);
  139. Tree^.left:=nil;
  140. Tree^.right:=nil;
  141. p:=1;
  142. mh:=0;
  143. Assign(input,'10.txt');
  144. {$I-}
  145. Reset(input);
  146. {$I+}
  147. if IOResult <> 0 then
  148. begin
  149. WriteLN('File 10.txt not found!');
  150. Halt(255);
  151. end;
  152. readln(inp);
  153. Close(Input);
  154. Assign(input,'CON');
  155. Reset(input);
  156. Create(tree);
  157. grD:=Detect;
  158. InitGraph(grD,grM,'');
  159. DrawTree(Tree,GetMaxX div 2,TextHeight(tree^.info)+20,2,(GetMaxY-20) div Height(Tree,1));
  160. ReadLn;
  161. CloseGraph;
  162. DelBranch(tree);
  163. End.