Some useful Delphi codes part 1 - Numbers

March 5th, 2007 by William Yang

Over the years I have made quite a few software programs in Delphi, and inevitably I have also come up with many useful functions. Most of them I have never published on the Internet before, now I would like to list on blog.

Part 1 are functions for managing integer or real numbers, they were written very early in 1997.

Show Code | Download Numman.pas

  1.  
  2. { ---------------------------------------------------
  3.     Numbers Manager  Copyright (r) by
  4.     Version : 1.75   Author : William Yang
  5.     Last Update 24 - Aug - 97
  6.   --------------------------------------------------- }
  7.  
  8. unit NumMan;
  9.  
  10. interface
  11.  
  12. uses Classes, SysUtils, Windows;
  13.  
  14. // Force an integer number to be between certain range
  15. function MakeBetween(S, nFrom, nTo : Integer) : Integer;
  16. // Check if an integer is between n1 and n2
  17. function Between(S, N1, N2 : Integer) : Boolean;
  18. // Check if an real/float number is between n1 and n2
  19. function fBetween(S, N1, N2 : Real) : Boolean;
  20. // Calculate rectangular width
  21. function RectWidth(Rect: TRect) : Integer;
  22. // Calculate rectangular height
  23. function RectHeight(Rect: TRect) : Integer;
  24. // Find smallest integer in an array
  25. function MinMost(Nums: array of Integer): Integer;
  26. // Find largest integer in an array
  27. function MaxMost(Nums: array of Integer): Integer;
  28. // Check if the integers in an array are equal
  29. function AllEqual(Nums: array of Integer): Boolean;
  30. // Check if the integers in an array are different
  31. function AllDiff(Nums: array of Integer): Boolean;
  32. //Check if these numbers in the range
  33. function AllBetween(Nums: array of Integer; nFrom, nTo : Integer): Boolean;
  34. {Check if the numbers are like (1, 2, 3, 4, 5),
  35. you can set InOrder to false if you want check(4,2,3,5,1) }
  36. function IsIncreasement(Nums: array of Integer; InOrder: Boolean): Boolean;
  37. {more customisable with amount that increase }
  38. function IsIncreasementExt(Nums: array of Integer; InOrder: Boolean;
  39.   Incs: Integer): Boolean;
  40. //Find a number an array of numbers, returns the index of the first catch.
  41. function FindNum(Num: Integer; Nums: array of Integer): Integer;
  42. //Find pairs, returns the total amount of pairs.
  43. function FindPairs(Nums: array of Integer): Integer;
  44. //Find the how many times the number appears.
  45. function NumAppears(Num: Integer; Nums: array of Integer): Integer;
  46. // A byte has 8 bits, ReadBits returns number value between certain bits in an integer
  47. function ReadBits(Num, Start, Count: Integer): Integer;
  48. // Returns how many bits are used to store this integer, e.g. 8  returns 4,  7 return 3
  49. function MaxBits(Num: Integer): Integer;
  50. // Translate integer to binaries
  51. function IntToBin(Num: Integer): String;
  52. // Modify certain bits in an integer
  53. function WriteBits(Num, Start, Val: Integer): Integer;
  54. // Integer swap
  55. procedure ISwap(var n1, n2: Integer);
  56. // Byte swap
  57. procedure BSwap(var n1, n2: Byte);
  58. // Real/ float number swap
  59. procedure FSwap(var n1, n2: Double);
  60. // Round up an real number by certain integer value, e.g. RoundBy(67.4, 10) return 70
  61. function RoundBy(ANum: Real; By: Integer): Integer;
  62. // Smallest float number
  63. function MinFloat(X, Y: Extended): Extended;
  64. // Largest float number
  65. function MaxFloat(X, Y: Extended): Extended;
  66.  
  67. implementation
  68.  
  69. function fBetween(S, N1, N2 : Real) : Boolean;
  70. begin
  71.   if (S >= N1) and (S <= N2) then
  72.     Result := True
  73.   else
  74.     Result := False;
  75. end;
  76.  
  77. function RoundBy(ANum: Real; By: Integer): Integer;
  78. begin
  79.   Result := Round(ANum / By);
  80.   Result := Result*By;
  81. end;
  82.  
  83. procedure ISwap(var n1, n2: Integer);
  84. var
  85.   t: Integer;
  86. begin
  87.   t := n1;
  88.   n1 := n2;
  89.   n2 := t;
  90. end;
  91.  
  92. procedure BSwap(var n1, n2: Byte);
  93. var
  94.   t: Byte;
  95. begin
  96.   t := n1;
  97.   n1 := n2;
  98.   n2 := t;
  99. end;
  100.  
  101. procedure FSwap(var n1, n2: Double);
  102. var
  103.   t: Double;
  104. begin
  105.   t := n1;
  106.   n1 := n2;
  107.   n2 := t;
  108. end;
  109.  
  110. function WriteBits(Num, Start, Val: Integer): Integer;
  111. begin
  112.   Val := Val shl (Start - 1);
  113.   Result := Num or Val;
  114. end;
  115.  
  116. function MaxBits(Num: Integer): Integer;
  117. begin
  118.   Result := 0;
  119.   repeat
  120.     Num := Num shr 1;
  121.     Inc(Result);
  122.   until Num <= 0;
  123. end;
  124.  
  125. function IntToBin(Num: Integer): String;
  126. var
  127.   Mask: Integer;
  128.   i, Bits: Integer;
  129. begin
  130.   Result := ''; Mask := 1;
  131.   Bits := MaxBits(Num);
  132.   for i := 1 to bits do
  133.   begin
  134.     if (Num and Mask) = Mask then
  135.       Result := Result + '1'
  136.     else
  137.       Result := Result + '0';
  138.     Mask := Mask shl 1;
  139.   end;
  140. end;
  141.  
  142. function ReadBits(Num, Start, Count: Integer): Integer;
  143. var
  144.   BitMask: Integer;
  145.   i, Max: Integer;
  146. begin
  147.   Max := MaxBits(Num);
  148.   {
  149.        0000 1111
  150.   and  1011 0111
  151.   ---- ---- ----
  152.        0000 0111
  153.   }
  154.   //Initialize Bitmask with 0.
  155.   BitMask := 0;
  156.   for i := Max downto 1 do
  157.   begin
  158.     if (i >= Start) and (i <= Start + Count - 1) then
  159.     begin
  160.       Bitmask := Bitmask or 1;
  161.     end;
  162.     if i > 1 then
  163.     begin
  164.       BitMask := BitMask shl 1;
  165.     end;
  166.   end;
  167.   Result := BitMask and Num;
  168.   Result := Result shr (Start - 1)
  169. end;
  170.  
  171. function FindPairs(Nums: array of Integer): Integer;
  172. var
  173.   i: Integer;
  174. begin
  175.   Result := 0;
  176.   for i := Low(Nums) to High(Nums) do
  177.   begin
  178.     if NumAppears(Nums[i], Nums) = 2 then
  179.       Inc(Result);
  180.   end;
  181.   Result := Result div 2;
  182. end;
  183.  
  184. function FindNum(Num: Integer; Nums: array of Integer): Integer;
  185. var
  186.   i:Integer;
  187. begin
  188.   Result := -1;
  189.   for i := Low(Nums) to High(Nums) do
  190.   begin
  191.     if Nums[i] = Num then
  192.     begin
  193.       Result := i;
  194.       Exit;
  195.     end;
  196.   end;
  197. end;
  198.  
  199. function NumAppears(Num: Integer; Nums: array of Integer): Integer;
  200. var
  201.   i:Integer;
  202. begin
  203.   Result := 0;
  204.   for i := Low(Nums) to High(Nums) do
  205.   begin
  206.     if Nums[i] = Num then
  207.     begin
  208.       Inc(Result);
  209.     end;
  210.   end;
  211. end;
  212.  
  213. function IsIncreasementExt(Nums: array of Integer; InOrder: Boolean;
  214.   Incs: Integer): Boolean;
  215. var
  216.   i,j, k : Integer;
  217. begin
  218.   Result := True;
  219.   if InOrder then
  220.   begin
  221.     j := Nums[Low(Nums)] + Incs;
  222.     for i := Low(Nums) + 1 to High(Nums) do
  223.     begin
  224.       if Nums[i] <> J then
  225.       begin
  226.         Result := False;
  227.         Exit;
  228.       end;
  229.       Inc(j, Incs);
  230.     end;
  231.   end
  232.   else
  233.   begin
  234.     k := MinMost(Nums);
  235.     //Get the smallest number to start with.
  236.     j := k + Incs;
  237.     while (FindNum(j, Nums) <> - 1) do
  238.     begin
  239.       Inc(j, Incs);
  240.     end;
  241.     //if j is equal to the total increasement + minmost value.
  242.     if j = k + (High(Nums) - Low(Nums)) * Incs then
  243.       Result := True
  244.     else
  245.       Result := False;
  246.   end;
  247. end;
  248.  
  249. function IsIncreasement(Nums: array of Integer; InOrder: Boolean): Boolean;
  250. begin
  251.   Result := IsIncreasementExt(Nums, InOrder, 1);
  252. end;
  253.  
  254. function AllBetween(Nums: array of Integer; nFrom, nTo : Integer): Boolean;
  255. var
  256.   i:Integer;
  257. begin
  258.   Result := True;
  259.   for i := Low(Nums) to High(Nums) do
  260.   begin
  261.     if not Between(Nums[i], nFrom, nTo) then
  262.     begin
  263.       Result := False;
  264.       Exit;
  265.     end;
  266.   end;
  267.  
  268. end;
  269.  
  270. function AllDiff(Nums: array of Integer): Boolean;
  271. var
  272.   i, j : Integer;
  273. begin
  274.   Result := True;
  275.   for i := Low(Nums) to High(Nums) do
  276.     for j := Low(Nums) to High(Nums) do
  277.     begin
  278.       if (i<>j) and (Nums[j] = Nums[i]) then
  279.       begin
  280.         Result := False;
  281.         Exit;
  282.       end;
  283.     end;
  284. end;
  285.  
  286. function AllEqual(Nums: array of Integer): Boolean;
  287. var
  288.   i : Integer;
  289. begin
  290.   Result := True;
  291.   for i := Low(Nums) + 1 to High(Nums) do
  292.   begin
  293.     if Nums[Low(Nums)] <> Nums[i] then
  294.     begin
  295.       Result := False;
  296.       Exit;
  297.     end;
  298.   end;
  299. end;
  300.  
  301. function MinMost(Nums: array of Integer): Integer;
  302. var
  303.   i,j, k : Integer;
  304. begin
  305.   //Go through each numbers.
  306.   for i := Low(Nums) to High(Nums) do
  307.   begin
  308.     k := 0;
  309.     //check if this number is smaller than others
  310.     for j := Low(Nums) to High(Nums) do
  311.     begin
  312.       if (Nums[i] <= Nums[j]) and (i <> j) then
  313.         Inc(k);
  314.     end;
  315.     {If there is 5 numbers, if a number smaller than other 4
  316.     then it is the smallest}
  317.     if k = High(Nums) - Low(Nums) then
  318.       Result := Nums[i];
  319.   end;
  320. end;
  321.  
  322. function MaxMost(Nums: array of Integer): Integer;
  323. var
  324.   i,j, k : Integer;
  325. begin
  326.   for i := Low(Nums) to High(Nums) do
  327.   begin
  328.     k := 0;
  329.     for j := Low(Nums) to High(Nums) do
  330.     begin
  331.       if (Nums[i] >= Nums[j]) and (i <> j) then
  332.         Inc(k);
  333.     end;
  334.     if k = High(Nums) - Low(Nums) then
  335.       Result := Nums[i];
  336.   end;
  337. end;
  338.  
  339. function RectWidth(Rect: TRect) : Integer;
  340. begin
  341.   Result := Rect.Right - Rect.Left;
  342. end;
  343.  
  344. function RectHeight(Rect: TRect) : Integer;
  345. begin
  346.   Result := Rect.Bottom - Rect.Top;
  347. end;
  348.  
  349. Function Min(X, Y : Integer) : Integer;
  350. begin
  351.   if X<=Y then
  352.     Result := X
  353.   else
  354.     Result := y;
  355. end;
  356.  
  357. Function Max(X, Y : Integer) : Integer;
  358. begin
  359.   if X>=Y then
  360.     Result := X
  361.   else
  362.     Result := y;
  363. end;
  364.  
  365. function MinFloat(X, Y: Extended): Extended;
  366. begin
  367.   if X < Y then Result := X else Result := Y;
  368. end;
  369.  
  370. function MaxFloat(X, Y: Extended): Extended;
  371. begin
  372.   if X > Y then Result := X else Result := Y;
  373. end;
  374.  
  375. function Between(S, N1, N2 : Integer) : Boolean;
  376. begin
  377.   if (S >= N1) and (S <= N2) then
  378.     Result := True
  379.   else
  380.     Result := False;
  381. end;
  382.  
  383. function MakeBetween(S, nFrom, nTo : Integer) : Integer;
  384. begin
  385.   Result := S;
  386.   while Result < nFrom do
  387.   begin
  388.     Result := Result + (nTo - nFrom);
  389.   end;
  390.   while Result > nTo do
  391.   begin
  392.     Result := Result - (nTo - nFrom);
  393.   end;
  394. end;
  395.  
  396.  
  397. end.
  398.  

Related links

adelaide web design


Leave a Reply