Está en la página 1de 15

CODIGO JAVA PARA CALCULAR INTEGRALES

/*
* Created on 27-feb-2005
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/

package com.welsungo.math.integral;

import org.nfunk.jep.JEP;

/**
* @author felipe
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class DefnIntegral {

//private Evaluator mEvaluator;


private String mFunction;
private double mDesde;
private double mHasta;

private double mNumIntervalos;

/** Parser */
private JEP myParser;

/** Current xValue */


private double xValue;
/* GUI components */
//private String mFunction;
private String mError;
private String mResult;

public DefnIntegral() {
myParser = new JEP();
myParser.initFunTab(); // clear the contents of the function table
myParser.addStandardFunctions();
myParser.setTraverse(true);
}

public void parseExpression(String pFunction) {


myParser.initSymTab(); // clear the contents of the symbol table
myParser.addStandardConstants();
myParser.addComplex(); // among other things adds i to the symbol table
myParser.addVariable("x", xValue);
myParser.setImplicitMul(true);
myParser.parseExpression(pFunction);
}

public void updateResult() {


System.out.println("updateResult");
Object result = myParser.getValueAsObject();
String errorInfo = myParser.getErrorInfo();

//Is the result ok?


if (result != null) {
setResult(result.toString());
} else {
setResult("");
}

System.out.println(" errorInfo = ["+errorInfo+"]");

// Get the error information


if (errorInfo != null) {
setError(errorInfo);
} else {
setError("");
}
}

/**
* @return Returns the desde.
*/
public double getDesde() {
return mDesde;
}

/**
* @param desde
* The desde to set.
*/
public void setDesde(double desde) {
mDesde = desde;
}

/**
* @return Returns the function.
*/
public String getFunction() {
return mFunction;
}

/**
* @param function
* The function to set.
*/
public void setFunction(String function) {
mFunction = function;
}

/**
* @return Returns the hasta.
*/
public double getHasta() {
return mHasta;
}

/**
* @param hasta
* The hasta to set.
*/
public void setHasta(double hasta) {
mHasta = hasta;
}

/**
* @return Returns the intervalo.
*/
public double getNumIntervalos() {
return mNumIntervalos;
}

/**
* @param intervalo
* The intervalo to set.
*/
public void setNumIntervalos(double pNumIntervalos) {
mNumIntervalos = pNumIntervalos;
}

/**
*
*/
public double getIntegral() {
// TODO
double sizeIntervalo = (mHasta - mDesde) / mNumIntervalos;

System.out.println("sizeIntervalo="+sizeIntervalo);

double aux = mDesde;


double dblIntegral = 0;

while (aux < mHasta) {


//dblInt = dblInt + valor de la funcion
this.setXValue(aux);
//this.parseExpression(mEvaluator.getFunction());
this.parseExpression(this.getFunction());
this.updateResult();

String strRes = this.getResult();


double dblRes = 0;
try {
dblRes = Double.parseDouble(strRes.trim());
} catch (NumberFormatException e){e.printStackTrace();}

System.out.println("f(x="+aux+") = "+dblRes);

dblIntegral = dblIntegral + dblRes;


aux = aux + sizeIntervalo;
}

//Compute Integral
//Valor de la funcion en mDesde
this.setXValue(mDesde);
//this.parseExpression(mEvaluator.getFunction());
this.parseExpression(this.getFunction());
this.updateResult();
String strDesde = this.getResult();
double dblDesde = 0;
try {
dblDesde = Double.parseDouble(strDesde.trim());
} catch (NumberFormatException e){e.printStackTrace();}

System.out.println("f(x="+mDesde+") = "+dblDesde);

//Valor de la funcion en mHasta


this.setXValue(mHasta);
//this.parseExpression(mEvaluator.getFunction());
this.parseExpression(this.getFunction());
this.updateResult();
String strHasta = this.getResult();
double dblHasta = 0;
try {
dblHasta = Double.parseDouble(strHasta.trim());
} catch (NumberFormatException e){e.printStackTrace();}

System.out.println("f(x="+mHasta+") = "+dblHasta);

dblIntegral = (dblIntegral-(dblDesde+dblHasta)/2)* sizeIntervalo;

/*
* for(double i=mDesde; i <mHasta; i+sizeIntervalo) { }
*/

return dblIntegral;
}

/**
* @return Returns the error.
*/
public String getError() {
return mError;
}
/**
* @param pError The error to set.
*/
public void setError(String pError) {
mError = pError;
}
/**
* @return Returns the result.
*/
public String getResult() {
return mResult;
}
/**
* @param pResult The result to set.
*/
public void setResult(String pResult) {
mResult = pResult;
}

