<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Matemática Virtual, Ing.MSc. Fernando Carrillo & Ing.MSc. Cecilia Izurieta</title>
    <!-- Bootstrap CSS -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <?php include 'encabezado.php'; ?>
    <main>
        <?php include 'menui.php'; ?>
    <div class="container mt-5">
        <h3 class="mb-4">Generador de Ejercicios de Operaciones Combinadas</h3>
        <div class="mt-4">
            <?php
                function generateExpression() {
                    $operators = ['+', '-', '*', '/','\sqrt{'];
                    $operators1 = ['+', '-'];
                    $expression = "";
                    $numTerms = rand(3, 10); // Number of terms in the expression
                    $r = rand(2,5);
                    $n2=[4,16,25,36,49,64,81,100, 121,144,169,196,225,256,289,324,361,400];

                    for ($i = 0; $i < $numTerms; $i++) {
                        $number = rand(1, 10);
                        $operator = $operators[array_rand($operators)];
                        $operator1 = $operators[array_rand($operators1)];
                        $n = $n2[array_rand($n2)];
                       // $r= $r[array_rand($r)];
                 if ($operator == "\sqrt{")

                     {
                        
                        if ($i == 0) {
                            $expression .= $number;
                        } else {
                            $n1=$number * $r;
                            $expression .= " $operator1 \\sqrt{{$n}} ";
                        }

                     }
                    elseif ($operator == "/") {
                        if ($i == 0) {
                            $expression .= $number;
                        } else {
                            $n1=$number * $r;
                            $expression .= " $operator1 $n1 $operator $number";
                        }
                    }
                    else
                    {
                        
                        if ($i == 0) {
                            $expression .= $number;
                        } else {
                            $expression .= " $operator $number";
                        }
                    }
                        }
                    // Evaluate the expression
                    //$result = eval("return $expression;");

                    //return [$expression, $result];
                    return [$expression];
                }

                for ($i = 1; $i <= $n; $i++) {
                    //list($expression, $result) = generateExpression();
                    list($expression) = generateExpression();
                    echo "<div class='card mb-3'>";
                    echo "<div class='card-body'>";
                    echo "<h5 class='card-title'>Ejercicio $i</h5>";
                    echo "<p class='card-text'> \\( $expression \\)</p>";
                    //echo "<p class='card-text'>Resultado: \\( $result \\)</p>";
                    echo "</div>";
                    echo "</div>";
                }
            ?>
        </div>
    </div>
    <!-- MathJax -->
    <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
    <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
    </main>
    <?php include 'subir.php'; ?>
</body>
</html>
