Advertisement
WarPie90

KeyDistance

Dec 8th, 2013
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.73 KB | None | 0 0
  1. program New;
  2.  
  3. function KeyDistance(CharA, CharB: Char): Extended;
  4. var
  5.   kbd:Array of TStrArray;
  6.   i,j,l:Integer;
  7.   A,B:TPoint;
  8.   CharASet,CharBSet:Boolean;
  9. begin
  10.   SetLength(kbd, 4);
  11.   SetLength(kbd[0], 12);
  12.   kbd[0][0] := '1!';
  13.   kbd[0][1] := '2@';
  14.   kbd[0][2] := '3#';
  15.   kbd[0][3] := '4$';
  16.   kbd[0][4] := '5%';
  17.   kbd[0][5] := '6^';
  18.   kbd[0][6] := '7&';
  19.   kbd[0][7] := '8*';
  20.   kbd[0][8] := '9(';
  21.   kbd[0][9] := '0)';  
  22.   kbd[0][10] := '-_';
  23.   kbd[0][11] := '+=';  
  24.   SetLength(kbd[1], 12);
  25.   kbd[1][0] := 'qQ';
  26.   kbd[1][1] := 'wW';
  27.   kbd[1][2] := 'eE';
  28.   kbd[1][3] := 'rR';
  29.   kbd[1][4] := 'tT';
  30.   kbd[1][4] := 'yY';
  31.   kbd[1][5] := 'uU';
  32.   kbd[1][6] := 'iI';
  33.   kbd[1][7] := 'oO';
  34.   kbd[1][8] := 'pP';
  35.   kbd[1][9] := '{[';
  36.   kbd[1][10] := '}]';
  37.   kbd[1][11] := '|\';  
  38.   SetLength(kbd[2], 11);
  39.   kbd[2][0] := 'aA';  
  40.   kbd[2][1] := 'sS';
  41.   kbd[2][2] := 'dD';
  42.   kbd[2][3] := 'fF';
  43.   kbd[2][4] := 'gG';
  44.   kbd[2][5] := 'hH';
  45.   kbd[2][6] := 'jJ';
  46.   kbd[2][7] := 'kK';
  47.   kbd[2][8] := 'lL';
  48.   kbd[2][9] := ':;';
  49.   kbd[2][10] := '"''';
  50.   SetLength(kbd[3], 11);
  51.   kbd[3][0] := '~';
  52.   kbd[3][1] := 'zZ';
  53.   kbd[3][2] := 'xX';
  54.   kbd[3][3] := 'cC';
  55.   kbd[3][4] := 'vV';
  56.   kbd[3][5] := 'bB';
  57.   kbd[3][6] := 'nN';
  58.   kbd[3][7] := 'mM';
  59.   kbd[3][8] := ',<';
  60.   kbd[3][9] := '.>';
  61.   kbd[3][10] := '?/';
  62.  
  63.   A.x := 2; //centralize unknown chars | to not muss up measuring.
  64.   A.y := 5;  
  65.   B.x := 2;
  66.   B.y := 5;
  67.   for l:=0 to High(kbd) do
  68.     for i:=0 to High(kbd[l]) do
  69.       for j:=1 to Length(kbd[l][i]) do
  70.       begin
  71.         if (kbd[l][i][j] = charA) and not(CharASet) then
  72.         begin
  73.           A.x := l;
  74.           A.y := i+j-1;
  75.           CharASet := True;
  76.         end;
  77.         if (kbd[l][i][j] = charB) and not(CharBSet) then
  78.         begin
  79.           B.x := l;
  80.           B.y := i+j-1;
  81.           CharBSet := True;
  82.         end;
  83.         if CharBSet and CharASet then Break;  
  84.       end;
  85.   Result := Sqrt(Sqr(A.x-B.x) + Sqr(A.y-B.y));  
  86. end;
  87.  
  88. function StrQwertyDist(StrA, StrB:String): Extended;
  89. var
  90.   i,L,lenA,lenB:Integer;
  91.   Undefined: Char;
  92. begin
  93.   Undefined := '|';
  94.   LenA := Length(StrA);
  95.   LenB := Length(StrB);  
  96.   L := Max(LenA, LenB);
  97.  
  98.   for i:=1 to L do
  99.   begin
  100.     if (i > lenA) then
  101.     begin
  102.       Result := Result + KeyDistance(Undefined, StrB[i]);  
  103.     end else if (i > lenB) then
  104.     begin
  105.       Result := Result + KeyDistance(StrA[i], Undefined);  
  106.     end else
  107.       Result := Result + KeyDistance(StrA[i], StrB[i]);  
  108.   end;
  109. end;
  110.  
  111. begin
  112.   WriteLn(StrQwertyDist('Hello world', 'Hwllp workf'));
  113.   WriteLn(StrQwertyDist('This is a long string', 'thix ia a lpng strkng'));
  114.   WriteLn(StrQwertyDist('Testing someting else here', 'what in the world is going on?'));
  115. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement