Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Php Education Class (first Code)
dogtag
post Jun 7 2007, 12:36 PM
Post #1


Newbie [Level 1]
*

Group: Members
Posts: 23
Joined: 7-June 07
Member No.: 44,336



Hi
I want to educate some PHP codes that i think they will be useful for all of you!

My 1st code is this:


CODE
<?php
class calculator {
    
    /**
     * Variable for holding all the numbers to add
     *
     * @var array
     */
    private $numbers = array();
    
    /**
     * Variable holding all the digits after the point
     *
     * @var array
     */
    private $afterPoint = array();
    
    /**
     * Maximum number of digits after the point
     * that a number has
     *
     * @var int
     */
    private $afterPointLength = 0;
    
    /**
     * Final result
     *
     * @var string
     */
    private $result = 0;
    
    /**
     * Constructor
     *
     */
    public function __construct() {
        
    }
    
    /**
     * Adds a number to the list of numbers
     * to be summed
     *
     * @param string $number
     */
    public function addNumber ($number) {
        $this->numbers[] = (string)$number;
    }
    
    /**
     * Adds two numbers
     *
     * @param string $n1
     * @param string $n2
     * @return string
     */
    private function doAdd ($n1, $n2) {
        
        if ($n1 == 0) return $n2;
        if ($n2 == 0) return $n1;
        
        if (strlen($n1) <=13 && strlen($n2) <= 13) {
            return (string)$n1+$n2;
        }
        
        $finalNumber = array();
        $c = 0;
        
        $n1 = (string)$n1;
        $n2 = (string)$n2;
        
        $m = max(strlen($n1), strlen($n2));
        $n1 = str_pad($n1, $m, " ", STR_PAD_LEFT);
        $n2 = str_pad($n2, $m, " ", STR_PAD_LEFT);
        
        $numbers1 = chunk_split($n1, 13, ";");
        $numbers1 = substr($numbers1, 0, -1);
        $numbers1 = explode(";", $numbers1);
        $numbers1 = array_reverse($numbers1);
        
        $numbers2 = chunk_split($n2, 13, ";");
        $numbers2 = substr($numbers2, 0, -1);
        $numbers2 = explode(";", $numbers2);
        $numbers2 = array_reverse($numbers2);
        
        $maxSize = max(count($numbers1), count($numbers2));
        
        for ($i=0;$i<$maxSize;$i++) {
            
            $totalZeros  = 0;
            $totalZeros1 = 0;
            $totalZeros2 = 0;
            for ($j=0;$j<strlen($numbers1[$i]);$j++) {
                if ($numbers1[$i][$j] == 0) {
                    $totalZeros1++;
                } else {
                    break;
                }
            }
            
            for ($j=0;$j<strlen($numbers1[$i]);$j++) {
                if ($numbers2[$i][$j] == 0) {
                    $totalZeros2++;
                } else {
                    break;
                }
            }
            
            $totalZeros = max($totalZeros1, $totalZeros2);
            
            $partialResult = (string)($numbers1[$i] + $numbers2[$i] + $c);
            $partialResult = str_pad($partialResult, strlen($partialResult)+$totalZeros, "0", STR_PAD_LEFT);
            if (strlen($partialResult) > max(strlen($numbers1[$i]),strlen($numbers2[$i]))) {
                $partialResult = (string)$partialResult;
                $c = $partialResult[0];
                $finalNumber[] = substr($partialResult, 1);
            } else {
                $c=0;
                $finalNumber[] = $partialResult;
            }
        }
        $finalNumber = array_reverse($finalNumber);
        $finalNumber = implode("", $finalNumber);
        if ($c != 0) $finalNumber = $c.$finalNumber;
        return $finalNumber;
    }
    
    /**
     * Make the calculation
     *
     * @return string
     */
    public function calc () {
        
        for ($i=0; $i<count($this->numbers);$i++) {
            $n = explode(".", $this->numbers[$i]);
            if (count($n) == 1) {
                $this->result = $this->doAdd($this->result, $this->numbers[$i]);
            } elseif (count($n) == 2) {
                $this->afterPoint[] = $n[1];
                $this->afterPointLength = max($this->afterPointLength, strlen($n[1]));
                $this->result = $this->doAdd($this->result, $n[0]);
            } else {
                trigger_error("<b>".$this->numbers[$i]."</b> is invalid !!", E_USER_ERROR);
            }
        }
        
        if ($this->afterPointLength > 0) {
            $r = 0;
            foreach ($this->afterPoint as $number) {
                $number = str_pad($number, $this->afterPointLength, "0", STR_PAD_RIGHT);
                $r = $this->doAdd($r, $number);
            }
            if (strlen($r) > $this->afterPointLength) {
                $this->result = $this->doAdd($this->result, substr($r, 0, -$this->afterPointLength));
                if (strrpos($r, "0") != strlen($r)-1)
                    $this->result = $this->result.".".substr($r, 1);
            } else {
                $this->result = $this->result.".".$r;
            }
        }
        return $this->result;
    }
}


// example
$calc = new calculator();
// generate a very big numer
$number  = "8347610948712974071265710983243861208762139437204172409821378273618976529283
476109487129740712657109";
$number = str_repeat($number, 10000);


$calc->addNumber($number);
$calc->addNumber($number);

$time_start = microtime(true);
$result = $calc->calc();
$time_end = microtime(true);

print "\nThe number has ".strlen($number)." digits. ";
print "\nThe result has ".strlen($result)." digits. ";

print "\nThe result is: ".$result;


print "\nThe sum was executed in ".($time_end-$time_start)." seconds.";

?>




This is a calcutator that can calculate SUM of two huge number.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. How do you test your php code(77)
  2. Whats The Best Code?(32)
  3. Redirect Code Help(8)
  4. Da Vinci Code(16)
  5. Html Tag For A Code Box(4)
  6. Myspace Music Player(9)
  7. Simple C File Handling In Action(3)
  8. Wap Source Code Viewer(4)
  9. Rpg Code And Rpg Toolkit(10)
  10. Your First Autoit(4)
  11. Each And Every Nokia Code For Your Mobile(7)
  12. Advice On Impressing Someone In My Class(8)
  13. Runescape 2 Private Server: Code/guide 1(11)
  14. Eclipse Exporting .jar Files(5)
  15. Alright, I'm Taking Computer Programming As A Class In School.(10)
  1. Do You Ever Skip Class?(62)
  2. Html Code Tester. Online Script(15)
  3. How Do I Code A Design?(6)
  4. Class Vs. Id?(7)
  5. Css Multiclassing(2)
  6. Mozilla: Firefox Plugin Shipped With Malicious Code(3)
  7. My Spy Suggestions For Team Fortress 2 Next Update S(5)
  8. Why Doesn't This Code Work On Computinghost?(2)
  9. Add Flashing Inbox To Invisionfree Forum(1)
  10. Syntax Highlighting For Code(4)
  11. [ask] Appraiser Education Online(0)
  12. Constant Interface Or Constant Class?(1)
  13. Davinci Code(3)


 



- Lo-Fi Version Time is now: 29th August 2008 - 08:40 PM