#! /usr/bin/perl # make-sums.pl # A simple program to generate a sheet of sum exercises for a # kindergarten or year 1 student # Copyright (C) 2005 Nick Urbanik # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. use warnings; use strict; my $min = 0; my $max = 13; my $first_max = 6; my $max_for_subst_sub = 10; my $sep = 1.06; use constant WORKING_DIR => "$ENV{HOME}/tmp"; use constant FILE_BASE => WORKING_DIR . "/t"; use constant TEX_FILE => FILE_BASE . '.tex'; my $top = <<'TOP'; \documentclass{article} \usepackage[a4paper,hmargin=15mm,vmargin=5mm]{geometry} \usepackage{amsmath} \newcommand{\BOX}{\fbox{\parbox[c]{1.5em}{\mbox{}\par\vspace*{2ex}\par}}} \pagestyle{empty} \begin{document} \huge \begin{minipage}[t]{0.46\linewidth} \begin{align*} TOP my $start_minipage = '\begin{minipage}[t]{0.46\linewidth}' . "\n"; my $end_column = <<'END_COL'; \end{align*} \end{minipage}% \hspace*{0.05\linewidth}% \begin{minipage}[t]{0.46\linewidth} \begin{align*} END_COL my $end = <<'END'; \end{align*} \end{minipage}% \end{document} END sub print_simple_add($) { my ( $max ) = @_; my $first = int rand( $max + 1 ); my $second = int rand( $max + 1 ); print TEX "$first + $second &= \\BOX\\\\[$sep ex]\n"; } sub print_simple_sub($) { my ( $max ) = @_; my $first = int rand( $max + 1 ); my $second = int rand( $first + 1 ); print TEX "$first - $second &= \\BOX\\\\[$sep ex]\n"; } sub print_subst_add($) { my ( $max ) = @_; my $first = int rand( $max + 1 ); my $total = $first + int rand( $max + 1 - $first ); print TEX "$first + \\BOX &= $total\\\\[$sep ex]\n"; } sub print_subst_sub($) { my ( $max ) = @_; my $first = int rand( $max + 1 ); my $total = int rand( $first + 1 ); print TEX "$first - \\BOX &= $total\\\\[$sep ex]\n"; } open TEX, '>', TEX_FILE or die "cannot write to @{[TEX_FILE]}: $!"; print TEX $top; for ( 1..3 ) { print_simple_add $first_max; } for ( 1..7 ) { print_simple_add $max; } for ( 1..10 ) { print_subst_add $max; } print TEX $end_column; for ( 1..4 ) { print_simple_sub $first_max; } for ( 1..6 ) { print_simple_sub $max; } for ( 1..10 ) { print_subst_sub $max_for_subst_sub; } print TEX $end; close TEX or die "cannot close file @{[TEX_FILE]}: $!"; chdir WORKING_DIR or die "Cannot change dir to @{[WORKING_DIR]}: $!"; #print qx/latex FILE_BASE/; #print qx/xdvi FILE_BASE &/;