Mega Code Archive

 
Categories / Delphi / Examples
 

Numberlinedistance

unit NumUnit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; const InitialValue = 1000000; MaxNumStrLength = 255; type TMainForm = class(TForm) Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; Label6: TLabel; Label7: TLabel; Edit1: TEdit; Edit2: TEdit; GetResultButton1: TButton; Edit3: TEdit; Label8: TLabel; Label9: TLabel; Edit4: TEdit; Edit5: TEdit; GetResultButton2: TButton; Edit6: TEdit; ExitButton: TButton; Label10: TLabel; Label11: TLabel; Label12: TLabel; Label13: TLabel; Label14: TLabel; Label15: TLabel; Label16: TLabel; Label17: TLabel; Label18: TLabel; procedure ExitButtonClick(Sender: TObject); procedure GetResultButton1Click(Sender: TObject); procedure GetResultButton2Click(Sender: TObject); private //private declarations function NumberLineDistReal(valOne, valTwo: Real): Real; function NumberLineDistInteger(valOne, valTwo: Integer): Integer; function IsConvertibleToInt(convStr: String): Boolean; function IsConvertibleToFloat(convStr: String): Boolean; public //public declarations end; var MainForm: TMainForm; implementation {$R *.DFM} //////////////////////////////////////////////////// procedure TMainForm.ExitButtonClick(Sender: TObject); begin Close; end; ////////////////////////////////////////////////////////// procedure TMainForm.GetResultButton1Click(Sender: TObject); var edit1Int: Integer; edit2Int: Integer; numLineDist: Integer; tempString: String; begin if ((IsConvertibleToInt(Edit1.Text) = True) and (IsConvertibleToInt(Edit2.Text) = True)) then begin edit1Int := StrToInt(Edit1.Text); edit2Int := StrToInt(Edit2.Text); numLineDist := NumberLineDistInteger(edit1Int, edit2Int); Edit3.Text := IntToStr(numLineDist); end else begin tempString := 'Please input only valid integer values'; Application.MessageBox(PChar(tempString), ' Invalid Input Value', mb_OK); end; end; ////////////////////////////////////////////////////////// procedure TMainForm.GetResultButton2Click(Sender: TObject); var edit4Real: Real; edit5Real: Real; numLineDist: Real; tempString: String; begin if ((IsConvertibleToFloat(Edit4.Text) = True) and (IsConvertibleToFloat(Edit5.Text) = True)) then begin edit4Real := StrToFloat(Edit4.Text); edit5Real := StrToFloat(Edit5.Text); numLineDist := NumberLineDistReal(edit4Real, edit5Real); Edit6.Text := FloatToStrF(numLineDist, ffGeneral, 5, 5); end else begin tempString := 'Please input only valid decimal values'; Application.MessageBox(PChar(tempString), ' Invalid Input Value', mb_OK); end; end; ///////////////////////////////////////////////////////////////// function TMainForm.NumberLineDistReal(valOne, valTwo: Real): Real; //UTILITY FUNCTION //simple arithmetic operations of the kind that you do all the time //in graphics can produce erroneous outputs if one is not extremely //careful about 'abs-ing' values where appropriate: this function //bundles up the algorithm to find the 'absolute distance along the //number line' between two Real values so that we can just call it //up as appropriate... begin Result := InitialValue; if ((valOne >= 0) and (valTwo >= 0)) then begin if (valOne >= valTwo) then Result := (valOne - valTwo) else Result := (valTwo - valOne); end; if ((valOne < 0) and (valTwo >= 0)) then begin if (valOne >= valTwo) then Result := (abs(valOne) + valTwo) else Result := (abs(valOne) + valTwo); end; if ((valOne >= 0) and (valTwo < 0)) then begin if (valOne >= valTwo) then Result := (valOne + abs(valTwo)) else Result := (abs(valTwo) + valOne); end; if ((valOne < 0) and (valTwo < 0)) then begin if (valOne >= valTwo) then Result := (abs(valTwo) - abs(valOne)) else Result := (abs(valOne) - abs(valTwo)); end; end; ////////////////////////////////////////////////////////////////////////// function TMainForm.NumberLineDistInteger(valOne, valTwo: Integer): Integer; //UTILITY FUNCTION //see notes in NumberLineDistReal() above... begin Result := InitialValue; if ((valOne >= 0) and (valTwo >= 0)) then begin if (valOne >= valTwo) then Result := (valOne - valTwo) else Result := (valTwo - valOne); end; if ((valOne < 0) and (valTwo >= 0)) then begin if (valOne >= valTwo) then Result := (abs(valOne) + valTwo) else Result := (abs(valOne) + valTwo); end; if ((valOne >= 0) and (valTwo < 0)) then begin if (valOne >= valTwo) then Result := (valOne + abs(valTwo)) else Result := (abs(valTwo) + valOne); end; if ((valOne < 0) and (valTwo < 0)) then begin if (valOne >= valTwo) then Result := (abs(valTwo) - abs(valOne)) else Result := (abs(valOne) - abs(valTwo)); end; end; /////////////////////////////////////////////////////////////// function TMainForm.IsConvertibleToInt(convStr: String): Boolean; {a general purpose function to test whether a string CAN be converted to an integer value. Useful to avoid EConvertError exceptions and also useful to have in a program when coding and testing to avoid the possibility of the program hanging on you...} type TDigChrs = set of Char; var tempArray: array[0..MaxNumStrLength] of Char; idx: Integer; lineLength: Integer; validIntStr: Boolean; digitChars: TDigChrs; begin digitChars := ['0'..'9']; validIntStr := True; digitChars := digitChars + ['-']; lineLength := Length(convStr); //copy the line into an array and //test each character as you go... for idx := 0 to (lineLength - 1) do begin tempArray[idx] := convStr[idx + 1]; if (not(tempArray[idx] in digitChars)) then validIntStr := False; if (idx >= MaxNumStrLength) then break; end; if (lineLength = 0) then validIntStr := False; Result := validIntStr; end; ///////////////////////////////////////////////////////////////// function TMainForm.IsConvertibleToFloat(convStr: String): Boolean; {a general purpose function to test whether a string CAN be converted to a floating point value. Useful to avoid EConvertError exceptions and also useful to have in a program when coding and testing to avoid the possibility of the program hanging on you...} type TDigChrs = set of Char; var tempArray: array[0..MaxNumStrLength] of Char; idx: Integer; lineLength: Integer; validFloatStr: Boolean; digitChars: TDigChrs; begin digitChars := ['0'..'9']; validFloatStr := True; digitChars := digitChars + ['-', '.']; lineLength := Length(convStr); {copy the line into an array and test each character as you go...} for idx := 0 to (lineLength - 1) do begin tempArray[idx] := convStr[idx + 1]; if (not(tempArray[idx] in digitChars)) then validFloatStr := False; if (idx >= MaxNumStrLength) then break; end; if (lineLength = 0) then validFloatStr := False; Result := validFloatStr; end; end.