-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalcu.java
More file actions
634 lines (554 loc) · 22.9 KB
/
Calcu.java
File metadata and controls
634 lines (554 loc) · 22.9 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package calculadora;
import java.io.*;
import java.util.Stack;
/**
*
* @author Sara
*/
public class Calcu extends javax.swing.JFrame {
/**
* Creates new form Calcu
*/
private Stack<String> pilam;
private Stack<String> pilaigual;
public Calcu() {
initComponents();
this.setSize(450,500);
this.setLocationRelativeTo(null);
pilam = new Stack<String>();
pilaigual = new Stack<String>();
this.Area.setEditable(false);
}
public void mostrarPila(Stack<String> pila){
Stack<String> pilax = new Stack<String>();
String token;
while(!pila.isEmpty()){
token = pila.pop();
pilax.push(token);
}
if(! pilax.isEmpty()){
token = pilax.pop();
this.pilaigual.push(token);
Area.append(token);
Area.append(" ");
}
}
public Double SacarOperando(Stack<String>pila){
String n = "";
String token = "";
Stack<String> pilax = new Stack<String>();
String aux="";
while((!(aux.equals("+"))&&!(aux.equals("-"))&&!(aux.equals("*"))&&!(aux.equals("/")))&&(!pila.isEmpty())){
token = pila.pop();
pilax.push(token);
if(!pila.isEmpty()){
aux = pila.pop();
pila.push(aux);
}
}
Stack<String> clon = (Stack<String>) pilax.clone();
while(!clon.isEmpty()){
System.out.println(clon.pop());
}
Stack<String> reverse = new Stack<String>();
while(!pilax.isEmpty()){
reverse.push(pilax.pop());
}
while(!reverse.isEmpty()){
n += reverse.pop();
}
return Double.parseDouble(n);
}
public Double Calcular(Stack<String> pila){
String operador = "";
Double token = null;
while((pila.size()>1)&&(!pila.isEmpty())){
Double op1 = this.SacarOperando(pila);
operador = pila.pop();
Double op2 = this.SacarOperando(pila);
switch(operador){
case "+":
token = op1+op2;
pila.push(token.toString());
token = null;
break;
case "-":
token = op1-op2;
pila.push(token.toString());
token = null;
break;
}
}
Double r = Double.parseDouble(pila.pop());
return r;
}
public ArrayList<String> CalcularReales(ArrayList<String> lista){
String c;
int lder, lizq;
int cont;
while(lista.contains(".")){
String izq = "", der = "";
for(int i = 0; i < lista.size(); i++){
c = lista.get(i);
cont = i;
if(c.equals(".")){
while(!c.equals("+")&&!c.equals("-")&&!c.equals("*")&&!c.equals("/")&&!(cont<=0)){
cont = cont-1;
c = lista.get(cont);
}
if(c.equals("+")|| c.equals("-")|| c.equals("*")|| c.equals("/")){
cont = cont + 1;
}
while(!(c.equals("."))){
if(cont < 0) cont = 0;
izq+=lista.get(cont);
cont = cont + 1;
c = lista.get(cont);
}
cont = i + 1;
while(!c.equals("+")&&!c.equals("-")&&!c.equals("*")&&!c.equals("/")&&!(cont>=lista.size())){
der+=lista.get(cont);
cont = cont + 1;
if(cont < lista.size()){
c = lista.get(cont);
}
}
c = izq + "." + der;
lder = der.length();
lizq = izq.length();
ArrayList<String> novo = new ArrayList<>();
for(int j = 0; j < i-lizq; j++){
novo.add(lista.get(j));
}
novo.add(c);
for(int j = lder+i+1; j < lista.size(); j++){
novo.add(lista.get(j));
}
lista = novo;
break;
}
}
}
return lista;
}
public Double CalcularLista(ArrayList<String> lista){
String c;
Double r = null;
String op1, op2;
lista = this.CalcularReales(lista);
while(lista.size() > 1){
if(lista.contains("*")|| (lista.contains("/"))){
for(int i = 0; i < lista.size(); i++){
c = lista.get(i);
if(c.equals("*")){
op1 = lista.get(i - 1);
op2 = lista.get(i + 1);
r = Double.parseDouble(op1) * Double.parseDouble(op2);
ArrayList<String> novo = new ArrayList<>();
for(int j = 0; j < i-1; j++){
novo.add(lista.get(j));
}
novo.add(r.toString());
for(int j = i+2; j < lista.size(); j++){
novo.add(lista.get(j));
}
lista = novo;
}
if(c.equals("/")){
op1 = lista.get(i - 1);
op2 = lista.get(i + 1);
r = Double.parseDouble(op1) / Double.parseDouble(op2);
ArrayList<String> novo = new ArrayList<>();
for(int j = 0; j < i-1; j++){
novo.add(lista.get(j));
}
novo.add(r.toString());
for(int j = i+2; j < lista.size(); j++){
novo.add(lista.get(j));
}
lista = novo;
}
}
}else{
Stack<String> pilax = new Stack<String>();
for(int i = 0; i < lista.size(); i++){
pilax.add(lista.get(i));
}
Stack<String> reverse = new Stack<String>();
while(!pilax.isEmpty()){
reverse.push(pilax.pop());
}
r = this.Calcular(reverse);
ArrayList<String> l = new ArrayList<>();
lista = l;
}
}
Area.setText(r.toString());
Area.append(" ");
return r;
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
Area = new javax.swing.JTextArea();
CE = new javax.swing.JButton();
siete = new javax.swing.JButton();
ocho = new javax.swing.JButton();
cuatro = new javax.swing.JButton();
cinco = new javax.swing.JButton();
seis = new javax.swing.JButton();
uno = new javax.swing.JButton();
tres = new javax.swing.JButton();
menos = new javax.swing.JButton();
igual = new javax.swing.JButton();
mas = new javax.swing.JButton();
division = new javax.swing.JButton();
multiplicar = new javax.swing.JButton();
nueve = new javax.swing.JButton();
dos = new javax.swing.JButton();
cero = new javax.swing.JButton();
punto = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
Area.setColumns(20);
Area.setRows(5);
jScrollPane1.setViewportView(Area);
getContentPane().add(jScrollPane1);
jScrollPane1.setBounds(0, 16, 430, 84);
CE.setText("CE");
CE.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
CEMouseClicked(evt);
}
});
CE.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
CEActionPerformed(evt);
}
});
getContentPane().add(CE);
CE.setBounds(160, 120, 75, 52);
siete.setText("7");
siete.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
sieteMouseClicked(evt);
}
});
siete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sieteActionPerformed(evt);
}
});
getContentPane().add(siete);
siete.setBounds(25, 180, 67, 59);
ocho.setText("8");
ocho.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
ochoMouseClicked(evt);
}
});
getContentPane().add(ocho);
ocho.setBounds(110, 180, 67, 59);
cuatro.setText("4");
cuatro.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
cuatroMouseClicked(evt);
}
});
getContentPane().add(cuatro);
cuatro.setBounds(25, 253, 67, 59);
cinco.setText("5");
cinco.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
cincoMouseClicked(evt);
}
});
getContentPane().add(cinco);
cinco.setBounds(110, 253, 67, 59);
seis.setText("6");
seis.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
seisMouseClicked(evt);
}
});
getContentPane().add(seis);
seis.setBounds(192, 253, 67, 59);
uno.setText("1");
uno.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
unoMouseClicked(evt);
}
});
uno.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
unoActionPerformed(evt);
}
});
getContentPane().add(uno);
uno.setBounds(25, 324, 67, 59);
tres.setText("3");
tres.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
tresMouseClicked(evt);
}
});
getContentPane().add(tres);
tres.setBounds(192, 324, 67, 59);
menos.setText("-");
menos.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
menosMouseClicked(evt);
}
});
getContentPane().add(menos);
menos.setBounds(350, 160, 67, 44);
igual.setText("=");
igual.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
igualMouseClicked(evt);
}
});
getContentPane().add(igual);
igual.setBounds(280, 270, 129, 110);
mas.setText("+");
mas.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
masMouseClicked(evt);
}
});
getContentPane().add(mas);
mas.setBounds(270, 160, 67, 44);
division.setText("/");
division.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
divisionMouseClicked(evt);
}
});
getContentPane().add(division);
division.setBounds(350, 210, 67, 44);
multiplicar.setText("*");
multiplicar.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
multiplicarMouseClicked(evt);
}
});
getContentPane().add(multiplicar);
multiplicar.setBounds(270, 210, 67, 44);
nueve.setText("9");
nueve.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
nueveMouseClicked(evt);
}
});
nueve.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
nueveActionPerformed(evt);
}
});
getContentPane().add(nueve);
nueve.setBounds(190, 180, 67, 59);
dos.setText("2");
dos.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
dosMouseClicked(evt);
}
});
getContentPane().add(dos);
dos.setBounds(113, 324, 67, 59);
cero.setText("0");
cero.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
ceroMouseClicked(evt);
}
});
cero.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ceroActionPerformed(evt);
}
});
getContentPane().add(cero);
cero.setBounds(110, 390, 67, 59);
punto.setText(".");
punto.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
puntoMouseClicked(evt);
}
});
punto.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
puntoActionPerformed(evt);
}
});
getContentPane().add(punto);
punto.setBounds(70, 120, 75, 52);
pack();
}// </editor-fold>//GEN-END:initComponents
private void CEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CEActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_CEActionPerformed
private void sieteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sieteActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_sieteActionPerformed
private void unoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_unoActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_unoActionPerformed
private void unoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_unoMouseClicked
pilam.push("1");
this.mostrarPila(pilam);
}//GEN-LAST:event_unoMouseClicked
private void tresMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tresMouseClicked
pilam.push("3");
this.mostrarPila(pilam);
}//GEN-LAST:event_tresMouseClicked
private void cuatroMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cuatroMouseClicked
pilam.push("4");
this.mostrarPila(pilam);
}//GEN-LAST:event_cuatroMouseClicked
private void cincoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cincoMouseClicked
pilam.push("5");
this.mostrarPila(pilam);
}//GEN-LAST:event_cincoMouseClicked
private void seisMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_seisMouseClicked
pilam.push("6");
this.mostrarPila(pilam);
}//GEN-LAST:event_seisMouseClicked
private void sieteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sieteMouseClicked
pilam.push("7");
this.mostrarPila(pilam);
}//GEN-LAST:event_sieteMouseClicked
private void ochoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ochoMouseClicked
pilam.push("8");
this.mostrarPila(pilam);
}//GEN-LAST:event_ochoMouseClicked
private void nueveMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_nueveMouseClicked
pilam.push("9");
this.mostrarPila(pilam);
}//GEN-LAST:event_nueveMouseClicked
private void nueveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nueveActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_nueveActionPerformed
private void dosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dosMouseClicked
pilam.push("2");
this.mostrarPila(pilam);
}//GEN-LAST:event_dosMouseClicked
private void masMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_masMouseClicked
pilam.push("+");
this.mostrarPila(pilam);
}//GEN-LAST:event_masMouseClicked
private void menosMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_menosMouseClicked
pilam.push("-");
this.mostrarPila(pilam);
}//GEN-LAST:event_menosMouseClicked
private void multiplicarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_multiplicarMouseClicked
pilam.push("*");
this.mostrarPila(pilam);
}//GEN-LAST:event_multiplicarMouseClicked
private void divisionMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_divisionMouseClicked
pilam.push("/");
this.mostrarPila(pilam);
}//GEN-LAST:event_divisionMouseClicked
private void igualMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_igualMouseClicked
Stack<String> reverse = new Stack<String>(); // pilaigual contiene los elementos tal y como los introdujeses en una pila
while(!this.pilaigual.isEmpty()){
reverse.push(this.pilaigual.pop());
}
ArrayList<String> lista = new ArrayList<>();
while(!reverse.isEmpty()){
lista.add(reverse.pop());
}
Double r= this.CalcularLista(lista);
System.out.println(r.toString());
Stack<String> pr = new Stack<String>();
pr.push(r.toString());
this.pilaigual = pr;
}//GEN-LAST:event_igualMouseClicked
private void ceroMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ceroMouseClicked
pilam.push("0");
this.mostrarPila(pilam);
}//GEN-LAST:event_ceroMouseClicked
private void ceroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ceroActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_ceroActionPerformed
private void CEMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CEMouseClicked
Area.setText(null);
this.pilaigual.removeAllElements();
Stack<String> clonb = (Stack<String>) this.pilam.clone();
while(!clonb.isEmpty()){
System.out.println(clonb.pop());
System.out.println("clonb");
}
this.pilam.removeAllElements();
}//GEN-LAST:event_CEMouseClicked
private void puntoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_puntoMouseClicked
pilam.push(".");
this.mostrarPila(pilam);
}//GEN-LAST:event_puntoMouseClicked
private void puntoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_puntoActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_puntoActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Calcu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Calcu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Calcu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Calcu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Calcu().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JTextArea Area;
private javax.swing.JButton CE;
private javax.swing.JButton cero;
private javax.swing.JButton cinco;
private javax.swing.JButton cuatro;
private javax.swing.JButton division;
private javax.swing.JButton dos;
private javax.swing.JButton igual;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JButton mas;
private javax.swing.JButton menos;
private javax.swing.JButton multiplicar;
private javax.swing.JButton nueve;
private javax.swing.JButton ocho;
private javax.swing.JButton punto;
private javax.swing.JButton seis;
private javax.swing.JButton siete;
private javax.swing.JButton tres;
private javax.swing.JButton uno;
// End of variables declaration//GEN-END:variables
}