php计算器类

时间:2013-01-23 05:00:29

标签: php class

我无法解析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('...');

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

任何形式的帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

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

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

<?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;
}

} 
?>

答案 1 :(得分:0)

尝试使用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);
}