program pro5;
uses crt,graph;
type arr=array[1..4] of string;
const a1: arr=('Ї а ¬Ґва','в Ў«Ёж  ','Ја дЁЄ  ','ўл室 ');
xn:real=-19; xk:real=25; dx:real=1;
var b:real;
mas1,mas2,mas3:array[1..60] of real;
graphdriver,graphmode,bp:integer; k:byte;
ch:char;

function firstfunc(x:real):real;
var fraction,f1:real; i,j:integer; fact:word;
begin
fraction:=x; f1:=x; fact:=2;
for i:=1 to 550 do begin
fraction:=-fraction*x*x/((2*i)*(2*i+1));
if abs(fraction)<0.0001 then break;
f1:=f1+fraction;
end;
firstfunc:=f1;
end;

function secfunc(x,b:real):real;
begin
secfunc:=sin(x-b);
end;

procedure environment(k,j:byte);
const c1:byte=7; c2:byte=2; c3:byte=0; var i:byte;
begin
textbackground(1);
case k of
0:begin
clrscr;
textbackground(7);textcolor(0);
gotoxy(20,10); writeln(' ЙНННННННННННННННННННННННННННН»');
gotoxy(20,11); writeln(' є ўлЎҐаЁвҐ ­г¦­л© Їг­Єв ¬Ґ­о є');
gotoxy(20,12); writeln(' МНННННННННННННННННННННННННННН№');
gotoxy(20,13); writeln(' є 1 ЙНННННННН» є');
gotoxy(20,14); writeln(' є 2 є є є');
gotoxy(20,15); writeln(' є 3 ИННННННННј є');
gotoxy(20,16); writeln(' є 4 є');
gotoxy(20,17); writeln(' ИННННННННННННННННННННННННННННј');
end;
1:begin
textcolor(0); textbackground(7);
for i:=1 to 4 do begin
gotoxy(26,12+i); write(a1[i]);
end;
textbackground(c2);
textcolor(c3); gotoxy(36,10+i); write(a1[j]);
end;
end;
textcolor(c2);textbackground(0);
end;

procedure parameters;
procedure calculation(c:real);
var i:byte; x:real;
begin
x:=xn;
for i:=1 to 48 do begin
mas1[i]:=firstfunc(x);
mas2[i]:=secfunc(x,c);
mas3[i]:=x;
x:=x+dx;
if x>=xk+1 then break;
end;
end;

procedure parameterscales;
begin
if (abs(b)>19) and (abs(b)<99) then bp:=10
else if (abs(b)>99) and (abs(b)<999) then bp:=100
else bp:=1;
end;

begin
textbackground(1);
clrscr;
textbackground(6);textcolor(0);
gotoxy(20,10);writeln(' ЙНННННННННННННННННННННННННННН»');
gotoxy(20,11);writeln(' є ўўҐ¤ЁвҐ §­ зҐ­ЁҐ Ї а ¬Ґва  є');
gotoxy(20,12);writeln(' МНННННННННННННННННННННННННННН№');
gotoxy(20,13);writeln(' є b= є');
gotoxy(20,14);writeln(' ИННННННННННННННННННННННННННННј');
gotoxy(36,13);
read(b);
calculation(b);
parameterscales;
readkey;

end;

