Форумы портала PHP.SU :: Версия для печати :: Ответы на вопросы из уроков по php
<?PHP class Converter { protected $cipherSet; protected $rgCiphers; protected $inputSet; protected $outputSet; protected $fromBase; protected $toBase; protected $iErrorCode; protected $sErrorMessage; /*Submit only full cipher set*/ function __construct($strSet) { $this->cipherSet=$strSet; $this->iErrorCode=0; $this->sErrorMessage=""; } /*multiplication with non-decimal base*/ protected function baseArithmeticMult($num) { $currentResultLen=count($this->rgCiphers); if ($currentResultLen==0) { return; } $rgMods=array(); $currentPos=0; $div=0; do { $divided=0; if ($currentResultLen>$currentPos) { $divided=$this->rgCiphers[$currentPos]*$num; } $divided+=$div; $rgMods[$currentPos]=$divided%$this->toBase; $div=(int)($divided/$this->toBase); $currentPos++; } while($currentResultLen>$currentPos||$div!=0); $this->rgCiphers=$rgMods; } /*addition with non-decimal base*/ protected function baseArithmeticPlus($num) { $currentPos=0; $divided=$num; do { $divided+=(int)($this->rgCiphers[$currentPos]); $this->rgCiphers[$currentPos]=$divided%$this->toBase; $divided=(int)($divided/$this->toBase); $currentPos++; } while ($buf>0); } /*error's getters*/ public function getErrorCode() { return $this->iErrorCode; } public function getErrorMessage() { return $this->sErrorMessage; } /*main function*/ public function baseConvert($num, $fromBase=0, $toBase=0) { $num=(string)($num); $totalCiphers=strlen($num); $this->inputSet=substr($this->cipherSet, 0, $fromBase); $this->outputSet=substr($this->cipherSet, 0, $toBase); $this->toBase=$toBase; $this->rgCiphers=array(); for ($currentPos=0; $currentPos<$totalCiphers; $currentPos++) { $currentCipher=$num[$currentPos]; $cipherSign=substr_count($this->inputSet, $currentCipher); if($cipherSign==0) { $this->iErrorCode=255; $this->sErrorMessage="Cipher '".$currentCipher."' was not found in cipher set: ".$this->inputSet; return null; } elseif($cipherSign>1) { $this->iErrorCode=255; $this->sErrorMessage="Cipher '".$currentCipher."' was found more than once in cipher set: ".$this->inputSet; return null; } if ($currentPos!=0) { $this->baseArithmeticMult($fromBase); } $this->baseArithmeticPlus(strpos($this->inputSet, $currentCipher)); } $convertedNum=''; $totalCiphers=count($this->rgCiphers); for ($currentPos=0; $currentPos<$totalCiphers; $currentPos++) { $convertedNum=$this->outputSet[$this->rgCiphers[$currentPos]].$convertedNum; } return $convertedNum; }}?>
Рубрики: Без рубрики |
Комментариев нет »