Skip to content

Commit

Permalink
readme update (using examples) + dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Алексей Скляров committed Dec 27, 2015
1 parent 3579320 commit 0ebb4ba
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 31 deletions.
54 changes: 52 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,58 @@ $ composer require sufir/php-calc
## Usage

``` php
<?php
// @todo
$lexer = new Lexer();
$converter = new Converter();
$calc = new Calc();

$calc->registerFunction('d20', function () {
return rand(1, 20);
});

$charAbilities = ['ability' => 15, /*... etc */];
$difficultyClass = 12;
$checkExpr = 'd20() + ($ability / 2 – 5)';

$tokens = $lexer->parse($checkExpr);
$result = $calc->evaluate(
$converter->converToPostfix($tokens),
$charAbilities
);

// simple d20 ability check
if ($result < $difficultyClass) {
echo 'You fail!!!';
} else {
echo 'Congratulation!';
}
```

``` php
$lexer = new Lexer();
$converter = new Converter();
$calc = new Calc(20);

$expr = '$Pi*$r^2';
$radiusList = [5, 10, 15, 25, 50];
$calc->defineVar('$Pi', '3.14159265358979323846');
$tokens = $lexer->parse($expr);

foreach ($radiusList as $radius) {
echo 'Pi * ', $radius , '^2 = ',
$calc->evaluate(
$converter->converToPostfix($tokens),
['r' => $radius]
),
"\n";
}
```

```
Pi * 5^2 = 78.53981633974483096150
Pi * 10^2 = 314.15926535897932384600
Pi * 15^2 = 706.85834705770347865350
Pi * 25^2 = 1963.49540849362077403750
Pi * 50^2 = 7853.98163397448309615000
```

## Testing
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
}
],
"require": {
"php": ">=5.6"
"php": ">=5.6",
"ext-bcmath": "*"
},
"require-dev": {
"phpunit/phpunit": "5.1.*",
Expand Down
28 changes: 0 additions & 28 deletions example/index.php

This file was deleted.

0 comments on commit 0ebb4ba

Please sign in to comment.