Calculator - operations with decimals

3

Once again I appear with this calculator here, I've been much helped with it and it's almost done!

All operations are working perfectly, with no error, except for a small detail ... it can only do calculations like 9+8 , 7*8 , etc.

I can not operate things like 18+30 than the result of 12 ! 25+52 result of 14 , and 23*2 result of 10 ...

In some cases it seems like it adds up the decimal numbers and then it operates by the next unit, in others it seems to be crazy!

The code is below.

import javax.swing.*;
import java.awt.event.*;

public class Calculadora extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;


    private JButton botaoMais;
    private JButton botaoMenos;
    private JButton botaoVezes;
    private JButton botaoDividi;
    private JButton botaoIgual;
    private JButton n1;
    private JButton n2;
    private JButton n3;
    private JButton n4;
    private JButton n5;
    private JButton n6;
    private JButton n7;
    private JButton n8;
    private JButton n9;
    private JButton n0;
    private char operacao;
    private JTextField campo;
    private int memoria;
    private int leitura;

    public Calculadora (){
        this.setTitle("Calculadora");
        this.setBounds(0,0,280,350);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.getContentPane().setLayout(null);

        leitura = 0;
        memoria = 0;


    n1 = new JButton();
    n1.setText("1");
    n1.setBounds(25, 70, 45, 45);
    this.add(n1);

    n2 = new JButton();
    n2.setText ("2");
    n2.setBounds(85,70,45,45);
    this.add(n2);

    n3 = new JButton();
    n3.setText("3");
    n3.setBounds(140,70,45,45);
    this.add(n3);

    n4 = new JButton();
    n4.setText("4");
    n4.setBounds(25,125,45,45);
    this.add(n4);

    n5 = new JButton();
    n5.setText("5");
    n5.setBounds(85,125,45,45);
    this.add(n5);

    n6 = new JButton();
    n6.setText("6");
    n6.setBounds(140,125,45,45);
    this.add(n6);

    n7 = new JButton();
    n7.setText("7");
    n7.setBounds(25,180,45,45);
    this.add(n7);

    n8 = new JButton();
    n8.setText("8");
    n8.setBounds (85,180,45,45);
    this.add(n8);

    n9 = new JButton();
    n9.setText("9");
    n9.setBounds (140,180,45,45);
    this.add (n9);

    n0 = new JButton();
    n0.setText("0");
    n0.setBounds(140,235,45,45);
    this.add(n0);

    botaoIgual = new JButton();
    botaoIgual.setText ("=");
    botaoIgual.setBounds(25, 235, 106, 45);
    this.add(botaoIgual);

    botaoMais = new JButton();
    botaoMais.setText("+");
    botaoMais.setBounds(195, 70, 45, 45);
    this.add(botaoMais);

    botaoMenos = new JButton();
    botaoMenos.setText("-");
    botaoMenos.setBounds(195,125,45,45);
    this.add(botaoMenos);

    botaoVezes = new JButton();
    botaoVezes.setText("x");
    botaoVezes.setBounds(195,180,45,45);
    this.add(botaoVezes);

    botaoDividi = new JButton();
    botaoDividi.setText("/");
    botaoDividi.setBounds(195,235,45,45);
    this.add(botaoDividi);

    campo = new JTextField();
    campo.setBounds(25, 25, 215, 30);
    this.add(campo);

    n1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura +=1;
            campo.setText(campo.getText()+ "1");


    }
  });
    n2.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura +=2;
            campo.setText(campo.getText()+ "2");
        }
    });
    n3.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            leitura +=3;
            campo.setText(campo.getText()+"3");
        }
    });
    n4.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura+=4;
            campo.setText(campo.getText()+ "4");
        }
    });
    n5.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            leitura+=5;
            campo.setText(campo.getText()+ "5");
        }
    });
    n6.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura+=6;
            campo.setText(campo.getText()+"6");
        }
    });
    n7.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura+=7;
            campo.setText(campo.getText()+ "7");

        }
    });
    n8.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura+=8;
            campo.setText(campo.getText()+"8");

        }
    });
    n9.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura+=9;
            campo.setText(campo.getText()+ "9");
        }
    });
    n0.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            leitura+=0;
            campo.setText(campo.getText()+ "0");
        }
    });
    botaoMais.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            operacao = '+';
            if (memoria > 0){
                memoria += leitura;
            }else {
                memoria = leitura;
            }
            leitura = 0;
                    campo.setText("");
        }
    });
    botaoMenos.addActionListener (new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            operacao = '-';
            if(memoria > 0){
                memoria -= leitura;
            }else {
                memoria = leitura;
            }
            leitura = 0;
            campo.setText("");
        }
    });
    botaoVezes.addActionListener (new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            operacao = '*';
            if (memoria > 0){
                memoria *= leitura;
            }else {
                memoria = leitura;
            }
            leitura = 0;
            campo.setText("");
        }
    });
    botaoDividi.addActionListener (new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            operacao = '/';
            if (memoria > 0){
                memoria /= leitura;
            }else {
                memoria = leitura;
            }
            leitura = 0;
            campo.setText("");
        }
    });
    botaoIgual.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            switch (operacao){
            case '+':{
                memoria +=leitura;
                break;
            }
            case '-':{
                memoria-=leitura;
                break;
            }
            case '*':{
                memoria *=leitura;
                break;
            }
            case '/':{
                memoria /=leitura;
                break;
            }
            }
            leitura = 0;
            campo.setText (""+ memoria);

        }
    });
}

public static void main(String[] args) {
    Calculadora exemplo = new Calculadora();
        exemplo.setVisible(true);
}
}
    
asked by anonymous 11.04.2014 / 06:47

1 answer

7

First of all, congratulations! You've come very close to a 100% functional implementation ...

As I mentioned in your first question ( Eclipse Java Calculator ) there were some bugs to fix. And one of them persisted ...

THE PROBLEM

When you click a numeric button ( 1 , 2 , 3 etc.) you increment the variable leitura with the value corresponding to the button, right?

As long as only 1 digit is typed, everything will work correctly ...

But when you enter 2 digits, your calculator is not entering the tens value correctly , and yes, just adding , like this:

  

The operation 18 + 30 = 12 because (1 + 8) + (3 + 0) = 12

     

The operation 25 + 52 = 14 because (2 + 5) + (5 + 2) = 14

     

The operation 23 * 2 = 10 because (2 + 3) * 2 = 10

FIXING THE PROBLEM

Come on (button 1 ):

n1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent evt){
        leitura +=1;
        campo.setText(campo.getText()+ "1");
}
});

//para 1 clique: leitura = 1;
//para 2 cliques: leitura = 2;
//para 3 cliques: leitura = 3;
...

How should it be ( 1 button):

n1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent evt){
        leitura *= 10;  // faltou esse pequeno detalhe em seu código!
        leitura += 1;
        campo.setText(campo.getText()+ "1");
}
});

//para 1 clique: leitura = 1;
//para 2 cliques: leitura = 11;
//para 3 cliques: leitura = 111;
...

Make this setting on all numeric buttons and the problem will be corrected!

Any questions please ask in the comments. I hope I have helped!

    
11.04.2014 / 08:25