Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Calculadora
- {
- public static void main(String[] args) {
- Scanner s = new Scanner(System.in).useLocale(Locale.US);
- System.out.print(">> ");
- String formula = s.nextLine();
- formula.replace(" ","");
- formula.replace("(","");
- formula.replace(")","");
- formula.replace("++","+");
- formula.replace("-+","-");
- formula.replace("+-","-");
- formula.replace("--","-");
- boolean op = false, neg1 = false, neg2 = false, isNum = false;
- String num1 = "", num2 = "", operacion = "";
- if(formula.startsWith("-")) {
- neg1 = true;
- formula = formula.substring(1);
- }
- else if(formula.startsWith("+")) {
- formula = formula.substring(1);
- }
- for(int i = 0; i < formula.length() && op == false; i++) {
- char c = formula.charAt(i);
- if(c == '/' || c == '*') {
- op = true;
- operacion = c + "";
- num1 = formula.substring(0, i - 1);
- num2 = formula.substring(i + 1);
- }
- else {
- if(c - '0' >= 0 && c - '0' <= 9) {
- if(isNum == false) {
- isNum = true;
- num1 += c + "";
- }
- else {
- op = true;
- operacion = c + "";
- num2 = formula.substring(i + 1);
- }
- }
- }
- }
- if(num2.startsWith("-")) {
- neg2 = true;
- num2 = num2.substring(1);
- }
- else if (num2.startsWith("+")){
- num2 = num2.substring(1);
- }
- boolean decimal1 = false, decimal2 = false;
- String num1Ent = "", num1Dec = "", num2Ent = "", num2Dec = "";
- for(int i = 0; i < num1.length(); i++) {
- char c = num1.charAt(i);
- if(c == '.') {
- decimal1 = true;
- num1Ent = num1.substring(0, i-1);
- num1Dec = num1.substring(i+1);
- }
- }
- for(int i = 0; i < num2.length(); i++) {
- char c = num1.charAt(i);
- if(c == '.') {
- decimal2 = true;
- num2Ent = num2.substring(0, i-1);
- num2Dec = num2.substring(i+1);
- }
- }
- double numero1 = 0.0, numero2 = 0.0;
- if(decimal1) {
- double ent1 = convertirNum(num1Ent, "entero");
- double dec1 = convertirNum(num1Dec, "decimal");
- numero1 = ent1 + dec1;
- }
- else if(decimal2) {
- double ent2 = convertirNum(num2Ent, "entero");
- double dec2 = convertirNum(num2Dec, "decimal");
- numero2 = ent2 + dec2;
- }
- else {
- numero1 = convertirNum(num1, "entero");
- numero2 = convertirNum(num1, "entero");
- }
- if(neg1)
- numero1 -= 2 * numero1;
- else if(neg2)
- numero2 -= 2 * numero2;
- double resultado = 0.0;
- if(operacion.equals("+")) {
- resultado = numero1 + numero2;
- }
- else if(operacion.equals("-")) {
- resultado = numero1 - numero2;
- }
- else if(operacion.equals("/")) {
- if(numero2 == 0) {
- System.out.println("Error: division por 0");
- return;
- }
- resultado = numero1 / numero2;
- }
- else {
- resultado = numero1 * numero2;
- }
- System.out.println(resultado);
- }
- public static double convertirNum(String num, String tipo) {
- double res = 0.0;
- for(int i = 0; i < num.length(); i++) {
- char c = num.charAt(i);
- res += (c - '0') * Math.pow(10, num.length() - i);
- }
- if(tipo.equals("entero")) {
- return res;
- }
- else {
- return res / Math.pow(10, num.length());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement