From 09ebe17b6195162e748dc5c7457f11e3e800b95e Mon Sep 17 00:00:00 2001 From: Lrixin <2565381053@qq.com> Date: Thu, 6 Aug 2020 21:22:27 +0800 Subject: [PATCH] main.java --- main.java | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 main.java diff --git a/main.java b/main.java new file mode 100644 index 0000000..51e055b --- /dev/null +++ b/main.java @@ -0,0 +1,108 @@ +package Hello; +import java.io.*; +import java.util.*; +import java.math.*; +public class Hello{ + + public static double mypow(double e,double x) { + double ans=Math.pow(e, x); + return ans; + } + + public static double mylog(double a,double x) { + double ans=Math.log(x)/Math.log(a); + return ans; + } + + public static double myadd(double a,double b) { + return a+b; + } + + public static double mysub(double a,double b) { + return a-b; + } + + public static double mymultiply(double a,double b) { + return a*b; + } + + public static double mydivide(double a,double b) { + return a/b; + } + + public static double mysin(double x) { + return Math.sin(x); + } + + public static double mycos(double x) { + return Math.cos(x); + } + + public static double mytan(double x) { + return Math.tan(x); + } + + public static void main(String args[]) { + Scanner cin=new Scanner(System.in); + System.out.printf("功能表:\n说明:输入add a b执行a+b\n" + + "pow e x\nlog a x\nadd a b\nsub a b\nmultiply a b\n" + + "divide a b\nsin x\ncos x\ntan x\nquit:退出\n"); + while (cin.hasNext()) { + String op=cin.next(); + double a,b,ans=0; + switch(op) { + case "pow": + a=cin.nextDouble(); + b=cin.nextDouble(); + ans=mypow(a,b); + break; + case "log": + a=cin.nextDouble(); + b=cin.nextDouble(); + ans=mylog(a,b); + break; + case "add": + a=cin.nextDouble(); + b=cin.nextDouble(); + ans=myadd(a,b); + break; + case "sub": + a=cin.nextDouble(); + b=cin.nextDouble(); + ans=mysub(a,b); + break; + case "multiply": + a=cin.nextDouble(); + b=cin.nextDouble(); + ans=mymultiply(a,b); + break; + case "divide": + a=cin.nextDouble(); + b=cin.nextDouble(); + while(b==0) { + System.out.printf("除数不能为0,请重新输入除数!\n"); + b=cin.nextDouble(); + } + ans=mydivide(a,b); + break; + case "sin": + a=cin.nextDouble(); + ans=mysin(a); + break; + case "cos": + a=cin.nextDouble(); + ans=mycos(a); + break; + case "tan": + a=cin.nextDouble(); + ans=mytan(a); + break; + default: + System.out.println("已退出计算器\n"); + return; + } + System.out.printf("%f\n",ans); + } + cin.close(); + } +}