怎样用java 实现计算器的 算法

2024-11-12 03:42:28
推荐回答(3个)
回答1:

package com.t.core.util;

/**
* 数学计算工具类
*
* @author Administrator
*
*/
publicclassMathUtil {
// ----------------三角运算----------------------
/**
* 弧度转化为角度
*
* @param degress
* @return
*/
publicstaticdouble toDegress(double degress) {

return Math.toDegrees(degress);
};

/**
* 角度转化为弧度
*
* @param radians
* @return
*/
publicstaticdouble toRadians(double radians) {

return Math.toRadians(radians);
};

/**
* 反余弦
*
* @param acos
* @return角度范围在0.0到pi之间
*/
publicstaticdouble aCos(double acos) {

return Math.acos(acos);
};

/**
* 反正弦
*
* @param asin
* @return角度范围在-pi/2到pi/2之间
*/
publicstaticdouble aSin(double asin) {

return Math.asin(asin);
};

/**
* 反正切
*
* @param atan
* @return角度范围在-pi/2到pi/2之间
*/
publicstaticdouble aTan(double atan) {

return Math.atan(atan);
};

/**
* 三角余弦
*
* @param cos
* @return
*/
publicstaticdouble cos(double cos) {

return Math.cos(cos);
};

/**
* 双曲余弦
*
* @param cosh
* @return
*/
publicstaticdouble cosH(double cosh) {

return Math.cosh(cosh);
};

/**
* 正弦
*
* @param sin
* @return
*/
publicstaticdouble sin(double sin) {

return Math.sin(sin);
};

/**
* 双曲正弦
*
* @param sinh
* @return
*/
publicstaticdouble sinH(double sinh) {

return Math.sinh(sinh);
};

/**
* 三角正切
*
* @param tan
* @return
*/
publicstaticdouble tan(double tan) {

return Math.tan(tan);
};

/**
* 双曲正切
*
* @param tanh
* @return
*/
publicstaticdouble tanH(double tanh) {

return Math.tanh(tanh);
};

/**
* 将矩形坐标(x,y)转换成极坐标(r,thet)
*
* @param x
* @param y
* @return
*/
publicstaticdouble aTan2(double x, double y) {

return Math.atan2(x,y);
};

// ----------------取整运算------------------
/**
* 取整,返回小于目标数的最大整数(地板函数)
*
* @param number
* @return
*/
publicstaticdouble floor(double number) {

return Math.floor(number);
};

/**
* 取整,返回大于目标数的最小整数(天花板函数)
*
* @param number
* @return
*/
publicstaticdouble ceil(double number) {

return Math.ceil(number);
};

/**
* 四舍五入取整
*
* @param number
* @return
*/
publicstaticdouble round(double number) {

return Math.round(number);
};

publicstaticfloat round(float number) {

return Math.round(number);
};

/**
* 平方根
*
* @param number
* @return
*/
publicstaticdouble sqrt(double number) {

return Math.sqrt(number);
};

/**
* 立方根
*
* @param number
* @return
*/
publicstaticdouble cbrt(double number) {

return Math.cbrt(number);
};

/**
* 欧拉数e的n次幂
*
* @param number
* @return
*/
publicstaticdouble exp(double number) {

return Math.exp(number);
};

/**
* sqrt(x2+y2),没有中间溢出或下溢
*
* @param number
* @param number2
* @return
*/
publicstaticdouble hypot(double number, double number2) {

return Math.hypot(number,number2);
};

/**
* 按照IEEE754标准规定,对两个参数进行余数运算
*
* @param number
* @param number2
* @return
*/
publicstaticdouble IEEEremainder(double number, double number2) {

return Math.IEEEremainder(number,number2);
};

/**
* 乘方
*
* @param number
* @param number2
* @return
*/
publicstaticdouble pow(double number, double number2) {

return Math.pow(number,number2);
};

/**
* 自然对数
*
* @param number
* @return
*/
publicstaticdouble log(double number) {

return Math.log(number);
};

/**
* 底数为10的对数
*
* @param number
* @return
*/
publicstaticdouble log10(double number) {

return Math.log10(number);
};

/**
* 参数与1之和的自然对数
*
* @param number
* @return
*/
publicstaticdouble log1p(double number) {

return Math.log1p(number);
};

// ------------符号相关运算------------------
/**
* 绝对值
*
* @param number
* @return
*/
publicstaticdouble abs(double number) {

return Math.abs(number);
};

publicstaticint abs(int number) {

return Math.abs(number);
};

publicstaticfloat abs(float number) {

return Math.abs(number);
};

publicstaticlong abs(long number) {

return Math.abs(number);
};

/**
* 符号赋值
*
* @param magnitude
* @param sign
* @return带有第二个浮点数符号的第一个浮点参数
*/
publicstaticdouble copySign(double magnitude, double sign) {

return Math.copySign(magnitude,sign);
};

publicstaticfloat copySign(float magnitude, float sign) {

return Math.copySign(magnitude,sign);
};

/**
* 符号函数
*
* @param number
* @return number等于0,返回0;number大于0,返回1;number小于0,返回-1
*/
publicstaticfloat copySign(float number) {

return Math.signum(number);
};

publicstaticdouble copySign(double number) {

return Math.signum(number);
};

/**
* 找出最大值
*
* @param number
* @param number2
* @return
*/
publicstaticdouble max(double number, double number2) {

return Math.max(number,number2);
};

publicstaticint max(int number, int number2) {

return Math.max(number,number2);
};

publicstaticfloat max(float number, float number2) {

return Math.max(number,number2);
};

publicstaticlong max(long number, long number2) {

return Math.max(number,number2);
};

/**
* 计算最小值
*
* @param number
* @param number2
* @return
*/
publicstaticlong min(long number, long number2) {

return Math.min(number,number2);
};

publicstaticint min(int number, int number2) {

return Math.min(number,number2);
};

publicstaticfloat min(float number, float number2) {

return Math.min(number,number2);
};

publicstaticdouble min(double number, double number2) {

return Math.min(number,number2);
};

/**
*
* @param start
* @param direction
* @return第一个参数和第二个参数之间与第一个参数相邻的浮点数
*/
publicstaticdouble nextAfter(double start, double direction) {

return Math.nextAfter(start,direction);
};

publicstaticfloat nextAfter(float start, float direction) {

return Math.nextAfter(start,direction);
};

/**
*
* @param number
* @return比目标数略大的浮点数
*/
publicstaticfloat nextUp(float number) {

return Math.nextUp(number);
};

publicstaticdouble nextUp(double number) {

return Math.nextUp(number);
};

/**
* 随机数
*
* @return
*/
publicstaticdouble random() {

return Math.random();
};
}

回答2:

用jsp做的啊




计算器




第一个数



第二个数








计算结果



 



回答3:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Calculator
{

public static void main(String[] args)
{
int first,second;
char operator;
try {
BufferedReader firstin = new BufferedReader ( new InputStreamReader (System.in));
BufferedReader secin = new BufferedReader ( new InputStreamReader (System.in));
BufferedReader oper = new BufferedReader ( new InputStreamReader (System.in));
System.out.print("please enter the first value:");
first=Integer.parseInt(firstin.readLine());
System.out.print("please enter the second value:");
second=Integer.parseInt(secin.readLine());

System.out.print("please enter the operator:");
String temp=oper.readLine();
operator=temp.charAt(0);
new Calculator(first,second,operator);
} catch (Exception e) {
System.out.print("Unwanted input detected!");
}

}

Calculator(int first,int second,char oper)
{
switch(oper)
{
case '+': System.out.print(first+second);break;
case '-': System.out.print(first-second);break;
case '*': System.out.print(first*second);break;
case '/': System.out.print(first/second);break;
default:System.out.print("please!");
}
}
}