Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit shapesform;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, Math;
- type
- { TForm1 }
- TForm1 = class(TForm)
- namebox: TEdit; // Username
- namelabel: TLabel; // Username Label
- continue: TButton; // Continue Button
- options: TGroupBox; // Shape Groupbox
- circler: TRadioButton; // Circle Radio Button
- rectangler: TRadioButton; // Rectange Radio Button
- triangler: TRadioButton; // Triangle Radio Button
- calc: TButton; // Calculate Button
- quit: TButton; // Quit Button
- Label1: TLabel; // Input Label 1
- Label2: TLabel; // Input Label 2
- Label3: TLabel; // Input Label 3
- Edit1: TEdit; // Input Box 1
- Edit2: TEdit; // Input Box 2
- Edit3: TEdit; // Input Box 3
- answer1: TLabel; // Area Answer Label
- answer2: TLabel; // Perimiter Answer Label
- procedure FormCreate(Sender: TObject);
- procedure continueClick(Sender: TObject);
- procedure circlerClick(Sender: TObject);
- procedure quitClick(Sender: TObject);
- procedure rectanglerClick(Sender: TObject);
- procedure trianglerClick(Sender: TObject);
- procedure calcClick(Sender: TObject);
- procedure Edit1Change(Sender: TObject);
- procedure Edit2Change(Sender: TObject);
- procedure Edit3Change(Sender: TObject);
- procedure Edit1Click(Sender: TObject);
- procedure Edit2Click(Sender: TObject);
- procedure Edit3Click(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- outFile: textfile;
- username: string;
- shape: char;
- implementation
- {$R *.dfm}
- procedure TForm1.FormCreate(Sender: TObject); // start
- begin
- assignfile(outFile, 'shapes.out');
- rewrite(outFile);
- form1.namebox.TabOrder := 0;
- form1.continue.TabOrder := 1;
- end;
- procedure clear;
- begin
- form1.answer1.Caption := '';
- form1.answer2.Caption := '';
- end;
- procedure showinput(lb1: string); overload; // one box
- begin
- form1.Label1.Visible := false;
- form1.Edit1.Visible := false;
- form1.Label2.Visible := true;
- form1.Label2.Caption := lb1;
- form1.Label2.Left := 176; form1.Label2.Top := 200;
- form1.Edit2.Visible := true;
- form1.Edit2.Text := '0';
- form1.Edit2.Left := 222; form1.Edit2.Top := 197;
- form1.Edit2.TabOrder := 3;
- form1.Label3.Visible := false;
- form1.Edit3.Visible := false;
- form1.calc.TabOrder := 4;
- form1.quit.TabOrder := 5;
- form1.quit.Default := false;
- form1.calc.Default := true;
- end;
- procedure showinput(lb1, lb2: string); overload; // two boxes
- begin
- form1.Label1.Visible := true;
- form1.Label1.Caption := lb1;
- form1.Label1.Left := 70;
- form1.Label1.Top := 200;
- form1.Edit1.Visible := true;
- form1.Edit1.Text := '0';
- form1.Edit1.Left := 126;
- form1.Edit1.Top := 197;
- form1.Edit1.TabOrder := 3;
- form1.Label2.Visible := false;
- form1.Edit2.Visible := false;
- form1.Label3.Visible := true;
- form1.Label3.Caption := lb2;
- form1.Label3.Left := 282;
- form1.Label3.Top := 200;
- form1.Edit3.Visible := true;
- form1.Edit3.Text := '0';
- form1.Edit3.Left := 326;
- form1.Edit3.Top := 197;
- form1.Edit3.TabOrder := 4;
- form1.calc.TabOrder := 5;
- form1.quit.TabOrder := 6;
- form1.quit.Default := false;
- form1.calc.Default := true;
- end;
- procedure showinput(lb1, lb2, lb3: string); overload; // three boxes
- begin
- form1.Label1.Visible := true;
- form1.Label1.Caption := lb1;
- form1.Label1.Left := 21;
- form1.Label1.Top := 200;
- form1.Edit1.Visible := true;
- form1.Edit1.Text := '0';
- form1.Edit1.Left := 64;
- form1.Edit1.Top := 197;
- form1.Edit1.TabOrder := 3;
- form1.Label2.Visible := true;
- form1.Label2.Caption := lb2;
- form1.Label2.Left := 173;
- form1.Label2.Top := 200;
- form1.Edit2.Visible := true;
- form1.Edit2.Text := '0';
- form1.Edit2.Left := 219;
- form1.Edit2.Top := 197;
- form1.Edit2.TabOrder := 4;
- form1.Label3.Visible := true;
- form1.Label3.Caption := lb3;
- form1.Label3.Left := 331;
- form1.Label3.Top := 200;
- form1.Edit3.Visible := true;
- form1.Edit3.Text := '0';
- form1.Edit3.Left := 373;
- form1.Edit3.Top := 197;
- form1.Edit3.TabOrder := 5;
- form1.calc.TabOrder := 6;
- form1.quit.TabOrder := 7;
- form1.quit.Default := false;
- form1.calc.Default := true;
- end;
- procedure TForm1.continueClick(Sender: TObject); // Continue
- begin
- username := namebox.text;
- namebox.visible := false;
- namelabel.visible := false;
- continue.visible := false;
- options.visible := true;
- circler.visible := true;
- rectangler.visible := true;
- triangler.visible := true;
- form1.circler.TabOrder := 0;
- form1.rectangler.TabOrder := 1;
- form1.triangler.TabOrder := 2;
- writeln(outFile, 'This program was run by ', username, '.');
- writeln(outFile);
- { TForm1.circlerClick : default }
- calc.visible := true;
- quit.visible := true;
- shape := 'c';
- showinput('Radius');
- end;
- // Delete letters upon entering
- procedure TForm1.Edit1Change(Sender: TObject);
- var
- edit: string;
- begin
- edit := form1.Edit1.Text;
- try
- strToFloat(edit);
- except
- delete(edit, length(edit), length(edit));
- end;
- form1.Edit1.Text := edit;
- end;
- procedure TForm1.Edit2Change(Sender: TObject);
- var
- edit: string;
- begin
- edit := form1.Edit2.Text;
- try
- strToFloat(edit);
- except
- delete(edit, length(edit), length(edit));
- end;
- form1.Edit2.Text := edit;
- end;
- procedure TForm1.Edit3Change(Sender: TObject);
- var
- edit: string;
- begin
- edit := form1.Edit3.Text;
- try
- strToFloat(edit);
- except
- delete(edit, length(edit), length(edit));
- end;
- form1.Edit3.Text := edit;
- end;
- // Delete the 0 when clicked
- procedure TForm1.Edit1Click(Sender: TObject);
- begin
- if form1.Edit1.Text = '0' then
- form1.Edit1.Text := '';
- end;
- procedure TForm1.Edit2Click(Sender: TObject);
- begin
- if form1.Edit2.Text = '0' then
- form1.Edit2.Text := '';
- end;
- procedure TForm1.Edit3Click(Sender: TObject);
- begin
- if form1.Edit3.Text = '0' then
- form1.Edit3.Text := '';
- end;
- // Calculations
- procedure circle(r: real);
- var
- a, C: real;
- begin
- clear;
- a := pi*power(r, 2); // area
- C := 2*pi*r; // circumference
- writeln(outFile, ' * CIRCLE: * ');
- writeln(outFile, 'Radius: ', round(r));
- writeln(outFile, 'The area of the circle: ', round(a));
- writeln(outFile, 'The circumference of the circle: ', round(C));
- writeln(outFile);
- form1.answer1.Caption := ('The area of the circle: ' + floatToStr(round(a)));
- form1.answer2.Caption := ('The circumference of the circle: ' + floatToStr(round(C)));
- end;
- procedure rectangle(l, w: real);
- var
- a, p: real;
- begin
- clear;
- a := l*w; // area
- p := ((2*l)+(2*w)); // perimiter
- writeln(outFile, ' * RECTANGLE: * ');
- writeln(outFile, 'Length: ', round(l));
- writeln(outFile, 'Width: ', round(w));
- writeln(outFile, 'The area of the rectangle: ', round(a));
- writeln(outFile, 'The perimiter of the rectangle: ', round(p));
- writeln(outFile);
- form1.answer1.Caption := ('The area of the rectangle: ' + floatToStr(round(a)));
- form1.answer2.Caption := ('The perimiter of the rectangle: ' + floatToStr(round(p)));
- end;
- procedure triangle(a, b, c: real);
- var
- area, p, s: real;
- begin
- clear;
- writeln(outFile, ' * TRIANGLE: * ');
- writeln(outFile, 'Side One: ', round(a));
- writeln(outFile, 'Side Two: ', round(b));
- writeln(outFile, 'Side Three: ', round(c));
- p := (a+b+c); // perimiter
- s := p/2; // semiperimeter
- area := (s*(s-a)*(s-b)*(s-c));
- if area < 0 then begin // Does not form a triangle
- writeln(outFile, 'The given sides do not form a triangle.');
- form1.answer1.Caption := ('The given sides do not form a triangle.');
- end
- else if area = 0 then begin // Forms a line
- writeln(outFile, 'The given sides form a straight line.');
- form1.answer1.Caption := ('The given sides form a straight line.');
- end
- else if area > 0 then begin // Forms a triangle
- area := sqrt(area); // area √[s(s-a)(s-b)(s-c)]
- writeln(outFile, 'The area of the triangle: ', floatToStr(round(area)));
- writeln(outFile, 'The perimiter of the triangle: ', floatToStr(round(p)));
- form1.answer1.Caption := ('The area of the triangle: ' + floatToStr(round(area)));
- form1.answer2.Caption := ('The perimiter of the triangle: ' + floatToStr(round(p)));
- end;
- writeln(outFile);
- end;
- procedure TForm1.circlerClick(Sender: TObject); // Select Circle {shape := 'c'}
- begin
- calc.visible := true;
- quit.visible := true;
- shape := 'c';
- showinput('Radius');
- end;
- procedure TForm1.rectanglerClick(Sender: TObject); // Select Rectangle {shape := 'r')
- begin
- calc.visible := true;
- quit.visible := true;
- shape := 'r';
- showinput('Length', 'Width');
- end;
- procedure TForm1.trianglerClick(Sender: TObject); // Select Triangle {shape := 't'}
- begin
- calc.visible := true;
- quit.visible := true;
- shape := 't';
- showinput('Side 1', 'Side 2', 'Side 3');
- end;
- procedure TForm1.calcClick(Sender: TObject); // Calculate
- begin
- case shape of
- 'c': circle(strToFloat(form1.Edit2.Text));
- 'r': rectangle(strToFloat(form1.Edit1.Text), strToFloat(form1.Edit3.Text));
- 't': triangle(strToFloat(form1.Edit1.Text), strToFloat(form1.Edit2.Text), strToFloat(form1.Edit3.Text));
- end;
- form1.calc.Default := false;
- form1.quit.Default := true;
- end;
- procedure TForm1.quitClick(Sender: TObject); // Quit
- begin
- form1.close;
- end;
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- writeln(outFile, 'This program was created by Michael MacLean 10/14/14');
- closefile(outFile);
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement