首页 > 试题广场 >

简单计算器

[编程题]简单计算器
  • 热度指数:44475 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
KiKi实现一个简单计算器,实现两个数的“加减乘除”运算,用户从键盘输入算式“操作数1运算符操作数2”,计算并输出表达式的值,如果输入的运算符号不包括在(+、-、*、/)范围内,输出“Invalid operation!”。当运算符为除法运算,即“/”时。如果操作数2等于0.0,则输出“Wrong!Division by zero!”

数据范围:字符串长度满足  ,保证运算符是一个char类型字符。

输入描述:
输入一行字符串,操作数1+运算符+操作数2 (其中合法的运算符包括:+、-、*、/)。


输出描述:
输出为一行。

如果操作数和运算符号均合法,则输出一个表达式,操作数1运算符操作数2=运算结果,各数小数点后均保留4位,数和符号之间没有空格。

如果输入的运算符号不包括在(+、-、*、/)范围内,输出“Invalid operation!”。当运算符为除法运算,即“/”时。

如果操作数2等于0.0,则输出“Wrong!Division by zero!”。
示例1

输入

1.0+3.0

输出

1.0000+3.0000=4.0000
示例2

输入

1.0;4.0

输出

Invalid operation!
示例3

输入

44.0/0.0

输出

Wrong!Division by zero!
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String inp = in.next();
        Double a;
        Double b;
        if (inp.contains("+")) {
            a = Double.parseDouble(inp.substring(0, inp.indexOf("+")));
            b = Double.parseDouble(inp.substring(inp.indexOf("+")+1));
            System.out.printf("%.4f+%.4f=%.4f", a, b, a + b);
        } else if (inp.contains("-")) {
            a = Double.parseDouble(inp.substring(0, inp.indexOf("-")));
            b = Double.parseDouble(inp.substring(inp.indexOf("-")+1));
            System.out.printf("%.4f-%.4f=%.4f", a, b, a - b);
        } else if (inp.contains("*")) {
            a = Double.parseDouble(inp.substring(0, inp.indexOf("*")));
            b = Double.parseDouble(inp.substring(inp.indexOf("*")+1));
            System.out.printf("%.4f*%.4f=%.4f", a, b, a * b);
        } else if (inp.contains("/")) {
            a = Double.parseDouble(inp.substring(0, inp.indexOf("/")));
            b = Double.parseDouble(inp.substring(inp.indexOf("/")+1));
            if (b == 0.0) {
                System.out.printf("Wrong!Division by zero!");
            } else {
                System.out.printf("%.4f/%.4f=%.4f", a, b, a / b);
            }
        } else {
            System.out.printf("Invalid operation!");
        }
    }
}


编辑于 2024-02-29 23:22:12 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        int f = 0;
        for (int i = 0; i < s.length(); i++) {
            char cha = s.charAt(i);
            if (cha == '+' || cha == '-' || cha == '*' || cha == '/') {
                f = i;
                break;
            }
        }
        if (f == 0) {
            System.out.println("Invalid operation!");
        } else {
            Double a = Double.valueOf(s.substring(0, f));
            String b = s.substring(f, f + 1);
            Double c = Double.valueOf(s.substring(f + 1));
            Double d = 0.0;
            if (b.equals("+")) {
                d = a + c;
                System.out.printf("%.4f+%.4f=%.4f", a, c, d);
            } else if (b.equals("-")) {
                d = a - c;
                System.out.printf("%.4f-%.4f=%.4f", a, c, d);
            } else if (b.equals("*")) {
                d = a * c;
                System.out.printf("%.4f*%.4f=%.4f", a, c, d);
            } else if (b.equals("/")) {
                if (c == 0) {
                    System.out.printf("Wrong!Division by zero!");
                } else {
                    d = a / c;
                    System.out.printf("%.4f/%.4f=%.4f", a, c, d);
                }
            }

        }

    }
}

