分组与数组

时间:2012-03-23 16:36:19

标签: php arrays division

              <?php
                 $txtfield = $_POST['number1'];
                  $arr = explode(' ',trim($txtfield));
                    $operator = $arr[0];
                          $count=count($arr);

                           $sum = array_sum($arr);
                              echo "Sum of the number:".$sum. "<br>";

                  $prod=1;
                   for ($i=1; $i<$count; $i++){
                     $prod = $prod * $arr[$i];}

                          echo "Product of the numbers:".$prod."<br>";

                             $diff=$arr[1];
                             for ($i=1; $i<$count; $i++){
                                   $diff= $diff - $arr[$i+1];
                       }

                             echo "Difference of the numbers:".$diff."<br>";

                                 $div=$arr[1];
                                     foreach ($i=1; $i<$count; $i++){
                                   $div=$div / $arr[$i];
                          }

                                  echo "Qoutient of the numbers:". $div. "<br>";
                            ?>

这行代码似乎无法正常工作。当我输入10&amp; 5输出为:

Sum of the number:15
Product of the numbers:50
Difference of the numbers:5
Qoutient of the numbers:0.2

2 个答案:

答案 0 :(得分:1)

假设I entered 10 & 5表示$arr看起来像这样:

$arr = array(
  1 => 10,
  2 => 5
);

...然后0.2是商的预期结果。这就是您的代码所做的事情:

$div = 10;
$div = 10 / 10; // 1
$div = 1 / 5; // 0.2

我认为您打算在2处初始化$i。这将给出您的预期输出。但更好的方法是:

$arr = array(
  10,
  5
);

// Make a copy of $arr before this if you need the original data intact
$quotient = array_shift($arr);

foreach ($arr as $val) {
  $quotient /= $val;
}

现在元素索引无关紧要,只有元素顺序很重要。

修改

以下是我将如何编写新发布的完整代码:

// Split on any number of whitespace characters to avoid bad user input
$arr = preg_split('/\s+/', $_POST['number1']);

// Remove the first element of the array so it doesn't interfere with calculations
$operator = array_shift($arr);

// This is fine
$sum = array_sum($arr);

// Product has a function of it's own
$prod = array_product($arr);

// Difference and quotient can be done in one loop
$diff = $quot = array_shift($arr);
foreach ($arr as $val) {
  $diff -= $val;
  $quot \= $val;
}    

// Echo the results
echo "Sum of the number:".$sum."<br>";
echo "Product of the numbers:".$prod."<br>"; 
echo "Difference of the numbers:".$diff."<br>"; 
echo "Qoutient of the numbers:".$quot."<br>";

答案 1 :(得分:0)

我重新格式化了您的程序并添加了静态值,以便它可以独立执行。

<?php
$arr = array(10, 5);
$count = count($arr);

$sum = array_sum($arr);
echo 'Sum of the number: '.$sum."<br>\n";

$prod = 1;
for($i = 0; $i < $count; $i++)
{
    $prod *= $arr[$i];
}
echo 'Product of the numbers: '.$prod."<br>\n";

$diff = $arr[0];
for($i = 1; $i < $count; $i++)
{
    $diff -= $arr[$i];
}
echo 'Difference of the numbers: '.$diff."<br>\n";

$div = $arr[0];
for($i = 1; $i < $count; $i++)
{
    $div /= $arr[$i];
}
echo 'Quotient of the numbers: '.$div."<br>\n";
?>

该程序的输出是:

Sum of the number: 15
Product of the numbers: 50
Difference of the numbers: 5
Quotient of the numbers: 2