{$N+} {$G+}
program curve;
uses graph, crt;

    function xvalue(x:double;i,r:integer;s1:string):double;
    var
       j,counter,code,mm,x3:integer;
       xx:double;
       c:char;
       s2:string;
       e: array [1..100] of double;
       d: array [1..100] of integer;
     begin


		 asm
		 lea di,d
		 mov cx,100
		 mov ax,0
		 rep stosw
		 lea di,e
		 mov cx,100
		 mov ax,0
		 rep stosw
		 end;
		 mm:=1;
		 x3:=1;
		 repeat
		 i:=i+3;
		 c:=s1[i];
		 until c=#40;   {(}
		 repeat
		 i:=i+1;
		 c:=s1[i];
		 if (c<=#57) and (c>=#47) then
		 begin
		 j:=i;
	    repeat
	    i:=i+1;
	    c:=s1[i];
	    until (c>#57) or (c<#48) or (c=' ');
		 s2:=copy(s1,j,i-j);
		 val(s2,e[mm],code);
		 mm:=mm+1;
		 c:=s1[i];
		 end;

		 if (c<=#47) and (c>=#42) then
	    begin
	    case c of
	    '+':d[x3]:=1;
	    '-':d[x3]:=2;
	    '*':d[x3]:=3;
	    '/':d[x3]:=4;
	    end;
	    x3:=x3+1;
	    end;

		if c='x' then
		begin
		e[mm]:=x;
		mm:=mm+1;
		end
		 until c=#41;   {)}

asm
mov mm,0
lea si,d
@beg:
add mm,1
push es
pop ds
lodsw
cmp ax,0
je @exit
cmp ax,1
je @beg
cmp ax,2
je @beg
	push ax
	push si
	pop di
	sub di,2
	mov ax,0
	stosw
	mov cx,100
	sub di,2
	rep movsw
	finit
	lea si,e
	mov ax,mm
	mov bx,8
	mul bx
	mov bx,si
	add bx,ax
	fld qword ptr[bx-8]
	fld qword ptr[bx]
	pop ax
	cmp ax,3
	je @m
	cmp ax,4
	je @d
	@m:
	fmul
	jmp @c
	@d:
	fdiv
	jmp @c
	  @c:
	  fstp qword ptr[bx-8]
	  push ds
	  pop es
	  push bx
	  pop di
	  mov ax,0
	  mov cx,4
	  rep stosw
	    mov cx,400
	    push di
	    pop si
	    sub di,8
	    rep movsw
	jmp @beg
	@exit:
		lea si,d
		push es
		pop ds
		lodsw
		cmp ax,0
		je @end
	push ax
	push si
	pop di
	sub di,2
	mov ax,0
	stosw
	mov cx,100
	sub di,2
	rep movsw
	finit
	lea si,e
	fld qword ptr[si]
	fld qword ptr[si+8]
	pop ax
	cmp ax,1
	je @a
	cmp ax,2
	je @s
	@a:
	fadd
	jmp @con
	@s:
	fsub
	jmp @con
	@con:
	  fstp qword ptr[si]
	  push ds
	  pop es
	  push si
	  pop di
	  mov ax,0
	  mov cx,4
	  rep stosw
	    mov cx,400
	    push di
	    pop si
	    sub di,8
	    rep movsw
	jmp @exit
	@end:
	finit
	end;
 xvalue:=e[1];

 end;

    procedure identify (x:double;s1:string;var z:double);

		       function icount (i:integer;s1:string):integer;
		       var
		       c:char;
		 begin
		 repeat
		 c:=s1[i];
		 i:=i+1;
		 until c=#40;   {(}
		 repeat
		 c:=s1[i];
		 i:=i+1;
		 until c=#41;   {)}
		 icount:=i-1;
		 end;



    var s2,s3:string;
    c:char;
    x3,counter,mm,i,i1,j,r,code:integer;
    t1,t2:double;
    t: array [1..100] of double;
    s: array [1..100] of integer;
    label 1,2;
begin
asm
lea di,s
mov cx,100
mov ax,0
rep stosw
lea di,t
mov cx,100
mov ax,0
rep stosw
end;
mm:=1;
i1:=1;
x3:=1;
r:=length(s1);
    repeat
    begin
       c:=s1[i1];
	if c>#65 then   {letters}

	    begin
	    s2:=copy(s1,i1,3);
		begin
	    if s2='cos' then
	    begin
	    t[mm]:=cos(xvalue(x,i1,r,s1));
	    mm:=mm+1;
	    i1:=icount(i1+3,s1); end
	    else
	    if s2='sin' then
	    begin
	    t[mm]:=sin(xvalue(x,i1,r,s1));
	    mm:=mm+1;
	    i1:=icount(i1+3,s1); end
	    else
	    if c='x' then
	    begin
	    t[mm]:=x;
	    mm:=mm+1;
	    end;
		end;
		end
		else

	if (c<=#57) and (c>#47) then
	    begin
	    j:=i1;
	    repeat
	    i1:=i1+1;
	    c:=s1[i1];
	    if c=#46 then
	    begin
	    i1:=i1+1;
	    c:=s1[i1];
	    end;
	    until (c>#57) or (c<=#47) or (c=' ');
	    s2:=copy(s1,j,i1-j);
	    val(s2,t[mm],code);
	    mm:=mm+1;
	    i1:=i1-1;
	    end
	    else

	if (c<=#47) and (c>=#42) then
	    begin
	    case c of
	    '+':s[x3]:=1;
	    '-':s[x3]:=2;
	    '*':s[x3]:=3;
	    '/':s[x3]:=4;
	    end;
	    x3:=x3+1;
	    end;
      end;
      i1:=i1+1;
      until i1>r;



1:
for i1:=1 to r do
case s[i1] of
3: begin t[i1]:=t[i1]*t[i1+1];
	 t[i1+1]:=0;
	 s[i1]:=0;
	 for j:=i1+1 to r do
	 begin
	 t[j]:=t[j+1];
	 s[j-1]:=s[j];
	 s[j]:=0;
	 t[j+1]:=0;
	 end;
	 goto 1
	 end;
4: begin t[i1]:=t[i1]/t[i1+1];
	 t[i1+1]:=0;
	 s[i1]:=0;
	 for j:=i1+1 to r do
	 begin
	 t[j]:=t[j+1];
	 s[j-1]:=s[j];
	 s[j]:=0;
	 t[j+1]:=0;
	 end;
	 goto 1
	 end;
end;

z:=t[1];
for i1:=1 to r do
case s[i1] of
1: z:=z+t[i1+1];
2: z:=z-t[i1+1];
end;

end;


var as1,as2,a,code,coords,counter,i,xt,y,ox,oy,px,py,t,t2,r,mx,my,c,cc,gd,gm: integer;
    b,bb:byte;
    a1,a2,z1,z2,z3,z4,x,x1,k,k1,z,m:double;
    s1,s2,s3,s4,s5: string;



begin
     {
     writeln;
     writeln('You can type 2 functions limited by mathematical operands like');
     writeln('+,-, *, / and you can use two garmonic functions like cos and sin, every');
     writeln('function must be typed correctly, using form exactly. For examle 5*x+cos(2-x/3).');
     readln;
     writeln;
     write('Input function 1 = '); readln(s1);
     write('Input function 2 = '); read(s2);
      write('Input frequency = '); read(t);}
      s1:='cos(2*x)';
      t:=1;
      s5:=s1;





      gd:=detect;
      initgraph (gd,gm,'');
      mx:=getmaxx;
      my:=getmaxy;
      ox:=mx div 2;
      oy:=my div 2;
      {t:=3;}
      m:=9.2*t;
      xt:=29;
      k:=1;
      a2:=10;
      begin
      z1:=0;
      z2:=0;
      z3:=0;
      z4:=0;
      {Now we find maximum value of every functions}
      for i:=0 to 320 do
      begin
      identify(i/m,s1,z);
      z1:=abs(z);
      if abs(z1)>abs(z2) then
      z2:=abs(z1);
      end;
      for i:=0 to 320 do
      begin
      identify(i/m,s2,z);
      z3:=abs(z);
      if abs(z3)>abs(z4) then
      z4:=abs(z3);
      end;
      if z2>z4 then
      a1:=round(z2)
      else
      a1:=round(z4);
      a2:=a1;
      if a2>10 then
      begin
      repeat
      a2:=a2/10;
      k:=k*0.1;
      until a2<=10;
      end;
      if a2<1 then
      a2:=1;
      if (a2>=1) and (a2<=10) then
      begin
	  if (a2>=1) and (a2<2) then k:=k*10
	  else
	  if (a2>=2) and (a2<10) then k:=k
	  else
	  if (a2=10) then k:=k*0.1;
      end;
      end;
      a1:=k*10;
      a2:=1/k;
      setcolor(12);
		{*******************************}
      for cc:=1 to 2 do
      begin
      x:=0;
      identify(x/m,s1,z);
      xt:=0;
      moveto(ox,oy-round(z*a1));

{begin of drawing graphic}
repeat
   begin
	x:=xt;
	identify(x/m,s1,z);
	px:=ox+xt;
	py:=oy-round(a1*z);
	xt:=xt+1;
	lineto(px,py);
   end;
   until px>getmaxx;
   identify(0,s1,z);
   moveto (ox,oy-round(z*a1));
   xt:=0;
   x:=0;
   repeat
   begin
	identify(x/m,s1,z);
	xt:=xt+1;
	x:=-xt;
	px:=ox-xt;
	py:=oy-round(z*a1);
	lineto(px,py);
   end;
   until px<0;
   s1:=s2;
   setcolor(9);
   end;
      setcolor(green);
      line(mx div 2,0,mx div 2,my);
      line(0,my div 2,mx,my div 2);
      xt:=29;
      settextstyle(2,0,4);
      setcolor(11);

{end of drawing graphic}

      {mapping}
      begin
      {Pi}
      outtextxy(ox+t*xt-15*t,355,'Pi');
      line (ox+t*xt,355,ox+t*xt,240);
      line (ox-20,350,ox+t*xt+20,350);
      {Arrows}
      moveto (ox,350);
      for i:=1 to 10 do
      begin
      lineto (ox-i,350-i div 3);
      lineto (ox-i,350+i div 3);
      end;
      moveto (ox+t*xt,350);
      for i:=1 to 10 do
      begin
      lineto (ox+t*xt+i,350-i div 3);
      lineto (ox+t*xt+i,350+i div 3);
      end;
      setlinestyle(2, 0, 3);
      {X coords}
      for i:=0 to getmaxx div xt do
      begin
      setcolor(11);
      circle(ox+i*t*xt,oy,1);
      circle(ox-i*t*xt,oy,1);  {
      setcolor(red);
      circle(ox+i*t*100,oy,1);}
      end;
      setcolor(11);
      {Y coords}
      for i:=0 to getmaxy do
      begin
      circle(ox,oy-i*round(10),1);
      circle(ox,oy+i*round(10),1);
       end;
       for i:=1 to getmaxy do
      begin
      setcolor(red);
      circle(ox,oy-i*round(50),1);
      circle(ox,oy+i*round(50),1);
       end;
       circle(ox,oy-1*round(10),1);
      circle(ox,oy+1*round(10),1);
      setcolor(11);
      {Text about maximums}
      str(round(a2), s3);
      str(round(-a2),s4);
      outtextxy(336,oy-10-6,s3);
      outtextxy(330,oy+10-6,s4);
      str(round(a2*10), s3);
      str(round(-a2*10),s4);
      outtextxy(336,oy-100-6,s3);
      outtextxy(330,oy+100-6,s4);
      str(round(a2*20), s3);
      str(round(-a2*20),s4);
      outtextxy(336,oy-200-6,s3);
      outtextxy(330,oy+200-6,s4);
      if length(s5)>0 then
      begin
      str(z2:4:4,s3);
      s4:='Maximum value of function:';
      s3:='is '+s3;
      outtextxy(10,10,s4);
      setcolor(12);
      outtextxy(10,20,s5);
      setcolor(11);
      outtextxy(10,30,s3);
      end;
      if length(s2)>0 then
      begin
      str(z4:4:4,s3);
      s3:='is '+s3;
      outtextxy(10,60,s4);
      setcolor(9);
      outtextxy(10,70,s2);
      setcolor(11);
      outtextxy(10,80,s3);
      end;
      end;
   readln;
   readln;
   cleardevice;
   closegraph;
   end.