javax_slr (javax_slr) wrote,
javax_slr
javax_slr

Генерация упражнений по умножению дробей

Скрипт груви генерирующий LaTeX с упражнениями по умножению дробей (и еще один с ответами)

//////////////////////////////////////////////////// 
int N = 10
def f1 = new File("d:\\temp\\ex.tex")
def f2 = new File("d:\\temp\\sol.tex")
///////////////////////////////////////////////////// 

def r = new Random(System.currentTimeMillis())

f1.delete();
f2.delete();

def header = """ 
\\documentclass[a4paper,12pt,onecolumn]{article}
\\begin{document} \n"""

f1 << header
f2 << header

N.times {



    // boolean - will it eliminate?
    boolean eliminate = r.nextBoolean()

    //choose 2 numbers in denominator
    int den1 = (r.nextInt(7) + 2)
    int den2 = (r.nextInt(7) + 2)

    boolean isFirstInteger = r.nextBoolean()

    def mult = r.nextInt(3) + 2
    int integer = mult * den1
    if (eliminate) {
        integer *= den2
    } else {
        integer *= (r.nextInt(7) + 2)
    }

    int firstFractionIntegerPart = r.nextInt(3) + 1
    int secondFractionIntegerPart = r.nextInt(3) + 1
    int firstFractionNumeratorPart = r.nextInt(den1-1) + 1
    int secondFractionNumeratorPart = r.nextInt(den2-1) + 1

    println "$eliminate mult = $mult  den1=$den1 den2=$den2 integer=$integer"


    def s = new StringBuffer("")

    if (isFirstInteger) {
        s << "\$\$ $integer "
        s << "  \\times "
        s << "$firstFractionIntegerPart\\frac{$firstFractionNumeratorPart}{$den1}"
    }
    else {
        s << "\$\$ $firstFractionIntegerPart\\frac{$firstFractionNumeratorPart}{$den1}"
        s << "  \\times "
        s << "$integer "
    }
    s << "  \\times "
    s << "$secondFractionIntegerPart\\frac{$secondFractionNumeratorPart}{$den2}  =  "

    f1 <<s
    f2 <<s

    f1 << "\$\$\n \\bigskip \n"




    // compute the answer

    int numerator = integer*(firstFractionIntegerPart*den1+firstFractionNumeratorPart)*(secondFractionIntegerPart*den2+secondFractionNumeratorPart)
    int denumerator = den1*den2

    int commondenum =  gcdR(numerator,denumerator)

    int reducedNumerator = numerator/commondenum
    int reducedDenumerator = denumerator/commondenum

    int integerNumerator = 0
    if (reducedNumerator > reducedDenumerator) {
        integerNumerator = reducedNumerator/reducedDenumerator
        f2 << integerNumerator
        reducedNumerator = reducedNumerator% reducedDenumerator
    }
    if (reducedNumerator!=0) {
    f2 << "\\frac{$reducedNumerator}{$reducedDenumerator}"
    }
    f2 << "\$\$\n \\bigskip\n"


    double res1 = integer * ((double) firstFractionIntegerPart + ((double)firstFractionNumeratorPart/den1)) * ((double) secondFractionIntegerPart + ((double)secondFractionNumeratorPart/den2 ))
    double res2 = integerNumerator + ((double)reducedNumerator/reducedDenumerator)

    assert Math.abs(res1-res2) < 0.001

}


def footer = """ 
\\end{document} \n"""

f1 << footer
f2 << footer


private def gcdR(int m, int n) {  m = m.abs(); n = n.abs(); n == 0 ? m : m%n == 0 ? n : gcdR(n, m%n) }

Tags: programming
Subscribe

  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 4 comments