From 321769c0b5eba5e2b19a9eb2c812bebb602fe595 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E9=B8=BF=E6=B6=9B?= <1123087130@qq.com> Date: Thu, 6 Aug 2020 20:10:56 +0800 Subject: [PATCH] Calculator.java --- Calculator.java | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 Calculator.java diff --git a/Calculator.java b/Calculator.java new file mode 100644 index 0000000..edfa983 --- /dev/null +++ b/Calculator.java @@ -0,0 +1,78 @@ +package cal; +import java.util.Scanner; +import java.math.*; + +public class Calculator { + public static void main(String[] args) + { + Scanner sc=new Scanner(System.in); + double result=1.0; + System.out.println("对数 0;sin 6;cos 7;tan 8;e的指数 1; "); + String Command=sc.nextLine(); + System.out.println("输入对应运算数值 两数以空格间隔"); + String parameters=sc.nextLine(); + if(Command.equals("0")) + result=Calculator.Log(parameters); + else if(Command.equals("1")) + { + //double x=Double.parseDouble(parameters); + ExFunction e = new ExFunction(Double.parseDouble(parameters)); + result=e.index(); + } +// else if(Command.equals("2")) +// result=Calculator.jjcc(parameters); + else if(Command.equals("6")) + result=Calculator.sin(parameters); + else if(Command.equals("7")) + result=Calculator.cos(parameters); + else if(Command.equals("8")) + result=Calculator.tan(parameters); + System.out.println(result); + + } + public static double Log(String s) + { + String[] parameters=new String[2]; + parameters=s.split(" "); + float a=Float.parseFloat(parameters[0]); + float b=Float.parseFloat(parameters[1]); + double result=Math.log(b)/Math.log(a); + return result; + } + public static double sin(String s) + { + double x=Double.parseDouble(s); + return Math.sin(Math.toRadians(x));//Math.toRadians()将角度转换为弧度 + } + public static double cos(String s) + { + double x=Double.parseDouble(s); + return Math.cos(Math.toRadians(x)); + } + public static double tan(String s) + { + double x=Double.parseDouble(s); + return Math.tan(Math.toRadians(x)); + } + +// public static double jjcc() +// { +// +// } +// public static double ex() +// { +// +// } +} + class ExFunction { + public double x; + + public ExFunction(double x) { + this.x = x; + } + + public double index() { + return Math.exp(x); + } + +} \ No newline at end of file