SRL Archive - Other Scripts

[Form] Calculator!! (yay?)


Bego

2006-07-03 00:28:40

I was bored so I figured I would make a calculator that looked real good and ran smooth like a program, I have suceeded, maybe I'll make buttons and such next time ;)

[php]{.Script Info:
# ScriptName = Calculator by Bego
# Author = Bego
# Description = Can divide, multiply, add, and subtract.
# Version = 2.0.5
# Date = Saturday, June 24, 2006, 1:20 PM
# Comments = Do the setup or don't - just an extra option
/Script Info}
{ __ _ _ _
/ _\ ___ _ __(_)_ __ | |_ | |__ _ _ _
\ \ / __| '__| | '_ \| __| | '_ \| | | |(_)
_\ \ (__| | | | |_) | |_ | |_) | |_| | _
\__/\___|_| |_| .__/ \__| |_.__/ \__, |(_)
|_| |___/

____
/\ _`\
\ \ \ \ \ __ __ ___
\ \ _ <' /'__`\ /'_ `\ / __`\
\ \ \ \ \/\ __//\ \ \ \/\ \ \ \
\ \____/\ \____\ \____ \ \____/
\/___/ \/____/\/___ \ \/___/
/\____/
\/___/ }

{Instructions:
1. DO THE SETUP ON LINE 52!!
2. Run the script…
3. Follow the instructions in the window which pops up!}
program Calculator;

const
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
////////////////////////SETUP////////////////////////
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////

RestartForm = false; {Once the math question is answered, open it up again
to do another question? TRUE or FALSE}
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
////////DO NOT EDIT ANYTHING BELOW THIS POINT////////
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////

var
frmDesign : TForm;
Label1 : TLabel;
Label2 : TLabel;
Edit1 : TEdit;
Edit2 : TEdit;
RadioButton1 : TRadioButton;
RadioButton2 : TRadioButton;
RadioButton3 : TRadioButton;
RadioButton4 : TRadioButton;
Button1 : TButton;
firstnum, secondnum, answer : integer;

procedure clickthebutton(sender: TObject);
begin
frmDesign.ModalResult:= mrOk;
end;

procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 250;
frmDesign.Top := 114;
frmDesign.BorderStyle := bsSingle;
frmDesign.Caption := 'Calculator';
frmDesign.ClientHeight := 94;
frmDesign.ClientWidth := 236;
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
Label1 := TLabel.Create(frmDesign);
Label1.Parent := frmDesign;
Label1.Left := 22;
Label1.Top := 14;
Label1.Width := 76;
Label1.Height := 16;
Label1.Caption := 'First Number';
Label1.Font.Color := clWindowText;
Label1.Font.Height := -13;
Label1.Font.Name := 'MS Sans Serif';
Label1.Font.Style := [];
Label1.ParentFont := False;
Label2 := TLabel.Create(frmDesign);
Label2.Parent := frmDesign;
Label2.Left := 1;
Label2.Top := 41;
Label2.Width := 98;
Label2.Height := 16;
Label2.Caption := 'Second Number';
Label2.Font.Color := clWindowText;
Label2.Font.Height := -13;
Label2.Font.Name := 'MS Sans Serif';
Label2.Font.Style := [];
Label2.ParentFont := False;
Edit1 := TEdit.Create(frmDesign);
Edit1.Parent := frmDesign;
Edit1.Left := 102;
Edit1.Top := 12;
Edit1.Width := 49;
Edit1.Height := 21;
Edit1.TabOrder := 8;
Edit2 := TEdit.Create(frmDesign);
Edit2.Parent := frmDesign;
Edit2.Left := 102;
Edit2.Top := 39;
Edit2.Width := 49;
Edit2.Height := 21;
Edit2.TabOrder := 9;
RadioButton1 := TRadioButton.Create(frmDesign);
RadioButton1.Parent := frmDesign;
RadioButton1.Left := 159;
RadioButton1.Top := 1;
RadioButton1.Width := 74;
RadioButton1.Height := 17;
RadioButton1.Caption := 'Multiply';
RadioButton1.TabOrder := 10;
RadioButton2 := TRadioButton.Create(frmDesign);
RadioButton2.Parent := frmDesign;
RadioButton2.Left := 159;
RadioButton2.Top := 16;
RadioButton2.Width := 60;
RadioButton2.Height := 17;
RadioButton2.Caption := 'Divide';
RadioButton2.TabOrder := 11;
RadioButton3 := TRadioButton.Create(frmDesign);
RadioButton3.Parent := frmDesign;
RadioButton3.Left := 159;
RadioButton3.Top := 31;
RadioButton3.Width := 73;
RadioButton3.Height := 17;
RadioButton3.Caption := 'Add';
RadioButton3.TabOrder := 12;
RadioButton4 := TRadioButton.Create(frmDesign);
RadioButton4.Parent := frmDesign;
RadioButton4.Left := 159;
RadioButton4.Top := 46;
RadioButton4.Width := 69;
RadioButton4.Height := 17;
RadioButton4.Caption := 'Subtract';
RadioButton4.TabOrder := 13;
Button1 := TButton.Create(frmDesign);
Button1.Parent := frmDesign;
Button1.OnClick:= @clickthebutton;
Button1.Left := 5;
Button1.Top := 64;
Button1.Width := 224;
Button1.Height := 25;
Button1.Caption := 'Calculate';
Button1.TabOrder := 14;
end;

