1

PHP スクリプトの解決に行き詰まっています。

一連の命令から結果を計算したい。命令は、1 行ごとにスペースで区切られたキーワードと数字で構成されます。命令はファイルから読み込まれ、結果が画面に出力されます。命令はいくつでも指定できます。命令は演算子 (加算、除算、減算、乗算) です。命令は数学的な優先順位を無視します。最後の指示は「適用」と数字 (「適用 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('...');

加算、除算、乗算、減算を呼び出すまたは切り替える方法と、適用ワードを区別してトリガーする方法

あらゆる種類の助けをいただければ幸いです。

4

2 に答える 2

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

} 
?>
于 2013-01-23T05:20:50.040 に答える
0

array_shift関数と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);
}
于 2013-01-23T05:22:30.553 に答える