发表于 2022-10-29 12:05:48 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            String str = scanner.nextLine();
            //将运算符号与数字分隔开
            String[] toStr = str.split("\\+|\\*|/|-|}| ");
            //将字符串类型转成double型
            Double[] num = new Double[toStr.length];
            for(int i = 0;i < toStr.length;i++){
                num[i] = new Double(toStr[i]);
            }
            //运算
            if(str.contains("+")){
                System.out.printf(String.format("%.4f",num[0]));
                System.out.printf("+");
                System.out.printf(String.format("%.4f",num[1]));
                System.out.printf("=");
                System.out.printf(String.format("%.4f",(num[0] + num[1])));
            }else if(str.contains("-")){
                System.out.printf(String.format("%.4f",num[0]));
                System.out.printf("-");
                System.out.printf(String.format("%.4f",num[1]));
                System.out.printf("=");
                System.out.printf(String.format("%.4f",(num[0] - num[1])));
            }else if(str.contains("*")){
                System.out.printf(String.format("%.4f",num[0]));
                System.out.printf("*");
                System.out.printf(String.format("%.4f",num[1]));
                System.out.printf("=");
                System.out.printf(String.format("%.4f",(num[0] * num[1])));
            }else if(str.contains("/")){
                 if(num[1] == 0.0){
                     System.out.printf("Wrong!Division by zero!");
                 }else{
                    System.out.printf(String.format("%.4f",num[0]));
                    System.out.printf("/");
                    System.out.printf(String.format("%.4f",num[1]));
                    System.out.printf("=");
                    System.out.printf(String.format("%.4f",(num[0] / num[1])));  
                 }
                
            }else{
                System.out.printf("Invalid operation!");
            }
      }
    }
}

发表于 2022-06-27 17:21:58 回复(0)
import java.util.*;
public class Main{
    public static void main(String args[]){
        Scanner input=new Scanner(System.in);
        String s=input.nextLine();
        String arr[]=s.split("\\+|\\*|/|-|}| ");//正则表达式,以+-*/分割传入参数
        Double [] math=new Double[arr.length];//字符串类型数组转double
        for(int i=0;i<arr.length;i++){math[i]=new Double(arr[i]);}
         if(s.contains("+")) {//判断传入值如果含+,则进行加法,并保留4位小数
                System.out.printf("%.4f",math[0]);
                System.out.printf("+");
                System.out.printf("%.4f",math[1]);
                System.out.printf("=");
                System.out.printf("%.4f",math[0]+math[1]);
             }
         else if(s.contains("-")) {
                System.out.printf("%.4f",math[0]);
                System.out.printf("-");
                System.out.printf("%.4f",math[1]);
                System.out.printf("=");
                System.out.printf("%.4f",math[0]-math[1]);
             }
         else if(s.contains("*")) {
                System.out.printf("%.4f",math[0]);
                System.out.printf("*");
                System.out.printf("%.4f",math[1]);
                System.out.printf("=");
                System.out.printf("%.4f",math[0]*math[1]);
             }
        else if(s.contains("/")) {
            if(math[1]==0){
                 System.out.print("Wrong!Division by zero!");
            }
            else{
                System.out.printf("%.4f",math[0]);
                System.out.printf("/");
                System.out.printf("%.4f",math[1]);
                System.out.printf("=");
                System.out.printf("%.4f",math[0]/math[1]);
            }     
             }
        else{
            System.out.print("Invalid operation!");
        }
        input.close();
    }
}

发表于 2022-05-05 23:15:01 回复(0)
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String exp = scanner.nextLine();
        String op = "+-*/";
        int i;
        char ch = '0';
        for (i = 0; i < exp.length(); i++) {
            ch = exp.charAt(i);
            if (ch != '.' && (ch < '0' || ch > '9')) {
                break;
            }
        }
        if (op.contains(ch + "")) {
            double num1 = Double.parseDouble(exp.substring(0, i));
            double num2 = Double.parseDouble(exp.substring(i + 1));
            double result;
            if (ch == '/' && num2 == 0.0) {
                System.out.println("Wrong!Division by zero!");
            } else {
                switch (ch) {
                    case '+':
                        result = num1 + num2;
                        break;
                    case '-':
                        result = num1 - num2;
                        break;
                    case '*':
                        result = num1 * num2;
                        break;
                    case '/':
                        result = num1 / num2;
                        break;
                    default:
                        result=0;
                }
                System.out.printf("%.4f%c%.4f=%.4f", num1, ch, num2,result);
            }
        } else {
            System.out.println("Invalid operation!");
        }

    }
}


