如何使用3个变量制作PHP计算器

时间:2017-09-22 19:22:38

标签: php

所以对于学校来说,我的任务是创建一个使用3个变量的简单PHP计算器。我认为我会使用switch语句来执行函数的所有计算,但在处理Order of Operations时会遇到问题。经过几个小时和多次尝试后,我不得不承认我很难过......有什么建议吗?

<?php
// function to calculate and return result
function calculate($x, $y, $op) { 
    // calculate $prod using switch (case) statement
    switch($op1) {
        case '+':
            $prod1 = $x + $y;
            break;
        case '-':
            $prod1 = $x - $y;
            break;
        case '*':
            $prod1 = $x * $y;
            break;
        case '/':
            if ($y == 0) {$prod1 = "&#8734";}
            else {$prod1 = $x / $y;}
    // return the result
    return $prod1;
    }
}
function calculate2($prod1, $z, $op2) { 
    // calculate $prod2 using switch (case) statement
        switch($op2) {
        case '+':
            $prod2 = $prod1 + $z;
            break;
        case '-':
            $prod2 = $prod1 - $z;
            break;
        case '*':
            $prod2 = $prod1 * $z;
            break;
        case '/':
            if ($z == 0) {$prod2 = "&#8734";}
            else {$prod2 = $prod1 / $z;}
    }
    // return the result
    return $prod2;
}
function calculate3($x, $prod2, $op1) { 
    // calculate $prod using switch (case) statement
        switch($op2) {
        case '+':
            $prod2 = $x + $prod1;
            break;
        case '-':
            $prod2 = $x - $prod1;
    }
    // return the result
    return $prod3;
}
function calculate4($y, $z, $op2) { 
    // calculate $prod using switch (case) statement
        switch($op2) {
        case '*':
            $prod2 = $y * $z;
            break;
        case '/':
            if ($z == 0) {$prod2 = "&#8734";}
            else {$prod2 = $y / $z;}
    }
    // return the result
    return $prod2;
}
// declare all variables
$x = 0;
$y = 0;
$z = 0;
$prod1 = 0;
$prod2 = 0;
$prod3 = 0;
$op1 = '';
$op2 = '';


// grab the form values from $_GET hash
extract($_GET);

?>

  <html>

  <head>
    <title>PHP Calculator Version 2.12</title>
  </head>

  <body>

    <h3>PHP Calculator (Version 2.12)</h3>

    <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">

      x = <input type="text" name="x" size="5" value="<?php print $x; ?>" /> op1 =
      <select name="op">
                <option value="+" <?php if ($op=='+') echo 'selected="selected"';?>>+</option>
                <option value="-" <?php if ($op=='-') echo 'selected="selected"';?>>-</option>
                <option value="*" <?php if ($op=='*') echo 'selected="selected"';?>>*</option>
                <option value="/" <?php if ($op=='/') echo 'selected="selected"';?>>/</option>
            </select> y = <input type="text" name="y" size="5" value="<?php  print $y; ?>" /> op2 =
      <select name="op2">
                <option value="+" <?php if ($op2=='+') echo 'selected="selected"';?>>+</option>
                <option value="-" <?php if ($op2=='-') echo 'selected="selected"';?>>-</option>
                <option value="*" <?php if ($op2=='*') echo 'selected="selected"';?>>*</option>
                <option value="/" <?php if ($op2=='/') echo 'selected="selected"';?>>/</option>
            </select> z = <input type="text" name="z" size="5" value="<?php print $z; ?>" />
      <input type="submit" name="calc" value="Calculate" />
    </form>

    <?php
        if(isset($calc)) {
            // check that $x & $y are numeric
            if (is_numeric($x) && is_numeric($y) && is_numeric($z)) {
                // check PEMDAS
                if ($op1 == '*' or '/') {
                    $prod1 = calculate($x, $y, $op1);
                    $prod2 = calculate2($prod1, $z, $op2);  
                }
                else if ($op2 == '*' or '/') {
                     $prod2 = calculate4($y, $z, $op2);
                     $prod3 = calculate3($x, $prod2, $op1);
                }
                else 
                    $prod1 = calculate($x, $y, $op1);
                    $prod2 = calculate2($prod1, $z, $op2);

                // print the result 
                echo "<p>$x $op $y $op2 $z = $prod2 </p>";
            }
            else {
                // unaccepatable values
                echo "<p>x, y, and z values are required to be numeric ... 
                         please re-enter values</p>";
            }
        }
    ?>
  </body>

  </html>

2 个答案:

答案 0 :(得分:1)

你真的只需要1个函数来进行计算,x为你的下一次计算保留一个值,结果是你前一个计算的结果

<html>
   <head>
     <title>PHP Calculator Version 2.12</title>
   </head>
   <body>
<?php
function calculate($x, $y, $op) {
    switch($op) {
        case '+':
            $result = $x + $y;
            break;
        case '-':
            $result = $x - $y;
            break;
        case '*':
            $result = $x * $y;
            break;
        case '/':
            if ($y == 0) {
                $result = "&#8734";
            } else {
                $result = $x / $y;
            }
    }
    return $result;
}

extract($_REQUEST);

