Symfony ecuación segundo grado

Controlador:

/**
     * @Route("/areas/ecuacion", name="ecuacion")
     */
    public function ecuacion() {
        $request = Request::createFromGlobals();
        $a = $request->request->get('a');
        $b = $request->request->get('b', 1);
        $c = $request->request->get('c', 1);
        if (!is_numeric($c) || !is_numeric($b) || !is_numeric($a)) {
            return $this->render('areas/error.html.twig',
                ['mensaje' => 'Los valores deben ser numéricos',
                'url'=>'/areas']);
        }
        $raiz = $b * $b - 4 * $a * $c;
        if ($raiz < 0) {
            $raiz1 = "No existe";
            $raiz2 = "No existe";
        } elseif ($raiz == 0) {
            $raiz1 = -$b / ($a * 2);
            $raiz2 = $raiz1;
        } else {
            $raiz1 = (-$b + sqrt($raiz)) / ($a * 2);
            $raiz2 = (-$b - sqrt($raiz)) / ($a * 2);
        }

        return $this->render('areas/ecuacion.html.twig', ['raiz1' => $raiz1,
                    'raiz2' => $raiz2,
                    'valores'=>[$a,$b,$c]]);
    }

Vistas:

  <form action="areas/ecuacion" method="post">
        <input type="number" name="a">x<sup>2</sup>+ <input type="number" name="b"> x+
        <input type="number" name="c"><br/>
        <input type="submit">
    </form>
{% extends 'base.html.twig' %}

{% block title %}Hello!{% endblock %}

{% block body %}
    <h1>Las soluciones de la ecuación {{valores[0]}}x<sup>2</sup>+{{valores[1]}}x+{{valores[2]}} son:</h1>
    {% if raiz1=='No existe' %}
        <p>La ecuación no tiene soluciones reales, quizás imaginarias</p>
   <!--     <img src="{{ asset('img/ninguna.gif') }}" alt=""/>-->

    {% elseif raiz1==raiz2 %}
        <p>La ecuación tiene una sóla raiz: {{raiz1}}</p>
   <!--     <img src="{{ asset('img/una.png') }}" alt=""/>-->
    {% else %}
        <p>Raiz 1: {{raiz1}}</p>
        <p>Raiz 2: {{raiz2}}</p>
    <!--    <img src="{{ asset('img/dos.png') }}" alt=""/>-->
    {% endif %}
    <iframe src="http://graph.tk/" id="my_graph" style="width:500px;height:400px">
</iframe>
<script>
var my_graph=document.getElementById("my_graph");

my_graph.onload=function(){
    function g(m){
        my_graph.contentWindow.postMessage(m,"http://graph.tk");
    };
    g("add:{{valores[0]}}x^2+{{valores[1]}}x+{{valores[2]}}");
    g("center:0,0");
}

</script>
{% endblock %}

Publicado por

Avatar del usuario

Juan Pablo Fuentes

Formador de programación y bases de datos