limengyan 4 years ago
commit ce97d76dda

@ -0,0 +1,24 @@
/**
* ex
* e^x=1+x/1!+x^2/2!+x^3/3!++x^n/n! 
**/
public class CalculateEx {
public static double calcalate(int x){
int n=1; //n用来算阶乘
long f=1; //总阶乘
double sum=1.0,temp=1.0; //sum为最终值temp算分子的次方
do{
f= f*n ;//阶乘
n++ ;
temp *= x; //计算次方
sum += temp / f;
} while(1.0/f>1e-6);
//System.out.println("e^"+x+"="+sum);
return sum;
}
}

@ -0,0 +1,31 @@
package computer;
import java.util.Scanner;
public class compute {
public static void main(String[] args) {
// TODO Auto-generated method stub
Double op1;
Double op2;
String op;
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个操作数(回车结束)");
op1=sc.nextDouble();
System.out.println("请输入运算符(回车结束)");
op=sc.next();
System.out.println("请输入第二个操作数(回车结束)");
op2=sc.nextDouble();
System.out.print("结果是:");
switch(op) {
case "+":System.out.println(coputer1(op1,op2));break;
case "-":System.out.println(coputer1(op1,op2));break;
case "*":System.out.println(coputer1(op1,op2));break;
case "/":System.out.println(coputer1(op1,op2));break;
case "^":System.out.println(function(op1,op2));break;
case "log":System.out.println(logax(op1,op2));break;
case "e^" :System.out.println(calcalate(op2));break;
}
}
Loading…
Cancel
Save