#!/bin/rc # math - basic arithmetic quiz # usage: math [-q n] [-r n] [-o '+-*/%^'] # set some default values rfork e right=0 wrong=0 questions=20 range=1 # in digits operands='+-' start=`{date -n} # parse optional flags for(i in $*){ switch($i){ case -q questions=$2 shift 2 case -r range=$2 shift 2 case -o operands=''$2'' shift 2 } } # rnum: generate a random digit based on cpu clock fn rnum{ awk '{ print $2 }' /dev/time | sed 's/.*(.)$/\1/' } # ask math questions opleft=$operands for(i in `{seq $questions}){ # generate random math puzzle a=`{rnum} b=`{rnum} if(test $range -gt 1){ for(i in `{seq `{echo $range - 1 | bc}}){ a=`{echo $a^`{rnum}} b=`{echo $b^`{rnum}} } } if(~ $#opleft 0) opleft=$operands if not{ opused=`{echo $opleft | sed 's/^(.).*/\1/'} opleft=`{echo $opleft | sed 's/^.(.*)/\1/'} } echo $a $"opused $b correct=`{echo $a $"opused $b | bc} answer=`{read} # evaluate given answer while(! ~ $answer $correct){ if(echo $answer | grep -s '^[-+]?[0-9]+$'){ echo What? wrong=`{echo $wrong + 1 | bc} } if not echo Please type a number '(no decimals!)' answer=`{read} } echo Right! right=`{echo $right + 1 | bc} } # print result of math quiz finish=`{date -n} time=`{echo $finish - $start | bc} total=`{echo $right + $wrong | bc} timepq=`{echo $time / $total | bc} if(~ $right 0) prct=0% if not prct=`{echo 'scale=2 ; '$right' / '$total'' | bc | sed 's/\.//' | sed 's/$/%/'} echo -n $right right, $wrong wrong '('$prct' correct)' in $time seconds '('$timepq's per answer)' echo