procedure SafeInitForm;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('InitForm', v);
end;

procedure ShowFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowFormModal;
var
v: TVariantArray;
begin
setarraylength(V, 0);
ThreadSafeCall('ShowFormModal', v);
end;

procedure DoMath;
begin
if(RadioButton1.Checked)then
begin
answer := firstnum * secondnum;
Writeln(inttostr(answer));
end else
if(RadioButton2.Checked)then
begin
if(Edit1.Text = '0') or (Edit2.Text = '0') then
begin
Writeln('Its impossible to divide by zero!');
end else
begin
answer := firstnum / secondnum;
Writeln(inttostr(answer));
end;
end else
if(RadioButton3.Checked)then
begin
answer := firstnum + secondnum;
Writeln(inttostr(answer));
end else
if(RadioButton4.Checked)then
begin
answer := firstnum - secondnum;
Writeln(inttostr(answer));
end else
if(RadioButton1.Checked = false) and
(RadioButton2.Checked = false) and
(RadioButton3.Checked = false) and
(RadioButton4.Checked = false) then
begin
Writeln('You did not select any math style!');
end;
if (RestartForm = true) then
begin
repeat
SafeInitForm;
SafeShowFormModal;
firstnum := strtoint(Edit1.Text);
secondnum := strtoint(Edit2.Text);
answer := 0
DoMath;
until(false);
end else
end;

begin
SafeInitForm;
SafeShowFormModal;
firstnum := strtoint(Edit1.Text);
secondnum := strtoint(Edit2.Text);
answer := 0;
DoMath;
end.[/php]

Its clean code nonetheless, I guess I'll always be known for clean code eh?

Pyro

2006-07-03 01:19:43

Cool but i dont like it how it always closes the window. Make it have a label below everythign and just change the text of the label when someoen presses calculate

Bego

2006-07-03 02:39:15

*cough* Look at the setup.

Janilabo

2006-07-03 02:58:38

Its gooood, Bego. You are the man. :D

The setup is on line 39, by the way.

Wizzup?

2006-07-03 03:00:21

Hmm just a note :

0/2 = 0, your script also gives a error here, but 0/2 is possible.
2/0 = ERROR, thats alright.

Some Other functions

Pow(base,exponent : extended) // Pow.
Sqrt(e:extended) // Square root.

WriteLn('hm = '+FloattoStr(Pow(0,0)))

:p SCAR returns zero, but shouldn't this be a error aswell, cause 0 to the power something is 0, but something to the power 0 is 1…..

Pyro

2006-07-03 03:35:05

From: Bego
*cough* Look at the setup.

Smacks Self in head. But still. Me thinks label better then debug

Bego

2006-07-03 03:46:12

From: Wizzup?
Hmm just a note :

0/2 = 0, your script also gives a error here, but 0/2 is possible.
2/0 = ERROR, thats alright.

Some Other functions

Pow(base,exponent : extended) // Pow.
Sqrt(e:extended) // Square root.

WriteLn('hm = '+FloattoStr(Pow(0,0)))

:p SCAR returns zero, but shouldn't this be a error aswell, cause 0 to the power something is 0, but something to the power 0 is 1…..


I could add that, but then again, why? And thanks for the 0/2 thing, I'll fix it. I put it so anything dividing by 0 is an error because dividing by 0 is impossible, but I guess I'm wrong. EDIT: I don't know how I would make it detect 0/2 and still go on with the equasion because of the way I set it up. Left it for now.

Pyro, yeah it would, but I'm too lazy, and it doesn't really matter, you can see the debug box fine.

XxKanexX

2006-07-03 09:29:51

Looks a bit like mine and was made a day after i posted mine :p

Good job ;)

Ron

2006-07-03 10:06:07

1/5 results in 0 lol

Yakman

2006-08-29 16:56:25

very nice, if you needed more maths functions you could just look in SRL maths section to get some ideas :biggrin:

t3h ub3r k1tten

2006-09-24 16:11:20

*looks at yakman*
You made ice magicks, maybe a bitmap calculator :P
Dont make me beat you to it!

ilikethis

2006-10-16 22:53:48

wow u people r cool