/**
* @return Returns the myParser.
*/
public JEP getMyParser() {
return myParser;
}
/**
* @param pMyParser The myParser to set.
*/
public void setMyParser(JEP pMyParser) {
myParser = pMyParser;
}
/**
* @return Returns the xValue.
*/
public double getXValue() {
return xValue;
}
/**
* @param pValue The xValue to set.
*/
public void setXValue(double pValue) {
xValue = pValue;
}
}

BINARIO

133 lines (120 sloc) 3.42 KB


import java.util.Scanner;

public class Calculadora {

public String resolverOperacion(String operando, String numeroUno, String numeroDos){


String resultado = "";

if(numeroUno.length() < numeroDos.length() ){


String temp = numeroDos.substring(0 , numeroDos.length()-numeroUno.length());
temp = temp.replace('1','0');
numeroUno = temp = temp+numeroUno;
}else{
String temp = numeroUno.substring(0 , numeroUno.length()-numeroDos.length());
temp = temp.replace('1','0');
numeroDos = temp = temp+numeroDos;
}
if(operando.equals("+")){
resultado = sumarNumerosBinarios(numeroUno, numeroDos);
}else if(operando.equals("-")){
resultado = restarNumerosBinarios(numeroUno, numeroDos);
if(resultado .charAt(0)=='-')
resultado = complementoADos(resultado);
}
return resultado;
}

public String sumarNumerosBinarios(String numeroUno, String numeroDos){


String resultado = "";
char acarreo = '0';
for(int i = numeroUno.length()-1; i>= 0; i--){
if(numeroUno.charAt(i) == '0' && numeroDos.charAt(i) == '0' ){
if(acarreo == '0'){
resultado = "0"+resultado;
}else {
resultado = "1"+resultado;
}
}else if((numeroUno.charAt(i) == '0' && numeroDos.charAt(i) == '1' )||
(numeroUno.charAt(i) == '1' && numeroDos.charAt(i) == '0')){
if(acarreo == '0'){
resultado = "1"+resultado;
}else {
acarreo = '1';
resultado = "0"+resultado;
}
}else if((numeroUno.charAt(i) == '1' && numeroDos.charAt(i) == '1' )){
if(acarreo == '0'){
acarreo = '1';
resultado = "0"+resultado;
}else {
resultado = "1"+resultado;
}
}
}
if(acarreo =='1'){
resultado = acarreo + resultado;
}
return resultado;
}

public String restarNumerosBinarios(String numeroUno, String numeroDos){


String resultado = "";
char debo = '0';

for(int i = numeroUno.length()-1; i>= 0; i--){


if((numeroUno.charAt(i) == '0' && debo == '0') ||
(numeroUno.charAt(i) == '1' && debo == '1')){
if(numeroDos.charAt(i) =='0'){
debo = '0';
resultado = "0"+resultado;
}else {
debo = '1';
resultado = "1"+resultado;
}
}else if(numeroUno.charAt(i) == '1' && debo == '0' ){
debo = '0';
if(numeroDos.charAt(i) =='0'){
resultado = "1"+resultado;
}else {
resultado = "0"+resultado;
}
}else if(numeroUno.charAt(i) == '0' && debo == '1' ){
debo = '1';
if(numeroDos.charAt(i) =='0'){
resultado = "1"+resultado;
}else {
resultado = "0"+resultado;
}
}
}
if(debo == '1')
resultado = "-"+resultado;
return resultado;
}
public String complementoADos(String numero){
int indice = numero.length();
String nuevo = "";
for(int i = numero.length()-1; i>= 0; i--){
if(numero.charAt(i)=='1'){
nuevo = numero.charAt(i)+nuevo;
indice = i-1;
break;
}
nuevo = numero.charAt(i)+nuevo;
}
for(int i = indice; i>= 0; i--){
if(numero.charAt(i)=='1'){
nuevo = '0'+nuevo;
}else if(numero.charAt(i)=='0'){
nuevo = '1'+nuevo;
}else{
nuevo = numero.charAt(i)+nuevo;
}
}
return nuevo;
}

public static void main(String[] args) {


Calculadora calculadora = new Calculadora();
Scanner in = new Scanner (System.in);
while(in.hasNextLine()){
String numeroUno = in.nextLine();
String operando = in.nextLine();
String numeroDos = in.nextLine();
System.out.println(calculadora.resolverOperacion(operando, numeroUno, numeroDos));
}
}

VENTANA

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;

public class Ventana extends JFrame {

private JPanel contentPane;


private JTextField tfNumeroUno;
private JTextField tfNumeroDos;
private JTextField tfResultado;
private Calculadora calculadora;

/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Ventana frame = new Ventana();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
* Create the frame.
*/
public Ventana() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 435, 241);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
GridBagLayout gbl_contentPane = new GridBagLayout();
gbl_contentPane.columnWidths = new int[]{0, 30, 0, 0, 0, 0};
gbl_contentPane.rowHeights = new int[]{30, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_contentPane.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0,
Double.MIN_VALUE};
gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
Double.MIN_VALUE};
contentPane.setLayout(gbl_contentPane);

