Site hosted by Angelfire.com: Build your free website today!

ESCUELA SUPERIOR DE COMPUTO

CALCULADORA

Maria de Lourdes Lopez Ramirez

Codigo Fuente:

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

import javax.swing.*;

public class Calcu extends Applet implements ActionListener {

double numero = 0, total = 0;

char operacion;

StringBuffer buffer = new StringBuffer();

JTextField tf= new JTextField();

private Button b1;

private Button b2;

private Button b3;

private Button b4;

private Button b5;

private Button b6;

private Button b7;

private Button b8;

private Button b9;

private Button b0;

private Button bz;

private Button bc;

private Button be;

private Button bo;

private Button bs;

private Button br;

private Button bd;

private Button bm;

private Button bp;

private Button bi;

private Button reset;

private Panel pn;

private JPanel pc;

private Panel ps;

public void init() {

b1=new Button("1");

b2 = new Button("2");

b3 = new Button("3");

b4 = new Button("4");

b5 = new Button("5");

b6 = new Button("6");

b7 = new Button("7");

b8 = new Button("8");

b9 = new Button("9");

b0 = new Button("0");

bz = new Button("Sqrt");

bc = new Button("x2");

be = new Button("x*");

bo = new Button("%");

bs = new Button("+");

br = new Button("-");

bd = new Button("/");

bm = new Button("*");

bp = new Button(".");

bi = new Button("=");

reset = new Button("CE");

pn = new Panel();

pc = new JPanel();

ps = new Panel();

setLayout(new BorderLayout());

pc.setBackground(Color.white);

pn.setLayout(new BorderLayout());

pn.add("Center",tf);

tf.setEditable(false);

tf.setHorizontalAlignment(JTextField.RIGHT);

tf.setFont(new java.awt.Font("Arial", java.awt.Font.BOLD, 12));

pc.setBorder(BorderFactory.createTitledBorder(""));

pc.setLayout(new GridLayout(5, 4,15,15));

pc.add(b7);

pc.add(b8);

pc.add(bd);

pc.add(b9);

pc.add(b4);

pc.add(b5);

pc.add(b6);

pc.add(b1);

pc.add(b2);

pc.add(b3);

pc.add(b0);

pc.add(bi);

pc.add(bm);

pc.add(bz);

pc.add(bc);

pc.add(be);

pc.add(bo);

pc.add(bs);

pc.add(br);

pc.add(bp);

pn.add(reset);

ps.setLayout(new BorderLayout());

//inferior.add("Center", reset);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

b7.addActionListener(this);

b8.addActionListener(this);

b9.addActionListener(this);

b0.addActionListener(this);

bs.addActionListener(this);

br.addActionListener(this);

bd.addActionListener(this);

bm.addActionListener(this);

bp.addActionListener(this);

bi.addActionListener(this);

bz.addActionListener(this);

bc.addActionListener(this);

be.addActionListener(this);

bo.addActionListener(this);

reset.addActionListener(this);

}

public void actionPerformed(ActionEvent e) {

Double d1, d2;

String s;

if(e.getSource() == b1) {

buffer.append('1');

} else if (e.getSource() == b2) {

buffer.append('2');

} else if (e.getSource() == b3) {

buffer.append('3');

} else if (e.getSource() == b4) {

buffer.append('4');

} else if (e.getSource() == b5) {

buffer.append('5');

} else if (e.getSource() == b6) {

buffer.append('6');

} else if (e.getSource() == b7) {

buffer.append('7');

} else if (e.getSource() == b8) {

buffer.append('8');

} else if (e.getSource() == b9) {

buffer.append('9');

} else if (e.getSource() == b0) {

s=tf.getText();

if(s==null)

{

s="";

tf.setText(s);

}

else buffer.append('0');

} else if (e.getSource() == bp) {

buffer.append('.');

} else if (e.getSource() == br) {

if(tf.getText() != "") {

d1= new Double(tf.getText());

numero = d1.doubleValue();

}

buffer = null;

buffer = new StringBuffer();

operacion = 'r';

} else if (e.getSource() == bd) {

if(tf.getText() != "") {

d1 = new Double(tf.getText());

numero = d1.doubleValue();

}

buffer= null;

buffer = new StringBuffer();

operacion = 'd';

} else if (e.getSource() == bm) {

if(tf.getText() != "") {

d1 = new Double(tf.getText());

numero = d1.doubleValue();

}

buffer= null;

buffer = new StringBuffer();

operacion = 'm';

} else if (e.getSource() == bs) {

if(tf.getText() != "") {

d1= new Double(tf.getText());

numero = d1.doubleValue();

}

buffer= null;

buffer= new StringBuffer();

operacion = 's';

} else if (e.getSource() == bz) {

if(tf.getText() != "") {

d1= new Double(tf.getText());

numero = d1.doubleValue();

numero = Math.sqrt(numero);

buffer= null;

buffer= new StringBuffer(Double.toString(numero));

tf.setText(buffer.toString());

}

} else if (e.getSource() == bc) {

if(tf.getText() != "") {

d1= new Double(tf.getText());

numero = d1.doubleValue();

numero = Math.pow(numero,2);

buffer= null;

buffer = new StringBuffer(Double.toString(numero));

tf.setText(buffer.toString());

}

} else if (e.getSource() == be) {

if(tf.getText() != "") {

d1= new Double(tf.getText());

numero = d1.doubleValue();

}

buffer= null;

buffer = new StringBuffer();

operacion = 'p';

} else if (e.getSource() == bo) {

if(tf.getText() != "") {

d1 = new Double(tf.getText());

numero = d1.doubleValue();

}

buffer= null;

buffer = new StringBuffer();

operacion = 'c';

}

else if (e.getSource() == bi) {

if(tf.getText() != "") {

d2 = new Double(tf.getText());

if(operacion == 'r') {

total = (numero-d2.doubleValue());

} else if(operacion == 'd') {

total = (numero/d2.doubleValue());

} else if(operacion == 'm') {

total = (numero*(d2.doubleValue()));

} else if(operacion == 's') {

total = (numero+d2.doubleValue());

} else if(operacion == 'p') {

total = Math.pow(numero,d2.doubleValue());

} else if(operacion == 'c') {

total = numero*d2.doubleValue()/100;

}

}

buffer= null;

buffer= new StringBuffer();

buffer.append(total);

}

tf.setText(buffer.toString());

if(e.getSource() == reset) {

numero = 0;

total = 0;

buffer= null;

buffer= new StringBuffer();

tf.setText(buffer.toString());

}

}

}