• Lenguaje

    Java

  • Descripción

    Calculadora simple de escritorio.
    Hace operaciones de suma, resta, multiplicación, división, raíz cuadrada, porcentaje y operaciones básicas de memoria.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class Calculadora extends Applet implements ActionListener, KeyEventDispatcher {

    private static final long serialVersionUID = 1L;
    private static String etiquetas[] = {
        "<\u2212", "CE", "C",
        "MC", "MR", "MS", "M+",
        "7", "8", "9", "\u00F7", "\u221A",
        "4", "5", "6", "\u00D7", "%",
        "1", "2", "3", "\u2212", "1/x",
        "0", "\u00B1", ".", "+", "="
    };
    private static int[][] locaciones = {
                    {99,  47}, {137,  47}, {175,  47},
        {12,  89}, { 12, 118}, { 12, 147}, { 12, 176},
        {61,  89}, { 99,  89}, {137,  89}, {175,  89}, {213,  89},
        {61, 118}, { 99, 118}, {137, 118}, {175, 118}, {213, 118},
        {61, 147}, { 99, 147}, {137, 147}, {175, 147}, {213, 147},
        {61, 176}, { 99, 176}, {137, 176}, {175, 176}, {213, 176}
    };
    private Button[] botones = new Button[etiquetas.length];
    private Label display, displayMemoria;
    private String cifra = null;
    private double valor = 0.0, memoria = 0.0, anterior = 0.0;
    private char operador = '\0';
   
    @Override
    public void init() {
        setLayout(null);
        display = new Display("0", Label.RIGHT);
        display.setBounds(12, 9, 239, 25);
        add(display);
        int i;
        for(i=0; i<etiquetas.length; i++) {
            botones[i] = new Button(etiquetas[i]);
            botones[i].addActionListener(this);
            botones[i].setBounds(locaciones[i][0], locaciones[i][1], 33, 23);
            add(botones[i]);
        }
        displayMemoria = new Display("", Label.CENTER);
        displayMemoria.setBounds(61, 47, 33, 23);
        add(displayMemoria);
        setBackground(new Color(212, 208, 200));
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
    }
   
    private void setValor(double valor) {
        this.valor = valor;
        cifra = null;
        display.setText(valor==0.0? "0": (valor == Math.floor(valor)? String.valueOf((long)valor): String.valueOf(valor)));
    }
   
    private void setMemoria(double memoria) {
        this.memoria = memoria;
        displayMemoria.setText(memoria==0.0? "": "M");
    }

    private void mostrarError() {
        display.setText("Error");
        cifra = null;
        valor = 0.0;
    }

    private void onButtonPress(int boton) {
        char tecla = etiquetas[boton].charAt(0);
        switch (boton) {
        //  DEL
        case 0:
            if (cifra == null || cifra.length() == 1 || (cifra.charAt(0)=='-' && cifra.length() == 2) || cifra.equals("0.") || cifra.equals("-0."))
                setValor(0.0);
            else {
                cifra = cifra.substring(0,cifra.length()-1);
                valor = Double.parseDouble(cifra);
                display.setText(cifra);
            }
            break;
        //  CE
        case 1:
            anterior = 0.0;
            operador = '\0';
        //  C
        case 2:
            setValor(0.0);
            break;
        //  MC
        case 3:
            setMemoria(0.0);
            break;
        //  MR
        case 4:
            setValor(memoria);
            break;
        //  MS
        case 5:
            setMemoria(valor);
            break;
        //  M+
        case 6:
            setMemoria(memoria+valor);
            break;
        //  SQRT
        case 11:
            if (valor>=0.0)
                setValor(Math.sqrt(valor));
            else
                mostrarError();
            break;
        //  /
        case 10:
        //  *
        case 15:
        //  %
        case 16:
        //  -
        case 20:
        //  +
        case 25:
        //  =
        case 26:
            double temporal = valor;
            switch (operador) {
            case '/':
                if (valor!=0.0)
                    setValor(anterior/valor);
                else
                    mostrarError();
                break;
            case '*':
                setValor(anterior*valor);
                break;
            case '%':
                setValor(anterior*valor/100.0);
                break;
            case '-':
                setValor(anterior-valor);
                break;
            case '+':
                setValor(anterior+valor);
                break;
            }
            anterior = operador == '/' || operador == '*' || operador == '%' || operador == '-' || operador == '+'? valor: temporal;
            if (boton == 10)
                operador = '/';
            else if (boton == 15)
                operador = '*';
            else if (boton == 20)
                operador = '-';
            else
                operador = tecla;
            cifra = null;
            break;
        //  1/x
        case 21:
            if (valor!=0.0)
                setValor(1.0/valor);
            else
                mostrarError();
            break;
        //  +/-
        case 23:
            if (valor!=0.0)
                if (cifra == null)
                    setValor(-valor);
                else {
                    valor = -valor;
                    cifra = cifra.charAt(0) == '-'? cifra.substring(1): '-' + cifra;
                    display.setText(cifra);
                }
            break;
        //  .
        case 24:
            if (cifra == null) {
                valor = 0.0;
                cifra = "0.";
                display.setText(cifra);
            } else if (!cifra.contains(".")) {
                cifra = cifra + '.';
                display.setText(cifra);
            }
            break;
        //  0 1 2 3 4 5 6 7 8 9
        default:
            if (cifra != null || tecla != '0') {
                cifra = cifra == null? String.valueOf(tecla): cifra + tecla;
                valor = Double.parseDouble(cifra);
                display.setText(cifra);
            }
        }
        botones[boton].requestFocus();
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        Object boton = event.getSource();
        for (int i=0; i<botones.length; i++)
            if (boton == botones[i]) {
                onButtonPress(i);
                break;
            }
    }
   
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getID() != KeyEvent.KEY_LAST)
            return false;
        int boton = -1;
        char tecla = Character.toLowerCase(event.getKeyChar());
        switch(tecla) {
        case '\b': boton= 0; break;
        case 127:
        case 'c': boton= 2; break;
        case '7': boton= 7; break;
        case '8': boton= 8; break;
        case '9': boton= 9; break;
        case '/': boton=10; break;
        case 's': boton=11; break;
        case '4': boton=12; break;
        case '5': boton=13; break;
        case '6': boton=14; break;
        case '*': boton=15; break;
        case 'p':
        case '%': boton=16; break;
        case '1': boton=17; break;
        case '2': boton=18; break;
        case '3': boton=19; break;
        case '-': boton=20; break;
        case 'r': boton=21; break;
        case '0': boton=22; break;
        case '.': boton=24; break;
        case '+': boton=25; break;
        case '\r':
        case '\n':
        case '=': boton=26; break;
        default : return false;
        }
        onButtonPress (boton);
        return false;
    }

    public static void main(String[] args) {
        Frame frame = new Frame("Calculadora");
        frame.setLayout(new BorderLayout());
        Calculadora calculadora = new Calculadora ();
        calculadora.init();
        frame.add(calculadora);
        frame.setSize(272, 242);
        frame.setLocationRelativeTo(null);
        frame.addWindowListener(new WindowAdapter(){
           
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
           
        });
        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher(){

            @Override
            public boolean dispatchKeyEvent(KeyEvent e) {
                if(e.getKeyCode()==KeyEvent.VK_ESCAPE)
                    System.exit(0);
                return false;
            }
           
        });
        frame.setResizable(false);
        frame.setVisible(true);
    }

}

class Display extends Label {
   
    private static final long serialVersionUID = 1L;
   
    public Display(String text, int alignment) throws HeadlessException {
        super(text, alignment);
        setBackground(Color.WHITE);
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.GRAY);
        g.drawLine(0, 0, 0, getHeight()-1);
        g.drawLine(0, 0, getWidth()-1, 0);
        super.paint(g);
    }
}