根据边长计算三角形的面积

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);   //"确定"按钮
var
  a,b,c,s,area: real;
  str1: string;
begin
  a := strtofloat(edit1.Text);
  b := strtofloat(edit2.Text);
  c := strtofloat(edit3.Text);
  if ((a+b>c) and (a+c>b) and (b+c>a)) then       //条件判断,三条边能否构成三角形
  begin
    s := (a+b+c)/2;
    area := sqrt(s*(s-a)*(s-b)*(s-c));            //计算三角形的面积
    str(area:8:3,str1);                           //整数部分为8位,小数部分为3位
    showmessage('面积为:'+str1);
  end
  else
    showmessage('无法构成三角形!');
end;

procedure TForm1.Button2Click(Sender: TObject);   //“清除”按钮
begin
  Edit1.Text := '';
  Edit2.Text := '';
  Edit3.Text := '';
end;

end.

 

posted @ 2011-12-03 09:49  endsnow  阅读(627)  评论(0)    收藏  举报