FANDOM


Hope this helps Delphi users just starting out. JMcSub 18:34, June 14, 2010 (UTC)

JMcSub, thank you for your article. Some things I would do different is use enumerations instead of string indicators. i.e.

Type
  TMathCalculation = (mcNone, mcAddition, mcSubtraction, mcDivision, mcMultplication);

This will help with any typo's.

Also try prevent code repetition.

To prevent this I use procedure InputNumber with the same parameters as an onclick event. in there i use the caption of the button to fill the edit. Instead of 11 onclick events there is just 1.

Also to prevent the error which you can get when the program is used on a pc with a different decimal seperator then the '.'

mind you i used some different names for some of the components and functions

unit Unit1;

interface

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

Type
  TMathCalculation = (mcNone, mcAddition, mcSubtraction, mcDivision,
    mcMultiplication);


type
  TCalculatorForm = class(TForm)
    edtNumber: TEdit;
    btn1: TButton;
    btn2: TButton;
    btn3: TButton;
    btn4: TButton;
    btn5: TButton;
    btn6: TButton;
    btn7: TButton;
    btn8: TButton;
    btn9: TButton;
    btn0: TButton;
    btnNegativePositive: TButton;
    btnClear: TButton;
    btnReset: TButton;
    btnEnter: TButton;
    lblOutcome: TLabel;
    btnAddition: TButton;
    btnSubtraction: TButton;
    btnDivision: TButton;
    btnMultiplication: TButton;
    btnDecimal: TButton;
    procedure InputNumber(Sender: TObject);
    procedure btnNegativePositiveClick(Sender: TObject);
    procedure btnClearClick(Sender: TObject);
    procedure btnResetClick(Sender: TObject);
    procedure btnAdditionClick(Sender: TObject);
    procedure btnSubtractionClick(Sender: TObject);
    procedure btnDivisionClick(Sender: TObject);
    procedure btnMultiplicationClick(Sender: TObject);
    procedure btnEnterClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    FMath: TMathCalculation;
    FNumber: Real;
    procedure SetCalculationType(CalcType: TMathCalculation);
  public
    { Public declarations }
  end;

var
  CalculatorForm: TCalculatorForm;

implementation

{$R *.dfm}


procedure TCalculatorForm.InputNumber(Sender: TObject);
begin
  edtNumber.Text := edtNumber.Text + (Sender as TButton).Caption;
end;

procedure TCalculatorForm.btnNegativePositiveClick(Sender: TObject);
var
  OriginalNumber: Real;
  TextNumber: string;
begin
  edtNumber.Text := FloatToStr(-(StrToFloat(edtNumber.Text)));
end;

procedure TCalculatorForm.btnClearClick(Sender: TObject);
begin
  edtNumber.Clear;
end;

procedure TCalculatorForm.btnResetClick(Sender: TObject);
begin
  lblOutcome.Caption := '';
  edtNumber.Clear;
  FNumber := 0;
  FMath := mcNone;
end;

procedure TCalculatorForm.SetCalculationType(CalcType: TMathCalculation);
begin
  FMath := CalcType;
  FNumber := StrToFloat(edtNumber.Text);
  edtNumber.Clear;
end;

procedure TCalculatorForm.btnAdditionClick(Sender: TObject);
begin
  SetCalculationType(mcAddition);
end;

procedure TCalculatorForm.btnSubtractionClick(Sender: TObject);
begin
  SetCalculationType(mcSubtraction);
end;

procedure TCalculatorForm.btnDivisionClick(Sender: TObject);
begin
  SetCalculationType(mcDivision);
end;

procedure TCalculatorForm.btnMultiplicationClick(Sender: TObject);
begin
  SetCalculationType(mcMultiplication);
end;

procedure TCalculatorForm.btnEnterClick(Sender: TObject);
const
  CalcOperators: String= '+-/*';
var
  Answer, SNumber: real;
  FNumberText,SNumberText,OutComeText: string;
  CalcOperator: Char;
begin
  SNumber := StrToFloat(edtNumber.Text);
  case FMath of
    mcAddition:       Answer := FNumber + SNumber;
    mcSubtraction:    Answer := FNumber - SNumber;
    mcDivision:       Answer := FNumber / SNumber;
    mcMultiplication: Answer := FNumber * SNumber;
  else
    Answer := NaN;
  end;

  OutComeText := FormatFloat('0.#####', Answer);
  FNumberText := FormatFloat('0.#####', FNumber);
  CalcOperator := CalcOperators[Integer(FMath)];
  SNumberText := FormatFloat('0.#####', SNumber);
  lblOutcome.Caption :=
    FNumberText + ' ' + CalcOperator + ' ' + SNumberText + ' = ' + OutComeText;
  edtNumber.Clear;
end;


procedure TCalculatorForm.FormCreate(Sender: TObject);
begin
  btnDecimal.Caption := DecimalSeparator;
end;

end.

Maybe I will change some things in your article later when I get home Xsintill 08:37, June 15, 2010 (UTC)


Yep, those look like great changes! Thanks! JMcSub 13:36, June 15, 2010 (UTC)


I tried to implement your "InputNumber" procedure into my code, but I could not get it to work. Do I need to create an OnClick event that calls this procedure as a sub-routine for every number button? Thanks. JMcSub 13:31, June 16, 2010 (UTC)

what you should do is put the InputNumber procedure in the same section as where delphi puts it's generated code. After that you can go to the onclick event in the object inspector and use the combobox to select the InputNumber procedure. i am building up a new machine at home. haven't got delphi here. Xsintill 15:56, June 16, 2010 (UTC)
--------Okay, I gotch'a. I changed that portion of the article, too. I'll try to implement the other changes, too. JMcSub 16:07, June 16, 2010 (UTC)

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.