java 带括号 四则运算 算法_带括号的加减乘除–递规算法.(java.) | 学步园

package com.livedoor.gl.core.bean.util;

import java.util.ArrayList;

import java.util.List;

public class oper {

private void printList(List l, String desc) {

/**

System.out.println(desc + "'s size:" + l.size());

for (Iterator iter = l.iterator(); iter.hasNext();) {

String e = (String) iter.next();

System.out.println(e);

}**/

}

private boolean isDiget(char f) {

if (f >= '0' && f <= '9')

return true;

if (f == '.')

return true;

return false;

}

private boolean isOperation(char f, char before) {

if(f=='(' || f==')')

return true;

if (f == '+' || f == '/' || f == '*')

return true;

if (f == '-' )

if((before == ')' || isDiget(before)))

return true;

else

return false;

return false;

}

private boolean isOperation(String element) {

if (element.length() == 1) {

if (element.equals("+") || element.equals("-")

|| element.equals("*") || element.equals("/"))

return true;

if(element.equals(")") || element.equals("("))

return true;

}

return false;

}

private boolean isOperationJJ(String element) {

if (element.length() == 1) {

if (element.equals("+") || element.equals("-")) {

return true;

}

}

return false;

}

private boolean isOperationCC(String element) {

if (element.length() == 1) {

if (element.equals("*") || element.equals("/")) {

return true;

}

}

return false;

}

private List getList(String source) {

List ret = new ArrayList();

char[] cArr = source.toCharArray();

String tmp = "";

for (int i = 0; i < cArr.length; i++) {

char before = 'x';

if (i != 0)

before = cArr[i - 1];

if (this.isOperation(cArr[i], before)) {

if(!tmp.equals(""))

ret.add(tmp);

tmp = "";

ret.add("" + cArr[i]);

} else {

tmp += "" + cArr[i];

}

}

if(!tmp.equals(""))

ret.add(tmp);

return ret;

}

private double caculateTwoNumber(String oper, String leftNum,

String rightNum) {

double ret = 0;

if (oper.equalsIgnoreCase("+")) {

ret = Double.valueOf(leftNum).doubleValue()

+ Double.valueOf(rightNum).doubleValue();

} else if (oper.equalsIgnoreCase("-")) {

ret = Double.valueOf(leftNum).doubleValue()

- Double.valueOf(rightNum).doubleValue();

} else if (oper.equalsIgnoreCase("*")) {

ret = Double.valueOf(leftNum).doubleValue()

* Double.valueOf(rightNum).doubleValue();

} else if (oper.equalsIgnoreCase("/")) {

ret = Double.valueOf(leftNum).doubleValue()

/ Double.valueOf(rightNum).doubleValue();

}

return ret;

}

private void caculateJJCC(List source) {

if (source.size() == 1)

return;

int offset = 0;

Object[] oArr = source.toArray();

for (int i = 0; i < oArr.length; i++) {

String sss = (String) oArr[i];

if (this.isOperationCC(sss)) {

double result = this.caculateTwoNumber(sss, source

.get(offset - 1), source.get(offset + 1));

source.set(offset, "" + result);

source.remove(offset + 1);

source.remove(offset - 1);

offset -= 2;

}

offset++;

}

this.printList(source, "tmpList CC ed.");

offset = 0;

String[] oArr2 = (String[])source.toArray(new String[source.size()]);

for (int i = 0; i < oArr2.length; i++) {

String sss = (String) oArr2[i];

if (this.isOperationJJ(sss)) {

double result = this.caculateTwoNumber(sss, source

.get(offset - 1), source.get(offset + 1));

source.set(offset, "" + result);

source.remove(offset + 1);

source.remove(offset - 1);

offset -= 2;

}

offset++;

}

this.printList(source, "source JJ ed.");

}

private int findLastedLeftKuohao(List source) {

if (!source.contains("("))

return -1;

return source.lastIndexOf("(");

}

@SuppressWarnings("unchecked")

private void caculateKuohao(List source) {

// 从source中找到最左面的左括号。到最近的右括号。取得startIndex,endIndex.并截取subList.

int startIndex = this.findLastedLeftKuohao(source);

int endIndex = 0;

List sub = null;

if (startIndex != -1) {

List tmp = source.subList(startIndex, source.size());

int rightIndex = tmp.indexOf(")");

if (rightIndex != -1) {

sub = tmp.subList(0, rightIndex + 1);

}

endIndex += startIndex + rightIndex;

this.caculateJJCC(sub);

source.remove(startIndex + 2);

source.remove(startIndex);

} else {

this.caculateJJCC(source);

}

// 判断,如果仍就有括号。就再次递归。否则,就退出。

if (this.findLastedLeftKuohao(source) != -1) {

this.caculateKuohao(source);

} else {

this.caculateJJCC(source);

return;

}

}

public String getResult(String fun){

List l = this.getList(fun);

this.caculateKuohao(l);

return (String)l.get(0);

}

/**

* @param args

*/

public static void main(String[] args) {

oper client = new oper();

System.out.println(client.getResult("7*2+(8*6)"));

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值