_coder_: Skriptbewertung

Beitrag lesen

hi there,

also ich habe mal einfach so folgendes Skript geschrieben weil ich mich im Kopfrechnen verbessern wollte.
Was haltet ihr von dem Skript?

#!/bin/bash  
<?php  
  
$max = 100;  
$min = -100;  
if($argc >= 2) {  
 if(is_numeric($argv[1])) {  
  $max = $argv[1];  
 } else {  
  echo 'argument (1) not numeric'."\n";  
  exit(0);  
 }  
 if($argc >= 3) {  
  if(is_numeric($argv[2])) {  
   $min = $argv[2];  
  }  
 } else {  
  echo 'argument (2) not numeric'."\n";  
  exit(0);  
 }  
 if($min > $max) {  
  $tmp = $max;  
  $max = $min;  
  $min = $tmp;  
  unset($tmp);  
 }  
}  
  
// we do no longer need this $vars  
unset($argv);  
unset($argc);  
  
// we employ the user for a loong time :)  
for(;;) {  
  
 // we use a area of numbers between $min (std.=-100) and $max (std.=100)  
 $first = rand($min, $max);  
 $second = rand($min, $max);  
 $op = rand(0,3);  
 $result = NULL;  
 $stdin = NULL;  
  
 switch($op) {  
  case 0:  
   $op = 'PLUS';  
   $result = $first + $second;  
   break;  
  case 1:  
   $op = 'MINUS';  
   $result = $first - $second;  
   break;  
  case 2:  
   $op = 'TIMES';  
   $result = $first * $second;  
   break;  
  case 3:  
   $op = 'OVER';  
   // division through 0 not defined  
   if($second == 0) {  
    // get an other number  
    do {  
     $second = rand($min, $max);  
    } while($second == 0);  
   }  
   $result = $first / $second;  
   // we only want to calculate max. 2 numbers after [.,]  
   if(ereg('^(-){0,1}[0-9]{1,}\.[0-9]{1,}$', $result)) {  
    while(ereg('^(-){0,1}[0-9]{1,}\.[0-9]{2,}$', $result)) {  
     $result = (++$first) / $second;  
    }  
   }  
   break;  
  default:  
   // will never happen  
   echo "\nERROR\n";  
   exit(0);  
   break;  
 }  
  
 $bracket = array(false,false);  
 if($first < 0) {  
  $bracket[0] = true;  
 }  
 if($second < 0) {  
  $bracket[1] = true;  
 }  
  
 echo ($bracket[0] ? shell_exec('printf "\033[01;33m(\033[0m\033[31m"') : shell_exec('printf "\033[34m"')),  
      str_replace('.', ',', $first),  
      ($bracket[0] ? shell_exec('printf "\033[0m\033[01;33m)\033[0m"') : shell_exec('printf "\033[0m"')),  
      shell_exec('printf " \033[033m'.$op.'\033[0m "'),  
      ($bracket[1] ? shell_exec('printf "\033[01;33m(\033[0m\033[31m"') : shell_exec('printf "\033[34m"')),  
      str_replace('.', ',', $second),  
      ($bracket[1] ? shell_exec('printf "\033[0m\033[01;33m)\033[0m"') : shell_exec('printf "\033[0m"')),  
      ' = ';  
  
 unset($bracket);  
  
 $stdin = trim(fgets(STDIN));  
 $round = round($stdin);  
  
 // if $op == 0|1|2 $top == $bottom == $round  
 $top = $stdin + ($round > $stdin ? $round - $stdin : $stdin - $round);  
 $bottom = $stdin - ($round > $stdin ? $round - $stdin : $stdin - $round);  
  
 $round = $stdin = NULL;  
  
 if($result >= $top && $result <= $bottom) {  
  echo shell_exec('printf "\033[032m[ OK ]\033[0m "')."\n";  
 } else {  
  echo shell_exec('printf "\033[031m[ FALSCH ]\033[0m "').' correct result = '.$result."\n";  
 }  
}  
  
  
?>