Advertisement
electricmaster

shapesform.pas

Oct 18th, 2014
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 9.76 KB | None | 0 0
  1. unit shapesform;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, Math;
  8.  
  9. type
  10.  
  11.   { TForm1 }
  12.  
  13.   TForm1 = class(TForm)
  14.     namebox: TEdit;            // Username
  15.     namelabel: TLabel;         // Username Label
  16.     continue: TButton;         // Continue Button
  17.     options: TGroupBox;        // Shape Groupbox
  18.     circler: TRadioButton;     // Circle Radio Button
  19.     rectangler: TRadioButton;  // Rectange Radio Button
  20.     triangler: TRadioButton;   // Triangle Radio Button
  21.     calc: TButton;             // Calculate Button
  22.     quit: TButton;             // Quit Button
  23.     Label1: TLabel;            // Input Label 1
  24.     Label2: TLabel;            // Input Label 2
  25.     Label3: TLabel;            // Input Label 3
  26.     Edit1: TEdit;              // Input Box 1
  27.     Edit2: TEdit;              // Input Box 2
  28.     Edit3: TEdit;              // Input Box 3
  29.     answer1: TLabel;           // Area Answer Label
  30.     answer2: TLabel;           // Perimiter Answer Label
  31.     procedure FormCreate(Sender: TObject);
  32.     procedure continueClick(Sender: TObject);
  33.     procedure circlerClick(Sender: TObject);
  34.     procedure quitClick(Sender: TObject);
  35.     procedure rectanglerClick(Sender: TObject);
  36.     procedure trianglerClick(Sender: TObject);
  37.     procedure calcClick(Sender: TObject);
  38.     procedure Edit1Change(Sender: TObject);
  39.     procedure Edit2Change(Sender: TObject);
  40.     procedure Edit3Change(Sender: TObject);
  41.     procedure Edit1Click(Sender: TObject);
  42.     procedure Edit2Click(Sender: TObject);
  43.     procedure Edit3Click(Sender: TObject);
  44.     procedure FormDestroy(Sender: TObject);
  45.   private
  46.     { Private declarations }
  47.   public
  48.     { Public declarations }
  49.   end;
  50.  
  51. var
  52.   Form1: TForm1;
  53.   outFile: textfile;
  54.   username: string;
  55.   shape: char;
  56.  
  57. implementation
  58.  
  59. {$R *.dfm}
  60.  
  61. procedure TForm1.FormCreate(Sender: TObject);  // start
  62. begin
  63.   assignfile(outFile, 'shapes.out');
  64.   rewrite(outFile);
  65.   form1.namebox.TabOrder := 0;
  66.   form1.continue.TabOrder := 1;
  67. end;
  68.  
  69.  
  70. procedure clear;
  71. begin
  72.   form1.answer1.Caption := '';
  73.   form1.answer2.Caption := '';
  74. end;
  75.  
  76. procedure showinput(lb1: string); overload; // one box
  77. begin
  78.   form1.Label1.Visible := false;
  79.   form1.Edit1.Visible := false;
  80.   form1.Label2.Visible := true;
  81.   form1.Label2.Caption := lb1;
  82.   form1.Label2.Left := 176; form1.Label2.Top := 200;
  83.   form1.Edit2.Visible := true;
  84.   form1.Edit2.Text := '0';
  85.   form1.Edit2.Left := 222; form1.Edit2.Top := 197;
  86.   form1.Edit2.TabOrder := 3;
  87.   form1.Label3.Visible := false;
  88.   form1.Edit3.Visible := false;
  89.   form1.calc.TabOrder := 4;
  90.   form1.quit.TabOrder := 5;
  91.   form1.quit.Default := false;
  92.   form1.calc.Default := true;
  93. end;
  94. procedure showinput(lb1, lb2: string); overload; // two boxes
  95. begin
  96.   form1.Label1.Visible := true;
  97.   form1.Label1.Caption := lb1;
  98.   form1.Label1.Left := 70;
  99.   form1.Label1.Top := 200;
  100.   form1.Edit1.Visible := true;
  101.   form1.Edit1.Text := '0';
  102.   form1.Edit1.Left := 126;
  103.   form1.Edit1.Top := 197;
  104.   form1.Edit1.TabOrder := 3;
  105.   form1.Label2.Visible := false;
  106.   form1.Edit2.Visible := false;
  107.   form1.Label3.Visible := true;
  108.   form1.Label3.Caption := lb2;
  109.   form1.Label3.Left := 282;
  110.   form1.Label3.Top := 200;
  111.   form1.Edit3.Visible := true;
  112.   form1.Edit3.Text := '0';
  113.   form1.Edit3.Left := 326;
  114.   form1.Edit3.Top := 197;
  115.   form1.Edit3.TabOrder := 4;
  116.   form1.calc.TabOrder := 5;
  117.   form1.quit.TabOrder := 6;
  118.   form1.quit.Default := false;
  119.   form1.calc.Default := true;
  120. end;
  121. procedure showinput(lb1, lb2, lb3: string); overload; // three boxes
  122. begin
  123.   form1.Label1.Visible := true;
  124.   form1.Label1.Caption := lb1;
  125.   form1.Label1.Left := 21;
  126.   form1.Label1.Top := 200;
  127.   form1.Edit1.Visible := true;
  128.   form1.Edit1.Text := '0';
  129.   form1.Edit1.Left := 64;
  130.   form1.Edit1.Top := 197;
  131.   form1.Edit1.TabOrder := 3;
  132.   form1.Label2.Visible := true;
  133.   form1.Label2.Caption := lb2;
  134.   form1.Label2.Left := 173;
  135.   form1.Label2.Top := 200;
  136.   form1.Edit2.Visible := true;
  137.   form1.Edit2.Text := '0';
  138.   form1.Edit2.Left := 219;
  139.   form1.Edit2.Top := 197;
  140.   form1.Edit2.TabOrder := 4;
  141.   form1.Label3.Visible := true;
  142.   form1.Label3.Caption := lb3;
  143.   form1.Label3.Left := 331;
  144.   form1.Label3.Top := 200;
  145.   form1.Edit3.Visible := true;
  146.   form1.Edit3.Text := '0';
  147.   form1.Edit3.Left := 373;
  148.   form1.Edit3.Top := 197;
  149.   form1.Edit3.TabOrder := 5;
  150.   form1.calc.TabOrder := 6;
  151.   form1.quit.TabOrder := 7;
  152.   form1.quit.Default := false;
  153.   form1.calc.Default := true;
  154. end;
  155.  
  156. procedure TForm1.continueClick(Sender: TObject);  // Continue
  157. begin
  158.   username := namebox.text;
  159.   namebox.visible := false;
  160.   namelabel.visible := false;
  161.   continue.visible := false;
  162.   options.visible := true;
  163.   circler.visible := true;
  164.   rectangler.visible := true;
  165.   triangler.visible := true;
  166.   form1.circler.TabOrder := 0;
  167.   form1.rectangler.TabOrder := 1;
  168.   form1.triangler.TabOrder := 2;
  169.   writeln(outFile, 'This program was run by ', username, '.');
  170.   writeln(outFile);
  171.  
  172.   { TForm1.circlerClick : default }
  173.   calc.visible := true;
  174.   quit.visible := true;
  175.   shape := 'c';
  176.   showinput('Radius');
  177. end;
  178.  
  179. // Delete letters upon entering
  180. procedure TForm1.Edit1Change(Sender: TObject);
  181. var
  182.   edit: string;
  183. begin
  184.   edit := form1.Edit1.Text;
  185.   try
  186.     strToFloat(edit);
  187.   except
  188.     delete(edit, length(edit), length(edit));
  189.   end;
  190.   form1.Edit1.Text := edit;
  191. end;
  192. procedure TForm1.Edit2Change(Sender: TObject);
  193. var
  194.   edit: string;
  195. begin
  196.   edit := form1.Edit2.Text;
  197.   try
  198.     strToFloat(edit);
  199.   except
  200.     delete(edit, length(edit), length(edit));
  201.   end;
  202.   form1.Edit2.Text := edit;
  203. end;
  204. procedure TForm1.Edit3Change(Sender: TObject);
  205. var
  206.   edit: string;
  207. begin
  208.   edit := form1.Edit3.Text;
  209.   try
  210.     strToFloat(edit);
  211.   except
  212.     delete(edit, length(edit), length(edit));
  213.   end;
  214.   form1.Edit3.Text := edit;
  215. end;
  216.  
  217. // Delete the 0 when clicked
  218. procedure TForm1.Edit1Click(Sender: TObject);
  219. begin
  220.   if form1.Edit1.Text = '0' then
  221.     form1.Edit1.Text := '';
  222. end;
  223. procedure TForm1.Edit2Click(Sender: TObject);
  224. begin
  225.   if form1.Edit2.Text = '0' then
  226.     form1.Edit2.Text := '';
  227. end;
  228. procedure TForm1.Edit3Click(Sender: TObject);
  229. begin
  230.   if form1.Edit3.Text = '0' then
  231.     form1.Edit3.Text := '';
  232. end;
  233.  
  234. // Calculations
  235. procedure circle(r: real);
  236. var
  237.   a, C: real;
  238. begin
  239.   clear;
  240.   a := pi*power(r, 2); // area
  241.   C := 2*pi*r;         // circumference
  242.   writeln(outFile, ' * CIRCLE: * ');
  243.   writeln(outFile, 'Radius: ', round(r));
  244.   writeln(outFile, 'The area of the circle: ', round(a));
  245.   writeln(outFile, 'The circumference of the circle: ', round(C));
  246.   writeln(outFile);
  247.   form1.answer1.Caption := ('The area of the circle: ' + floatToStr(round(a)));
  248.   form1.answer2.Caption := ('The circumference of the circle: ' + floatToStr(round(C)));
  249. end;
  250. procedure rectangle(l, w: real);
  251. var
  252.   a, p: real;
  253. begin
  254.   clear;
  255.   a := l*w;           // area
  256.   p := ((2*l)+(2*w)); // perimiter
  257.   writeln(outFile, ' * RECTANGLE: * ');
  258.   writeln(outFile, 'Length: ', round(l));
  259.   writeln(outFile, 'Width: ', round(w));
  260.   writeln(outFile, 'The area of the rectangle: ', round(a));
  261.   writeln(outFile, 'The perimiter of the rectangle: ', round(p));
  262.   writeln(outFile);
  263.   form1.answer1.Caption := ('The area of the rectangle: ' + floatToStr(round(a)));
  264.   form1.answer2.Caption := ('The perimiter of the rectangle: ' + floatToStr(round(p)));
  265. end;
  266. procedure triangle(a, b, c: real);
  267. var
  268.   area, p, s: real;
  269. begin
  270.   clear;
  271.   writeln(outFile, ' * TRIANGLE: * ');
  272.   writeln(outFile, 'Side One: ', round(a));
  273.   writeln(outFile, 'Side Two: ', round(b));
  274.   writeln(outFile, 'Side Three: ', round(c));
  275.   p := (a+b+c); // perimiter
  276.   s := p/2;        // semiperimeter
  277.   area := (s*(s-a)*(s-b)*(s-c));
  278.  
  279.   if area < 0 then begin // Does not form a triangle
  280.     writeln(outFile, 'The given sides do not form a triangle.');
  281.     form1.answer1.Caption := ('The given sides do not form a triangle.');
  282.   end
  283.   else if area = 0 then begin // Forms a line
  284.     writeln(outFile, 'The given sides form a straight line.');
  285.     form1.answer1.Caption := ('The given sides form a straight line.');
  286.   end
  287.   else if area > 0 then begin // Forms a triangle
  288.     area := sqrt(area); // area √[s(s-a)(s-b)(s-c)]
  289.     writeln(outFile, 'The area of the triangle: ', floatToStr(round(area)));
  290.     writeln(outFile, 'The perimiter of the triangle: ', floatToStr(round(p)));
  291.     form1.answer1.Caption := ('The area of the triangle: ' + floatToStr(round(area)));
  292.     form1.answer2.Caption := ('The perimiter of the triangle: ' + floatToStr(round(p)));
  293.   end;
  294.   writeln(outFile);
  295. end;
  296.  
  297. procedure TForm1.circlerClick(Sender: TObject); // Select Circle {shape := 'c'}
  298. begin
  299.   calc.visible := true;
  300.   quit.visible := true;
  301.   shape := 'c';
  302.   showinput('Radius');
  303.  
  304. end;
  305. procedure TForm1.rectanglerClick(Sender: TObject); // Select Rectangle {shape := 'r')
  306. begin
  307.   calc.visible := true;
  308.   quit.visible := true;
  309.   shape := 'r';
  310.   showinput('Length', 'Width');
  311. end;
  312.  
  313.  
  314. procedure TForm1.trianglerClick(Sender: TObject);  // Select Triangle {shape := 't'}
  315. begin
  316.   calc.visible := true;
  317.   quit.visible := true;
  318.   shape := 't';
  319.   showinput('Side 1', 'Side 2', 'Side 3');
  320. end;
  321.  
  322. procedure TForm1.calcClick(Sender: TObject);  // Calculate
  323. begin
  324.   case shape of
  325.     'c': circle(strToFloat(form1.Edit2.Text));
  326.     'r': rectangle(strToFloat(form1.Edit1.Text), strToFloat(form1.Edit3.Text));
  327.     't': triangle(strToFloat(form1.Edit1.Text), strToFloat(form1.Edit2.Text), strToFloat(form1.Edit3.Text));
  328.   end;
  329.   form1.calc.Default := false;
  330.   form1.quit.Default := true;
  331. end;
  332.  
  333. procedure TForm1.quitClick(Sender: TObject);  // Quit
  334. begin
  335.   form1.close;
  336. end;
  337.  
  338. procedure TForm1.FormDestroy(Sender: TObject);
  339. begin
  340.   writeln(outFile, 'This program was created by Michael MacLean 10/14/14');
  341.   closefile(outFile);
  342. end;
  343.  
  344. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement