1 测试
静态调用测试
package cn.edut.clac;
public class Demo_calculator {
public static void main(String[] args) {
String expression = "1 + 2 * 3 + "
+ "( 8 / 2 * 1 + 25 / 5 + ( 7 + ( 1 + 1 ) / 2 ) / 2 ) * 6"
+ " - 7";
int result = Calculator.calcExpression(expression);
System.out.println("运算表达式:"+ expression );
System.out.println("结果:"+result);
}
}
类内部测试
@Test
public void Test01() {
String suffix_expression = "1 2 3 * + 4 5 + 6 * + 7 - 2 /";
System.out.println("后缀表达式:"+parseExpression(suffix_expression).toString());
System.out.println("运算结果:"+doCalc(parseExpression(suffix_expression)));;
}
@Test
public void Test02() {
String infix_expression = "1 + 2 * 3 + ( 4 + 5 ) * 6 - 7" ;
System.out.println("中缀表达式:"+infix_expression);
List<String> suffix_strTemp = parseSufixExpression(
parseExpression(infix_expression )
);
System.out.println("转成后缀表达式:"+suffix_strTemp.toString());
System.out.println("计算结果:"+doCalc(suffix_strTemp));
String infix_expression2 = "1 + 2 * 3 + ( 8 / 2 * 1 + 25 / 5 + ( 7 + ( 1 + 1 ) / 2 ) / 2 ) * 6 - 7" ;
System.out.println("中缀表达式:"+infix_expression2);
List<String> suffix_strTemp2 = parseSufixExpression(
parseExpression(infix_expression2 )
);
System.out.println("转成后缀表达式:"+suffix_strTemp2.toString());
System.out.println("计算结果:"+doCalc(suffix_strTemp2));
}
2 部分代码
中缀转后缀表达式
private static int currentIndex = 0 ;
private static List<String> parseSufixExpression(List<String> infixExpression){
List<String> resultList = new ArrayList<String>();
Stack<String> stackOperation = new Stack<>();
for( ;
currentIndex<infixExpression.size() ;
currentIndex++) {
String s = infixExpression.get(currentIndex);
if(!s.matches("\\D")) {
resultList.add(s);
}else if(s.equals("(")) {
currentIndex++ ;
resultList.addAll(parseSufixExpression(infixExpression));
}else if(s.equals(")")){
break;
}else {
switch(s) {
case "+":
case "-":
case "*":
case "/":
if(stackOperation.size()>0) {
while(stackOperation.size()>0) {
String strTemp = stackOperation.pop();
if(isHeightLevel(s,strTemp)) {
stackOperation.push(strTemp);
break;
}else {
resultList.add(strTemp);
}
}
stackOperation.push(s);
}else {
stackOperation.push(s);
}
break;
default :throw new RuntimeException();
}
}
}
while(stackOperation.size()>0) {
resultList.add(stackOperation.pop());
}
initialIndex(infixExpression);
return resultList;
}
private static void initialIndex(List<String> Expression) {
if(!(currentIndex < Expression.size())) {
currentIndex = 0 ;
}
}
private static boolean isHeightLevel(String s , String sTemp) {
return getLevel(s)-getLevel(sTemp)>0 ? true : false;
}
private static int getLevel(String s) {
switch(s) {
case "+":
case "-":return 0 ;
case "*":
case "/":return 1 ;
}
throw new RuntimeException();
}
后缀表达式求值
private static List<String> parseExpression(String str){
String[] strTemp = str.split("\\s+");
List<String> outList = new ArrayList<>();
for(String s : strTemp) {
outList.add(s) ;
}
return outList;
}
private static int doCalc(List<String> str) {
Stack<Integer> stackInteger = new Stack<>();
for(String s : str) {
if(!s.matches("\\D")) {
stackInteger.push(Integer.parseInt(s));
}
else {
int num2 = stackInteger.pop();
int num1 = stackInteger.pop();
int result = 0 ;
switch(s) {
case "+" : result = num1+num2; break;
case "-" : result = num1-num2; break;
case "*" : result = num1*num2; break;
case "/" : result = num1/num2; break;
default : throw new RuntimeException("四则运算符号对不上");
}
stackInteger.push(result) ;
}
}
return stackInteger.pop();
}
3 完整代码(Calculator类)
package cn.edut.clac;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import org.junit.Test;
public class Calculator {
public static int calcExpression(String s) {
return doCalc( parseSufixExpression(parseExpression(s)) );
}
@Test
public void Test01() {
String suffix_expression = "1 2 3 * + 4 5 + 6 * + 7 - 2 /";
System.out.println("后缀表达式:"+parseExpression(suffix_expression).toString());
System.out.println("运算结果:"+doCalc(parseExpression(suffix_expression)));;
}
@Test
public void Test02() {
String infix_expression = "1 + 2 * 3 + ( 4 + 5 ) * 6 - 7" ;
System.out.println("中缀表达式:"+infix_expression);
List<String> suffix_strTemp = parseSufixExpression(
parseExpression(infix_expression )
);
System.out.println("转成后缀表达式:"+suffix_strTemp.toString());
System.out.println("计算结果:"+doCalc(suffix_strTemp));
String infix_expression2 = "1 + 2 * 3 + ( 8 / 2 * 1 + 25 / 5 + ( 7 + ( 1 + 1 ) / 2 ) / 2 ) * 6 - 7" ;
System.out.println("中缀表达式:"+infix_expression2);
List<String> suffix_strTemp2 = parseSufixExpression(
parseExpression(infix_expression2 )
);
System.out.println("转成后缀表达式:"+suffix_strTemp2.toString());
System.out.println("计算结果:"+doCalc(suffix_strTemp2));
}
private static int currentIndex = 0 ;
private static List<String> parseSufixExpression(List<String> infixExpression){
List<String> resultList = new ArrayList<String>();
Stack<String> stackOperation = new Stack<>();
for( ;
currentIndex<infixExpression.size() ;
currentIndex++) {
String s = infixExpression.get(currentIndex);
if(!s.matches("\\D")) {
resultList.add(s);
}else if(s.equals("(")) {
currentIndex++ ;
resultList.addAll(parseSufixExpression(infixExpression));
}else if(s.equals(")")){
break;
}else {
switch(s) {
case "+":
case "-":
case "*":
case "/":
if(stackOperation.size()>0) {
while(stackOperation.size()>0) {
String strTemp = stackOperation.pop();
if(isHeightLevel(s,strTemp)) {
stackOperation.push(strTemp);
break;
}else {
resultList.add(strTemp);
}
}
stackOperation.push(s);
}else {
stackOperation.push(s);
}
break;
default :throw new RuntimeException();
}
}
}
while(stackOperation.size()>0) {
resultList.add(stackOperation.pop());
}
initialIndex(infixExpression);
return resultList;
}
private static void initialIndex(List<String> Expression) {
if(!(currentIndex < Expression.size())) {
currentIndex = 0 ;
}
}
private static boolean isHeightLevel(String s , String sTemp) {
return getLevel(s)-getLevel(sTemp)>0 ? true : false;
}
private static int getLevel(String s) {
switch(s) {
case "+":
case "-":return 0 ;
case "*":
case "/":return 1 ;
}
throw new RuntimeException();
}
private static List<String> parseExpression(String str){
String[] strTemp = str.split("\\s+");
List<String> outList = new ArrayList<>();
for(String s : strTemp) {
outList.add(s) ;
}
return outList;
}
private static int doCalc(List<String> str) {
Stack<Integer> stackInteger = new Stack<>();
for(String s : str) {
if(!s.matches("\\D")) {
stackInteger.push(Integer.parseInt(s));
}
else {
int num2 = stackInteger.pop();
int num1 = stackInteger.pop();
int result = 0 ;
switch(s) {
case "+" : result = num1+num2; break;
case "-" : result = num1-num2; break;
case "*" : result = num1*num2; break;
case "/" : result = num1/num2; break;
default : throw new RuntimeException("四则运算符号对不上");
}
stackInteger.push(result) ;
}
}
return stackInteger.pop();
}
}