Sunday, August 29, 2010

[Level 2] Math exam generator

This morning, my son ask me to give him a 50 question math exam.
So I wrote a math exam generator to produce the exam.
PS. You need OpenOffice to open result file.

#!/bin/bash
set -f
declare -i nQ=${1:-50}
declare    sF=$0.csv; [ -f $sF ] && rm $sF;
declare -i i=0
declare    aOperator=(+ - * /); declare -i nOperator=4
declare    aOperator=(+ -); declare -i nOperator=2

getRandomNumber() {
  declare -i n=$RANDOM*89/32767+10
  echo $n
}

getRandomOperator() {
  declare -i n=$RANDOM*$nOperator/32767
  echo ${aOperator[$n]}
}

for i in `seq 1 $nQ`
do
  declare -i n1=`getRandomNumber`
  declare -i n2=`getRandomNumber`
  declare    sO=`getRandomOperator`
  echo "$n1,$sO,$n2,=,,=if(isblank(e$i);0;if(e$i=(a$i${sO}c$i);1;0))" >> $sF
done
echo ",,,correct,=,=sum(f1:f$i),/$i" >> $sF
echo ",,,score,=,=sum(f1:f$i)/$i*100" >> $sF
soffice $sF


Wish this helps.
regards,
Stanley Huang