Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.06 KB | None | 0 0
  1. import java.util.*;
  2. public class Calculadora
  3. {
  4.     public static void main(String[] args) {
  5.         Scanner s = new Scanner(System.in).useLocale(Locale.US);
  6.         System.out.print(">> ");
  7.         String formula = s.nextLine();
  8.         formula.replace(" ","");
  9.         formula.replace("(","");
  10.         formula.replace(")","");
  11.         formula.replace("++","+");
  12.         formula.replace("-+","-");
  13.         formula.replace("+-","-");
  14.         formula.replace("--","-");
  15.         boolean op = false, neg1 = false, neg2 = false, isNum = false;
  16.         String num1 = "", num2 = "", operacion = "";
  17.         if(formula.startsWith("-")) {
  18.             neg1 = true;
  19.             formula = formula.substring(1);
  20.         }
  21.         else if(formula.startsWith("+")) {
  22.             formula = formula.substring(1);
  23.         }
  24.         for(int i = 0; i < formula.length() && op == false; i++) {
  25.             char c = formula.charAt(i);
  26.             if(c == '/' || c == '*') {
  27.                 op = true;
  28.                 operacion = c + "";
  29.                 num1 = formula.substring(0, i - 1);
  30.                 num2 = formula.substring(i + 1);
  31.             }
  32.             else {
  33.                 if(c - '0' >= 0 && c - '0' <= 9) {
  34.                     if(isNum == false) {
  35.                         isNum = true;
  36.                         num1 += c + "";
  37.                     }
  38.                     else {
  39.                         op = true;
  40.                         operacion = c + "";
  41.                         num2 = formula.substring(i + 1);
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.         if(num2.startsWith("-")) {
  47.             neg2 = true;
  48.             num2 = num2.substring(1);
  49.         }
  50.         else if (num2.startsWith("+")){
  51.             num2 = num2.substring(1);
  52.         }
  53.         boolean decimal1 = false, decimal2 = false;
  54.         String num1Ent = "", num1Dec = "", num2Ent = "", num2Dec = "";
  55.         for(int i = 0; i < num1.length(); i++) {
  56.             char c = num1.charAt(i);
  57.             if(c == '.') {
  58.                 decimal1 = true;
  59.                 num1Ent = num1.substring(0, i-1);
  60.                 num1Dec = num1.substring(i+1);
  61.             }
  62.         }
  63.         for(int i = 0; i < num2.length(); i++) {
  64.             char c = num1.charAt(i);
  65.             if(c == '.') {
  66.                 decimal2 = true;
  67.                 num2Ent = num2.substring(0, i-1);
  68.                 num2Dec = num2.substring(i+1);
  69.             }
  70.         }
  71.         double numero1 = 0.0, numero2 = 0.0;
  72.         if(decimal1) {
  73.             double ent1 = convertirNum(num1Ent, "entero");
  74.             double dec1 = convertirNum(num1Dec, "decimal");
  75.             numero1 = ent1 + dec1;
  76.         }
  77.         else if(decimal2) {
  78.             double ent2 = convertirNum(num2Ent, "entero");
  79.             double dec2 = convertirNum(num2Dec, "decimal");
  80.             numero2 = ent2 + dec2;
  81.         }
  82.         else {
  83.             numero1 = convertirNum(num1, "entero");
  84.             numero2 = convertirNum(num1, "entero");
  85.         }
  86.         if(neg1)
  87.             numero1 -= 2 * numero1;
  88.         else if(neg2)
  89.             numero2 -= 2 * numero2;
  90.         double resultado = 0.0;
  91.         if(operacion.equals("+")) {
  92.             resultado = numero1 + numero2;
  93.         }
  94.         else if(operacion.equals("-")) {
  95.             resultado = numero1 - numero2;
  96.         }
  97.         else if(operacion.equals("/")) {
  98.             if(numero2 == 0) {
  99.                 System.out.println("Error: division por 0");
  100.                 return;
  101.             }
  102.             resultado = numero1 / numero2;
  103.         }
  104.         else {
  105.             resultado = numero1 * numero2;
  106.         }
  107.         System.out.println(resultado);
  108.     }
  109.     public static double convertirNum(String num, String tipo) {
  110.         double res = 0.0;
  111.         for(int i = 0; i < num.length(); i++) {
  112.             char c = num.charAt(i);
  113.             res += (c - '0') * Math.pow(10, num.length() - i);
  114.         }
  115.         if(tipo.equals("entero")) {
  116.             return res;
  117.         }
  118.         else {
  119.             return res / Math.pow(10, num.length());
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement