olá pessoal, fiz um projeto de uma calculadora cientifica, está quase completa (mas ela roda para quem quiser ver o que foi feito até agora), o meu problema é colocar nesta calculadora as expressões lógicas not or , etc, e colocar cálculos de binário, hexadecimal, etc. como a calculadora do windows por exemplo.
alguém poderia continuar a calculadora para mim
só explicando
a primeira classe é o painel com os botões, o frame de telinha etc, e a classe principal
/**
*
*/
/**
- @author aluysio/erick
*/
import java.awt.;
import java.awt.event.;
import java.math.;
import javax.swing.;
class CalculatorPanel extends JPanel implements ActionListener
{
public CalculatorPanel()
{
setLayout(null);
setBackground(Color.gray);
//------------ Display -----------------------
display = new JTextField(“0”);
display.setEditable(false);
add(display);
display.setBounds(10,15,300,25);
//------------ Botões ------------------------
b1 = new Button(“0”);
b1.setBackground(Color.white);
b1.setForeground(Color.blue);
add(b1);
b1.setBounds(10,155,30,30);
b1.addActionListener(this);
b2 = new Button(“1”);
b2.setBackground(Color.white);
b2.setForeground(Color.blue);
add(b2);
b2.addActionListener(this);
b2.setBounds(10,120,30,30);
b3 = new Button(“4”);
b3.setBackground(Color.white);
b3.setForeground(Color.blue);
add(b3);
b3.addActionListener(this);
b3.setBounds(10,85,30,30);
b4 = new Button(“7”);
b4.setBackground(Color.white);
b4.setForeground(Color.blue);
add(b4);
b4.addActionListener(this);
b4.setBounds(10,50,30,30);
b5 = new Button("+/-");
b5.setBackground(Color.white);
b5.setForeground(Color.blue);
add(b5);
b5.addActionListener(this);
b5.setBounds(45,155,30,30);
b6 = new Button(“2”);
b6.setBackground(Color.white);
b6.setForeground(Color.blue);
add(b6);
b6.addActionListener(this);
b6.setBounds(45,120,30,30);
b7 = new Button(“5”);
b7.setBackground(Color.white);
b7.setForeground(Color.blue);
add(b7);
b7.addActionListener(this);
b7.setBounds(45,85,30,30);
b8 = new Button(“8”);
b8.setBackground(Color.white);
b8.setForeground(Color.blue);
add(b8);
b8.addActionListener(this);
b8.setBounds(45,50,30,30);
b9 = new Button(".");
b9.setBackground(Color.white);
b9.setForeground(Color.blue);
add(b9);
b9.addActionListener(this);
b9.setBounds(80,155,30,30);
b10 = new Button(“3”);
b10.setBackground(Color.white);
b10.setForeground(Color.blue);
add(b10);
b10.addActionListener(this);
b10.setBounds(80,120,30,30);
b11 = new Button(“6”);
b11.setBackground(Color.white);
b11.setForeground(Color.blue);
add(b11);
b11.addActionListener(this);
b11.setBounds(80,85,30,30);
b12 = new Button(“9”);
b12.setBackground(Color.white);
b12.setForeground(Color.blue);
add(b12);
b12.addActionListener(this);
b12.setBounds(80,50,30,30);
b13= new Button("+");
b13.setBackground(Color.white);
b13.setForeground(Color.gray);
add(b13);
b13.addActionListener(this);
b13.setBounds(115,155,30,30);
b14 = new Button("-");
b14.setBackground(Color.white);
b14.setForeground(Color.gray);
add(b14);
b14.addActionListener(this);
b14.setBounds(115,120,30,30);
b15 = new Button("\u00d7");
b15.setBackground(Color.white);
b15.setForeground(Color.gray);
add(b15);
b15.addActionListener(this);
b15.setBounds(115,85,30,30);
b16 = new Button("\u00f7");
b16.setBackground(Color.white);
b16.setForeground(Color.gray);
add(b16);
b16.addActionListener(this);
b16.setBounds(115,50,30,30);
b17= new Button("=");
b17.setBackground(Color.green);
b17.setForeground(Color.black);
add(b17);
b17.addActionListener(this);
b17.setBounds(150,120,30,65);
b18 = new Button("%");
b18.setBackground(Color.white);
b18.setForeground(Color.red);
add(b18);
b18.addActionListener(this);
b18.setBounds(150,85,30,30);
b19 = new Button(“sqrt”);
b19.setBackground(Color.white);
b19.setForeground(Color.red);
add(b19);
b19.addActionListener(this);
b19.setBounds(150,50,30,30);
b20= new Button(“E”);
b20.setBackground(Color.gray);
b20.setForeground(Color.orange);
add(b20);
b20.addActionListener(this);
b20.setBounds(195,155,30,30);
b21 = new Button(“PI”);
b21.setBackground(Color.gray);
b21.setForeground(Color.orange);
add(b21);
b21.addActionListener(this);
b21.setBounds(195,120,30,30);
b22 = new Button(“n!”);
b22.setBackground(Color.gray);
b22.setForeground(Color.orange);
add(b22);
b22.addActionListener(this);
b22.setBounds(195,85,30,30);
b23 = new Button(“In”);
b23.setBackground(Color.gray);
b23.setForeground(Color.orange);
add(b23);
b23.addActionListener(this);
b23.setBounds(195,50,30,30);
b24= new Button(“sin”);
b24.setBackground(Color.gray);
b24.setForeground(Color.orange);
add(b24);
b24.addActionListener(this);
b24.setBounds(230,155,30,30);
b25 = new Button(“cos”);
b25.setBackground(Color.gray);
b25.setForeground(Color.orange);
add(b25);
b25.addActionListener(this);
b25.setBounds(230,120,30,30);
b26 = new Button(“tan”);
b26.setBackground(Color.gray);
b26.setForeground(Color.orange);
add(b26);
b26.addActionListener(this);
b26.setBounds(230,85,30,30);
b27 = new Button(“log”);
b27.setBackground(Color.gray);
b27.setForeground(Color.orange);
add(b27);
b27.addActionListener(this);
b27.setBounds(230,50,30,30);
b28= new Button(“asin”);
b28.setBackground(Color.gray);
b28.setForeground(Color.orange);
add(b28);
b28.addActionListener(this);
b28.setBounds(265,155,30,30);
b29 = new Button(“acos”);
b29.setBackground(Color.gray);
b29.setForeground(Color.orange);
add(b29);
b29.addActionListener(this);
b29.setBounds(265,120,30,30);
b30 = new Button(“atan”);
b30.setBackground(Color.gray);
b30.setForeground(Color.orange);
add(b30);
b30.addActionListener(this);
b30.setBounds(265,85,30,30);
b31 = new Button(“mod”);
b31.setBackground(Color.gray);
b31.setForeground(Color.orange);
add(b31);
b31.addActionListener(this);
b31.setBounds(265,50,30,30);
b32= new Button(“x^2”);
b32.setBackground(Color.gray);
b32.setForeground(Color.orange);
add(b32);
b32.addActionListener(this);
b32.setBounds(300,155,30,30);
b33 = new Button("x^3");
b33.setBackground(Color.gray);
b33.setForeground(Color.orange);
add(b33);
b33.addActionListener(this);
b33.setBounds(300,120,30,30);
b34 = new Button("x^y");
b34.setBackground(Color.gray);
b34.setForeground(Color.orange);
add(b34);
b34.addActionListener(this);
b34.setBounds(300,85,30,30);
b35 = new Button("e^x");
b35.setBackground(Color.gray);
b35.setForeground(Color.orange);
add(b35);
b35.addActionListener(this);
b35.setBounds(300,50,30,30);
b36= new Button("M");
b36.setBackground(Color.blue);
b36.setForeground(Color.yellow);
add(b36);
b36.addActionListener(this);
b36.setBounds(345,155,30,30);
b37 = new Button("RM");
b37.setBackground(Color.blue);
b37.setForeground(Color.yellow);
add(b37);
b37.addActionListener(this);
b37.setBounds(345,120,30,30);
b38 = new Button("M2");
b38.setBackground(Color.blue);
b38.setForeground(Color.yellow);
add(b38);
b38.addActionListener(this);
b38.setBounds(345,85,30,30);
b39 = new Button("M3");
b39.setBackground(Color.blue);
b39.setForeground(Color.yellow);
add(b39);
b39.addActionListener(this);
b39.setBounds(345,50,30,30);
b40= new Button("C");
b40.setBackground(Color.red);
b40.setForeground(Color.white);
add(b40);
b40.addActionListener(this);
b40.setBounds(385,120,30,65);
b41 = new Button("CE");
b41.setBackground(Color.red);
b41.setForeground(Color.white);
add(b41);
b41.addActionListener(this);
b41.setBounds(385,50,30,65);
}
public void actionPerformed(ActionEvent evt)
{
String s = evt.getActionCommand();
if (s.equals("C"))
{
operando1 = 0;
op = "=";
display.setText("0");
start = true;
}
else if('0' <= s.charAt(0) && s.charAt(0) <= '9'|| s.equals("."))
{
if (start)
{
display.setText(s);
start = false;
}
else
{
display.setText(display.getText() + s);
}
}
else
{
if (s.equals("PI"))
{
double aux = 1;
aux = Math.PI;
display.setText("" + aux);
}
if(s.equals("E"))
{
double aux = 1;
aux = Math.E;
display.setText("" + aux);
}
if(s.equals("%"))
{
double x = Double.parseDouble(display.getText());
double total = (operando1*x)/100;
display.setText("" + total);
}
if(s.equals("+/-"))
{
double total = 0;
double x = Double.parseDouble(display.getText());
if (x != 0)
total = (x * -1);
display.setText("" + total);
}
String memoria = "M RM M2 M3";
if(memoria.indexOf(s)!= -1)
{
JOptionPane.showMessageDialog(
null, "Este recursos será implementado na Calculadora versão 2.0 do aluysio e erick !!!");
}
String comp = "sqrt In n! PI log tan cos sin x^2"
+ "x^3 asin acos atan e^x" ;
if (comp.indexOf(s) != -1)
{
calculate1(Double.parseDouble
(display.getText()),s);
start = true;
}
String comum = "= + - * / \u00d7 \u00f7 x^y mod";
if (s.equals("CE"))
{
display.setText("" + operando1);
op = s;
start = true;
}else if (comum.indexOf(s) != -1)
{
double x = Double.parseDouble(display.getText());
calculate(x);
op = s;
start = true;
}
}
}
public void calculate1(double opr,String op)
{
if (op.equals("sqrt")) opr = Math.sqrt(opr);
else if(op.equals("In")) opr = Math.log(opr);
else if(op.equals("n!")) opr = fatorial(opr);
else if(op.equals("log")) opr = Math.log(opr);
else if(op.equals("tan")) opr = Math.tan((opr*Math.PI) / 180);
else if(op.equals("cos")) opr = Math.cos((opr*Math.PI) / 180);
else if(op.equals("sin")) opr = Math.sin((opr*Math.PI) / 180) ;
else if(op.equals("asin")) opr = Math.asin(opr);
else if(op.equals("acos")) opr = Math.acos(opr);
else if(op.equals("atan")) opr = Math.atan(opr);
else if(op.equals("x^2")) opr = Math.pow(opr,2);
else if(op.equals("x^3")) opr = Math.pow(opr,3);
else if(op.equals("e^x")) opr = Math.exp(opr);
display.setText("" + opr);
}
public double fatorial(double nro)
{
if(nro <=1)
return 1;
else
return nro*fatorial(nro-1);
}
public double umSobre(double nro)
{
if (nro != 0)
return ((1)/(nro));
else
return 0;
}
public void calculate(double n)
{
if (op.equals("+")) operando1 += n;
else if (op.equals("-")) operando1 -= n;
else if (op.equals("\u00d7")) operando1 *= n;
else if (op.equals("\u00f7")) operando1 /= n;
else if (op.equals("=")) operando1 = n;
else if (op.equals("x^y")) operando1 = Math.pow(operando1,n);
else if (op.equals("mod")) operando1 %= n;
display.setText("" + operando1);
}
private JTextField display;
private double operando1 = 0;
private BigDecimal memory[];
private boolean start = true;
private String op = "=";
private Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,
b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,
b27,b28,b29,b30,b31,b32,b33,b34,b35,b36,b37,b38,
b39,b40,b41;
}
import javax.swing.;
import java.awt.Container;
import java.awt.Menu;
import java.awt.MenuBar;
import java.awt.MenuItem;
import java.applet.;
import java.awt.Event;
/**
*
*/
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
- @author aluysio/erick
*/
class CalculatorFrame extends JFrame {
JTextArea t;
public CalculatorFrame() {
setTitle("Calculadora - Programa faz-tudo");
setSize(435, 240);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
Container contentPane = getContentPane();
contentPane.add(new CalculatorPanel());
// -------------- Menu -----------------------
MenuBar bar = new MenuBar();
Menu calc = new Menu("Calculadora");
MenuItem exit = new MenuItem(" Sair ");
calc.add(exit);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
public void actionPerformed1(ActionEvent e) {
// TODO Auto-generated method stub
}
});
bar.add(calc);
Menu about = new Menu("Sobre");
bar.add(about);
MenuItem about1 = new MenuItem(" Calculadora");
about.add(about1);
about1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"Calculadora Versão 1.0 - Setembro/2008 " + "\n "
+ "\n " + "\n\t Feito por Aluysio Nunes Campos e Erick Kunhavalik");
}
});
bar.add(about);
setMenuBar(bar);
// --------------------------------------------
}
}
import javax.swing.JFrame;
/**
*
*/
/**
- @author aluysio/ erick
*/
public class Calculadora
{
public static void main(String[] args)
{
CalculatorPanel calculadora =
new CalculatorPanel();
calculadora.show();
CalculatorFrame tela = new CalculatorFrame();
tela.show();
JFrame frame = new JFrame();
frame.show();
}
}