![]() |
Delphi-Version: 7
Fehler in Rechenprogramm
Hallo Leute,
ich habe mir eben gedacht mal ein kleines Rechenprogramm zu erstellen. Ich habe zunächst eine kleine Passwort-abfrage erstellt und dann zwei Radioboxen, in einer lässt sich einstellen welche Rechenart man rechnen möchte und in der 2. kann man den Zahlenbereich festlegen (100 oder 1000). Ich habe allerdings zwei Probleme zum einen hängt sich das Programm manchmal auf und zum andren, funktioniert das ganze für die Division nicht. Und zwar sollen dort vom Programm solange zwei Zufallszahlen n und m genereriert werden, bis das Ergebnis n/m ein integer Wert ist. Ich habe das ganze einfach so gemacht zunächst wird der Wert k per Zufall ausgewählt und dann wird n und m einfach so lange gebildet bis n/m k ergibt. Wenn ich das Programm dann jedoch starte und Division und den Zahlenbereich angeglickt habe und Button1 angegklickt habe kommt die Fehlermeldung :"Invalid floating point operation". Schonmal vielen Dank im Vorraus und hier ist der Quellcode:
Delphi-Quellcode:
unit Unit1;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; Label1: TLabel; Label2: TLabel; Label3: TLabel; Edit2: TEdit; RadioGroup1: TRadioGroup; RadioGroup2: TRadioGroup; Label13: TLabel; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; passworttrue: boolean; passwort, eingabe: string; n, m, k : integer; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin passwort := '123456'; eingabe:= edit1.Text; passworttrue := (passwort=eingabe); if passworttrue then begin label1.Visible:= true; label2.Visible:= true; label3.Visible:= true; edit2.Visible:= true; end; randomize; k := random(1000)+1; case RadioGroup1.ItemIndex of 0: begin label2.caption:= '+'; REPEAT if radiogroup2.ItemIndex=0 then begin n := random(100) +1; m:= random(100) +1; end; UNTIL m+n <=100; REPEAT if radiogroup2.ItemIndex=1 then begin n := random(1000)+1; m:= random(1000) +1; end; UNTIL m+n <=1000; label1.Caption := floattostr(n); label3.Caption := floattostr(m); end; 1: begin label2.caption:= '-'; REPEAT if radiogroup2.ItemIndex=0 then begin n := random(100) +1; m:= random(100) +1; end; UNTIL n>m; REPEAT if radiogroup2.ItemIndex=1 then begin n := random(1000)+1; m:= random(1000) +1; end; UNTIL n>m; label1.Caption := floattostr(n); label3.Caption := floattostr(m); end; 2: begin label2.caption:= 'x'; REPEAT if radiogroup2.ItemIndex=0 then begin n := random(100) +1; m:= random(100) +1; end; UNTIL n*m <=100; REPEAT if radiogroup2.ItemIndex=1 then begin n := random(1000)+1; m:= random(1000) +1; end; UNTIL n*m <= 1000; label1.Caption := floattostr(n); label3.Caption := floattostr(m); end; 3: begin label2.caption:= ':'; REPEAT if radiogroup2.ItemIndex=0 then begin n := random(100) +1; m:= random(100) +1; end; UNTIL (n>=m) and (n/m = k); REPEAT if radiogroup2.ItemIndex=1 then begin n := random(1000)+1; m:= random(1000) +1; end; UNTIL (n>=m) and (n/m = k); label1.Caption := floattostr(n); label3.Caption := floattostr(m); end; end; end; end. |
AW: Fehler in Rechenprogramm
Zitat:
[edit] Sry, Willkommen in der DP :dp: [/edit] |
AW: Fehler in Rechenprogramm
Super! Das Problem mit dem Aufhängen hab ich schonmal nicht mehr, fehlt nur noch das mit der Division. Hier der aktuelle Quellcode:
Delphi-Quellcode:
unit Unit1;
interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) Edit1: TEdit; Button1: TButton; Label1: TLabel; Label2: TLabel; Label3: TLabel; Edit2: TEdit; RadioGroup1: TRadioGroup; RadioGroup2: TRadioGroup; Label13: TLabel; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; passworttrue: boolean; passwort, eingabe: string; n, m, k : integer; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); begin passwort := '123456'; eingabe:= edit1.Text; passworttrue := (passwort=eingabe); if passworttrue then begin label1.Visible:= true; label2.Visible:= true; label3.Visible:= true; edit2.Visible:= true; end; randomize; k := random(1000)+1; case RadioGroup1.ItemIndex of 0: begin label2.caption:= '+'; if radiogroup2.ItemIndex=0 then begin REPEAT n := random(100) +1; m:= random(100) +1; UNTIL m+n <=100; end; if radiogroup2.ItemIndex=1 then begin REPEAT n := random(1000)+1; m:= random(1000) +1; UNTIL m+n <=1000; end; label1.Caption := floattostr(n); label3.Caption := floattostr(m); end; 1: begin label2.caption:= '-'; if radiogroup2.ItemIndex=0 then begin REPEAT n := random(100) +1; m:= random(100) +1; UNTIL n>m; end; if radiogroup2.ItemIndex=1 then begin REPEAT n := random(1000)+1; m:= random(1000) +1; UNTIL n>m; end; label1.Caption := floattostr(n); label3.Caption := floattostr(m); end; 2: begin label2.caption:= 'x'; if radiogroup2.ItemIndex=0 then begin REPEAT n := random(100) +1; m:= random(100) +1; UNTIL n*m <=100; end; if radiogroup2.ItemIndex=1 then begin REPEAT n := random(1000)+1; m:= random(1000) +1; UNTIL n*m <= 1000; end; label1.Caption := floattostr(n); label3.Caption := floattostr(m); end; 3: begin label2.caption:= ':'; if radiogroup2.ItemIndex=0 then begin REPEAT n := random(100) +1; m:= random(100) +1; UNTIL (n>=m) and (n/m = k); end; if radiogroup2.ItemIndex=1 then begin REPEAT n := random(1000)+1; m:= random(1000) +1; UNTIL (n>=m) and (n/m = k); end; label1.Caption := floattostr(n); label3.Caption := floattostr(m); end; end; end; end. |
AW: Fehler in Rechenprogramm
Ganzzahlige Divisionen macht man mit DIV, es sei denn, man möchte tatsächlich eine Fließkommazahl als Ergebnis. Die kannst Du aber kaum mit "=" vergleichen, da es so gut wie zwangsläufig zu Rundungsungenauigkeiten kommt. Hier empfiehlt sich dann die Funktion
![]() |
AW: Fehler in Rechenprogramm
Okay, könntest du mir schreiben wie das hier dann genau aussieht, weil ich weiß jetzt nicht was ich dort machen soll. Also die zwei Zahlen sollen so zufällig gewählt werden, dass es ein rundes Ergebnis gibt.
|
AW: Fehler in Rechenprogramm
Nicht "/", sondern "div" schreiben.
|
AW: Fehler in Rechenprogramm
Okay aber jetzt hängt es sich bei Knopfdruck auf.
|
AW: Fehler in Rechenprogramm
Dann beobachte doch mal die ermittelten Werte, den Debugger kann ich auch nicht ersetzen.
|
AW: Fehler in Rechenprogramm
Hallo,
Zitat:
Delphi-Quellcode:
k := random(1000)+1;
und
Delphi-Quellcode:
ansehe, dann kann die Abbruchbedingung nur erfüllt werden, wenn k <= 100 gilt. Für ein k > 100 kann n div m = k nie erfüllt werden, denn das Maximum für n ist 100, das Minimum für m ist 1 - also bestenfalls n div m = 100 zu erreichen.
REPEAT
n := random(100) +1; m:= random(100) +1; UNTIL (n>=m) and (n/m = k); Wenn Du Glück hast und 1 <= k <= 100 erfüllt ist, dann wird die Schleife u. U. aber auch eine ganze Weile laufen bis n = k * m gilt. Wenn Du kein Glück hast (und das ist sehr wahrscheinlich): Endlosschleife. In der zweiten Schleife - Random(1000) - ist das Erreichen der Abbruchbedingung prinzipiell möglich; selbst bei k = 1000 kann mit n = 1000 und m = 1 die Abbruchbedingung erreicht werden - ist halt nur eine Frage der Zeit wann die Konstellation eintritt. Gruß |
AW: Fehler in Rechenprogramm
Um Divisions-Aufgaben zu generieren (ganzzahlige), kann man auch so vorgehen, dass man zwei Zufallszahlen erzeugt und diese multipliziert. Das Ergebnis ist der Dividend und eine von den beiden ersten Zahlen der Divisor. Die andere Zahl ist dann der Quotient.
Beispiel: 3 * 4 ergibt 12. 12 / 3 ergibt 4 und 12 / 4 ergibt 3. Statt / ist in diesem Fall div besser. So hat man immer Divisionsaufgaben, die "aufgehen" :) |
Alle Zeitangaben in WEZ +1. Es ist jetzt 08:21 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz