【问题标题】:php calculator classphp计算器类
【发布时间】:2013-01-23 05:00:28
【问题描述】:

我被困在解析 PHP 脚本中。

我想根据一组指令计算结果。指令由关键字和数字组成,每行用空格分隔。从文件加载指令并将结果输出到屏幕。可以指定任意数量的指令。指令是运算符(加、除、减、乘)。指令将忽略数学优先级。最后一条指令应该是“apply”和一个数字(例如“apply 3”)。然后使用该数字初始化计算器,并将前面的指令应用于该数字。

[Input]
add 2
multiply 3
apply 3
[Output]
15

这是我尝试过的,但我无法获得完成方法的逻辑

class Calculator {
    public $result = 0;
    public $queue = Array();
    public parseString($text) {
       // parse input string
       $cmds = explode(" ", $text);
       foreach($cmds as $cmd) {
          $cmd = trim($cmd);
          if(!$cmd) continue; // blank or space, ignoring
          $this->queue[] = $cmd;
       }

       // lets process commands
       $command = false;
       foreach($this->queue as $index => $cmd) { 
           if(is_number($cmd)) {
               // if it's number fire previous command if exists
               if(!$command || !method_exists($this, $command)) {
                   throw new Exception("Unknown command $command");
               }
               $this->$command($index, $cmd);
           }else{
               $command = $cmd;
           }
       }
    }
    public function apply($index, $number) {
       // manipulate $result, $queue, $number
    }
    public function add($index, $number) {
       // manipulate $result, $queue, $number
    }
    public function substract($index, $number) {
       // manipulate $result, $queue, $number
    }
}

$calculator = new Calculator();
$calculator->parseString('...');

如何调用或切换加、除、乘、减以及如何区分和触发应用词

我们将不胜感激。

【问题讨论】:

    标签: php class


    【解决方案1】:

    您应该先处理申请,然后将其从队列数组中删除。在开始循环使用命令之前,只需测试应用命令并首先运行它。这简化了整个过程。

    经过几分钟的实验和聊天,已经解决了。

    <?php
    error_reporting(E_ALL);
    class Calculator {
    
    public $result = 0;
    public $queue = array();
    
    public function parseString($text) {
    
        // parse input string
        $split = explode(" ", $text); //This gets your input into new lines
        for ($i = 0; $i < count($split); $i += 2) $pairs[] = array($split[$i], $split[$i+1]);
    
        foreach ($pairs as $bits) {
            if ($bits[0] == "apply") {
                $this->apply($bits[1]); //Set result equal to apply.
                $this->queue[] = "output";
            } else {
                $this->queue[] = $bits; //Set the queue item as an array of (command, value).
            }
        }
        //var_dump($this->queue);
        //die;
        // lets process commands
        foreach ($this->queue as $index => $cmd) {
            if ($cmd == "output") {
                echo "Answer: " .$this->result;
                return;
            } else {
                switch($cmd[0]) {
                    case "add":
                        $this->add($cmd[1]);
                        break;
                    case "subtract":
                        $this->subtract($cmd[1]);
                        break;
                    case "multiply":
                        $this->multiply($cmd[1]);
                        break;
                    case "divide":
                        $this->divide($cmd[1]);
                        break;
                    default:
                        echo "Unknown command!";
                        break;
                }
            }
        }
    }
    
    public function apply($number) {
        // manipulate $result, $queue, $number
        $this->result = $number;
    
    }
    
    public function add($number) {
        // manipulate $result, $queue, $number
        $this->result += $number;
    
    }
    
    public function subtract($number) {
        // manipulate $result, $queue, $number
        $this->result -= $number;
    
    }
    
    public function multiply($number) {
        // manipulate $result, $queue, $number
        $this->result *= $number;
    }
    
    public function divide($number) {
        // manipulate $result, $queue, $number
        $this->result /= $number;
    }
    
    } 
    ?>
    

    【讨论】:

    • thnks jhon,我怎样才能循环并设置先申请?,对不起我的 php 水平。
    • 感谢 jhon,我照原样复制了代码,但输出错误,只取第一个数字,不做任何加法或乘法之类的操作
    • 确保所有功能设置正确。看起来你还没有乘法或除法函数。
    • 我已经添加了 public function multiply($index, $number) { // 操作 $result, $queue, $number $this->result *= $number; } public function divide($index, $number) { // 操作 $result, $queue, $number $this->result /= $number; }
    • 一秒钟让我看看。输入全部在一行上吗?
    【解决方案2】:

    尝试使用array_shift and array_pop 函数:

    //pop the apply num off end off the queue
    $result= array_pop($this->queue); 
    //now pop the apply operator off end of the queue
    $operator = array_pop($this->queue); 
    
    //now get the first operator and numbers using array_shift
    $operator = array_shift($this->queue); //get first operator
    $num = array_shift($this->queue); //get first number
    
    //loop perform operation on result using number till done.
    while($num !== null)
    {
      $result = $operator($result, $num);
      $operator = array_shift($this->queue);
      $num = array_shift($this->queue);
    }
    

    【讨论】:

      猜你喜欢
      • 2011-12-21
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      • 2017-04-14
      • 1970-01-01
      相关资源
      最近更新 更多