1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
| <?php
//
// Une calculatrice simple en ligne de commande
//
// ligne de commande seulement !
if (!PHP_SAPI === 'cli') {
echo "CLI mode only\n";
exit(-1);
}
// définition des opérations
define('OP_MULTIPLY', '*');
define('OP_ADD', '+');
define('OP_DIVIDE', '/');
define('OP_MODULO', '%');
// messages utilisés
$message['usage'] =
$message['help'] = <<< EOF
Usage: {$argv[0]} [options] <a> <b>
Returns the result of <a> [operation] <b>,
see available operations in options below.
Available Options:
-h, --help Display this message
-m, --multiply Use the multiplication operation
-a, --add Use the addition operation
-d, --div Use the division operation
--modulo Use the module operation
--abs Returns absolute result
--int Returns integer result
EOF;
// valeurs par défaut des options
$options = array(
'operation' => null,
'abs' => false,
'int' => false,
);
// help
if (in_array('-h', $argv) || in_array('--help', $argv)) {
echo $message['help'];
exit(0);
}
// traiter les paramètres de la ligne de commande
for ($i = 1; $i < $argc -2; $i++) {
switch ($argv[$i]) {
case '-m':
case '--multiply':
$options['operation'] = OP_MULTIPLY;
break;
case '-a':
case '--add':
$options['operation'] = OP_ADD;
break;
case '-d':
case '--div':
$options['operation'] = OP_DIVIDE;
break;
case '--modulo':
$options['operation'] = OP_MODULO;
break;
case '--abs':
$options['abs'] = true;
break;
case '--int':
$options['int'] = true;
break;
default:
echo "Unrecoginized option: {$argv[$i]}\n";
echo $message['usage'];
break;
}
}
if (!$options['operation']) {
echo "Error: No operation specified";
echo $message['usage'];
exit(-1);
}
// obtenir les valeurs <a> et <b> et les valider
list($a, $b) = array_slice($argv, -2) + array(null, null);
if (!preg_match('~[-+]?([0-9]*\.[0-9]+|[0-9]+)~', $a)) {
echo "Error: $a is not a valid number\n";
exit(-1);
}
if (!preg_match('~[-+]?([0-9]*\.[0-9]+|[0-9]+)~', $b)) {
echo "Error: $b is not a valid number\n";
exit(-1);
}
$a = floatval($a);
$b = floatval($b);
//
// traitement principal
//
if (($result = perform_operation($a, $b, $options['operation'])) === false) {
echo "Error: unable to get a result!";
exit(-1);
}
if ($options['int'])
$result = (int)$result;
if ($options['abs'])
$result = abs($result);
echo "{$a} {$options['operation']} {$b} = {$result}\n";
exit(0);
//
// fonctions
//
function perform_operation ($left, $right, $operation) {
switch ($operation) {
case OP_MULTIPLY:
return $left * $right;
case OP_ADD:
return $left + $right;
case OP_DIVIDE:
return $right !== 0 ? $left / $right : 'NaN'; // attention à la division par 0
case OP_MODULO:
return $right !== 0 ? $left % $right : 'NaN'; // attention à la division par 0
default:
return false;
}
} |
Partager