Ybadoo - Soluções em Software Livre
Tutoriais
Programação Orientada a Objetos

Desenvolva uma primitiva chamada QUADRATIC que retorne uma lista com as raízes reais de uma equação do segundo grau, ou NIL caso a equação não tenha raízes reais.

 

Quadratic.java Application.java

Arquivo Quadratic.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial11.exercicio02;

import java.util.LinkedList;
import java.util.List;

import org.jatha.Jatha;
import org.jatha.compile.CompilerException;
import org.jatha.compile.LispPrimitive;
import org.jatha.dynatype.LispValue;
import org.jatha.machine.SECDMachine;

/**
 * Primitiva para retornar as raízes de uma equação do segundo grau
 */
public class Quadratic extends LispPrimitive
{
  /**
   * Inicializar a primitiva
   *
   * @param lisp interpretador LISP
   */
  public Quadratic(Jatha lisp)
  {
    super(lisp, "QUADRATIC", 3);
  }

  /* (non-Javadoc)
   * @see org.jatha.compile.LispPrimitive#Execute(org.jatha.machine.SECDMachine)
   */
  @Override
  public void Execute(SECDMachine machine) throws CompilerException
  {
    long c = (long)machine.S.pop().toJava("Long");
    long b = (long)machine.S.pop().toJava("Long");
    long a = (long)machine.S.pop().toJava("Long");

    List<LispValue> roots = new LinkedList<>();

    double discriminant = (b * b) - (4 * a * c);

    if(discriminant > 0)
    {
      roots.add(f_lisp.makeBignum((-b + Math.sqrt(discriminant)) / (2 * a)));

      roots.add(f_lisp.makeBignum((-b - Math.sqrt(discriminant)) / (2 * a)));
    }
    else if(discriminant == 0)
    {
      roots.add(f_lisp.makeBignum(-b / (2 * a)));
    }
    else
    {
      roots.add(f_lisp.NIL);
    }

    machine.S.push(f_lisp.makeList(roots));

    machine.C.pop();
  }
}

Arquivo Application.java

/*************************************************************************
 * Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)  *
 *                  Ybadoo - Solucoes em Software Livre (ybadoo.com.br)  *
 *                                                                       *
 * Permission is granted to copy, distribute and/or modify this document *
 * under the terms of the GNU Free Documentation License, Version 1.3 or *
 * any later version published by the  Free Software Foundation; with no *
 * Invariant Sections,  no Front-Cover Texts, and no Back-Cover Texts. A *
 * A copy of the  license is included in  the section entitled "GNU Free *
 * Documentation License".                                               *
 *                                                                       *
 * Ubuntu 16.10 (GNU/Linux 4.8.0-39-generic)                             *
 * OpenJDK Version "1.8.0_121"                                           *
 * OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)               *
 *************************************************************************/

package com.ybadoo.tutoriais.poo.tutorial11.exercicio02;

import org.jatha.Jatha;

/**
 * Classe responsavel pela execucao da primitiva QUADRATIC
 */
public class Application
{
  /**
   * Construtor para inicializar a execucao da primitiva QUADRATIC
   */
  private Application()
  {

  }

  /**
   * Metodo principal da linguagem de programacao Java
   *
   * @param args argumentos da linha de comando (nao utilizado)
   */
  public static void main(String[] args)
  {
    Jatha myLisp  = new Jatha(false, false);

    myLisp.init();

    myLisp.start();

    myLisp.COMPILER.Register(new Quadratic(myLisp));

    System.out.println(myLisp.eval("(QUADRATIC  1 -2 -3)")); // (3 -1)
    System.out.println(myLisp.eval("(QUADRATIC  1  8 16)")); // (-4)
    System.out.println(myLisp.eval("(QUADRATIC 10  6 10)")); // (NIL)
  }
}