Tính Giá Trị Biểu Thức Bằng Cách Chuyển Trung Tố Sang Hậu Tố – (^^^)

Hàm main

{

String[] testString = {"2 + 3 * 4 / 5", "2*3 - 4 + 5 / 6", " 35 - 42* 17 /2 + 10", " 33.2 - 17.5 * 2.0 ^ 3.2", "3 * (4 + 5)", "32*(41-(52+2))/(201+31)"}; InfixToPostfix converter = new InfixToPostfix(); String sinfix = testString[5]; System.out.println("infix = "+sinfix); String spostfix = converter.convertToPostfix(sinfix); System.out.println("postfix = "+spostfix); String[] mathe = spostfix.split(" "); System.out.println("value = "+valueMath(mathe));

}

//ham tinh gia tri bieu thuc hau to

public String valueMath(String[] elementMath){ Stack<String> S = new Stack<String>(); InfixToPostfix IFP = new InfixToPostfix(); for (int i=0; i<elementMath.length; i++) { if (!elementMath[i].equals("")) { char c = elementMath[i].charAt(0); if (!IFP.isOperator(c)) S.push(elementMath[i]); else { double num = 0f; double num1 = Float.parseFloat(S.pop()); double num2 = Float.parseFloat(S.pop()); switch (c) { case '+': num = num2 + num1; break; case '-': num = num2 - num1; break; case '*': num = num2 * num1; break; case '/': num = num2 / num1; break; default: break; } S.push(Double.toString(num)); } } } return S.pop(); } ----------------------------------------------------------------------- Class chuyển biểu thức trung tố sang hậu tố:
import java.util.*; public class InfixToPostfix { // Constructor: (default) // Private methods: public boolean isOperator(char c) { // Tell whether c is an operator. return c == '+' || c == '-' || c == '*' || c == '/' || c == '^' || c=='(' || c==')'; }//end isOperator private boolean isSpace(char c) { // Tell whether c is a space. return (c == ' '); }//end isSpace private boolean lowerPrecedence(char op1, char op2) { // Tell whether op1 has lower precedence than op2, where op1 is an // operator on the left and op2 is an operator on the right. // op1 and op2 are assumed to be operator characters (+,-,*,/,^). switch (op1) { case '+': case '-': return !(op2=='+' || op2=='-') ; case '*': case '/': return op2=='^' || op2=='('; case '^': return op2=='('; case '(': return true; default: // (shouldn't happen) return false; } } // end lowerPrecedence // Method to convert infix to postfix: public String convertToPostfix(String infix) { // Return a postfix representation of the expression in infix. Stack operatorStack = new Stack(); // the stack of operators char c; // the first character of a token StringTokenizer parser = new StringTokenizer(infix,"+-*/^() ",true); // StringTokenizer for the input string StringBuffer postfix = new StringBuffer(infix.length()); // result // Process the tokens. while (parser.hasMoreTokens()) { String token = parser.nextToken(); // get the next token // and let c be c = token.charAt(0); // the first character of this token if ( (token.length() == 1) && isOperator(c) ) { // if token is // an operator while (!operatorStack.empty() && !lowerPrecedence(((String)operatorStack.peek()).charAt(0), c)) // (Operator on the stack does not have lower precedence, so // it goes before this one.) postfix.append(" ").append((String)operatorStack.pop()); if (c==')') { // Output the remaining operators in the parenthesized part. String operator = (String)operatorStack.pop(); while (operator.charAt(0)!='(') { postfix.append(" ").append(operator); operator = (String)operatorStack.pop(); } } else operatorStack.push(token);// Push this operator onto the stack. } else if ( (token.length() == 1) && isSpace(c) ) { // else if // token was a space ; // ignore it } else { // (it is an operand) postfix.append(" ").append(token); // output the operand }//end if }// end while for tokens // Output the remaining operators on the stack. while (!operatorStack.empty()) postfix.append(" ").append((String)operatorStack.pop()); // Return the result. return postfix.toString(); }//end convertToPostfix // end main }//end class InfixToPostfix

Share this:

  • X
  • Facebook
Like Loading...

Related

Từ khóa » Chuyen Bieu Thuc Trung To Sang Hau To