Advertisement
Juyle

Balanceo de ecuaciones

Feb 20th, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. public static void main(String[] args) {
  2. Stack<String> pila = new Stack<String>();
  3. int c1=0;
  4. int c2=0;
  5. int c3=0;
  6. String xp = "{(a+b)+(c-d)*6/8} + {[r*3]+[d*r]}";
  7. System.out.println(xp);
  8. for (int x = 0; x < xp.length(); x++) {
  9. String a1 = String.valueOf(xp.charAt(x));
  10. pila.push(a1);
  11. if (!(a1 == "}") || !(a1 == "]") || !(a1 == ")") || !(a1 == "{") || !(a1 == "[") || !(a1 == "(")) {
  12. pila.pop();
  13. System.out.println(pila);
  14. } else {
  15. for (int x1 = 0; x1 < pila.size(); x1++) {
  16. if (pila.get(x1) == "{") {
  17. c1++;
  18. if (!(pila.search("}")==-1)) {
  19. c1=c1-1;
  20. }
  21. } else if(pila.get(x1) == "[") {
  22. c2++;
  23. if (!(pila.search("]")==-1)) {
  24. c2=c2-1;
  25. }
  26. } else if(pila.get(x1) == "(") {
  27. c3++;
  28. if (!(pila.search(")")==-1)) {
  29. c3=c3-1;
  30. }
  31. }
  32. }
  33. }
  34. }
  35. if (c1==0 || c2==0 || c3==0) {
  36. System.out.println("La ecuacion esta balanciada");
  37. } else {
  38. System.out.println("La no ecuacion esta balanciada");
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement