Search code examples
lazarus

Delphi Exponent Calculator


Sorry for my English, as I am from Germany.

I built a program: http://i.epvpimg.com/I0xie.png

And I want an exponent calculator (I am learning for test in school), but I have a problem...

If I do the number "Zahl" (meaning "number" in German). For example: Number= "2", then I do exponent = "1".

Normally I should get the result 2 but I am getting a 4, why?

What is the problem?

Here is my Code:

unit unit_oberflaeche;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, unit_inhalt;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);

  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  rechner: Texponentrechner;

implementation

{$R *.lfm}

{ TForm1 }


procedure TForm1.FormCreate(Sender: TObject);
begin
    rechner := Texponentrechner.Create;
end;

procedure TForm1.Button1Click(Sender: TObject);
VAR i, LVexponent, LVzahl, result: INTEGER;
     BEGIN
     LVexponent := StrToInt(Edit2.Text);
     LVzahl     := StrToInt(Edit1.Text);
     rechner.set_exponent(LVexponent);
     rechner.set_zahl(LVzahl);
         FOR i := 1 TO LVexponent DO
             BEGIN
               result := result * LVzahl

             end;
         //result := LVzahl;
     Panel1.Caption := IntToStr(result);
     end;

end.

And here is the other part:

unit unit_inhalt;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils;


TYPE
  Texponentrechner = class
  private
    { private declarations }
    Fexponent : INTEGER;
    Fzahl     : INTEGER;

  public
    { public declarations }

    procedure set_exponent   (WPexponent:INTEGER);
    procedure set_zahl       (WPzahl:INTEGER);

    function  berechne_betrag():INTEGER;
    end;




implementation

procedure Texponentrechner.set_exponent(WPexponent:INTEGER);
 BEGIN
      Fexponent := WPexponent;
end;

procedure Texponentrechner.set_zahl(WPzahl:INTEGER);
 BEGIN
      Fzahl := WPzahl;
 end;

function Texponentrechner.berechne_betrag():INTEGER;
 BEGIN
      result := Fzahl * Fzahl;
 end;

end.

Solution

  • I assume that your exponent calculation has to be done in Texponentrechner class. First, your calculation there is wrong because it returns your number multiplied by itself, and second you are never calling that function in the first place.

    So your berechne_betrag function should look like this:

    function Texponentrechner.berechne_betrag(): integer;
    var i: integer;
    begin
      Result := 1;
      for i := 1 to Fexponent do
        Result := Result * Fzahl;
    end;
    

    Then you should actually call that function to get the result

    rechner.set_exponent(LVexponent);
    rechner.set_zahl(LVzahl);
    result := rechner.berechne_betrag;
    Panel1.Caption := IntToStr(result);
    

    Also you are creating rechner object instance in FormCreate, but you are never releasing it and thus you are creating memory leak. You should call rechner.Free when you are finished using object. Since you have made it global var you create in FormCreate, proper place to release it will be in FormDestroy

    But even better practice would be to make it local to Button1Click method.

    ...
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        Label1: TLabel;
        Label2: TLabel;
        Panel1: TPanel;
        procedure Button1Click(Sender: TObject);
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.lfm}
    
    { TForm1 }
    
    procedure TForm1.Button1Click(Sender: TObject);
    var 
      LVexponent, LVzahl, result: integer;
      rechner: Texponentrechner;
    begin
      rechner := Texponentrechner.Create;
      try
        LVexponent := StrToInt(Edit2.Text);
        LVzahl     := StrToInt(Edit1.Text);
        rechner.set_exponent(LVexponent);
        rechner.set_zahl(LVzahl);
        result := rechner.berechne_betrag;
        Panel1.Caption := IntToStr(result);
      finally
        rechner.Free;
      end;
    end;
    
    end.