Sandstorm
2009-03-02 18:45:21
Yea. I was bored, so I made a scar calculator.
Any suggestions? Works pretty well, can do decimals and it won't screw up at a division by zero.
Let me know what you think!
~Sandstorm
Any suggestions? Works pretty well, can do decimals and it won't screw up at a division by zero.
Let me know what you think!
program Calculator;
var
EndItemForm : Boolean;
ItemIdform : TForm;
Edit1 : TEdit;
Button1, Button2, Button3, Button4,
Button5, Button6, Button7, Button8,
Button9, Button10, Button11, Button12,
Button13, Button14, Button15, Button16,
Button20, Button21, Button22, SinB,
CosB, TanB, MemS, MemD, MemR : TButton;
Num, T : Integer;
Sign : Array[0..100000] Of Integer;
Number : Array[0..100000] Of Extended;
procedure ItemFormOnClose(Sender : TObject; var Action : TCloseAction);
begin
if (not(ItemIdform.ModalResult = 1)) then
EndItemForm := True;
end;
function IsScarActive: Boolean;
begin
Result:= GetApplication.Active;
end;
Procedure NumbersClick(Sender : TObject);
Begin
If (Edit1.Text = ' 0.') Then
Edit1.Text := '';
Case Sender Of
Button1 : Edit1.Text := Edit1.Text + '1';
Button2 : Edit1.Text := Edit1.Text + '2';
Button3 : Edit1.Text := Edit1.Text + '3';
Button5 : Edit1.Text := Edit1.Text + '4';
Button6 : Edit1.Text := Edit1.Text + '5';
Button7 : Edit1.Text := Edit1.Text + '6';
Button9 : Edit1.Text := Edit1.Text + '7';
Button10 : Edit1.Text := Edit1.Text + '8';
Button11 : Edit1.Text := Edit1.Text + '9';
Button13 : Edit1.Text := Edit1.Text + '0';
Button20 : Edit1.Text := Edit1.Text + '.';
End;
End;
Procedure Clear(Sender : TObject);
Var
I : Integer;
Begin
Edit1.Text := ' 0.';
For I := 0 To T + 1 Do
Number[I] := 0;
For I := 0 To Num + 1 Do
Sign[I] := 0;
Num := 0;
T := 0;
End;
Procedure SQR(Sender : TObject);
Begin
Edit1.Text := FloatToStr(Sqrt(StrToFloat(Edit1.Text)));
End;
Procedure SinCosTan(Sender : TObject);
Begin
Case Sender Of
SinB : Edit1.Text := FloatToStr(Sin(StrToFloat(Edit1.Text)));
CosB : Edit1.Text := FloatToStr(Cos(StrToFloat(Edit1.Text)));
TanB : Edit1.Text := FloatToStr(Tan(StrToFloat(Edit1.Text)));
End;
End;
Function Divisionzzz : Boolean;
Var
I : Integer;
Begin
For I := 0 To T - 1 Do
Begin
Wait(1);
If (Sign[I] = 1) and (Number[I + 1] = 0) Then
Begin
Result := True;
Exit;
End;
End;
End;
Procedure Solve(Sender : TObject);
Var
I, C, H : Integer;
Begin
Number[T] := StrToFloat(Edit1.Text);
If Divisionzzz Then
Begin
Edit1.Text := 'Division by Zero!';
Exit;
End;
For I := 0 To T Do
Begin
If I = 0 Then
Begin
Case Sign[I] Of
1 : Edit1.Text := FloatToStr(Number[I] / Number[i + 1]);
2 : Edit1.Text := FloatToStr(Number[I] * Number[i + 1]);
3 : Edit1.Text := FloatToStr(Number[I] - Number[i + 1]);
4 : Edit1.Text := FloatToStr(Number[I] + Number[i + 1]);
5 : Begin
H := StrToIntDef(Edit1.Text, 3);
Edit1.Text := FloatToStr(Number[I] * Number[0]);
For C := 0 To H - 2 Do
Edit1.Text := FloatToStr(StrToFloat(Edit1.Text) * Number[0]);
Exit;
End;
End;
End else
Begin
Case Sign[I] Of
1 : Edit1.Text := FloatToStr(StrToFloat(Edit1.Text) / Number[i + 1]);
2 : Edit1.Text := FloatToStr(StrToFloat(Edit1.Text) * Number[i + 1]);
3 : Edit1.Text := FloatToStr(StrToFloat(Edit1.Text) - Number[i + 1]);
4 : Edit1.Text := FloatToStr(StrToFloat(Edit1.Text) + Number[i + 1]);
End;
End;
End;
End;
Procedure SetOperation(Sender : TObject);
Begin
Case Sender Of
Button4 : Begin
Sign[Num] := 1;
Number[T] := StrToFloat(Edit1.Text);
Edit1.Text := '';
Inc(Num);
Inc(T);
End;
Button8 : Begin
Sign[Num] := 2;
Number[T] := StrToFloat(Edit1.Text);
Edit1.Text := '';
Inc(Num);
Inc(T);
End;
Button12 : Begin
Sign[Num] := 3;
Number[T] := StrToFloat(Edit1.Text);
Edit1.Text := '';
Inc(Num);
Inc(T);
End;
Button16 : Begin
Sign[Num] := 4;
Number[T] := StrToFloat(Edit1.Text);
Edit1.Text := '';
Inc(Num);
Inc(T);
End;
Button22 : Begin
Sign[Num] := 5;
Number[T] := StrToFloat(Edit1.Text);
Edit1.Text := '';
Inc(Num);
Inc(T);
End;
End;
End;
Procedure MemoryChange(Sender : TObject);
Var
I, T : Integer;
H : String;
C : Array of Integer;
Begin
Case Sender Of
MemD : Begin
C := [0, 1, 2];
If (InIntArray(C, StrToInt(Edit1.Text))) Then
Begin
DeleteIni('Memory' + GetNumbers(Edit1.Text), 'Num', ScriptPath + 'Memory.ini');
Exit;
End;
For I := 0 To 2 Do
Begin
H := ReadIni('Memory' + GetNumbers(Edit1.Text), 'Num', ScriptPath + 'Memory.ini');
If H <> '' Then
DeleteIni('Memory' + GetNumbers(Edit1.Text), 'Num', ScriptPath + 'Memory.ini')
Else
Inc(T);
End;
If T = 3 Then
Edit1.Text := 'No numbers found in memory!';
End;
MemR : Begin
C := [0, 1, 2];
WriteLn(Edit1.Text);
If (InIntArray(C, StrToInt(Edit1.Text))) Then
Readini('Memory' + GetNumbers(Edit1.Text), 'Num', ScriptPath + 'Memory.ini');
For I := 0 To 2 Do
Begin
H := ReadIni('Memory' + GetNumbers(Edit1.Text), 'Num', ScriptPath + 'Memory.ini');
If H <> '' Then
Edit1.Text := H
Else
Inc(T);
End;
If T = 3 Then
Edit1.Text := 'No numbers found in memory!';
End;
MemS : Begin
For I := 0 To 3 Do
Begin
H := ReadIni('Memory' + IntToStr(I), 'Num', ScriptPath + 'Memory.ini');
If H <> '' Then
Begin
Inc(T);
End Else
Begin
Writeini('Memory' + IntToStr(I), 'Num', Edit1.Text, ScriptPath + 'Memory.ini');
Break;
End;
If T >= 3 Then
Begin
Writeini('Memory' + IntToStr(0), 'Num', Edit1.Text, ScriptPath + 'Memory.ini');
Break;
End;
End;
End;
End;
End;
procedure ItemForm;
var
TimeItemForm : Integer;
begin
TimeItemForm := GetSystemTime;
ItemIdform := CreateForm;
with ItemIdform do
begin
OnClose := @ItemFormOnClose;
BorderStyle := bsSingle;
Position := poScreenCenter;
BorderIcons := [biMinimize,biSystemMenu];
ClientWidth := 10 + 10 + 5 + 5 + 10 + 135;
ClientHeight := 260;
Caption := 'Calc';
Color := 14215660;
Font.Color := clWindowText;
Font.Height := -11;
Font.Name := 'MS Sans Serif';
Font.Style := [];
PixelsPerInch := 96;
end;
Edit1 := TEdit.Create(ItemIdform);
with Edit1 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 8;
Top := 8;
Width := 121;
Height := 21;
TabOrder := 8;
Text := ' 0.';
end;
Button1 := TButton.Create(ItemIdform);
with Button1 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 8;
Top := 72;
Width := 25;
Height := 25;
Caption := '1';
TabOrder := 9;
OnClick := @NumbersClick;
end;
SinB := TButton.Create(ItemIdform);
with SinB do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 8;
Top := 230;
Width := 30;
Height := 25;
Caption := 'Sine';
TabOrder := 9;
OnClick := @SinCosTan;
end;
CosB := TButton.Create(ItemIdform);
with CosB do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 40;
Top := 230;
Width := 40;
Height := 25;
Caption := 'Cosine';
TabOrder := 9;
OnClick := @SinCosTan;
end;
TanB := TButton.Create(ItemIdform);
with TanB do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 80;
Top := 230;
Width := 50;
Height := 25;
Caption := 'Tangent';
TabOrder := 9;
OnClick := @SinCosTan;
end;
Button2 := TButton.Create(ItemIdform);
with Button2 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 40;
Top := 72;
Width := 25;
Height := 25;
Caption := '2';
TabOrder := 10;
OnClick := @NumbersClick;
end;
Button3 := TButton.Create(ItemIdform);
with Button3 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 72;
Top := 72;
Width := 25;
Height := 25;
Caption := '3';
TabOrder := 11;
OnClick := @NumbersClick;
end;
Button4 := TButton.Create(ItemIdform);
with Button4 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 104;
Top := 72;
Width := 25;
Height := 25;
Caption := '/';
TabOrder := 12;
OnClick := @SetOperation;
end;
Button5 := TButton.Create(ItemIdform);
with Button5 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 8;
Top := 104;
Width := 25;
Height := 25;
Caption := '4';
TabOrder := 13;
OnClick := @NumbersClick;
end;
Button6 := TButton.Create(ItemIdform);
with Button6 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 40;
Top := 104;
Width := 25;
Height := 25;
Caption := '5';
TabOrder := 14;
OnClick := @NumbersClick;
end;
Button7 := TButton.Create(ItemIdform);
with Button7 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 72;
Top := 104;
Width := 25;
Height := 25;
Caption := '6';
TabOrder := 15;
OnClick := @NumbersClick;
end;
Button8 := TButton.Create(ItemIdform);
with Button8 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 104;
Top := 104;
Width := 25;
Height := 25;
Caption := '*';
TabOrder := 16;
OnClick := @SetOperation;
end;
Button9 := TButton.Create(ItemIdform);
with Button9 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 8;
Top := 136;
Width := 25;
Height := 25;
Caption := '7';
TabOrder := 17;
OnClick := @NumbersClick;
end;
Button10 := TButton.Create(ItemIdform);
with Button10 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 40;
Top := 136;
Width := 25;
Height := 25;
Caption := '8';
TabOrder := 18;
OnClick := @NumbersClick;
end;
Button11 := TButton.Create(ItemIdform);
with Button11 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 72;
Top := 136;
Width := 25;
Height := 25;
Caption := '9';
TabOrder := 19;
OnClick := @NumbersClick;
end;
Button12 := TButton.Create(ItemIdform);
with Button12 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 104;
Top := 136;
Width := 25;
Height := 25;
Caption := '-';
TabOrder := 20;
OnClick := @SetOperation;
end;
Button13 := TButton.Create(ItemIdform);
with Button13 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 8;
Top := 168;
Width := 25;
Height := 25;
Caption := '0';
TabOrder := 21;
OnClick := @NumbersClick;
end;
Button14 := TButton.Create(ItemIdform);
with Button14 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 104;
Top := 168;
Width := 25;
Height := 25;
Caption := '=';
TabOrder := 22;
OnClick := @Solve;
end;
Button15 := TButton.Create(ItemIdform);
with Button15 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 8;
Top := 40;
Width := 89;
Height := 25;
Caption := 'Clear';
TabOrder := 23;
OnClick := @Clear;
end;
Button16 := TButton.Create(ItemIdform);
with Button16 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 104;
Top := 40;
Width := 25;
Height := 25;
Caption := '+';
TabOrder := 24;
OnClick := @SetOperation;
end;
Button20 := TButton.Create(ItemIdform);
with Button20 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 40;
Top := 168;
Width := 58;
Height := 25;
Caption := 'Decimal';
TabOrder := 24;
OnClick := @NumbersClick;
end;
Button21 := TButton.Create(ItemIdform);
with Button21 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 5;
Top := 200;
Width := 68;
Height := 25;
Caption := 'Square Root';
TabOrder := 24;
OnClick := @SQR;
end;
Button22 := TButton.Create(ItemIdform);
with Button22 do
begin
Parent := ItemIdform;
Left := 10 + 10 + 5 + 5 + 10 + 75;
Top := 200;
Width := 60;
Height := 25;
Caption := 'Exponent';
TabOrder := 24;
OnClick := @SetOperation;
end;
MemD := TButton.Create(ItemIdform);
with MemD do
begin
Parent := ItemIdform;
Left := 3;
Top := 72;
Width := 40;
Height := 25;
Caption := 'MemD';
TabOrder := 24;
OnClick := @MemoryChange;
end;
MemR := TButton.Create(ItemIdform);
with MemR do
begin
Parent := ItemIdform;
Left := 3;
Top := 8;
Width := 40;
Height := 25;
Caption := 'MemR';
TabOrder := 24;
OnClick := @MemoryChange;
end;
MemS := TButton.Create(ItemIdform);
with MemS do
begin
Parent := ItemIdform;
Left := 3;
Top := 40;
Width := 40;
Height := 25;
Caption := 'MemS';
TabOrder := 24;
OnClick := @MemoryChange;
end;
WriteLn('ItemForm compiled in ' + IntToStr(GetSystemTime - TimeItemForm) + ' milliseconds!');
end;
procedure SafeItemForm;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ItemForm', V);
end;
procedure ShowItemFormModal;
begin
ItemIdform.ShowModal;
Repeat
If ((ItemIdForm.Focused) and IsKeyDown('9')) Then
Disguise('hi9');
Until(EndItemForm);
end;
procedure SafeShowItemFormModal;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ShowItemFormModal', V);
end;
procedure MainItemForm;
begin
try
SafeItemForm;
SafeShowItemFormModal;
finally
FreeForm(ItemIdform);
except
WriteLn('An error seems to have occurred in: ItemForm');
end;
end;
begin
ClearDebug;
MainItemForm;
if (EndItemForm) then
TerminateScript;
end.
~Sandstorm