Here is a simple calculator program I made in Delphi. There is also an IF THEN ELSE statement if the user is trying to divide by zero so that we do not encounter a run-time error.
The activity is based on an activity from the “Delphi for CAPS Part 1” textbook by Marina Myburgh.
Code excerpt
procedure TForm1.btnClearClick(Sender: TObject); begin edtX.Text := IntToStr(0); edtY.Text := IntToStr(0); lblAnswer.Caption := '...'; end; procedure TForm1.btnDivideClick(Sender: TObject); var ianswer : real; begin ix := StrToInt(edtX.Text); iy := StrToInt(edtY.Text); if (iy=0) then ShowMessage('I cannot divide by zero') else begin ianswer := ix/iy; lblAnswer.Caption := IntToStr(ix) + ' / ' + IntToStr(iy) + ' = ' +FloatToStrf(ianswer,ffFixed,4,2); end; end; procedure TForm1.btnMinusClick(Sender: TObject); begin ix := StrToInt(edtX.Text); iy := StrToInt(edtY.Text); ianswer := ix-iy; lblAnswer.Caption := IntToStr(ix) + ' - ' + IntToStr(iy) + ' = ' + IntToStr(ianswer); end; procedure TForm1.btnMultiplyClick(Sender: TObject); begin ix := StrToInt(edtX.Text); iy := StrToInt(edtY.Text); ianswer := ix*iy; lblAnswer.Caption := IntToStr(ix) + ' X ' + IntToStr(iy) + ' = ' + IntToStr(ianswer); end; procedure TForm1.btnPlusClick(Sender: TObject); begin ix := StrToInt(edtX.Text); iy := StrToInt(edtY.Text); ianswer := ix+iy; lblAnswer.Caption := IntToStr(ix) + ' + ' + IntToStr(iy) + ' = ' + IntToStr(ianswer); end;
Be the first to comment