Chinaunix首页 | 论坛 | 博客
  • 博客访问: 322013
  • 博文数量: 15
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 179
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-09 18:16
文章分类

全部博文(15)

文章存档

2019年(1)

2018年(1)

2015年(7)

2013年(6)

我的朋友

分类: Java

2013-09-09 19:55:37


点击(此处)折叠或打开

  1. package basic;

  2. import java.util.Stack;

  3. class Arithmetic2 {
  4.     public static void main(String[] args) {
  5.         System.out.println("!!!!!!!!");
  6.         // 中缀 => 后缀表达式
  7.         String s = "(+0.2 + (+3 + 0.5) / (-2 * 2) - 3 ) * 2"; // 中缀
  8.         String strTemp = "";
  9.         int index = -1;
  10.         boolean lastIsNum = false; //前一个字符是否是数字
  11.         String[] str = new String[s.length()]; // 后缀
  12.         Stack<String> operationStack = new Stack<String>();
  13.         Stack<Double> resultStack = new Stack<Double>();
  14.         for (int i = 0; i < s.length(); i++) {
  15.             str[i] = "";
  16.         }
  17.         for (int i = 0; i < s.length(); i++) {
  18.             char ch = s.charAt(i);
  19.             if (ch == ' ') {
  20.                 continue;
  21.             }
  22.             if (ch >= '0' && ch <= '9' || ch == '.') {
  23.                 if (lastIsNum) {
  24.                     str[index] += String.valueOf(ch);
  25.                 } else {
  26.                     str[++index] += String.valueOf(ch);
  27.                 }
  28.                 lastIsNum = true;
  29.             }
  30.             if (ch == '+' || ch == '-') {
  31.                 if (i == 0 || s.substring(i - 1, i).equals("(")) {
  32.                     lastIsNum = true;
  33.                     str[++index] += String.valueOf(ch);
  34.                 } else {
  35.                     lastIsNum = false;
  36.                     while (!operationStack.isEmpty() && !operationStack.peek().equals("(")) {
  37.                         str[++index] += operationStack.pop();
  38.                     }
  39.                     operationStack.push(String.valueOf(ch));
  40.                 }
  41.             }
  42.             if (ch == '*' || ch == '/') {
  43.                 lastIsNum = false;
  44.                 operationStack.push(String.valueOf(ch));
  45.             }
  46.             if (ch == '(') {
  47.                 lastIsNum = false;
  48.                 operationStack.push(String.valueOf(ch));
  49.             }
  50.             if (ch == ')') {
  51.                 lastIsNum = false;
  52.                 while (!operationStack.isEmpty() && !operationStack.peek().equals("(")) {
  53.                     str[++index] += operationStack.pop();
  54.                 }
  55.                 operationStack.pop();
  56.             }
  57.         }
  58.         if (!operationStack.isEmpty()) {
  59.             str[++index] += operationStack.pop();
  60.         }
  61. // System.out.println(new ArrayList(Arrays.asList(str)));
  62.         index = -1;
  63.         try {
  64.             while (index < s.length() && !str[++index].equals("")) {
  65.                 strTemp = str[index];
  66.                 if (isNum(strTemp)){
  67.                     resultStack.push(Double.valueOf(strTemp));
  68.                 } else {
  69.                     double num2 = resultStack.pop();
  70.                     double num1 = resultStack.pop();
  71.                     if (strTemp.equals("+")) {
  72.                         resultStack.push(num1 + num2);
  73.                     }
  74.                     if (strTemp.equals("-")) {
  75.                         resultStack.push(num1 - num2);
  76.                     }
  77.                     if (strTemp.equals("*")) {
  78.                         resultStack.push(num1 * num2);
  79.                     }
  80.                     if (strTemp.equals("/")) {
  81.                         resultStack.push(num1 / num2);
  82.                     }
  83.                 }
  84.             }
  85.             
  86.         } catch (Exception e) {
  87. // e.printStackTrace();
  88.             System.out.println("the expression is incorrect");
  89.             System.exit(1);
  90.         }
  91.         System.out.println(resultStack.pop());
  92.     }
  93.     
  94.     public static boolean isNum(String str){
  95.         return str.matches("^([+-]?\\d+)(\\.\\d+)?$");//浮点数
  96.     }
  97. }


阅读(2335) | 评论(3) | 转发(0) |
0

上一篇:没有了

下一篇:马的遍历贪婪法求一个解

给主人留下些什么吧!~~

konger1232017-03-21 19:53:18

特意注册个账号提醒:此算法存在问题!!!
经测试,例如:1+5*6+3*(2+3*2+2-1+3*3)+10/5-6*1,实际计算结果应为81,经此算法计算后结果为6.0。
拆分判断时推荐使用StringTokenizer tokenizer = new StringTokenizer(str, \"+-*/()\", true);进行判断。

shaka2022013-09-11 16:52:36

7大爷 不错不错

多谢,大龄青年发现没有好好学习,只能慢慢检起来

回复 | 举报

7大爷2013-09-10 09:24:52

 不错不错