unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 =
class(TForm)
Rechnen: TButton;
TFx: TEdit;
TFy: TEdit;
TFs: TEdit;
TFa: TEdit;
TFb: TEdit;
TFe: TEdit;
Rechnen2: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Edit1: TEdit;
Button1: TButton;
procedure RechnenClick(Sender: TObject);
procedure Rechnen2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.RechnenClick(Sender: TObject);
VAR
x, y, s : REAL;
begin
x:= strtofloat (TFx.text);
y:= strtofloat (TFy.text);
s:= x*y;
TFs.Text := floattostr (s);
showmessage ('
kannste haben :>');
end;
procedure TForm1.Rechnen2Click(Sender: TObject);
VAR
a, b, e : REAL;
begin
a:= strtofloat (TFa.text);
b:= strtofloat (TFb.text);
e:= a/b;
TFe.Text := floattostr (e);
showmessage ('
kannste haben :>');
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Form1: TForm1;
implementation
{$R *.dfm}
type
TFraction =
record
Numerator: Integer;
Denominator: Integer;
end;
function Dec2Frac(
const Decimal, Accuracy: Double): TFraction;
var
tempd: Double;
f: TFraction;
neg: Shortint;
begin
f.Numerator := 0;
f.Denominator := 1;
if Decimal < 0
then
neg := -1
else
neg := 1;
tempd := 0;
while Abs(tempd - Decimal) > Accuracy
do begin
if Abs(tempd) > Abs(Decimal)
then
inc(f.Denominator)
else
f.Numerator := f.Numerator + neg;
tempd := f.Numerator / f.Denominator;
end;
//tempi := gcd(f.Numerator,f.Denominator);
Result := f;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Decimal: Double;
Rational: TFraction;
begin
Decimal := strtofloat(TFe.text);
// Umwandlung in einen Bruch
// je kleiner der 2. Parameter, desto genauer das Ergebnis und desto länger die Rechenzeit
Rational := Dec2Frac(Decimal, 0.000001);
// Ergebnis anzeigen (Ausgabe für 5.46: 273/50):
ShowMessage('
Numerator: ' + IntToStr(Rational.Numerator) + #13#10 +
'
Denumerator: ' + IntToStr(Rational.Denominator));
end;
end.