procedure make;
var f:text; i:byte;
begin
textbackground(4);textcolor(0);
clrscr;
assign(f,'exp5_res.txt');
rewrite(f);
gotoxy(1,1);
writeln('ЙНННННННННННННННННННННН»');
writeln('є x є sin(x) єsin(x-b)є');
writeln('єННННєННННННННєННННННННє');
writeln(f,'ЙНННННННННННННННННННННН»');
writeln(f,'є x є sin(x) єsin(x-b)є');
writeln(f,'єННННєННННННННєННННННННє');
for i:=4 to 19 do begin
writeln('є ',mas3[i]:3:0,'є',mas1[i]:8:2,'є',mas2[i]:8:2,'є');
writeln(f,' ',mas3[i]:3:0,'є',mas1[i]:8:2,'є',mas2[i]:8:2);
end;
gotoxy(1,20); writeln('ИННННННННННННННННННННННј');
for i:=20 to 35 do begin
gotoxy(35,1);writeln('ЙНННННННННННННННННННННН»');
gotoxy(35,2);writeln('є x є sin(x) єsin(x-b)є ');
gotoxy(35,3);writeln('єННННєННННННННєННННННННє ');
gotoxy(35,i-16);
writeln('є ',mas3[i]:3:0,'є',mas1[i]:8:2,'є',mas2[i]:8:2,'є');
writeln(f,' ',mas3[i]:3:0,'є',mas1[i]:8:2,'є',mas2[i]:8:2);
gotoxy(35,20); writeln('ИННННННННННННННННННННННј');
writeln(' ­ ¦¬ЁвҐ Є« ўЁиг ¤«п Їа®¤®«¦Ґ­Ёп... ');
end;
close(f);
readkey;
end;

procedure graphics;
const c1:byte=2; c2:byte=6;
var y,x:longint; st:string[3]; i,l:byte;
begin
if bp=0 then exit;
initgraph(graphdriver,graphmode,'c:\tp\bgi');
setviewport(0,0,460,410,true);
setcolor(3);
outtextxy(120,1,'function graphics');
str(bp,st); outtextxy(156,188,st);

line(1,204,450,204); line(450,204,445,199); line(450,204,445,209);
line(180,15,180,350);line(180,15,176,19); line(180,15,184,19);
outtextxy(170,208,'o'); outtextxy(188,210,'l');
outtextxy(165,15,'y'); outtextxy(450,214,'x');

y:=0; l:=1;
while (y<450) do begin
line(y,202,y,206);
inc(y,10);
end;

y:=24;
while (y<350) do begin
line(178,y,182,y);
inc(y,10);
end;
setcolor(c1); outtextxy(295,300,'y=f(x)');

for i:=1 to 45 do begin
y:=trunc(205-mas1[i]);
if (y>=0) and (y<=400) then begin
y:=trunc(205-mas1[i]);
moveto(trunc(180+mas3[i]*10),y); l:=i+2; break;
end;
end;

for i:=1 to 45 do begin
y:=trunc(205-mas1[i]*10/bp);
x:=180+trunc(mas3[i]*10);
if x>450 then break;
if y>400 then begin lineto(x,400); break; end;
if y<0 then begin lineto(x,0); break; end;
lineto(x,y);
end;

setcolor(c2);
outtextxy(395,320,'y=y(x)');

for i:=1 to 45 do begin
y:=trunc(205-mas2[i]*10/bp);
if (y>0) and (y<=400) then begin
y:=trunc(205-mas2[i]*10/bp);
moveto(trunc(180+mas3[i]*10),y); l:=i+2; break;
end;
end;

for i:=L to 45 do begin
y:=trunc(205-mas2[i]*10/bp);
x:=180+trunc(mas3[i])*10;
if x>450 then break;
if y>400 then begin lineto(x,400); break; end;
if y<0 then begin lineto(x,0); break; end;
lineto(x,y);
end;

setcolor(7);
outtextxy(5,360,'­ ¦¬ЁҐ Є« ўЁиг ¤«п Їа®¤®«¦Ґ­Ёп');
readkey;
cleardevice; closegraph;
end;

procedure out;
begin
textcolor(7); textbackground(0); clrscr; halt;
end;

begin
clrscr;
graphdriver:=detect;
graphmode:=2;
k:=1;
environment(0,0);
environment(1,k);
while true do begin
if keypressed then begin
ch:=readkey;
case ord(ch) of
72: begin dec(k); if k=0 then k:=4; environment(1,k); end;
80: begin inc(k); if k=5 then k:=1; environment(1,k); end;
13: begin
case k of
1: parameters;
2: make;
3: graphics;
4: out;
end;
environment(0,0); environment(1,k);
end;
end;
end;
end;
end.