2017-10-13 119 views
-1

我的問題是查找字符的7字符串,其中包含從致命錯誤:未捕獲的錯誤:類「功能」沒有找到PHP

acegikoprs只有字母

使得gen_hash(the_string)是

如果散列是由以下僞代碼定義:

Int64 gen_hash (String s) { 
    Int64 h = 7 
    String letters = "acegikoprs" 
    for(Int32 i = 0; i < s.length; i++) { 
     h = (h * 37 + letters.indexOf(s[i])) 
    } 
    return h 
} 

例如,如果我們試圖找到gen_hash(the_string)爲677850704066的7字母字符串,則答案將是「kppracg」。

解決方案
test1.php

我沒有這個問題解決在PHP中,我無法運行此代碼我在PHP我沒有,關於PHP類和它們的許多知識函數,任何人都可以解決這個代碼並描述我。在此先感謝,如果有人幫助我,我會很滿意。

<?php 

$set = "acdegilmnoprstuw"; 
$CONST_HASH = 7.0; 
$CONST_MULT = 37.0; 

$hash = new Func(function($string = null) use (&$CONST_HASH, &$CONST_MULT, &$set) { 
    $_hash = $CONST_HASH; 
    for ($i = 0.0; $i < get($string, "length"); $i++) { 
    $_hash = _plus(to_number($_hash) * to_number($CONST_MULT), call_method($set, "indexOf", get($string, $i))); 
    } 
    return $_hash; 
}); 

$decode = new Func(function($_hash = null) use (&$CONST_MULT, &$Math, &$set) { 
    $decoded = ""; $positionsInSet = new Arr(); 
    for ($i = 0.0; $_hash > $CONST_MULT; $i++) { 
    set($positionsInSet, $i, call_method($Math, "floor", (float)(to_number($_hash) % to_number($CONST_MULT)))); 
    $_hash /= 37.0; 
    } 
    for ($i = to_number(get($positionsInSet, "length")) - 1.0; $i >= 0.0; $i--) { 
    $decoded = _plus($decoded, get($set, get($positionsInSet, $i))); 
    } 
    return $decoded; 
}); 
+1

'Func'不是內部的PHP類。你在哪裏找到這個代碼示例?您可能需要包含第三方庫。或者,這只是錯誤的。 – iainn

+0

先生其實我已經將此代碼轉換爲PHP,有任何方式來運行此代碼就像在我的PHP代碼中做一些更改。 – Abhy

+0

那麼,你說你已經把它轉換成了PHP,但是你寫的東西根本不起作用,至少是它自己。你爲什麼選擇這樣寫呢?如果你正在學習一個教程,請回頭閱讀其餘部分。 – iainn

回答

0

而是與運營商分配的內聯函數的變量,創建一個單獨的功能編碼解碼在那裏你可以做的散列和匹配的哈希碼。

讓我給你如何做到這一點的片段。我假設你使用普通的PHP。

/* Call to function encode which returns you the encoded value and store in $encode variable */ 

$encode = encode($par1, $par2); 

/* Call to function decode which returns you the decoded value and store in $decode variable */ 

$decode = decode($par1, $par2); 

/* Function to encode your code */ 
function encode($par1, $par2){ 
    return $encode_value 
} 

/* Function to decode your code */ 
function decode($par1, $par2){ 
    return $decode_value 
} 
相關問題