package org.hq.calculator;
import java.math.BigDecimal;
import java.util.Scanner;
public class Calculator {
public static final String errorInfo = "Error";
public static final String infinityInfo = "Infinity";
public static final String naNInf = "NaN";
public static final double maxNum = 1.5E16;
// Factorial
public static String factorial(int n) {
double resDou = 1;
if (n > 170) {
return infinityInfo;
} else {
for (int i = 1; i <= n; i++) {
resDou *= i;
if (resDou > Double.MAX_VALUE)
return infinityInfo;
}
return Double.toString(resDou);
}
}
// 以10为低
public static String log(double num) {
double resDou = Math.log(num) / Math.log(10);
if( resDou > maxNum){
return infinityInfo;
}
return String.valueOf(resDou);
}
// ln指数计算
public static String ln(double num) {
double resDou = Math.log(num);
if( resDou > maxNum){
return infinityInfo;
}
return String.valueOf(resDou);
}
// sin: 1,按照弧度计算,2:按照度数计算
public static String sin(int type, double num) {
if (1 == type) {
double resDou = Math.sin(num);
if( resDou > maxNum){
return infinityInfo;
}
return String.valueOf(resDou);
} else if (2 == type) {
double radian = num*Math.PI/180;
double resDou = Math.sin(radian);
if( resDou > maxNum){
return infinityInfo;
}
return String.valueOf(resDou);
} else {
LogInfo.printError("Wrong Type in Sin:" + num);
return "";
}
}
// cos
public static String cos(int type, double num) {
if (1 == type) {
double resDou = Math.cos(num);
if( resDou > maxNum){
return infinityInfo;
}
return String.valueOf(resDou);
} else if (2 == type) {
double radian = num * Math.PI / 180;// 角度转化为弧度
double resDou = Math.cos(radian);
if( resDou > maxNum){
return infinityInfo;
}
return String.valueOf(resDou);
} else {
LogInfo.printError("Wrong Type in Sin:" + type);
return "";
}
}
// tan
public static String tan(int type, double num) {
if (1 == type) {
double resDou = Math.tan(num);
if( resDou > maxNum){
return infinityInfo;
}
return String.valueOf(resDou);
} else if (2 == type) {
double radian = num * Math.PI / 180;// 角度转化为弧度
double resDou = Math.tan(radian);
if( resDou > maxNum){
return infinityInfo;
}
return String.valueOf(resDou);
} else {
LogInfo.printError("Wrong Type in Sin:" + type);
return "";
}
}
// exp
public static String exp(double num) {
double resDou = Math.exp(num);
if( resDou > maxNum){
return infinityInfo;
}
return String.valueOf(resDou);
}
public boolean isOperator(char _operator) {
return ('+' == _operator || '-' == _operator || '*' == _operator
|| '/' == _operator || '^' == _operator || '(' == _operator || ')' == _operator);
}
// 判断一个字符串是否为数字
public boolean isNumStr(String _numberStr) {
return !("+".equals(_numberStr) || "^".equals(_numberStr)
|| "-".equals(_numberStr) || "*".equals(_numberStr)
|| "/".equals(_numberStr) || ")".equals(_numberStr)
|| "(".equals(_numberStr) || "=".equals(_numberStr));
}
// 判断一个字符串是否为运算符
public boolean isOperatorStr(String _operatorStr) {
return ("+".equals(_operatorStr) || "^".equals(_operatorStr)
|| "-".equals(_operatorStr) || "*".equals(_operatorStr) || "/"
.equals(_operatorStr));
}
public boolean isnumber(char ch) {
return (ch >= '0' && ch <= '9');
}
// 比较运算符的优先级,返回值含义:
// 1: 优先级高于 0:优先级低于 -1:错误的运算符 2:优先级相同
public int compareCaculatorOperator(String _caculatorOperator1,
String _caculatorOperator2) {
if ("^".equals(_caculatorOperator1)) {
if ("^".equals(_caculatorOperator2)) {
return 2;
} else if ("*".equals(_caculatorOperator2)
|| "/".equals(_caculatorOperator2)
|| "+".equals(_caculatorOperator2)
|| "-".equals(_caculatorOperator2)) {
return 1;
} else {
return -1;
}
} else if ("*".equals(_caculatorOperator1)
|| "/".equals(_caculatorOperator1)) {
if ("^".equals(_caculatorOperator2)) {
return 0;
} else if ("*".equals(_caculatorOperator2)
|| "/".equals(_caculatorOperator2)) {
return 2;
} else if ("+".equals(_caculatorOperator2)
|| "-".equals(_caculatorOperator2)) {
return 1;
} else {
return -1;
}
} else if ("+".equals(_caculatorOperator1)
|| "-".equals(_caculatorOperator1)) {
if ("^".equals(_caculatorOperator2)
|| "*".equals(_caculatorOperator2)
|| "/".equals(_caculatorOperator2)) {
return 0;
} else if ("+".equals(_caculatorOperator2)
|| "-".equals(_caculatorOperator2)) {
return 2;
} else {
return -1;
}
} else {
return -1;
}
}
// 将一个表达式转化为栈结构
public ExStack getInfxStackfromStr(String infx) {
// 新建一个栈存储表达式
ExStack myStack = new ExStack();
// 做一个标记(是否获得了一个数字)
// 如果为假,将‘-’看做数字的一部分,为真,将‘-’看做运算符
boolean getANum = false;
// 临时字符串
StringBuffer temStr = new StringBuffer();
char temChar = ' ';
// 如果表达式以'-('开头,自动插入0
if (infx.length() > 2 && '-' == infx.charAt(0) && '(' == infx.charAt(1)) {
myStack.push("0");
}
for (int i = 0; i < infx.length(); i++) {
temChar = infx.charAt(i);
if ((!getANum && '-' == temChar) || !isOperator(temChar)) // 是数字
{
temStr.append(temChar);
getANum = true;
} else { // 是一个操作符
if (!"".equals(temStr.toString())) {
myStack.push(temStr.toString());// 压栈
temStr.delete(0, temStr.length()); // 清空temStr
}
myStack.push("" + temChar); // 压入运算符
if (')' != temChar) {
getANum = false;
} else { // ‘)’好后面的‘-’好应被看做运算符
getANum = true;
}
}
}
if (0 != temStr.length()) {
myStack.push(temStr.toString());
}
return myStack;
}
public ExStack infixToPosfix(ExStack infixStack) {
String[] exps = infixStack.getDataArray();
ExStack myPosfixStack = new ExStack();
ExStack temStack = new ExStack();
boolean getNumber = false;
boolean getOperator = false;
for (int i = 0; i < infixStack.size(); i++) // 注意遍历区间控制!!
{
if (isNumStr(exps[i])) // 是数字,直接放入posfix序列中
{
getOperator = false;
if (getNumber) // 如果之前处理的也是数字,在它后面插入一个*号
{
myPosfixStack.push(exps[i]);
myPosfixStack.push("*");
} else {
myPosfixStack.push(exps[i]);
getNumber = true;
}
} else if ("(".equals(exps[i])) // 遇到开括号,压栈
{
temStack.push(exps[i]);
} else if (")".equals(exps[i])) // 遇到闭括号时
{
if (temStack.isEmpty()) {
LogInfo.printError("brackets not match!");
return null;
} else {
while (!"(".equals(temStack.top())) {
myPosfixStack.push(temStack.top());
temStack.pop();
if (temStack.isEmpty()) {
LogInfo.printError("brackets not match!");
return null;
}
}
if ("(".equals(temStack.top())) {
temStack.pop(); // 弹出开括号
}
}
} else if (isOperatorStr(exps[i])) // 为运算符
{
getNumber = false;
if (getOperator) // 连续两个运算符
{
LogInfo.printError("Wrong Exps.");
return null;
} else {
getOperator = true;
}
while (!temStack.isEmpty()
&& !"(".equals(temStack.top())
&& compareCaculatorOperator(temStack.top(), exps[i]) > 0) {
myPosfixStack.push(temStack.top());
temStack.pop();
}
temStack.push(exps[i]); // 将输入运算符放入栈内
}
}
while (!temStack.isEmpty()) {
if ("(".equals(temStack.top())) {
LogInfo.printError("brackets not match!");
return null;
}
myPosfixStack.push(temStack.top());
t
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ExpCalculator.rar (30个子文件)
ExpCalculator
.project 389B
src
org
hq
calculator
Calculator.java 13KB
ExStack.java 884B
CalculatorUI.java 11KB
LogInfo.java 647B
.settings
org.eclipse.jdt.core.prefs 629B
.classpath 301B
bin
org
hq
calculator
CalculatorUI$8.class 816B
CalculatorUI$2.class 816B
CalculatorUI$3.class 816B
CalculatorUI$10.class 818B
CalculatorUI$6.class 816B
CalculatorUI$9.class 816B
CalculatorUI$15.class 821B
CalculatorUI$OnCalcButtonClickListener.class 1KB
LogInfo.class 2KB
ExStack.class 1KB
CalculatorUI$1.class 816B
CalculatorUI.class 8KB
CalculatorUI$14.class 821B
Calculator.class 9KB
CalculatorUI$7.class 816B
CalculatorUI$5.class 816B
CalculatorUI$13.class 821B
CalculatorUI$4.class 816B
CalculatorUI$17.class 815B
CalculatorUI$16.class 815B
CalculatorUI$12.class 821B
CalculatorUI$OnExitButtonClickListener.class 1KB
CalculatorUI$11.class 818B
共 30 条
- 1
he_qiao_2010
- 粉丝: 40
- 资源: 32
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页