# 用java实现计算String类型的四则运算——用栈和后缀表达式实现

最近看数据结构，看到栈和后缀表达式的地方，于是自己动手用写了一段很简单的实现计算String类型的四则运算的例子。



class D1 implements Serializable {
public static void main(String[] args) {
//这里做测试
D1 d = new D1();
String str = "9+(3-1)*3-10/2";
List<String> list = d.infixToSuffix(str);
Integer num = d.getReslut(list);
System.out.println(num);
}

/**
* 传入转换完毕的后缀表达式，计算
* @param list
* @return
*/
public Integer getReslut(List<String> list){
Integer n1 = null;
Integer n2 = null;
Integer reslut = null;
for(int i=0;i<list.size();i++){
String num = list.get(i);
try {
Integer n = Integer.valueOf(num);
opt.offerFirst(n);
}catch (Exception e){
n2 = opt.pollFirst();
n1 = opt.pollFirst();
if("+".equals(num)){
reslut = n1 + n2;
}else if("-".equals(num)){
reslut = n1 - n2;
}else if("*".equals(num)){
reslut = n1 * n2;
}else if("/".equals(num)){
reslut = n1 / n2;
}
opt.offerFirst(reslut);
}
}

return  opt.pollFirst();
}

//9+(3-1)*3-10/2 = 10
//  9 3 1 - 3 * 10 2 / - +
/**
* 中缀表达式转后缀表达式，直接传入算数式
* @param str
* @return
*/
public List<String> infixToSuffix(String str) {
List<String> numlist = getNums(str);
List<String> list = new ArrayList<String>();
for (int i = 0; i < numlist.size(); i++) {
//            Character c = str.charAt(i);
String num = numlist.get(i);
if("(".equals(num)||"*".equals(num)||"/".equals(num)){
deque.offerFirst(num);
}else if("+".equals(num)||"-".equals(num)){
}else if(")".equals(num)){
rightKuo(deque, num, list);
}else{
}
if (i == numlist.size() - 1) {
while (true) {
String next = deque.pollFirst();
if (null == next) {
break;
}
}
}
}
return list;
}

private void addAndMul(Deque<String> deque, String c, List<String> list) {
while (true) {
String    next = deque.peekFirst();
if (null == next || "+".equals(next) || "-".equals(next) || "(".equals(next)) {
deque.offerFirst(c);
break;
}
}
}
private void rightKuo(Deque<String> deque, String c, List<String> list) {
while (true) {
String next = deque.pollFirst();
if ("(" .equals(next)) {
break;
}
}
}
/**
* 将String类型算数表达式转换为list，将数字与运算符分开
* @param str
* @return
*/
private List<String> getNums(String str) {
String nums = "0123456789";
String opts = "()+-*/";
//        String[] ss = new String[str.length()];
List<String> list = new ArrayList<String>();
int j = 0;
for (int i = 0; i < str.length(); i++) {
if (i == 0) {
//                ss[j] = str.substring(i, i + 1);
} else {
String per = str.substring(i - 1, i);
String now = str.substring(i, i + 1);
if(opts.indexOf(now)>-1 ){
j++;
//                    ss[j] = now;
}else if(nums.indexOf(now)>-1){
if(nums.indexOf(per)>-1){
//                        ss[j]=ss[j]+now;
list.set(j,list.get(j)+now);
}else{
j++;
//                        ss[j] = now;
}
}
}
}
return list;
}
}

#### java利用栈实现四则运算

2015-09-29 12:25:10

#### JAVA实现字符串四则运算

2016-11-03 21:19:15

#### JAVA四则运算字符串解释器

2017-03-12 13:06:40

#### Java 用栈处理四则运算

2015-09-28 17:08:01

#### 栈的应用--四则运算表达式求值（java代码）

2016-02-11 22:22:55

#### java 四则运算 栈的实现

2018-05-11 19:08:48

#### java 写一个简单的计算器

2016-10-04 18:19:06

#### 通过后缀表达式模拟计算器

2017-07-25 22:26:30

#### java后缀表达式实现表达式求值

2017-08-04 21:04:45

#### 后缀表达式的计算器Java实现

2017-05-09 13:42:58