PHP.SU

Программирование на PHP, MySQL и другие веб-технологии
PHP.SU Портал     На главную страницу форума Главная     Помощь Помощь     Поиск Поиск     Поиск Яндекс Поиск Яндекс     Вакансии  Пользователи Пользователи

Страниц (1): [1]

> Найдено сообщений: 1
luckyday Отправлено: 28 Ноября, 2018 - 17:31:59 • Тема: Ответы на вопросы из уроков по php • Форум: Уроки php

Ответов: 67
Просмотров: 4256
EuGen пишет:
Урок № 5. Математические функции


0. Вернет true, если принять, что такие числа допустимы. Если читатель знает предмет вопроса (или не поленился заглянуть в википедию), то он знает, что трансцедентные числа - обычные вещественные числа. Но являются они корнями многочлена с иррациональными коэффициентами. Единственное что - их представление в типе float языка php сделает их уже не трансцентными вовсе. Так что - с точки зрения float функция is_nan вернет true. Если же считать, что трансцедентные числа нельзя записать представлением float, то и вообще ничего не вернет (агрумента то и не будет)
1.
PHP:
скопировать код в буфер обмена
  1.  
  2. <?PHP
  3. class Converter
  4. {
  5.         protected $cipherSet;
  6.         protected $rgCiphers;
  7.    
  8.         protected $inputSet;
  9.         protected $outputSet;
  10.         protected $fromBase;
  11.         protected $toBase;
  12.  
  13.        
  14.         protected $iErrorCode;
  15.         protected $sErrorMessage;
  16.        
  17.         /*Submit only full cipher set*/
  18.         function __construct($strSet)
  19.         {
  20.                 $this->cipherSet=$strSet;
  21.                 $this->iErrorCode=0;
  22.                 $this->sErrorMessage="";
  23.         }
  24.         /*multiplication with non-decimal base*/
  25.         protected function baseArithmeticMult($num)
  26.         {
  27.                 $currentResultLen=count($this->rgCiphers);
  28.                 if ($currentResultLen==0)
  29.                 {
  30.                         return;
  31.                 }
  32.                 $rgMods=array();
  33.                 $currentPos=0;
  34.                 $div=0;
  35.                 do
  36.                 {
  37.                         $divided=0;
  38.                         if ($currentResultLen>$currentPos)
  39.                         {
  40.                                 $divided=$this->rgCiphers[$currentPos]*$num;
  41.                         }
  42.                         $divided+=$div;
  43.                         $rgMods[$currentPos]=$divided%$this->toBase;
  44.                         $div=(int)($divided/$this->toBase);
  45.                         $currentPos++;
  46.                 }
  47.                 while($currentResultLen>$currentPos||$div!=0);
  48.                 $this->rgCiphers=$rgMods;
  49.         }
  50.         /*addition with non-decimal base*/
  51.         protected function baseArithmeticPlus($num)
  52.         {
  53.                 $currentPos=0;
  54.                 $divided=$num;
  55.                 do
  56.                 {
  57.                         $divided+=(int)($this->rgCiphers[$currentPos]);
  58.                         $this->rgCiphers[$currentPos]=$divided%$this->toBase;
  59.                         $divided=(int)($divided/$this->toBase);
  60.                         $currentPos++;
  61.                 }
  62.                 while ($buf>0);
  63.         }
  64.         /*error's getters*/
  65.         public function getErrorCode()
  66.         {
  67.                 return $this->iErrorCode;
  68.         }
  69.        
  70.         public function getErrorMessage()
  71.         {
  72.                 return $this->sErrorMessage;
  73.         }
  74.         /*main function*/
  75.         public function baseConvert($num, $fromBase=0, $toBase=0)
  76.         {
  77.                 $num=(string)($num);
  78.                 $totalCiphers=strlen($num);
  79.                 $this->inputSet=substr($this->cipherSet, 0, $fromBase);
  80.                 $this->outputSet=substr($this->cipherSet, 0, $toBase);
  81.                 $this->toBase=$toBase;
  82.                 $this->rgCiphers=array();
  83.                 for ($currentPos=0; $currentPos<$totalCiphers; $currentPos++)
  84.                 {
  85.                         $currentCipher=$num[$currentPos];
  86.                         $cipherSign=substr_count($this->inputSet, $currentCipher);
  87.                         if($cipherSign==0)
  88.                         {
  89.                                 $this->iErrorCode=255;
  90.                                 $this->sErrorMessage="Cipher '".$currentCipher."' was not found in cipher set: ".$this->inputSet;
  91.                                 return null;
  92.                         }
  93.                         elseif($cipherSign>1)
  94.                         {
  95.                                 $this->iErrorCode=255;
  96.                                 $this->sErrorMessage="Cipher '".$currentCipher."' was found more than once in cipher set: ".$this->inputSet;
  97.                                 return null;
  98.                         }
  99.                         if ($currentPos!=0)
  100.                         {
  101.                                 $this->baseArithmeticMult($fromBase);
  102.                         }
  103.                         $this->baseArithmeticPlus(strpos($this->inputSet, $currentCipher));
  104.                 }
  105.                 $convertedNum='';
  106.                 $totalCiphers=count($this->rgCiphers);
  107.                 for ($currentPos=0; $currentPos<$totalCiphers; $currentPos++)
  108.                 {
  109.                         $convertedNum=$this->outputSet[$this->rgCiphers[$currentPos]].$convertedNum;
  110.                 }
  111.                 return $convertedNum;
  112.         }
  113. }
  114. ?>
  115.  

2. Некоторые примеры:
is_nan(sqrt(-1)) и is_finite(sqrt(-1))
is_nan(acos(2)) и is_finite(acos(2))



Здравствуйте, спасибо за отличный сайт и кучи материала!
Я дошел до д/з 5 урока 2го задания сделал его кое как, но работает. Потом посмотрел решение - лучше бы я этого не делал. Я правильно понимаю что на 5м уроке я должен уметь писать подобного рода функцию, как в решении? Непонятно ничерта))


CODE (htmlphp):
скопировать код в буфер обмена
  1. $a=61;
  2. function convert($a) {
  3. if ($a<36){
  4. $b = base_convert ($a, 10, 36);
  5.         echo $b;
  6. }
  7. elseif ($a<62) {
  8. $a=$a-26;
  9.         $b = base_convert ($a, 10, 36);
  10.         echo strtoupper($b);
  11. }
  12. else {
  13. echo "введите число в диапазоне от 0 до 61";
  14. }      
  15. }
  16. echo convert($a);
  17.  

Страниц (1): [1]
Powered by PHP  Powered By MySQL  Powered by Nginx  Valid CSS  RSS

 
Powered by ExBB FM 1.0 RC1. InvisionExBB