Está en la página 1de 3

package simulinterfaz;

import
import
import
import
import
import
import
import
import

java.awt.Color;
java.awt.Desktop;
java.awt.event.ActionEvent;
java.awt.event.ActionListener;
java.io.*;
java.util.ArrayList;
java.util.logging.Level;
java.util.logging.Logger;
javax.swing.*;

public class simulcongmul extends JFrame implements ActionListener {


private
private
private
private

JLabel label, label2, label3;


JTextField tfield, tfield2, tfield3;
JButton boton, boton3;
int resultado = 0;

simulcongmul() {
setLayout(null);
label = new JLabel("X0 - Semilla: ");
label.setBounds(10, 10, 120, 20);
add(label);
tfield = new JTextField();
tfield.setBounds(135, 11, 50, 20);
add(tfield);
label2 = new JLabel("a - Multiplicador: ");
label2.setBounds(10, 30, 120, 20);
add(label2);
tfield2 = new JTextField();
tfield2.setBounds(135, 31, 50, 20);
add(tfield2);
label3 = new JLabel("m - Mod: ");
label3.setBounds(10, 50, 130, 20);
add(label3);
tfield3 = new JTextField();
tfield3.setBounds(135, 51, 50, 20);
add(tfield3);
boton = new JButton("Calcular");
boton.setBounds(10, 110, 100, 20);
boton.addActionListener(this);
add(boton);
boton3 = new JButton("Volver");
boton3.setBounds(140, 110, 100, 20);
boton3.addActionListener(this);
add(boton3);
}
@Override
public void actionPerformed(ActionEvent e) {
//declaracion de archivo de salida y escritor y otras cosas
File salida = new File("salidacongmulti.txt");
PrintWriter escritor = null;
int semillasiempre = 0;

int arre[] = new int[5];


ArrayList listan = new ArrayList();
ArrayList lista = new ArrayList();
//todo dentro del try catch
try {
if (e.getSource() == boton) {
String presemilla = tfield.getText();
String premultip = tfield2.getText();
String premod = tfield3.getText();
int xn1;
int x0 = Integer.parseInt(presemilla);
int a = Integer.parseInt(premultip);
int m = Integer.parseInt(premod);
semillasiempre = x0;
//Trabajara solo si se cumple la regla
for (int i = 0; i < 999999; i++) {
resultado = (a * x0);
xn1 = resultado % m;
arre[i] = xn1;
if (xn1 < 0) {
xn1 = resultado;
}
//System.out.println(n + "\t" + x0 + "\t" + resultado + "\t"
+ xn1);
listan.add(i);
lista.add(xn1);
x0 = xn1;
if (x0 == semillasiempre) {
i = 999999;
}
/*if (arre[i]==arre[i+1]&&arre[i]==arre[i+2]&&arre[i]==arre[
i+3]) {
i=m;
}*/
}
System.out.println(lista);
System.out.println(listan);
}
/*Para limpiar (sin uso)
if (e.getSource() == boton) {
txtf1.setText("");
txtf2.setText("");
txtf3.setText("");
txtf4.setText("");
}*/
//crear archivo y escribir---------------------------------------if (!salida.exists()) {
salida.createNewFile();
} else {
int nueva = lista.size();
escritor = new PrintWriter(salida);
escritor.println("sem" + "\t" + semillasiempre);
for (int i = 0; i < nueva; i++) {
escritor.println(listan.get(i) + "\t" + lista.get(i));
}
//cerrar escritor
escritor.close();
}

//para salir del programa


this.setVisible(false);
}
} catch (Exception h) {
System.out.println("Asi no funciona. Ingresa bien los valores");
}
}
//main----------------------------------public static void main(String[] args) throws FileNotFoundException, IOExcep
tion {
simulcongmul traslasimu = new simulcongmul();
traslasimu.setBounds(200, 200, 300, 190);
traslasimu.setVisible(true);
traslasimu.setForeground(Color.cyan);
traslasimu.setResizable(false);
traslasimu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
traslasimu.setTitle("Congruencial multiplicativa");
}
}

También podría gustarte