larg.pas 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. Uses CRT,GRAPH;
  2. Const
  3. MinX : real = -1;
  4. MaxX : real = 9;
  5. MinY : real = MaxInt;
  6. MaxY : real = -MaxInt;
  7. {---------------------}
  8. LagrTable = 10;
  9. {---------------------}
  10. CStep = 1000;
  11. TVer = 0.01;
  12. ZX : word = 250;
  13. ZY : word = 200;
  14. Left = 30;
  15. Right = 400;
  16. Top = 20;
  17. Bottom = 400;
  18. {-----------------------------}
  19. Type
  20. LD = array [0..LagrTable] of record
  21. x,y : real;
  22. end;
  23. TB = array [0..LagrTable] of boolean;
  24. var
  25. Thresh : real;
  26. Pts : LD;
  27. BegiP,
  28. EndiP : TB;
  29. Function Func(x: real): real;
  30. Begin
  31. { Func := ln(abs(x));}
  32. { Func := Sin(x)*x;}
  33. Func := 1.4*x+2;
  34. { Func := X*X/10 * sin (x/2)}
  35. End;
  36. {-----------------------------}
  37. Function Lagr(X:real; var T : TB):real;
  38. var
  39. i,j,n : word;
  40. c,ret : real;
  41. begin
  42. n := LagrTable;
  43. ret:=0;
  44. for i := 0 to n do if T[i] then
  45. begin
  46. c:=1;
  47. for j := 0 to n do
  48. if (j <> i) AND T[j] then C := C * (X-Pts[j].X) / (Pts[i].X-Pts[j].X);
  49. ret := ret + Pts[i].Y*C;
  50. end;
  51. Lagr := ret;
  52. end;
  53. {-----------------------------}
  54. Procedure SetMinMax(var T:tb);
  55. var
  56. c:real;
  57. i:word;
  58. begin
  59. if MaxX < MinX then
  60. begin
  61. C := MaxX;MaxX := MinX;MinX := C;
  62. end;
  63. for i := 0 to Cstep do
  64. begin
  65. C := Lagr(MinX+i/CStep*(MaxX-MinX),T);
  66. if C > MaxY then MaxY:=C;
  67. if C < MinY then MinY:=C;
  68. end;
  69. end;
  70. {-----------------------------}
  71. Procedure InitG;
  72. Var
  73. grDriver: Integer;
  74. grMode: Integer;
  75. i : integer;
  76. Begin
  77. grDriver := Detect;
  78. InitGraph(grDriver, grMode,'');
  79. if GraphResult <> grOk then
  80. begin
  81. WriteLn('Graph error!');
  82. Halt(255);
  83. end;
  84. End;{InitG}
  85. {---------------------------------}
  86. Procedure DrawAxis;
  87. var
  88. i : integer;
  89. s : string;
  90. begin
  91. if (MinX < 0) AND (MaxX > 0) then
  92. begin
  93. ZX := round(Left + abs(MinX)/(MaxX-MinX)*(Right-Left));
  94. end
  95. else
  96. if MinX >= 0 then ZX := Left
  97. else ZX := Right;
  98. if (MinY < 0) AND (MaxY > 0) then
  99. begin
  100. ZY := round(Bottom - abs(MinY)/(MaxY-MinY)*(Bottom-Top));
  101. end
  102. else
  103. if MinY >= 0 then ZY := Bottom
  104. else ZY := Top;
  105. SetColor(White);
  106. Line(Left,ZY,Right+10,ZY);
  107. Line(Right-5,ZY-5,Right+10,ZY);
  108. Line(Right-5,ZY+5,Right+10,ZY);
  109. Line(ZX,Top-10,ZX,Bottom);
  110. Line(ZX,Top-10,ZX-5,Top+5);
  111. Line(ZX,Top-10,ZX+5,Top+5);
  112. OutTextXY(ZX-10,ZY-10,'0');
  113. OutTextXY(ZX-15,Top,'Y');
  114. OutTextXY(Right,ZY+10,'X');
  115. Str(MaxX:0:2,S);
  116. OutTextXY(Right,ZY-10,S);
  117. Str(MinX:0:2,S);
  118. if MinX<0 then OutTextXY(Left,ZY-10,S);
  119. Str(MaxY:0:2,S);
  120. OutTextXY(ZX+10,Top,S);
  121. Str(MinY:0:2,S);
  122. if MinY<0 then OutTextXY(ZX+10,Bottom,S);
  123. end;
  124. {--------------------}
  125. Procedure LoadData;
  126. var
  127. i : longint;
  128. begin
  129. for i := 0 to LagrTable do
  130. begin
  131. Pts[i].x := MinX + i/LagrTable*(MaxX-MinX);
  132. Pts[i].y := Func(Pts[i].x);
  133. BegiP[i] := true;
  134. EndiP[i] := true;
  135. end;
  136. end;
  137. {--------------------}
  138. Procedure FillTable(var T : TB);
  139. var
  140. i:word;
  141. ax,ay : string;
  142. begin
  143. SetFillStyle(SolidFill,Black);
  144. Bar(440,30,630,380);
  145. SetColor(Cyan);
  146. Rectangle(440,30,630,380);
  147. SetColor(Blue);
  148. for i := 0 to LagrTable do if T[i] then
  149. begin
  150. Str(Pts[i].x:0:2,aX); aX:='X'+Chr(i+$30)+'='+aX;
  151. Str(Pts[i].y:0:2,aY); aY:='Y'+Chr(i+$30)+'='+aY;
  152. SetTextJustify(LeftText,CenterText);
  153. OutTextXY(450,round(50 + i/LagrTable*300),aX);
  154. OutTextXY(550,round(50 + i/LagrTable*300),aY);
  155. end;
  156. end;
  157. {-----------------------------}
  158. Procedure DrawLagr(var T : TB);
  159. Var
  160. C : real;
  161. i : word;
  162. x, y : real;
  163. Begin
  164. if (MaxY-MinY) > (MaxX-MinX) then
  165. C := (Bottom-Top)/(MaxY-MinY)
  166. else
  167. C := (Right-Left)/(MaxX-MinX);
  168. MoveTo(round(ZX+MinX*C),round(ZY-Lagr(MinX,T)*C));
  169. for i := 0 to CStep do
  170. begin
  171. x:=MinX + i/CStep*(MaxX-MinX);
  172. y:=Lagr(X,T);
  173. LineTo(round(ZX+X*C),round(ZY-Y*C));
  174. end;
  175. End;{DrawLagr}
  176. {-------------}
  177. Function Check(var T1, T2 : TB):boolean;
  178. var
  179. i : word;
  180. x, y1, y2 : real;
  181. Begin
  182. Check := true;
  183. for i := 0 to CStep do
  184. begin
  185. x:=MinX + i/CStep*(MaxX-MinX);
  186. y1:=Lagr(X,T1);
  187. y2:=Lagr(X,T2);
  188. if abs(y1-y2) > Thresh then
  189. begin
  190. Check := false;
  191. Exit;
  192. end;
  193. end;
  194. end;
  195. var
  196. CKick : word;
  197. ifDel : boolean;
  198. cS : word;
  199. Begin
  200. LoadData;
  201. InitG;
  202. SetMinMax(BegIp);
  203. Thresh := TVer * (MaxY-MinY);
  204. DrawAxis;
  205. { WriteGraph(CStep);}
  206. FillTable(BegIp);
  207. SetColor(Green);
  208. DrawLagr(BegIp);
  209. CKick :=0;
  210. repeat
  211. ifDel := false;
  212. for cS := 0 to LagrTable do if EndiP[cS] then
  213. begin
  214. EndiP[cS] := false;
  215. if Check(BegiP,EndiP) then
  216. begin
  217. inc(CKick);
  218. ifDel := true;
  219. end
  220. else
  221. EndiP[cs] := true;
  222. end;
  223. until not ifDel;
  224. ReadKey;
  225. FillTable(EndiP);
  226. SetColor(White);
  227. DrawLagr(EndiP);
  228. ReadLn;
  229. CloseGraph;
  230. WriteLn('‚모­ãâ® â®ç¥ª - ',CKick);
  231. End.