program kursovaya;
uses crt,graph;
var nPunkt,KeyAscii,i:integer;
    Key:char;
    a,b,c:string;
    Xmin,Xmax,s:real;
    x,y:array[1..235] of real;

procedure About;
 begin
  clrscr;
  Textcolor(3);
  GotoXY (22,5);
  Write('Kursovaya rabota po informatike');
  GotoXY (15,6);
  Write('"Raschet znacheniya funkcii i postroenie ee graphica"');
  Textcolor(5);
  GotoXY (20,8);
  Write ('Programma rasschitivaet funkciu i stroit graphik');
  GotoXY(37, 9);
  Write('3*x^2 -6');
  GotoXY(30, 10);
  Write('y = ----------------');
  GotoXY(37, 11);
  Write('2*x');
  Textcolor(3);
  GotoXY (40,17);
  Write ('Prepodavatel Sluzhenikina N.V.');
  GotoXY (40,18);
  Write('Vipolnila studentka gruppi St-110103');
  GotoXY (40,19);
  Write ('Ogoreltseva Maria');
  GotoXY(27,25);
  Write ('Ekaterinburg 2012');
  Readln;
 end;

procedure Vvod_dannih (var a1,b1,c1:real);
var o,z:integer;
    a,b,c:string;
 begin
  clrscr;
  repeat
  clrscr;

  repeat
  clrscr;
  GotoXY (15,5);
  Textcolor(3);
  Write ('Vvedite nizhni predel Xmin   ');
  Readln (a);
  Val (a,a1,o);
  if o<>0 then begin
  GotoXY (15,15);
  Textcolor(4);
  write('Vveden nechislovoi format. Povtorite vvod');
  readln;
  end;
  until o=0;

  repeat
  clrscr;
  GotoXY (15,6);
  Textcolor(3);
  Write ('Vvedite verhni predel Xmax   ');
  Readln (b);
  Val (b,b1,o);
  if o<>0 then begin
  GotoXY (15,15);
  Textcolor(4);
  write ('Vveden nechislovoi format. Povtorite vvod');
  readln;
  end;
  until o=0;
  if Xmin>Xmax then begin
  GotoXY(15,15);
  Textcolor(4);
  write('Xmax dolzhen bit bolshe Xmin. Povtorite vvod');
  Readln;
  end;
  until Xmin<Xmax;

  repeat
  clrscr;
  GotoXY (15,7);
  Textcolor(3);
  Write ('Vvedite shag izmeneniya funkcii s   ');
  readln(c);
  Val (c,c1,o);
  If o<>0then begin
  GotoXY(15,15);
  Textcolor(4);
  write ('Vveden nechislovoi format. Povtorite vvod');
  readln;
  end;
  If s<=0 then begin
  GotoXY (15,15);
  Textcolor(4);
  write('Shag dolzhen bit bolshe 0. Povtorite vvod');
  readln;
  end;
  If (Xmax-Xmin)<s then begin
  GotoXY(15,15);
  Textcolor(4);
  Write ('Shag dolzhen bit bolshe raznosti Xmax i Xmin. Povtorite vvod');
  Readln;
  end;
  until (o=0) and (s>0) and ((Xmax-Xmin)>s);
  clrscr;
  Textcolor(5);
  GotoXY (30,8);
  write ('Xmin=',a);
  GotoXY (30,10);
  write ('Xmax=',b);
  GotoXY (30,12);
  write ('s=',c);
  Readln;
 end;

 procedure Raschet (Xmin,Xmax,s:real; var x,y:array of real);
 var
 f,i,k:integer;
     m,n: real;
 begin
  clrscr;
  If (Xmin=0) and (Xmax=0) and (s=0) then begin
  GotoXY (30,10);
  Textcolor(4);
  write ('Znachenia ne zadani!');
  end
  else begin
  f:=trunc(((Xmax-Xmin)/s)+1);
  if frac((Xmax-Xmin)/s)<>0 then f:=f+1;
  m:=Xmin;
  k:=1;
  For i:=1 to f do
  begin
   if m=0 then
   begin
    i:=i+1;
    m:=m+s;
    GotoXY(25,k);
    Textcolor(4);
    write ('x=  0    Razriv funkcii');
    k:=k+1;
   end;

   Textcolor(3);
   n:=((3*sqr(m)-6)/(2*m));
   x[i]:=m;
   y[i]:=n;
   GotoXY(25,k);
   writeln ('x=',x[i]:5:2,'  ','y= ',y[i]:5:2);
   m:=m+s;
     if m>Xmax then m:=Xmax;
   k:=k+1;
   If i>24  then readln;
  end;
  end;
  Readln;
 end;

