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

Desenvolva uma primitiva chamada MYEX que retorne o valor de ex utilizando a fórmula ex = x0/0! + x1/1! + x2/2! + x3/3! + ... + xn/n!. O valor de n será fornecido pelo usuário, devendo ser um valor inteiro e positivo. O valor de x será fornecido pelo usuário, podendo ser um valor (inteiro ou real) qualquer. Por exemplo, caso o valor fornecido pelo usuário para n seja 4 e para x seja 2, o programa deverá apresentar como resposta o valor 7, ou seja, 20/0! + 21/1! + 22/2! + 23/3! + 24/4!. Caso o usuário forneça um valor inválido para n, o programa deverá apresentar como resposta o valor nil

 

MyEx.java Application.java

Arquivo MyEx.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.exercicio04;

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 LISP para o cálculo da função exponencial
 * de base e por meio da utilização da série de Taylor
 */
public class MyEx extends LispPrimitive
{
  /**
   * Inicializar a primitiva
   *
   * @param lisp interpretador LISP
   */
  public MyEx(Jatha lisp)
  {
    super(lisp, "MYEX", 2);
  }

  /* (non-Javadoc)
   * @see org.jatha.compile.LispPrimitive#Execute(org.jatha.machine.SECDMachine)
   */
  @Override
  public void Execute(SECDMachine machine) throws CompilerException
  {
    LispValue nLisp = machine.S.pop();

    if (nLisp.basic_integerp())
    {
      long iterations = (long) nLisp.toJava("Long");

      if (iterations > 0L)
      {
        double x = (double) machine.S.pop().toJava("Double");

        double euler = 1.0;

        double power = 1.0;

        long factorial = 1L;

        for (long i = 1L; i <= iterations; i++)
        {
          euler = euler + power/factorial;

          power = power * x;

          factorial = factorial * i;
        }

        machine.S.push(f_lisp.makeBignum(euler));
      }
      else
      {
        machine.S.push(f_lisp.NIL);  
      }
    }
    else
    {
      machine.S.push(f_lisp.NIL);
    }

    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.exercicio04;

import org.jatha.Jatha;

/**
 * Classe responsável pela execução da primitiva MYEX
 */
public class Application
{
  /**
   * Construtor para inicializar a execução da primitiva MYEX
   */
  private Application()
  {

  }

  /**
   * Método principal da linguagem de programação Java
   *
   * @param args argumentos da linha de comando (não utilizado)
   */
  public static void main(String[] args)
  {
    Jatha jatha  = new Jatha(false, false);

    jatha.init();

    jatha.start();

    jatha.COMPILER.Register(new MyEx(jatha));

    System.out.println(jatha.eval("(MYEX 2 4)")); // 7
  }
}