表达式求值

package oj.test;

import java.math.BigInteger;
import java.util.*;

public class Demo11 {

 /**
  * @表达式求值
  * 给定一个字符串描述的算术表达式,计算出结果值。输入字符串长度不超过100,合法的字符包括”+, -, *, /, (, ),[,],{,}”,”0-9”,
  * 字符串内容的合法性及表达式语法的合法性由做题者检查。本题目只涉及整型计算。
  */
 
 public static void main(String[] args) {
  Scanner sc = new Scanner(System.in);
  String str = sc.nextLine();
  str = str.replaceAll("\\[", "\\(");

  str = str.replaceAll("\\]", "\\)");
  str = str.replaceAll("\\{", "\\(");
  str = str.replaceAll("\\}", "\\)");
  str = str.replaceAll("\\(-", "\\(0-");
  str = str.replaceAll("\\)-", "\\)0-");
  //sop(str);

  ArrayList<String> result = getStringList(str);
  //sop(result);
  result = getPostOrder(result);
  //sop(result);
  int num = caculate(result);
  sop(num);

 }
 //计算后缀表达式
 private static int caculate(ArrayList<String> postOrder) {
  Stack<Integer> stack = new Stack<Integer>();
  Iterator<String> it = postOrder.iterator();
  while(it.hasNext()){
   String temp = it.next();
   if(Character.isDigit(temp.charAt(0)))
    stack.push(Integer.parseInt(temp));
   else{
    char ch = temp.charAt(0);
    int back = stack.pop();
    int front = stack.pop();
    int res=0;
    switch(ch){
    case '+':
     res = front + back;
     break;
    case '-':
     res = front - back;
     break;
    case '*':
     res = front * back;
     break;
    case '/':
     res = front / back;
     break;
    }
    stack.push(res);
   }
  }
  return stack.pop();
 }

 //将中缀表达式转化为后缀表达式
 private static ArrayList<String> getPostOrder(ArrayList<String> inOrderList) {
  ArrayList<String> result = new ArrayList<String>();
  Stack<String> stack = new Stack<String>();
  Iterator<String> it = inOrderList.iterator();
  while(it.hasNext()){
   String temp = it.next();
   if(Character.isDigit(temp.charAt(0)))
    result.add(temp);
   else{
    switch(temp.charAt(0)){
    case '(':
     stack.push(temp);
     break;
    case ')':
     while(!stack.peek().equals("(")){
      result.add(stack.pop());
     }
     stack.pop();
     break;
    default:
     while(!stack.isEmpty() && compare(stack.peek(),temp)){
      result.add(stack.pop());
     }
     stack.push(temp);
     break;
    }
   }
  }
  while(!stack.isEmpty())
   result.add(stack.pop());
  
  return result;
 }
 
 //比较运算符等级
 private static boolean compare(String peek, String cur) {
  if(peek.equals("("))
   return false;
  char[] buf = {peek.charAt(0),cur.charAt(0)};
  int[] pre = {1,1};
  for(int i=0;i<2;i++){
   switch(buf[i]){
   case '*':
   case '/':
    pre[i]=2;
    break;
   }
  }
  
  if(pre[1]>pre[0])
   return false;
  else
   return true;
 }

 //将字符串转化成List
 private static ArrayList getStringList(String str) {
  ArrayList<String> result = new ArrayList<String>();
  String num = "";
  for(int i=0;i<str.length();i++){
   if(Character.isDigit(str.charAt(i))){
    num = num+str.charAt(i);
   }else{
    if(num!=""){
    result.add(num);
    }
    result.add(str.charAt(i)+"");
    num="";
   }
  }
  if(num!="")
   result.add(num);
  return result;
 }

 public static void sop(Object o){
  System.out.println(o);
 }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值