JLabel lblNewLabel = new JLabel("Numero uno");


GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
gbc_lblNewLabel.anchor = GridBagConstraints.EAST;
gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel.gridx = 0;
gbc_lblNewLabel.gridy = 0;
contentPane.add(lblNewLabel, gbc_lblNewLabel);

tfNumeroUno = new JTextField();


tfNumeroUno.setHorizontalAlignment(SwingConstants.RIGHT);
GridBagConstraints gbc_tfNumeroUno = new GridBagConstraints();
gbc_tfNumeroUno.fill = GridBagConstraints.HORIZONTAL;
gbc_tfNumeroUno.anchor = GridBagConstraints.EAST;
gbc_tfNumeroUno.gridwidth = 3;
gbc_tfNumeroUno.insets = new Insets(0, 0, 5, 5);
gbc_tfNumeroUno.gridx = 1;
gbc_tfNumeroUno.gridy = 0;
contentPane.add(tfNumeroUno, gbc_tfNumeroUno);
tfNumeroUno.setColumns(10);

JLabel lblNewLabel_1 = new JLabel("Numero Dos");


GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints();
gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST;
gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_1.gridx = 0;
gbc_lblNewLabel_1.gridy = 2;
contentPane.add(lblNewLabel_1, gbc_lblNewLabel_1);

tfNumeroDos = new JTextField();


tfNumeroDos.setHorizontalAlignment(SwingConstants.RIGHT);
GridBagConstraints gbc_tfNumeroDos = new GridBagConstraints();
gbc_tfNumeroDos.fill = GridBagConstraints.HORIZONTAL;
gbc_tfNumeroDos.anchor = GridBagConstraints.EAST;
gbc_tfNumeroDos.gridwidth = 3;
gbc_tfNumeroDos.insets = new Insets(0, 0, 5, 5);
gbc_tfNumeroDos.gridx = 1;
gbc_tfNumeroDos.gridy = 2;
contentPane.add(tfNumeroDos, gbc_tfNumeroDos);
tfNumeroDos.setColumns(10);

JButton btnMenos = new JButton("-");


btnMenos.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent arg0) {


if(calculadora == null)
calculadora = new Calculadora();

if(!tfNumeroDos.equals("") && !tfNumeroUno.equals(""))


tfResultado.setText(calculadora.resolverOperacion("-", tfNumeroUno.getText(),
tfNumeroDos.getText()));
}
});
GridBagConstraints gbc_btnMenos = new GridBagConstraints();
gbc_btnMenos.insets = new Insets(0, 0, 5, 5);
gbc_btnMenos.gridx = 2;
gbc_btnMenos.gridy = 4;
contentPane.add(btnMenos, gbc_btnMenos);

JButton btnMas = new JButton("+");


btnMas.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if(calculadora == null)
calculadora = new Calculadora();

if(!tfNumeroDos.equals("") && !tfNumeroUno.equals(""))


tfResultado.setText(calculadora.resolverOperacion("+", tfNumeroUno.getText(),
tfNumeroDos.getText()));
}
});
GridBagConstraints gbc_btnMas = new GridBagConstraints();
gbc_btnMas.insets = new Insets(0, 0, 5, 5);
gbc_btnMas.gridx = 3;
gbc_btnMas.gridy = 4;
contentPane.add(btnMas, gbc_btnMas);

JLabel lblNewLabel_2 = new JLabel("Resultado");


GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints();
gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5);
gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST;
gbc_lblNewLabel_2.gridx = 0;
gbc_lblNewLabel_2.gridy = 6;
contentPane.add(lblNewLabel_2, gbc_lblNewLabel_2);
tfResultado = new JTextField();
tfResultado.setHorizontalAlignment(SwingConstants.RIGHT);
GridBagConstraints gbc_tfResultado = new GridBagConstraints();
gbc_tfResultado.insets = new Insets(0, 0, 5, 5);
gbc_tfResultado.fill = GridBagConstraints.HORIZONTAL;
gbc_tfResultado.anchor = GridBagConstraints.EAST;
gbc_tfResultado.gridwidth = 3;
gbc_tfResultado.gridx = 1;
gbc_tfResultado.gridy = 6;
contentPane.add(tfResultado, gbc_tfResultado);
tfResultado.setColumns(10);
}