发表于 2021-10-22 15:35:41 回复(0)
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNextLine()) {
            //先用contains判判断算术操作符是否存在
            //若不存在 打印错误信息并检测下一行
            //若存在 用split方法分开 
            //用parseDouble把字符串转为数值 利用相应的操作符进行运算  并打印结果
            String str = scan.nextLine();
            if (str.contains("+")) {
                String[] strs = str.split("\\+");
                double a = Double.parseDouble(strs[0]);
                double b = Double.parseDouble(strs[1]);
                System.out.printf("%.4f+%.4f=%.4f", a, b, a + b);
            } else if (str.contains("-")) {
                String[] strs = str.split("-");
                double a = Double.parseDouble(strs[0]);
                double b = Double.parseDouble(strs[1]);
                System.out.printf("%.4f-%.4f=%.4f", a, b, a - b);
            } else if  (str.contains("*")) {
                String[] strs = str.split("\\*");
                double a = Double.parseDouble(strs[0]);
                double b = Double.parseDouble(strs[1]);
                System.out.printf("%.4f*%.4f=%.4f", a, b, a * b);
            } else if (str.contains("/")) {
                String[] strs = str.split("/");
                double a = Double.parseDouble(strs[0]);
                double b = Double.parseDouble(strs[1]);
                if (b == 0) {
                         System.out.print("Wrong!Division by zero!");
                     } else {
                         System.out.printf("%.4f/%.4f=%.4f", a, b, a / b);
                     }
            } else {
                System.out.print("Invalid operation!");
            }
            System.out.println();
        }
    }
}

发表于 2021-10-18 16:26:44 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String operation = sc.nextLine();
            if(operation.contains("+")){
                Double op1 = Double.valueOf(operation.split("\\+")[0]);
                Double op2 = Double.valueOf(operation.split("\\+")[1]);
                System.out.printf("%.4f+%.4f=%.4f\n", op1, op2, (op1+op2));
            }else if(operation.contains("-")){
                Double op1 = Double.valueOf(operation.split("\\-")[0]);
                Double op2 = Double.valueOf(operation.split("\\-")[1]);
                System.out.printf("%.4f-%.4f=%.4f\n", op1, op2, (op1-op2));            }else if(operation.contains("*")){
                Double op1 = Double.valueOf(operation.split("\\*")[0]);
                Double op2 = Double.valueOf(operation.split("\\*")[1]);
                System.out.printf("%.4f*%.4f=%.4f\n", op1, op2, (op1*op2));            }else if(operation.contains("/")){
                Double op1 = Double.valueOf(operation.split("\\/")[0]);
                Double op2 = Double.valueOf(operation.split("\\/")[1]);
                if (op2 == 0.0) {
                    System.out.println("Wrong!Division by zero!");
                } else {
                    System.out.printf("%.4f/%.4f=%.4f\n", op1, op2, (op1/op2));                }
            }else{
                System.out.println("Invalid operation!");
            }
        }
    }
}
发表于 2021-10-13 00:23:26 回复(0)
import java.util.Date;
import java.util.Iterator;
import java.util.Scanner;
import java.util.ServiceConfigurationError;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String s = sc.nextLine();
                if (s.contains("+")) {
                    String[] s1 = s.split("\\+");
                    double a = Double.parseDouble(s1[0]);
                    double b = Double.parseDouble(s1[1]);
                    System.out.printf("%.4f+%.4f=%.4f\n", a, b, (a + b));
                } else if (s.contains("-")) {
                    String[] s1 = s.split("-");
                    double a = Double.parseDouble(s1[0]);
                    double b = Double.parseDouble(s1[1]);
                    System.out.printf("%.4f-%.4f=%.4f\n", a, b, (a - b));
                } else if (s.contains("*")) {
                    String[] s1 = s.split("\\*");
                    double a = Double.parseDouble(s1[0]);
                    double b = Double.parseDouble(s1[1]);
                    System.out.printf("%.4f*%.4f=%.4f\n", a, b, (a * b));
                } else if (s.contains("/")) {
                    String[] s1 = s.split("/");
                    double a = Double.parseDouble(s1[0]);
                    double b = Double.parseDouble(s1[1]);
                        if(b==0.0){
                            System.out.println("Wrong!Division by zero!");
                        }
                        else {System.out.printf("%.4f/%.4f=%.4f\n", a, b, (a / b));}
                }
                else {
                    System.out.println("Invalid operation!");
                }
            }

        }
    }

