Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 1 | Rev 621 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/bin/php
<?php

/*
        QBPWCF, Quick Build PHP website Component base on Fedora Linux.
    Copyright (C) 2015~2023 Min-Jhin,Chen

    This file is part of QBPWCF.

    QBPWCF 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
    (at your option) any later version.

    QBPWCF 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 QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
    
*/

/*
將兩個16進未數值進行運算
*/

#如果不存在要轉換成16進位的數值
if( !isset($_SERVER['argv'][1]) || !isset($_SERVER['argv'][2]) || !isset($_SERVER['argv'][3]) )
{
        #show help
        help();

        #停止執行
        exit;
}

#取得10進位的兩個數字
$num1=base_convert($_SERVER['argv'][1],16,10);
$num2=base_convert($_SERVER['argv'][3],16,10);

#取得運算符號
$op=$_SERVER['argv'][2];

#判斷運算符號
switch($op)
{
        #如果是加法
        case "+":
                        $result=$num1+$num2;
                break;
                
        #如果是減法
        case "-":
                        $result=$num1-$num2;
                break;
                
        #如果是乘法
        case "x":
                        $result=$num1*$num2;
                break;
                
        #如果是除法        
        case "/":
                        $result=$num1/$num2;
                break;
                
        #其他運算符號
        default:
        
                #show help
                help();
                
                #停止執行
                exit;
}

#印出 16 進位的數值
echo sprintf('%X',$result).PHP_EOL;

#help function
function help()
{
        echo $_SERVER['argv'][0]." 16進位數值 [+,-,*,/] 16進位數值".PHP_EOL;
}

?>