$x = calculate($x, $y, $op);
?>
   <body>
    <form method="get"">
      x = <input type="text" name="x" size="5" value="<?php print $x; ?>" /><br />
      op = <select name="op">
        <option value="+" <?php if ($op === '+') { echo 'selected="selected"'; } ?>>+</option>
        <option value="-" <?php if ($op === '-') { echo 'selected="selected"'; } ?>>-</option>
        <option value="*" <?php if ($op === '*') { echo 'selected="selected"'; } ?>>*</option>
        <option value="/" <?php if ($op === '/') { echo 'selected="selected"'; } ?>>/</option>
      </select><br />
      y = <input type="text" name="y" size="5" value="<?php  print $y; ?>" /><br />
      <input type="submit" name="calc" value="Calculate" /><br />
    </form>
  </body>
</html>

现在,为了好玩,写一个分数计算器! :)

答案 1 :(得分:1)

你的代码对于初学者来说并不是那么糟糕,但它仍然需要一些工作。为什么要使用多个函数进行大致相同的计算?你是一个软件,你可以多次调用任何函数吗?

然后,检查您的代码。你有很多变量错误,因为你将它们命名为不同。

此外,您的变量命名也不是那么易读。给出变量名称,以便知道它们应该是什么。在这个小代码级别上,这不是什么大不了的事。如果你使用大量的代码,一个好的命名可以弥补很多!

如果您打算继续编程,您希望训练自己尽早检查这些问题。这将为您节省大量时间,因为您将接受培训以自动完成此操作。

我或多或少地纠正了你的代码。我已经写了一些注释来暗示部分和修复,所以你了解发生了什么变化以及原因。

如果你获得这个计算器项目的成绩,请记住,仍然有很多东西你可以做得更好。至少如果你想获得最好的成绩。

  • 检查您的命名。

  • 解决安全问题。 (如果有人看到你作为初学者这样做,他们会留下深刻的印象!)

  • 评论您的代码。你有意见,但还不够。评论任何可能的内容,以便人们通过阅读评论来了解代码的作用。

一些更正后的代码:

<?php
// you only need one function. why would you need a lot of functions doing the same?
function calculate($x, $y, $op) {
    // calculate $prod using switch (case) statement
    switch($op) {
        case '+':
            $prod = $x + $y;
            break;
        case '-':
            $prod = $x - $y;
            break;
        case '*':
            $prod = $x * $y;
            break;
        case '/':
            if ($y == 0) {$prod = "&#8734";}
            else {$prod = $x / $y;}
            break;
    }

    // do return your result AFTER the switch. else you will not get any results in most cases
    return $prod;
}

// you can do this, but keep in mind, this is more or less a security issue!
// you may want to check how to work with $_GET variables. as it is for school work, it is not critical yet,
// but if you plan to use php in future you may want to have a look at some ways to avoid problems.
extract($_GET);

// in this example is no need to predefine the variables as you had it, as wich each post the value get lost and it only uses the information from $_GET

?>
 <html>

  <head>
    <title>PHP Calculator Version 2.12</title>
  </head>

  <body>

    <h3>PHP Calculator (Version 2.12)</h3>

    <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">

      x = <input type="text" name="x" size="5" value="<?php print $x; ?>" /> op1 =
      <select name="op1">
                <option value="+" <?php if ($op1=='+') echo 'selected="selected"';?>>+</option>
                <option value="-" <?php if ($op1=='-') echo 'selected="selected"';?>>-</option>
                <option value="*" <?php if ($op1=='*') echo 'selected="selected"';?>>*</option>
                <option value="/" <?php if ($op1=='/') echo 'selected="selected"';?>>/</option>
            </select> y = <input type="text" name="y" size="5" value="<?php  print $y; ?>" /> op2 =
      <select name="op2">
                <option value="+" <?php if ($op2=='+') echo 'selected="selected"';?>>+</option>
                <option value="-" <?php if ($op2=='-') echo 'selected="selected"';?>>-</option>
                <option value="*" <?php if ($op2=='*') echo 'selected="selected"';?>>*</option>
                <option value="/" <?php if ($op2=='/') echo 'selected="selected"';?>>/</option>
            </select> z = <input type="text" name="z" size="5" value="<?php print $z; ?>" />
      <input type="submit" name="calc" value="Calculate" />
    </form>

   <?php
        if(isset($calc)) {
            if (is_numeric($x) && is_numeric($y) && is_numeric($z)) {
                // if you use OR / AND or any other operator, make sure both sides have a counterpart to work with.
                if ($op1 == '*' or $op1 == '/') {
                    // you can use the same function multiple times as seen below
                    $prod1 = calculate($x, $y, $op1);
                    $prod2 = calculate($prod1, $z, $op2);
                }
                else if ($op2 == '*' or $op2 == '/') {
                    $prod1 = calculate($y, $z, $op2);
                    $prod2 = calculate($x, $prod1, $op1);
                }
                else {
                    $prod1 = calculate($x, $y, $op1);
                    $prod2 = calculate($prod1, $z, $op2);
                }

                // make sure what you print does also exist!
                echo "<p>$x $op1 $y $op2 $z = $prod2 </p>";
            }
            else {
                // unaccepatable values
                echo "<p>x, y, and z values are required to be numeric ... 
                         please re-enter values</p>";
            }
        }
    ?>
  </body>
  </html>