genimi/calculater

Calculater library for php

v1.0.1 2021-01-20 02:41 UTC

This package is auto-updated.

Last update: 2024-12-20 16:28:53 UTC


README

PHPUnit

安装

composer require genimi/calculater

使用

<?php
use Know\Calculater\Calculater;

$params = [
    1 => 1,
    2 => 4,
    3 => 6,
    4 => 16,
    5 => 100,
];

$calculater = new Calculater();
$string = '+ (1) (+ (1) (2))';
$result = $calculater->calculate($string, $params); // 6

$string = '++ 1 3';
$result = $calculater->calculate($string, $params); // 11

// 对应的选项
$extParams = [
    1 => 0,
    2 => 1,
    3 => 1,
    4 => 0,
    5 => 2,
];

//(数字①-数字②)代表第①题的选项②(②从0开始,按顺序计。用户选此选项计单项分,未选不计)
$string = '+ (1) (+ (1-1) (2-1))'; // 1 + 0 + 4
$result = $calculater->calculate($string, $params, $extParams); // 5