PROGRAM calculator(input,output);
USES crt,graph;
VAR
stack:array[1..100] of string;
a,b,i,j,gd,gm,top,code:integer;
exp,c,x:string;
flag:boolean;
e,f:real;
PROCEDURE help;
BEGIN
setcolor(lightcyan);
outtextxy(30,357,'''a'' -> ALL CLEAR');
outtextxy(450,357,'''+'' -> ADD');
outtextxy(30,321,'''c'' -> CLEAR');
outtextxy(450,321,'''-'' -> SUBTRACT');
outtextxy(30,285,'''s'' -> SQUARE ROOT');
outtextxy(450,285,'''x'' -> MULTIPLY');
outtextxy(30,249,'''%'' -> PERCENTAGE');
outtextxy(450,249,'''/'' -> DIVIDE');
outtextxy(150,410,'''.'' ->DECIMAL POINT ; ''='' ->EQUAL TO');
END;
PROCEDURE draw;
BEGIN
rectangle(190,90,410,390);
rectangle(200,110,400,150);
for i:=1 to 5 do
for j:=1 to 5 do
begin
a:=(j-1)*42;
b:=(i-1)*36;
rectangle(200+a,200+b,230+a,230+b);
end;
setcolor(lightblue);
for i:=1 to 3 do
for j:=1 to 3 do
begin
str((i-1)*3+j,c);
outtextxy(252+(j-1)*42,321-(i-1)*36,c);
end;
outtextxy(254,357,'0');
outtextxy(296,354,'.');
outtextxy(211,249,'%');
outtextxy(207,211,'+');
outtextxy(210,214,'/');
outtextxy(214,216,'-');
outtextxy(338,357,'=');
outtextxy(380,357,'+');
outtextxy(380,321,'-');
outtextxy(380,285,'x');
outtextxy(380,249,'Ä');
outtextxy(380,243,'.');
outtextxy(380,252,'.');
outtextxy(211,321,'C');
outtextxy(208,357,'AC');
outtextxy(208,285,'û');
outtextxy(214,282,'-');
help;
setcolor(white);
outtextxy(20,460,'Press spacebar to exit');
END;
PROCEDURE calc;
BEGIN
if c ='+' then e:=f+e
else if c='-' then e:=f-e
else if c='x' then e:=f*e
else if c='/' then if e=0 then c:='-E-' else e:=f/e;
END;
PROCEDURE push;
BEGIN
top:=top+1;
stack[top]:=c;
END;
PROCEDURE pop;
BEGIN
x:=stack[top];
top:=top-1;
END;
PROCEDURE popping;
BEGIN
pop;
val(x,e,code);
pop;
c:=x;
pop;
val(x,f,code);
END;
BEGIN
gd:=detect;
initgraph(gd,gm,'c:\turbo\bgi');
setpalette(0,lightred);
draw;
setviewport(205,112,397,148,clipon);
settextstyle(sansseriffont,horizdir,3);
outtextxy(173,1,'0');
a:=176;
i:=0;
top:=0;
c:='';
for j:=1 to 20 do
exp[j]:='0';
repeat
i:=i+1;
exp[i]:=readkey;
CASE exp[i] OF
'0'..'9','.':begin
if flag=true then begin
clearviewport;
flag:=false;
end;
if i<13 then begin
if (stack[top]='s') or (stack[top]='S') then
begin
c:='';
pop;
end;
c:=c+exp[i];
setviewport(205,112,395,148,clipon);
clearviewport;
outtextxy(a,1,exp[i]);
for j:=(i-1) downto 1 do
outtextxy(a-16*(i-j),1,exp[j]);
end;
end;
'=','+','-','x','/','S','s','%':begin
push;
CASE exp[i] OF
'S','s':begin
val(c,e,code);
e:=sqrt(e);
str(e:1:5,c);
b:=length(c);
clearviewport;
outtextxy(a-16*(b-1),1,c);
i:=0;
end;
'=':;
'%':begin
popping;
if e=0 then
c:='-E-'
else begin
e:=(f/e)*100;
str(e:1:5,c);
end;
b:=length(c);
clearviewport;
outtextxy(a-16*(b-1),1,c);
i:=0;
end;
else
begin
c:=exp[i];
push;
flag:=true;
c:='';
i:=0;
end;
END;
end;
'C','c':begin
val(copy(c,1,1),j,code);
if j in [0..9] then begin
c:='';
i:=0;
end;
clearviewport;
outtextxy(a,1,'0');
end;
'a','A':begin
while top<>0 do pop;
c:='';
i:=0;
clearviewport;
outtextxy(a,1,'0');
end;
else i:=i-1;
END;
if exp[i]='=' then begin
popping;
calc;
clearviewport;
if c<>'-E-' then
if frac(e)=0 then str(e:1:0,c) else str(e:1:5,c);
b:=length(c);
outtextxy(a-16*(b-1),1,c);
i:=0;
end;
until exp[i+1]=' ';
closegraph;
END.