php计算器类
我被困在解析PHP脚本中 我想根据一组指令计算一个结果。指令由关键字和数字组成,每行用空格分隔。指令从文件加载,结果输出到屏幕。可以指定任意数量的指令。指令是运算符(加、除、减、乘)。这些指令将忽略数学优先级。最后一条指令应该是“应用”和一个数字(例如,“应用3”)。然后用该数字初始化计算器,并将前面的说明应用于该数字php计算器类,php,class,Php,Class,我被困在解析PHP脚本中 我想根据一组指令计算一个结果。指令由关键字和数字组成,每行用空格分隔。指令从文件加载,结果输出到屏幕。可以指定任意数量的指令。指令是运算符(加、除、减、乘)。这些指令将忽略数学优先级。最后一条指令应该是“应用”和一个数字(例如,“应用3”)。然后用该数字初始化计算器,并将前面的说明应用于该数字 [Input] add 2 multiply 3 apply 3 [Output] 15 这是我尝试过的,但我无法获得完成这些方法的逻辑 class Calculator {
[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('...');
如何调用或切换加法、除法、乘法、减法以及如何区分和触发apply单词
任何形式的帮助都将不胜感激。您应该先处理申请,然后将其从队列数组中删除。在开始循环执行命令之前,只需测试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;
}
}
?>
您应该先处理apply,然后将其从队列数组中删除。在开始循环执行命令之前,只需测试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;
}
}
?>
尝试使用以下功能:
//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);
}
尝试使用以下功能:
//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);
}
thnks jhon,我如何循环并设置first apply?很抱歉我的php级别。感谢jhon,我复制了原样的代码,并且给出了错误的输出,只取第一个数字,不做任何加法、乘法或其他操作。请确保所有函数都设置正确。看起来您还没有乘法或除法函数。我已经将它们添加到了公共函数乘法($index,$number){//mobile$result,$queue,$number$this->result*=$number;}公共函数除法($index,$number){//操纵$result、$queue、$number$this->result/=$number;}让我看一下。输入都在一行上吗?thnks jhon,我如何循环并设置第一个应用程序?很抱歉我的php级别。感谢jhon,我复制了原样的代码,并且给出了错误的输出,只取第一个数字,不做任何加法、乘法或其他操作确保您拥有所有的函数s设置正确。看起来您还没有乘法或除法函数。我已经将它们添加到了公共函数乘法($index,$number){//operation$result,$queue,$number$this->result*=$number;}公共函数divide($index,$number){//操纵$result、$queue、$number$this->result/=$number;}一秒钟让我看看。输入都在一行吗?