Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Problem mit php script (https://www.delphipraxis.net/37097-problem-mit-php-script.html)

transporter2 30. Dez 2004 23:57


Problem mit php script
 
Kann das jemand bitte in delphi übersetzen:


function new_coord($x,$y)
{
$x = (string) (($x + 250) / 0.5);
$y = (string) (($y + 250) / 0.5);

if($x < 0 || $y < 0) {
return false;
}

return array('con' => substr($y, 0, 1).substr($x, 0, 1), 'sec' => substr($y, 1, 1).substr($x, 1, 1), 'sub' => substr($y, 2, 1) / 2 * 5 + substr($x, 2, 1) / 2);
}

Dax 31. Dez 2004 00:07

Re: Problem mit php script
 
Delphi-Quellcode:
function new_coord(var x, y: string): Variant;
begin
  x := IntToStr((StrToInt(x) + 250) * 2);
  y := IntToStr((StrToInt(y) + 250) * 2);

  if (x < 0) or (y < 0) then
  begin
    Result := false;
    Exit;
  end;

  Result := CreateVarArray([0, 2], varInteger);
  Result[0] := y[1] + x[1];
  Result[1] := y[2] + x[2];
  Result[2] := IntToStr(StrToInt(y[3]) div 10 + StrToInt(x[3]) div 2);
end;
Auch wenn ich nicht verstehe, warum du das nicht selbst machst :?

supermuckl 31. Dez 2004 00:11

Re: Problem mit php script
 
dax du hast da nen boolean result und benutzt das als array ? *nixblick*

Dax 31. Dez 2004 00:12

Re: Problem mit php script
 
Ach Mist, das hab' ich ein { übersehen -.-

Generell kann ich aber sagen, das Übersetzungen von php nach Delphi oft schwer bis gar nicht möglich sind..

supermuckl 31. Dez 2004 00:13

Re: Problem mit php script
 
jo. weil ich da fast alle variablen mit allem verwenden kann *g*
kein inttostr usw notwendig

Dax 31. Dez 2004 00:15

Re: Problem mit php script
 
Die einzige Möglichkeit ist, wie in der Übersetzung, Variants zu benutzen...

Aenogym 31. Dez 2004 00:17

Re: Problem mit php script
 
hi,

hmhmhm. naja, erstens wäre es schön, wenn du die [code] tags benutzen würdest. und zweitens ist das nicht ganz leicht. denn in delphi solltest du vorher schon wissen, welchen typ die func zurückgeben soll (also nicht mal boolean und mal ein array). das wäre eventuell mit einem record oder einer kalsse zu lösen.

achja und drittens ist der code fehlerhaft. du gibst einmal false zurück, wenn die bedingung zutriffst. dieses result wird aber sofort wieder mit einem array überschrieben.... ;)

ich habs aber spaßeshalber mal trotzdem versucht zu übersetzen:

Delphi-Quellcode:
type
  TNewCoordResult = record
    ok: boolean;
    Con, Sec, Sub: double;
  end;

function new_coord(x, y: double): TNewCoordResult;
var tempx, tempy: string;
begin
  result.ok := true;

  tempx := floattostr((x + 250) / 0.5);
  tempy := floattostr((y + 250) / 0.5);

  if (tempx < 0) or (tempy < 0) then
  begin
    result.ok := false;
    exit; //edited: ganz wichtig ;)
  end;

  result.con := strtofloat(copy(floattostr(tempy), 0, 1) + copy(floattostr(tempx), 0, 1));
  result.sec := strtofloat(copy(floattostr(tempy), 1, 1) + copy(floattostr(tempx), 1, 1));
  result.sub := strtofloat(floattostr(strtofloat(copy(floattostr(tempy), 2, 1)) / 2 * 5) + floattostr(strtofloat(copy(floattostr(tempx), 2, 1)) / 2));
end;
ohne gewähr

edit: oh meion gott, die letzte zeile ist aber mega unübersichtlich. @dax und supemuckl: man kanns auch ohne variants lösen.... man muss nur beim weiteren bearbeiten des results (der record) unterscheiden, ob das boolean false ist, oder ob die anderen werte gesetzt sind...

edit2: jetzt wird die funktionsebene auch verlassen ;)

Aenogym

Dax 31. Dez 2004 00:20

Re: Problem mit php script
 
Aenogym, du siehst da was falsch. return bewirkt in php, wie in C(++), dass die aktuelle Funktionseben auch noch verlassen wird ;)

Aenogym 31. Dez 2004 00:21

Re: Problem mit php script
 
Zitat:

Zitat von Dax
Aenogym, du siehst da was falsch. return bewirkt in php, wie in C(++), dass die aktuelle Funktionseben auch noch verlassen wird ;)

:wall: mist, stimmt

Aenogym


Alle Zeitangaben in WEZ +1. Es ist jetzt 10:01 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz