码迷,mamicode.com
首页 > 其他好文 > 详细

Infix Expression Calculation

时间:2015-04-12 14:43:08      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

 

  This program provides another two cases of the application of Stack besides what I have mentioned in a previous article:

  (1) Class InfixExpr converts an Infix Expression to a Suffix Expression by using an operator stack;

  (2) Class Suffix calculates the value of a suffix expression by using a stack containing operands.

 

  Sample Input and Output:

  技术分享

 

  1 import java.util.*;
  2 import java.io.*;
  3 
  4 class Suffix {
  5     private Stack<Integer> stack;
  6     
  7     public Suffix() {
  8         stack = new Stack<Integer>();
  9     }
 10     public void addToken(String str) {
 11         if (str.equals("+") || str.equals("-") || str.equals("*") ||
 12                 str.equals("/") || str.equals("^")) {
 13             int b = stack.pop().intValue();
 14             int a = stack.pop().intValue();
 15             if (str.equals("+")) {
 16                 stack.add(new Integer(a+b));
 17             } else if (str.equals("-")) {
 18                 stack.add(new Integer(a-b));
 19             } else if (str.equals("*")) {
 20                 stack.add(new Integer(a*b));
 21             } else if (str.equals("/")) {
 22                 stack.add(new Integer(a/b));
 23             } else {
 24                 int val = 1;
 25                 for (int i=0;i<b;i++) {
 26                     val *= a;
 27                 }
 28                 stack.add(new Integer(val));
 29             }
 30         } else {    // an operand
 31             stack.add(Integer.valueOf(str));
 32         }
 33     }
 34     public int getVal() {
 35         return stack.peek().intValue();
 36     }
 37     public void clear() {
 38         stack.clear();
 39     }
 40 }
 41 
 42 public class InfixExpr {
 43     private static BufferedReader in;
 44     private static StringTokenizer tokenizer;
 45     private static Stack<String> stack;
 46     private static Suffix server;
 47     
 48     private static void printAndCal(String expr) {
 49         System.out.println(expr);
 50         tokenizer = new StringTokenizer(expr);
 51         // ATTENTION: we can‘t use a for-loop based on
 52         //        tokenizer.countTokens() since it is mutable
 53         while (tokenizer.hasMoreTokens()){
 54             String str = tokenizer.nextToken();
 55             if (str.equals("(")) {
 56                 stack.add(str);
 57             } else if (str.equals(")")) {
 58                 String op = stack.pop();
 59                 while (!op.equals("(")) {
 60                     server.addToken(op);
 61                     op = stack.pop();
 62                 }
 63             } else if (str.equals("+") || str.equals("-") || str.equals("*") ||
 64                 str.equals("/") || str.equals("^")) {
 65                 while (!stack.isEmpty()&&!stack.peek().equals("(")&&prior(stack.peek(),str)) {
 66                     server.addToken(stack.pop());
 67                 }
 68                 stack.add(str);
 69             } else {    // an operand
 70                 server.addToken(str);
 71             }
 72         }
 73         while (!stack.isEmpty()) {
 74             server.addToken(stack.pop());
 75         }
 76         System.out.println(server.getVal());
 77     }
 78     private static boolean prior(String op1,String op2) {
 79         // Returns whether op1 can be operated first when
 80         //        it is aligned with operator op2
 81         if (op2.equals("^")) {
 82             return false;
 83         } else if (op2.equals("*")||op2.equals("/")) {
 84             return !op1.equals("+")&&!op1.equals("-");
 85         } else {    
 86             return true;
 87         }
 88     }
 89     public static void main(String[] args) throws IOException {
 90         in = new BufferedReader(new FileReader("infix.in"));
 91         stack = new Stack<String>();
 92         server = new Suffix();
 93         int n = Integer.parseInt(in.readLine());
 94         System.out.println(n);
 95         for (int i=0;i<n;i++) {
 96             server.clear();
 97             printAndCal(in.readLine());
 98         }
 99         in.close();
100     }
101 }

 

Infix Expression Calculation

标签:

原文地址:http://www.cnblogs.com/DevinZ/p/4419367.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!