开发者

not evaluation doubles

Okay so I am sending doubles value to this and its not parsing them as doubles instead it is completely ignoring the decimal values. here is my code if I enter 2.0 + 5.0 it makes it 2 0 5 0 +. =(

import java.beans.Expression;
import java.util.ArrayList;
import java.uti开发者_JAVA技巧l.Scanner;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Infix2Postfix {

private static String grabDigits(String s, int start){
    String num = "";
    for(int i=start; i < s.length(); i++){
        char ch = s.charAt(i);
        if(Character.isDigit(ch))
            num += ch;
        else
            return num;
    }

    return num;
}

private static Double apply(Double a, char op, Double b){
    switch(op){
    case '+' : return a + b;
    case '-' : return a - b;
    case '*' : return a * b;
    case '/' : return b == 0 ? null : a / b;
    default:
        return null;
    }
}
public static Double evalPostfix(String expr){
    Stack<Double> s = new Stack<Double>();


    for(int i=0; i<expr.length(); ){
        char ch = expr.charAt(i);

        if(Character.isDigit(ch)){
            String numStr = grabDigits(expr, i);
            i += numStr.length();
            Double value;

            if(isColmn(numStr)){
            value = getvaluefromcolmn(numStr);
            }else{
                value = Double.parseDouble(numStr);

            }
            s.push(value);
        }
        else {
            if(isOp(ch)){
                if(s.size() < 2) return null;
                Double b = s.pop();  // right arg
                Double a = s.pop();  // left arg
                s.push(apply(a, ch, b));
            }
            else if(!Character.isWhitespace(ch))
                return null;
            i++;  // consume individual char
        }
    }
    if(s.size() != 1) return null;
    return s.pop();
}

private static Double getvaluefromcolmn(String numStr) {
    // TODO Auto-generated method stub
    return null;
}

private static boolean isColmn(String numStr) {
    // TODO Auto-generated method stub
    return false;
}

private static int prec(char op){
    switch(op){
    case '+' :
    case '-' :
        return 0;
    case '*' :
    case '/' : 
        return 1;
    default:
        return -1;
    }
}
private static boolean isOp(char ch){
    return prec(ch) != -1;
}
public static String infix2postfix(String expr) {
    Stack<Character> s = new Stack<Character>();
    String pExpr = "";
    int numOperands = 0;
    int numOperators = 0;

    for(int i=0; i<expr.length(); i++){
        char ch = expr.charAt(i);
        if(Character.isDigit(ch)){
            pExpr += " " + ch;
            // could have used the grabDigits method here ...
            while(i+1 < expr.length() && Character.isDigit(expr.charAt(i+1))){
                pExpr += expr.charAt(i+1);
                i++;
            }
            numOperands++;
        }
        else if (ch == '(')
            s.push(ch);
        else if (ch == ')'){
            while(!s.empty() && s.peek() != '('){
                pExpr = pExpr + " " + s.pop() + " ";
                numOperators++;
            }
            if(s.empty())
                return "no matching open paren";
            if(numOperators >= numOperands)
                return "too many operators";
            s.pop();

        }
        else if(isOp(ch)){
            // pop operators with same or higher precedence
            while(!s.empty() && isOp(s.peek()) && prec(s.peek()) >= prec(ch)){
                pExpr = pExpr + " " + s.pop();
                numOperators++;
            }
            if(numOperators >= numOperands)
                return "too many operators";
            s.push(ch);
        }
        // else white space - do nothing

    }
    while(!s.empty()){
        char op = s.pop();
        if(!isOp(op))
            return "error";
        pExpr += " " + op;
    }
    return pExpr;

}

public static void exp(String expr, ArrayList<ArrayList<Comparable<?>>> entries){
    expr.replace("(", " ( ");
    expr.replace(")", " ) ");
    expr.replace("+", " + ");
    expr.replace(" - ", "  -  ");
    expr.replace("/", " /    ");
    expr.replace("*", " *   ");

    System.out.println("infix:   " + expr);
    System.out.println("this is at expreesion after replacing "+ expr);
    System.out.println("postfix: " + infix2postfix(expr));
    System.out.println("--------");
}
public static void main(String [] args){


    Scanner kbd = new Scanner(System.in);


    // ArrayList<int> tst;
    ArrayList<Integer> tst2;


    System.out.print("> ");
    while(kbd.hasNextLine()){
        String expr = kbd.nextLine();
         expr = expr.replaceAll("\\s+","");
        System.out.println(expr);
        Pattern pattern = Pattern.compile("\\s+");
          Matcher matcher = pattern.matcher(expr);
          boolean check = matcher.find();
          String str = matcher.replaceAll(" ");
    expr =  expr.replace("(", " ( ");

    expr =expr.replace(")", " ) ");
    expr =expr.replace("+", " + ");
    expr =expr.replace("-", " - ");
    expr =expr.replace("/", " / ");
    expr =expr.replace("*", " * ");
    String[] exprArray = expr.split(" ");



          System.out.println(str+ " this is expr "+exprArray[1]);
    System.out.println(expr);
        ArrayList<ArrayList<Comparable<?>>> entries = null;
        String pExpr = infix2postfix(expr);

        //System.out.println(evalPostfix(expr));

        System.out.println("  postfix version: " + pExpr);
        System.out.println("  eval(\"" + pExpr + "\"): " + evalPostfix(pExpr));
        System.out.print("> ");

    }


}

}


I didn't check your code in detail but the following line

while(i+1 < expr.length() && Character.isDigit(expr.charAt(i+1))) ...

looks like you are parsing only digits but not '.' for each expression.

The same holds true for your method grabDigits.


By stopping at the first non-digit you are stopping at the decimal point, and therefore ignoring both it and everything after it.

Don't convert strings to doubles with your own code, use Double.parseDouble().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