Einzelnen Beitrag anzeigen

paresy

Registriert seit: 24. Aug 2004
Ort: Lübeck
105 Beiträge
 
Delphi 2007 Professional
 
#15

Re: 10 Zahl und Erg. -> + u. - Berechnen

  Alt 28. Dez 2004, 23:16
ich hab den code von Ka1 genommen und nen wenig optimiert... damit die string vergleiche raus sind und nen proof einbaut der nen error ausgibt wenn was nicht stimmen sollte

Delphi-Quellcode:
unit Unit1;

interface

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

type
  TNumbers = record
   myResult : Integer;
   myNumbers : Array[0..9] of Integer;
  end;

  TForm1 = class(TForm)
    lblIS: TLabel;
    edResult: TEdit;
    btnCalc: TButton;
    lstResults: TListBox;
    procedure btnCalcClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    EditsNumbers : Array[0..9] of TEdit;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

const DefaultNumbers: TNumbers = (myResult: -10;
                                  myNumbers: (1,2,3,5,9,7,3,6,9,1);
                                  );

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var i: Integer;
begin

 for i:=0 to 9 do
  begin
   EditsNumbers[i] := TEdit.Create(Self);
   EditsNumbers[i].Parent := Self;
   EditsNumbers[i].Top := 8;
   EditsNumbers[i].Left := 8+i*36;
   EditsNumbers[i].Width := 25;
   EditsNumbers[i].TabOrder := i;
  end;

 //Load default Numbers
 for i:=0 to 9 do
  EditsNumbers[i].Text := IntToStr(DefaultNumbers.myNumbers[i]);
 edResult.Text := IntToStr(DefaultNumbers.myResult);

end;

procedure TForm1.btnCalcClick(Sender: TObject);
var
  i,i1 : Integer;
  W : Integer;
  S1 : String;
  Proof : Integer;
  Numbers: TNumbers;
begin

  //copy numbers
  for i:=0 to 9 do
   Numbers.myNumbers[i] := StrToInt(EditsNumbers[i].Text);
  Numbers.myResult := StrToInt(edResult.Text);

  lstResults.Clear;
  For i:=0 to 511 do
  begin
    W:=Numbers.myNumbers[0];
    For i1:=1 to 9 do
    begin
     If (i and (1 shl i1)) = 0 then
      W:=W-Numbers.myNumbers[i1]
     else
      W:=W+Numbers.myNumbers[i1];
    end;
    If Numbers.myResult=W then
    begin
      S1:=IntToStr(Numbers.myNumbers[0]);
      Proof := Numbers.myNumbers[0];
      For i1:=1 to 9 do
       If (i and (1 shl i1)) = 0 then
        begin
         S1:=S1+'-'+IntToStr(Numbers.myNumbers[i1]);
         Proof := Proof - Numbers.myNumbers[i1];
        end
       else
        begin
         S1:=S1+'+'+IntToStr(Numbers.myNumbers[i1]);
         Proof := Proof + Numbers.myNumbers[i1];
        end;
       if Proof <> Numbers.myResult then
        S1 := 'ERROR: '+S1;
       lstResults.Items.Add(S1+'='+IntToStr(Numbers.myResult));
    end;
  end;
end;

end.
grüße, paresy
Angehängte Dateien
Dateityp: rar matheapp_246.rar (4,7 KB, 8x aufgerufen)
  Mit Zitat antworten Zitat