mocking-magician / moneysaurus
货币转换库。解决找零问题的解决方案。
v1.5.0
2019-07-21 17:41 UTC
Requires
- php: ^7.1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- phpbench/phpbench: @dev
- phpstan/phpstan: @dev
- phpunit/phpunit: ^8
README
描述
Moneysaurus是一个用于解决找零问题的PHP库。
找零问题涉及找到最小数量的硬币(特定面额)的总和等于给定金额的问题。
这个库适用于任何货币。只需定义一个系统即可。系统是可以用于找零的硬币和/或纸币价值的集合。
安装
只需运行
composer require mocking-magician/moneysaurus
示例
您可以从创建一个系统开始
<? use MockingMagician\Moneysaurus\System; use MockingMagician\Moneysaurus\QuantifiedSystem; use MockingMagician\Moneysaurus\Algorithms\GreedyAlgorithm; // There, you can see an example with the Euro currency system $euroSystem = new System(...[ 0.01, 0.02, 0.05, 0.10, 0.20, 0.50, 1.0, 2.0, 5.0, 10.0, 20.0, 50.0, 100.0, 200.0, 500.0, ]); // Then initialize a quantified system. // A quantified system, is a system with a defined quantity of each coin/bank note available. $quantifiedSystem = new QuantifiedSystem($euroSystem); // By default, each value has been initialized with an zero amount quantity value. // So after you can set the available quantity for each coin/bank note $quantifiedSystem->setQuantity(0.01, 50); $quantifiedSystem->setQuantity(0.02, 50); $quantifiedSystem->setQuantity(0.05, 50); $quantifiedSystem->setQuantity(0.10, 50); $quantifiedSystem->setQuantity(0.20, 50); $quantifiedSystem->setQuantity(0.50, 50); $quantifiedSystem->setQuantity(1.0, 50); $quantifiedSystem->setQuantity(2.0, 50); $quantifiedSystem->setQuantity(5.0, 50); $quantifiedSystem->setQuantity(10.0, 50); $quantifiedSystem->setQuantity(20.0, 50); $quantifiedSystem->setQuantity(50.0, 50); $quantifiedSystem->setQuantity(100.0, 50); $quantifiedSystem->setQuantity(200.0, 50); $quantifiedSystem->setQuantity(500.0, 50); $resolver = new GreedyAlgorithm($quantifiedSystem); $change = $resolver->change(11.21);
另一种解决方案是初始化一个空的量化系统,然后添加系统值及其可用数量。
<? use MockingMagician\Moneysaurus\QuantifiedSystem; use MockingMagician\Moneysaurus\Algorithms\GreedyAlgorithm; $quantifiedSystem = new QuantifiedSystem(); $quantifiedSystem->addValue(0.01, 50); $quantifiedSystem->addValue(0.02, 50); $quantifiedSystem->addValue(0.05, 50); $quantifiedSystem->addValue(0.10, 50); $quantifiedSystem->addValue(0.20, 50); $quantifiedSystem->addValue(0.50, 50); $quantifiedSystem->addValue(1.0, 50); $quantifiedSystem->addValue(2.0, 50); $quantifiedSystem->addValue(5.0, 50); $quantifiedSystem->addValue(10.0, 50); $quantifiedSystem->addValue(20.0, 50); $quantifiedSystem->addValue(50.0, 50); $quantifiedSystem->addValue(100.0, 50); $quantifiedSystem->addValue(200.0, 50); $quantifiedSystem->addValue(500.0, 50); $resolver = new GreedyAlgorithm($quantifiedSystem); $change = $resolver->change(11.21);
接下来是什么?
- 添加一个全局解析器对象,该对象可以具有多个解析算法。
- 为系统对象记录器创建一个接口。
- 在量化系统中添加一个推断方法。