Код является по сути набором методов, то есть объединение в класс - весьма условно и скорее служит для удобства, нежели преследует архитектурные цели.
PHP:
скопировать код в буфер обмена
скопировать код в буфер обмена
- class Calculator
- {
- {
- {
- return null;
- }
- $sA = $rgArgs[0];
- $sB = $rgArgs[1];
- return $this->_pow_simple($sA, $sB);
- }
- public function factorial($sA)
- {
- {
- return null;
- }
- $sA=(string)$sA;
- return $this->_factorial_simple($sA);
- }
- {
- {
- return null;
- }
- $sA=(string)$sA;
- }
- public function mult()
- {
- {
- return null;
- }
- {
- return (string)$rgArgs[0];
- }
- $sResult = '1';
- for($i=0;$i<count($rgArgs);$i++)
- {
- $sResult = $this->_mult_simple($sResult, $rgArgs[$i]);
- }
- return $sResult;
- }
- public function sum()
- {
- {
- return null;
- }
- {
- return (string)$rgArgs[0];
- }
- $sResult = '0';
- for($i=0;$i<count($rgArgs);$i++)
- {
- $sResult = $this->_get_negative($rgArgs[$i])?
- $this->_diff_simple($sResult, $this->abs($rgArgs[$i])):
- $this->_sum_simple($sResult, $rgArgs[$i]);
- }
- return $sResult;
- }
- public function diff()
- {
- {
- return null;
- }
- $sA = $rgArgs[0];
- $sB = $rgArgs[1];
- //-|A| -(-|B|) = |B|-|A|
- if($this->_get_negative($sA) && $this->_get_negative($sB))
- {
- return $this->_diff_simple($this->abs($sB), $this->abs($sA));
- }
- //|A| -(-|B|) = |A|+|B|
- if(!$this->_get_negative($sA) && $this->_get_negative($sB))
- {
- return $this->sum($this->abs($sA), $this->abs($sB));
- }
- //(-|A|) -(|B|) = -(|A|+|B|)
- if($this->_get_negative($sA) && !$this->_get_negative($sB))
- {
- return '-'.$this->sum($this->abs($sA), $this->abs($sB));
- }
- //|A| -(|B|) = |A|-|B|
- if(!$this->_get_negative($sA) && !$this->_get_negative($sB))
- {
- return $this->_diff_simple($this->abs($sA), $this->abs($sB));
- }
- }
- protected function _pow_simple($sA, $sB)
- {
- if($sB=='0' || $sB=='1')
- {
- return $sA;
- }
- if($sA=='1')
- {
- return '1';
- }
- $sD = $sA;
- $sI = '1';
- while($sI!=$sB)
- {
- $sA = $this->_mult_simple($sA, $sD);
- $sI = $this->_sum_simple($sI, '1');
- }
- return $sA;
- }
- protected function _factorial_simple($sA)
- {
- if($sA=='1')
- {
- return '1';
- }
- return $this->mult($this->_factorial_simple($this->diff($sA, '1')), $sA);
- }
- protected function _mult_simple($sA, $sB)
- {
- $sSign = '';
- if($this->_get_negative($sA) ^ $this->_get_negative($sB))
- {
- $sSign = '-';
- }
- for($i=0;$i<strlen($sA);$i++)
- {
- for($j=0; $j<strlen($sB);$j++)
- {
- $iCR = (int)$sA[$i]*(int)$sB[$j];
- $k = $i+$j;//-1;
- while($iCR>0)
- {
- $rgC[$k] = $iCR % 10;
- $iCR = (int)($iCR/10);
- $k++;
- }
- }
- }
- }
- protected function _diff_simple($sA, $sB)
- {
- {
- }
- {
- }
- $sSign = '';
- $iC = 0;
- if($this->_compare_longs($sA, $sB)==-1)
- {
- $sA = $sA ^ $sB;
- $sB = $sA ^ $sB;
- $sA = $sA ^ $sB;
- $sSign = '-';
- }
- for($i=$iMax-1;$i>=0;$i--)
- {
- $iC += (int)$sA[$i]-(int)$sB[$i]+10;
- $sA[$i] = (string)($iC % 10);
- $iC = $iC<10?-1:0;
- }
- }
- protected function _sum_simple($sA, $sB)
- {
- {
- }
- {
- }
- $iC = 0;
- for($i=$iMax-1;$i>=0;$i--)
- {
- $iC += (int)$sA[$i]+(int)$sB[$i];
- $sA[$i] = (string)($iC % 10);
- $iC = (int)($iC/10);
- }
- if($iC>0)
- {
- $sA = (string)$iC.$sA;
- }
- return $sA;
- }
- protected function _get_negative($sA)
- {
- return $sA[0]=='-';
- }
- protected function _compare_longs($sA, $sB)
- {
- if($iA<$iB)
- {
- return -1;
- }
- if($iA>$iB)
- {
- return 1;
- }
- for($i=0;$i<$iA;$i++)
- {
- if($sA[$i]>$sB[$i])
- {
- return 1;
- }
- if($sA[$i]<$sB[$i])
- {
- return -1;
- }
- }
- return 0;
- }
- protected function _get_args($rgArgs)
- {
- {
- }
- {
- $rgArgs = $rgArgs[0];
- }
- return $rgArgs;
- }
- }