drpzr64329 2018-09-02 02:57
浏览 36
已采纳

使用oops概念制作一个php计算器类

Hi guys i am trying to make a php calculator class which need to multiple numbers through fucntion like when user try to run like this through commpand line claculator.php add 2,3,4,5,6 and it should give output as 20 and it should allow add method to use a new line character as a number separator example calculator.php add 2 3,4

And it should allow defining what delimiter is used to separate numbers example: calculator.php add \\;\\2;3;4

And it should not accept negative numbers too.And if user pass number as more than 1000 it shouwld ignoe and give me the result exmaple php calculator.php add 10,20,1010,20 and it should ginore 1010 and give out put as 50

And i have tried this code which will accept only 2 parameters

 class Calculator {
    private $_val1 , $_val2;

    public function __construct($val1, $val2){
        $this->_val1 = $val1;
        $this->_val2 = $val2;
    }

    public function add(){
        return $this->_val1 + $this->_val2;
    }

    public function subtract(){
        return $this->_val1 - $this->_val2;
    }

    public function multiply (){
        return $this->_val1 * $this->_val2;
    }

    public function divide () {
        return $this->_val1 / $this->_val2;
    }
}

$calc = new Calculator(3,4);
echo "<p>3 + 4 = ".$calc->add(). "</p>";

$calc = new Calculator (15,12);
echo "<p>15 - 12 = ".$calc->subtract(). "</p>";

$calc = new Calculator (20,2);
echo "<p> 20 * 2 = ".$calc->multiply(). "</p>";

$calc = new Calculator (20,2);
echo "<p> 20 / 2 = ".$calc ->divide(). "</p>";

Can anyone help me out how can i do those things

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongshi1188 2018-09-02 04:19
    关注

    I think this is what you looking for:

    Calculator class

    <?php
    
    /**
     * Calculator class
     */
    class Calculator
    {
        const min = 0;
        const max = 1000;
    
        /**
         * Calculate the summation
         * @param Array $number
         * @return integer
         */
        public static function add( $numbers ){
            return array_sum( self::_filter_numbers( $numbers ) );
        }
    
        /**
         * Substract numbers
         * @param Array $numbers
         * @return integer
         */
        public static function subtract( $numbers ){
            $filtered_numbers = self::_filter_numbers( $numbers );
            $first_number = array_shift( $filtered_numbers );
            return ( $first_number - array_sum( $filtered_numbers ) );
        }
    
        /**
         * Calculate the product of the given numbers
         * @param Array $numbers
         * @return integer
         */
        public static function multiply( $numbers ){
            return array_product( self::_filter_numbers( $numbers ) );
        }
    
    
        /**
         * Divide the given numbers
         * @param Array $numbers
         * @return double
         */
        public static function divide( $numbers ){
            $filtered_numbers = self::_filter_numbers( $numbers );
            $first_number = array_shift( $filtered_numbers );
            return ($first_number / array_product( $filtered_numbers ));
        }
    
    
        /**
         * Filter Invalid numbers
         * @param Array $numbers
         * @return Array Valid Numbers
         */
        private static function _filter_numbers( $numbers ){
            return array_filter( $numbers, function( $number ){
                return self::_is_valid_number( $number );
            } );
        }
    
        /**
         * Check if the given number is in the interval [0, 1000]
         * @param integer $number
         * @return boolean
         */
        private static function _is_valid_number( $number ){
            return ( $number >= self::min && $number <= self::max );
        }
    }
    

    Tests

    $test_numbers_1 = [ -2, -1, 0, 1, 2, 1000, 2000 ];
    printf( "<h4>Test 1</h4>
    " );
    printf( "<p>Numbers: %s</p>
    ", implode( ',', $test_numbers_1 ) );
    printf( "<p>Add: %d</p>
    ", Calculator::add( $test_numbers_1 ) );
    printf( "<p>Substract: %d</p>
    ", Calculator::subtract( $test_numbers_1 ) );
    printf( "<p>Multiply: %d</p>
    ", Calculator::multiply( $test_numbers_1 ) );
    printf( "<p>Divide: %f</p>
    ", Calculator::divide( $test_numbers_1 ) );
    
    $test_numbers_1 = [ -2, -1, 1, 2, 1000, 2000 ];
    printf( "<h4>Test 2</h4>
    " );
    printf( "<p>Numbers: %s</p>
    ", implode( ',', $test_numbers_1 ) );
    printf( "<p>Add: %d</p>
    ", Calculator::add( $test_numbers_1 ) );
    printf( "<p>Substract: %d</p>
    ", Calculator::subtract( $test_numbers_1 ) );
    printf( "<p>Multiply: %d</p>
    ", Calculator::multiply( $test_numbers_1 ) );
    printf( "<p>Divide: %f</p>
    ", Calculator::divide( $test_numbers_1 ) );
    

    Test results

    <h4>Test 1</h4>
    <p>Numbers: -2,-1,0,1,2,1000,2000</p>
    <p>Add: 1003</p>
    <p>Substract: -1003</p>
    <p>Multiply: 0</p>
    <p>Divide: 0.000000</p>
    <h4>Test 2</h4>
    <p>Numbers: -2,-1,1,2,1000,2000</p>
    <p>Add: 1003</p>
    <p>Substract: -1001</p>
    <p>Multiply: 2000</p>
    <p>Divide: 0.000500</p>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?