home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Best of German Only 1
/
romside_best_of_german_only_1.iso
/
wissen
/
dos
/
wgraph
/
entpack.exe
/
CALC!.EXE
/
GCALC.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1993-02-06
|
7KB
|
292 lines
UNIT GCalc;
INTERFACE
uses GDecl, {WGRAPH -Units}
GEvent,
GDrivers,
GViews,
GDlg,
graph;
type tCalcState=(calcOK,calcValid,calcError);
PCalculator=^TCalculator;
TCalculator=object(TDlgWindow)
Status:tCalcState;
Zahl:string[15];
Sign:char; {Vorzeichen}
Operator:char;
Operand:real;
constructor Init(var Bounds:TRect;ATitle:str80;AType:byte);
procedure SetPalette; virtual;
procedure InitBackground; virtual;
procedure Berechne(Key:char);
procedure ClearDisplay;
procedure DrawDisplay;
procedure Draw; virtual;
procedure HandleEvent; virtual;
end;
PCalcBackground=^TCalcBackground;
TCalcBackground=object(TBackground)
procedure Draw; virtual;
end;
IMPLEMENTATION
{Implementation TCalculator}
constructor TCalculator.Init(var Bounds:TRect;ATitle:str80;AType:byte);
begin
TDlgWindow.Init(Bounds,ATitle,AType);
DlgInput:=false;
SetPushButton(10,85,22,22,'7',1007);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(35,85,22,22,'8',1008);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(60,85,22,22,'9',1009);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(100,85,22,22,'/',1010);
ChangePalColor(6,Blue);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(128,85,22,22,'C',1011);
ChangePalColor(6,Blue);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(10,110,22,22,'4',1004);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(35,110,22,22,'5',1005);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(60,110,22,22,'6',1006);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(100,110,22,22,'*',1012);
ChangePalColor(6,Blue);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(128,110,22,22,'+',1013);
ChangePalColor(6,Blue);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(10,135,22,22,'1',1001);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(35,135,22,22,'2',1002);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(60,135,22,22,'3',1003);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(100,135,22,22,'-',1014);
ChangePalColor(6,Blue);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(128,135,22,22,'%',1015);
ChangePalColor(6,Blue);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(10,160,22,22,'0',1000);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(35,160,22,22,'.',1016);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(60,160,22,22,'±',1017);
ChangePalColor(6,Blue);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
SetPushButton(100,160,50,22,'=',1018);
ChangePalColor(6,Blue);
ChangePalColor(7,LightGray);
ChangePalColor(8,LightGray);
Zahl:='0'; Sign:=' '; Operand:=0;
Status:=calcOK; Operator:='=';
end;
procedure TCalculator.SetPalette;
begin
Palette:=pal[palRed];
Palette[4]:=#14;
Palette[5]:=#14;
Palette[6]:=#1;
end;
procedure TCalculator.InitBackground;
var RR:TRect;
begin
RR:=Frame^.Area;
Bgrd:=new(PCalcBackground, Init(RR));
List^.InsertItem(Bgrd);
end;
procedure TCalculator.Berechne(Key:char);
var z:real;
procedure Fehler;
begin
Status:=calcError;
Zahl:='ERROR';
Sign:=' ';
end;
procedure CheckFirst;
begin
if Status=calcOK then
begin
Status:=calcValid;
Zahl:='0';
Sign:=' ';
end;
end;
procedure SetDisplay(z:real);
var s:string;
begin
str(z:0:10,s);
if s[1]<>'-' then Sign:=' ' else
begin
delete(s,1,1);
Sign:='-';
end;
if length(s)>15+1+10 then Fehler else
begin
while s[length(s)]='0' do dec(s[0]);
if s[length(s)]='.' then dec(s[0]);
Zahl:=s;
end;
end;
procedure GetDisplay(var z:real);
var err:integer;
begin
val(Sign+Zahl,z,err);
end;
{----------}
begin
Key:=UpCase(Key);
if (Status=calcError) and (Key<>'C') then Key:=' ';
case Key of
'0'..'9' : begin
CheckFirst;
if Zahl='0' then Zahl:='';
Zahl:=Zahl+Key;
end;
'.' : begin
CheckFirst;
if Pos('.',Zahl)=0 then Zahl:=Zahl+'.';
end;
#8,#27 : begin
CheckFirst;
if length(Zahl)=1 then Zahl:='0' else dec(Zahl[0]);
end;
'_',#241 : if Sign=' ' then Sign:='-' else Sign:=' ';
'+','-',
'*','/',
'=','%',
'W',#13 : begin
if Status=calcValid then
begin
Status:=calcOK;
GetDisplay(z);
if Key='%' then
case Operator of
'+','-': z:=Operand*z/100;
'*','/': z:=z/100;
end; {case}
case Operator of
'+':SetDisplay(Operand+z);
'-':SetDisplay(Operand-z);
'*':SetDisplay(Operand*z);
'/':if z=0 then Fehler else SetDisplay(Operand/z);
end; {case}
end;
Operator:=Key;
GetDisplay(Operand);
end;
'C' : ClearDisplay;
end; {case}
end;
procedure TCalculator.ClearDisplay;
var Border:TRect;
begin
Border:=Frame^.Area;
with Border do
begin
SetFillStyle(SolidFill,Yellow);
Bar(A.x+10,A.y+20,B.x-10,A.y+40);
end;
Status:=calcOK;
Zahl:='0';
Sign:=' ';
Operator:='=';
end;
procedure TCalculator.DrawDisplay;
var z:string;
Border:TRect;
begin
Border:=Frame^.Area;
with Border do
begin
SetColor(Black);
z:=Sign+Zahl;
SetTextJustify(RightText,TopText);
Mouse.HideMouse;
SetFillStyle(SolidFill,White);
Bar(A.x+10,A.y+20,B.x-10,A.y+40);
OutTextXY(B.x-16,A.y+27,z);
Mouse.ShowMouse;
SetTextJustify(LeftText,TopText);
end;
end;
procedure TCalculator.Draw;
begin
TDlgWindow.Draw;
DrawDisplay;
end;
procedure TCalculator.HandleEvent;
const KeyCodes:string='0123456789/C*+-%._=';
var i:integer;
begin
TDlgWindow.HandleEvent;
i:=Event.Command-1000;
if i>=0 then
begin
Berechne(char(KeyCodes[i+1]));
DrawDisplay;
end;
if (Event.What=evKeyboard) and (pos(UpCase(Keyb.KeyCode),KeyCodes)<>0) then
begin
Berechne(Keyb.KeyCode);
DrawDisplay;
end;
end;
procedure TCalcBackground.Draw;
begin
with Border do
begin
SetFillStyle(SolidFill,LightGray);
Bar(A.x,A.y,B.x,B.y);
SetFillStyle(SolidFill,White);
Bar(A.x+10,A.y+20,B.x-10,A.y+40);
end;
end;
END.