procedure Graphik (Xmin,Xmax:real);
 var grDriver,grMode,yk: Integer;
     j,stepg,i:longint;
     x,y,max:real;
     st,k:string;
begin
grdriver:=detect;
InitGraph(grDriver, grMode,'');
clrscr;
setcolor(white);
Line(290,20,290,400);
Line(40,240,600,240);
Line(290,20,295,25);
Line(290,20,285,25);
Line(600,240,595,245);
Line(600,240,595,235);
OutTextXY(300,250,'0');
OutTextXY(260,20,'y');
OutTextXY(600,245,'x');
SetColor(white);

stepg:=round((xmax-xmin)*100);
max:=(3*xmax*xmax-6)/(xmax*2);
x:=xmin;
for i:=1 to stepg do
begin
 if x<>0 then
 begin
  y:=(3*x*x-6)/(x*2);
  if sqrt(sqr(y))<=sqrt(sqr(max)) then putpixel(round((i/100/(xmax-xmin))*500+40),240-round(y/max*200),8);
 end;
 x:=x+0.01;

 str(Xmin:5:2, k);
 OutTextXY(70, 425, k);
 str(Xmax:5:2, k);
 OutTextXY(525, 425, k);
 SetColor(white);
 OutTextXY(358, 5 , ' 3*x^2-6 ');
 OutTextXY(345, 20, 'y = ------------');
 OutTextXY(358, 35, ' 2*x');
 SetColor(black);
 end;
 Readln;
 CloseGraph;
end;



begin
  clrscr;
  nPunkt:=1;

repeat
  clrscr;
  if nPunkt=1 then Textcolor(3) else TextColor(2);
  GotoXY(35,7);
  Writeln('O programme');
  if nPunkt=2 then Textcolor(3) else TextColor(2);
  GotoXY(35,8);
  Writeln('Vvod dannih');
  if nPunkt=3 then Textcolor(3) else TextColor(2);
  GotoXY(35,9);
  Writeln('Raschet');
  if nPunkt=4 then Textcolor(3) else TextColor(2);
  GotoXY(35,10);
  Writeln('Graphic');
  if nPunkt=5 then Textcolor(3) else TextColor(2);
  GotoXY(35,11);
  Writeln('Vihod');

  Key:=ReadKey;
  KeyAscii:=Ord(key);
  if KeyAscii=0 then
  begin
    Key:=ReadKey;
    KeyAscii:=Ord(Key);
  end;

  if KeyAscii=80 then nPunkt:=nPunkt+1;
  if KeyAscii=72 then nPunkt:=nPunkt-1;

  if nPunkt>5 then nPunkt:=1;
  if nPunkt<1 then nPunkt:=5;

  if KeyAscii=13 then
   begin
    if nPunkt=1 then About;
    if nPunkt=2 then Vvod_dannih (Xmin,Xmax,s);
    if nPunkt=3 then Raschet (Xmin,Xmax,s,x,y);
    if nPunkt=4 then Graphik (Xmin,Xmax);
    if nPunkt=5 then Exit;
   end;
  until (KeyAscii=113) or (KeyAscii=27);
  ReadKey;
  end.


