0

我想用 PHP 编写一个脚本,它会为我做一些数学运算。

它应该完成以下任务:

例子

假设您有一家果汁在线商店。你想要

  1. 将几种果汁保存到数据库中(想想:橙汁、柠檬汁、苹果汁)。

  2. 您还希望以份量和价格(例如 100 毫升/2.50 美元)保存这些果汁。

  3. 您想混合/组合不同的果汁并获得毫升/美元的总量。

如果我们把它加起来:

橙汁:100ml / 2,50 USD | 我要 50 毫升 = 1.25 美元。

苹果汁:100ml / 1,50 美元 | 我要 10 毫升 = 0.15 美元。

总果汁混合物:60 毫升 = 1.40 美元。

到目前为止,一切都很好。我写了以下代码:

<?php
  function devide() {
  $portion = 100;         //variable for ml    
  $price = 2.50;          //variable for price
  $portionNew = 50;       //value for new portion put into total mix
    if($portionNew > 0) {
      echo ($price / 100 * $portionNew);  //calculates price of new portion
    } 
  }
      echo "Total Juice" . (" = ");  
      echo devide() . (" USD") ;
?>

此代码仅计算您从一种果汁中取出的部分的价格。如果你从 100 毫升果汁中取出 30 毫升,它会与价格相呼应。

这些是我的问题:

  1. 如何使此脚本不仅回显新价格,还回显新部分($portionNew)?

  2. 关于我希望最终脚本执行的操作,函数是否正确?还是我应该考虑使用对象或数组?

  3. 我应该如何继续使用这个脚本,以便它添加多个 ( $portionNews/ $price) 来告诉我最终价格和总果汁混合物的部分?

4

1 回答 1

0
  1. 只需echo 'Portion: '.$portionNew在您的devide函数中添加类似的内容。(我相信你可能想打电话给它divide?)

  2. 你可以创建一个Juice类,但我认为在这种情况下这有点过分了。我可能会坚持使用数组来存储果汁及其价格以及混合多种果汁的功能。

  3. 您可以创建一个混合果汁的函数。您将指定一个变量,其中包含当前价格和混合物中的份量、要添加的果汁的类型和数量,它会返回一个表示混合物中新价格和果汁量的值。

如果你想将果汁存储在一个数组中,它可能如下所示:

// In this case, we have an array that maps the juice type to a price (in cents
// per 100 ml).
$juices = array(
    'orange' => 250,
    'apple' => 150
);

// If you decide you need to store more information about a juice, you can change
// this array to map a juice type to another associative array that contains the
// price and your additional information.
// Beware: if you use this, you have to change the mixJuices function to access
// a juice's "price" value:
//     $pricePerLiter = $juices[$juiceType]['price'] * 10;
$juices = array(
    'orange' => array(
        'price' => 250,
        'color' => 'orange'
    ),
    'apple' => array(
        'price' => 150,
        'color' => 'yellow'
    )
);

请注意,我将价格设置为美分而不是美元。通过这样做,我避免在计算价格时使用浮点数以及与之相关的所有问题(请参阅PHP 文档中关于浮点精度的红色通知

混合果汁的功能可能如下所示:

// The function needs to know what's currently in the mix, which is why it's
// taking the $currentMix as a parameter. Also, it needs to know about all the
// available juices, which is why it's taking the $juices as another parameter.
// A mix is represented by an associative array that contains the current amount
// of juice in the mix (identified by "portion") and the current price
// (identified by "price"). Note that you need to pass the $juices array I
// defined earlier, so it can access the different types of juices and their 
// prices.
function mixJuices($currentMix, $juices, $juiceType, $juicePortion)
{
    $pricePerLiter = $juices[$juiceType] * 10;
    $price = $pricePerLiter * $juicePortion / 1000;
    $currentMix['portion'] = $currentMix['portion'] + $juicePortion;
    $currentMix['price'] = $currentMix['price'] + $price;
    return $currentMix;
}

也可以编写此函数以使用全局$mix变量。如果你这样做了,你就不需要将 $currentMix 传递给函数。但是,您也会失去同时创建多个混音的能力(因为对修改后mixJuices的函数的所有调用都会修改相同的全局混音)。

你可以像这样定义我上面定义的函数:

// At the beginning, there's no juice at all, so it doesn't cost anything (yet)
$mix = array('portion' => 0, 'price' => 0);

// Let's mix some juice in
$mix = mixJuices($mix, $juices, 'apple', 500);
$mix = mixJuices($mix, $juices, 'orange', 250);

// Print the amount of juice and the price
// The price is divided by 100 so we can display it in dollars instead of cents
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
    $mix['portion'], $mix['price'] / 100);

这种方法允许你做各种有趣的事情。

假设您有两个客户想要订购果汁混合物。$mix您可以通过创建两个不同的数组轻松地混合同一类型的多个果汁:

// Process customer 1's order
$mixCustomer1 = array('portion' => 0, 'price' => 0);
$mixCustomer1 = mixJuices($mixCustomer1, $juices, 'apple', 250);
$mixCustomer1 = mixJuices($mixCustomer1, $juices, 'orange', 250);
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
    $mixCustomer1['portion'], $mixCustomer1['price'] / 100);
// 500 milliliters of this juice mix will cost 10.00 USD.

// Process customer 2's order
$mixCustomer2 = array('portion' => 0, 'price' => 0);
$mixCustomer2 = mixJuices($mixCustomer2, $juices, 'apple', 150);
$mixCustomer2 = mixJuices($mixCustomer2, $juices, 'orange', 350);
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
    $mixCustomer2['portion'], $mixCustomer2['price'] / 100);
// 500 milliliters of this juice mix will cost 11.00 USD.

现在假设客户 2 有某种优惠券可以让他享受 20% 的果汁折扣:

// Apply the discount
$discountedJuices = array_map(function($price) { return (1 - 0.2) * $price; }, $juices);

// Process customer 2's order
$mixCustomer2 = array('portion' => 0, 'price' => 0);
$mixCustomer2 = mixJuices($mixCustomer2, $discountedJuices, 'apple', 150);
$mixCustomer2 = mixJuices($mixCustomer2, $discountedJuices, 'orange', 350);
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
    $mixCustomer2['portion'], $mixCustomer2['price'] / 100);
// 500 milliliters of this juice mix will cost 8.80 USD.
于 2013-08-05T21:25:25.917 回答