发表于 2021-10-02 19:21:18 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String string;
        String[] number;
        String operator;
        double result;
        boolean isNotOperation;
        while (scanner.hasNext()) {
            //考虑到会出现空格的情况,必须用nextLine()才能接收完整的数据
            //next()在遇到空格时就会停止接收数据
            string = scanner.nextLine();
            //split()中的[]为正则表达式
            number = string.split("[+\\-*/]");
            //先判断是否为正确的运算符
            //若不是,则number数组中只有源字符串(不会被分开),下边取运算符时会出现数组越界异常
            if (number.length < 2) {
                System.out.println("Invalid operation!");
            } else {
                operator = string.substring(number[0].length(), number[0].length() + 1);
                result = 0;
                isNotOperation = false;
                if (Double.parseDouble(number[1]) == 0) {
                    System.out.println("Wrong!Division by zero!");
                } else {
                    switch (operator) {
                        case "+":
                            result = Double.parseDouble(number[0]) + Double.parseDouble(number[1]);
                            break;
                        case "-":
                            result = Double.parseDouble(number[0]) - Double.parseDouble(number[1]);
                            break;
                        case "*":
                            result = Double.parseDouble(number[0]) * Double.parseDouble(number[1]);
                            break;
                        case "/":
                            result = Double.parseDouble(number[0]) / Double.parseDouble(number[1]);
                            break;
                    }
                        System.out.printf("%.4f%s%.4f=%.4f", Double.parseDouble(number[0]), operator, Double.parseDouble(number[1]), result);
                }
            }
        }
    }
}
编辑于 2021-03-17 18:11:40 回复(0)
好多
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String out;
        double two;
        double sum;
        int index;
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String s=input.nextLine();
            index=s.indexOf("+");
            if(index!=-1){
                two=Double.parseDouble(s.substring(index+1));
                if(two==0){
                    System.out.println("Wrong!Division by zero!");
                    continue;
                }
                sum=Double.parseDouble(s.substring(0,index))+two;
                out=String.format("%.4f",Double.parseDouble(s.substring(0,index)))+"+"+String.format("%.4f",Double.parseDouble(s.substring(index+1)))+"="+String.format("%.4f",sum);
                System.out.println(out);
                continue;
            }
            index=s.indexOf("-");
            if(index!=-1){
                two=Double.parseDouble(s.substring(index+1));
                if(two==0){
                    System.out.println("Wrong!Division by zero!");
                    continue;
                }
                sum=Double.parseDouble(s.substring(0,index))-two;
                out=String.format("%.4f",Double.parseDouble(s.substring(0,index)))+"-"+String.format("%.4f",Double.parseDouble(s.substring(index+1)))+"="+String.format("%.4f",sum);
                System.out.println(out);
                continue;
            }
            index=s.indexOf("*");
            if(index!=-1){
                two=Double.parseDouble(s.substring(index+1));
                if(two==0){
                    System.out.println("Wrong!Division by zero!");
                    continue;
                }
                sum=Double.parseDouble(s.substring(0,index))*two;
                out=String.format("%.4f",Double.parseDouble(s.substring(0,index)))+"*"+String.format("%.4f",Double.parseDouble(s.substring(index+1)))+"="+String.format("%.4f",sum);
                System.out.println(out);
                continue;
            }
            index=s.indexOf("/");
            if(index!=-1){
                two=Double.parseDouble(s.substring(index+1));
                if(two==0){
                    System.out.println("Wrong!Division by zero!");
                    continue;
                }
                sum=Double.parseDouble(s.substring(0,index))/two;
                out=String.format("%.4f",Double.parseDouble(s.substring(0,index)))+"/"+String.format("%.4f",Double.parseDouble(s.substring(index+1)))+"="+String.format("%.4f",sum);
                System.out.println(out);
                continue;
            }
            System.out.println("Invalid operation!");
        }
    }
}


发表于 2020-10-30 11:50:47 回复(0)