PHP Classes
Icontem

File: cifra.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of zandor  >  Cifra  >  cifra.php  
File: cifra.php
Role: Class source
Content type: text/plain
Description: class code
Class: Cifra
Convert numbers between representation formats
 

Contents

Class file image Download
<?php

# cifra class
# coded by Alessandro Rosa
# e-mail : zandor_zz@yahoo.it
# site : http://malilla.supereva.it

# Copyright (C) 2006  Alessandro Rosa

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

# Compiled with PHP 4.4.0

$oneDIGITarray = array( "0" => "",
                        
"1" => "I"
                        
"2" => "II",
                        
"3" => "III",
                        
"4" => "IV",
                        
"5" => "V",
                        
"6" => "VI",
                        
"7" => "VII",
                        
"8" => "VIII",
                        
"9" => "IX" ) ;

$twoDIGITarray = array( "0" => "",
                        
"1" => "X",
                        
"2" => "XX",
                        
"3" => "XXX",
                        
"4" => "XL",
                        
"5" => "L",
                        
"6" => "LX",
                        
"7" => "LXX",
                        
"8" => "LXXX",
                        
"9" => "XC" ) ;

$threeDIGITarray = array( "0" => "",
                          
"1" => "C"
                          
"2" => "CC",
                          
"3" => "CCC",
                          
"4" => "CD",
                          
"5" => "D",
                          
"6" => "DC",
                          
"7" => "DCC",
                          
"8" => "DCCC",
                          
"9" => "CM" ) ;

$fourDIGITarray = array( "0" => "",
                         
"1" => "M",
                         
"2" => "MM",
                         
"3" => "MMM" ) ;

$ALPHANUMERICALarray = array( => "0",
                            
=> "1",
                            
=> "2",
                            
=> "3",
                            
=> "4",
                            
=> "5",
                            
=> "6",
                            
=> "7",
                            
=> "8",
                            
=> "9",
                            
10 => "A",
                            
11 => "B",
                            
12 => "C",
                            
13 => "D",
                            
14 => "E",
                            
15 => "F"
                          
) ;


class 
cifra
{
    function 
cifra()
    {
        
$this->arabic "" ;
        
$this->roman "" ;
    }

    function 
set_arabic$a )   { $this->arabic $a ; }

    function 
pack$a )
    {
        
$packing_zeros - ( strlen$a ) % ) ;
        if ( 
$packing_zeros == ) return $a ;
        
        for ( 
$i $i $packing_zeros$i++ ) $a "0$a" ;
        
        return 
$a ;
    }

    function 
findstr$strIn$strQuery )
    {
        
$length_in strlen$strIn );
        
$length_qry strlen$strQuery );
    
        for( 
$i $i <= ( $length_in $length_qry ); $i++ )
        {
              
$strOut substr$strIn$i$length_qry );
              if ( 
strcmp$strQuery$strOut ) == ) return true ;
        }
    
        return 
false ;
    }

    
    
//////////////////////////////////////////////////////
    
function get_arabic()       { return $this->arabic ; }
    function 
get_roman()        { return $this->roman ;  }

    function 
format_number$num$base )    { return "$num<sub>$base</sub>" ;  }


    function 
to_roman$a )
    {
        
$this->set_arabic$a );
        
$a $this->pack$a ) ;
        
        if ( 
$a >= 4000 ) return "out of range" ;
        
        
$inputDIGIT "$a" ;
        
$inputLEN strlen$inputDIGIT );
        
        
$a = array();
        
        for ( 
$i $i $inputLEN$i++ )       
        {
            
$digit $inputDIGIT{$i} ;
            
            switch( 
$i )
            {
                case 
3:
                  
$a $GLOBALS['oneDIGITarray'] ;
                break;
                case 
2:
                  
$a $GLOBALS['twoDIGITarray'] ;
                break;
                case 
1:
                  
$a $GLOBALS['threeDIGITarray'] ;
                break;
                case 
0:
                  
$a $GLOBALS['fourDIGITarray'] ;
                break;
                default:
                return 
"-1";
                break;
            }
            
            
$this->roman .= $a["$digit"] ;
        }
        
        return 
$this->roman ;
    }

    function 
to_arabic$a )
    {
        
$sum ;
        
////////////////////////////////////////////////
        
$i ;
        
$c ;
        
$digitARRAY $GLOBALS['fourDIGITarray'] ;
        
$token "";

        foreach( 
$digitARRAY as $value )
        {
            if ( 
$this->findstr$a$value ) === true && strlen$value ) >= strlen$token ) )
            {
                
$c $i ;
                
$token $value ;
            }

            
$i++ ;
        }
        
        
$s $c pow10) ;
        
$sum += $s ;
        
$a substr$astrlen$token ) );
        
////////////////////////////////////////////////
        
$i ;
        
$c ;
        
$digitARRAY $GLOBALS['threeDIGITarray'] ;
        
$token "";

        foreach( 
$digitARRAY as $value )
        {
            if ( 
$this->findstr$a$value ) === true && strlen$value ) >= strlen$token ) )
            {
                
$c $i ;
                
$token $value ;
            }

            
$i++ ;
        }
        
        
$s $c pow10) ;
        
$sum += $s ;
        
$a substr$astrlen$token ) );
        
////////////////////////////////////////////////
        
$i ;
        
$c ;
        
$digitARRAY $GLOBALS['twoDIGITarray'] ;
        
$token "";

        foreach( 
$digitARRAY as $value )
        {
            if ( 
$this->findstr$a$value ) === true && strlen$value ) >= strlen$token ) )
            {
                
$c $i ;
                
$token $value ;
            }

            
$i++ ;
        }
        
        
$s $c pow10) ;
        
$sum += $s ;
        
$a substr$astrlen$token ) );
        
////////////////////////////////////////////////
        
$i ;
        
$c ;
        
$digitARRAY $GLOBALS['oneDIGITarray'] ;
        
$token "";

        foreach( 
$digitARRAY as $value )
        {
            if ( 
strcmp$a$value ) == 0  && strlen$value ) >= strlen$token ) )
            {
                
$c $i ;
                
$token $value ;
            }

            
$i++ ;
        }
        
        
$s $c pow10) ;
        
$sum += $s ;
        
$a substr$astrlen$token ) );
        
////////////////////////////////////////////////
        
$this->arabic $sum ;
        return 
$sum ;
    }
    
    function 
to_base$inputNUM$base )
    {
          if ( 
$base || $base 16 ) return "NO BASE ALLOWED !" ;
          
          
$ret_str "" ;
          
          
$ALPHANUMarray $GLOBALS['ALPHANUMERICALarray'] ;
          
          while( 
$inputNUM >= $base )
          {
                
$remainder $inputNUM $base ;
                
$digit $ALPHANUMarray[$remainder];
                
$ret_str "$digit" $ret_str ;
                
$inputNUM = ( $inputNUM $remainder ) / $base ;
          }
    
          
$digit $ALPHANUMarray[$inputNUM];
          
$ret_str "$digit" $ret_str ;
          
          
$ret_str $this->pack$ret_str ) ;
          
          return 
$ret_str ;
    }

    function 
to_decimal$inputNUM$base )
    {
        
$sum ;
        
$len strlen$inputNUM );
        
        for ( 
$i $len 1$i >= $i-- )
        {
            
$c $inputNUM{$i} + 0;
            
$exponent $len $i ;
            
$sum += $c pow$base$exponent );
        }
        
        return 
$sum ;
    }
    
    
    
///////////// CLASS MEMBERS //////////////////////////
    
    
var $arabic ;
    var 
$roman ;
}

?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products