package com.caculate;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Stack;
public class Caculate {
public static void main(String[] args) {
String str = "9+(3-1)*3+3/2";
LastExpression le = new Lastexpression_r();
le.toLastexpression_r(str);
le.pop();//调用栈中剩下的符号元素
String ss = le.getResult();
System.out.println(ss);
LastCaculate lc = new LastCaculate();
lc.caculate(ss);
System.out.println("result:"+lc.getResult());
}
}
class LastExpression {
Stack stack = new Stack();
int level = 0;
StringBuilder sb = new StringBuilder();
public void toLastexpression_r(String str) {
for(int i = 0;i
if(str.charAt(i)>='0'&&str.charAt(i)<='9') {
//
System.out.print(str.charAt(i));//数字直接输出
sb.append(str.charAt(i));
} else {
operate(str.charAt(i));//处理各个符号
}
}
}
private void operate(char ch) {
switch(ch) {
case '+':
case '-':
push(ch,1);break;
case '*':
case '/':
push(ch,2);break;
case '(':
push(ch,0);break;
case ')':
push(ch,3);break;
default:break;
}
}
private void push(char ch,int priority) {
if(priority == 0) {
stack.push(ch);
} else if(priority==2) {
stack.push(ch);
} else if(priority == 3) {
do {
//
System.out.print(stack.pop());
sb.append(stack.pop());
} while(stack.pop()!='(');
priority = 0;
} else if(priority
while(!stack.empty()) {
//
System.out.print(stack.pop());
sb.append(stack.pop());
}
stack.push(ch);
} else {
stack.push(ch);
}
level = priority;//最后更新一下level便于对后面的优先级符号进行比较
}
public void pop() {
while(!stack.empty()) {
//
System.out.print(stack.pop());
sb.append(stack.pop());
}
}
public String getResult() {
return sb.toString();
}
}
class LastCaculate {
Stack stack = new Stack();
public void caculate(String str) {
for(int i = 0;i
if(str.charAt(i)>='0'&&str.charAt(i)<='9') {
stack.push(str.charAt(i)-'0');
} else {
operate(str.charAt(i));//处理各个符号
}
}
}
private void operate(char ch) {
switch(ch) {
case '+':
add();break;
case '-':
sub();break;
case '*':
multiply();break;
case '/':
divide();break;
default:break;
}
}
private void add() {
double num1 = 0,num2 = 0;
if(stack.peek()!=null) {
num1 = Double.parseDouble(""+stack.pop());
} if(stack.peek()!=null) {
num2 = Double.parseDouble(""+stack.pop());
}
double result = num2 + num1;
stack.push(result);
}
private void sub() {
double num1 = 0,num2 = 0;
if(stack.peek()!=null) {
num1 = Double.parseDouble(""+stack.pop());
} if(stack.peek()!=null) {
num2 = Double.parseDouble(""+stack.pop());
}
double result = num2 - num1;
stack.push(result);
}
private void multiply() {
double num1 = 0,num2 = 0;
if(stack.peek()!=null) {
num1 = Double.parseDouble(""+stack.pop());
} if(stack.peek()!=null) {
num2 = Double.parseDouble(""+stack.pop());
}
double result = num2 * num1;
stack.push(result);
}
private void divide() {
double num1 = 0,num2 = 0;
if(stack.peek()!=null) {
num1 = Double.parseDouble(""+stack.pop());
} if(stack.peek()!=null) {
num2 = Double.parseDouble(""+stack.pop());
}
double result = 0;
if(num1!=0) {
result = num2 / num1;
stack.push(result);
} else {
System.out.println("除数不能为0!");
return;
}
}
public double getResult() {
double num = Double.parseDouble(""+stack.pop());
BigDecimal bg = new BigDecimal(num);
double result = bg.setScale(1, 4).doubleValue();//保留1位小数,四舍五入
return result;
}
}
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Stack;
public class Caculate {
System.out.println(ss);
System.out.println("result:"+lc.getResult());
}
class LastExpression {
//
//
//
//
}
class LastCaculate {
}