嘿,我已经建立了这个小计算器,并认为我会分享它,以便对更多人有所帮助:) 您只需要更改$ opt变量的值即可获得不同的输出。 如果您有任何意见或改进,请随时添加。 当我有空的时候,我会发布更多。
<?php
$opt = "Example 1";
$x;
$x1;
switch ($opt) {
case "Example 1":
$keywords = array("add", "multiply", "apply");
$values = array("2", "3", "4");
break;
case "Example 2":
$keywords = array("multiply", "apply");
$values = array("9", "5");
break;
case "Example 3":
$keywords = array("apply");
$values = array("1");
break;
case "Example 4":
$keywords = array("divide", "substract", "apply");
$values = array("2", "3", "8");
break;
default:
echo "No valid example has been chosen";
break;
}
$klen = count($keywords);
$klen -=1;
$x = $values[$klen];
echo "Apply " .$x. "\n";
$klen -=1;
$j =0;
foreach ($keywords as $i) {
$x1 = $values[$j];
$j +=1;
switch ($i) {
case "add":
$x += $x1;
echo "Add " .$x1. "\n";
break;
case "multiply":
$x *= $x1;
echo "Multiply " .$x1. "\n";
break;
case "divide":
$x /=$x1;
echo "Divide " .$x1. "\n";
break;
case "substract":
$x -=$x1;
echo "Substract " .$x1. "\n";
break;
case "apply":
break;
}
}
echo "The result is " .$x;
?>