请教大家一个问题,我现在要从一个文件里一行一行的读取四则表达运算式并判断是否正确。
如:100=40+60
如果不正确则记录该行行号及等号左右表达式的结果,但不立即抛出异常,等所有表达式都判断完毕后再打印出哪行表达式结果有问题。
如 等号右边表达式结果为result1,左边为result2
if (!result2.equals(result1))
{....
现在是如果一遇到结果不正确就抛出异常,不能再继续检查下去,请问如何解决。
应该如何去做?
谢谢

解决方案 »

  1.   

    这个不能算异常,不相等直接return就好了,也可以先打印出错信息再return
      

  2.   

    解决这个问题最好不要用异常,使用异常的代价有些高。
    在判断过程中,可以
    将存储四则运算原式 或 记录该行行号及等号左右表达式的结果
    存储到一个对象(如Vector)中,
    待判断结束后,将结果对象中的内容一并取出并显示 或 做其它处理
      

  3.   

    这个很简单,你用一个collection,可以考虑用HashMap<lineNumber,result>保存错误结果,在最后输出这个hashmap就ok了
      

  4.   

    可以在finally语句中执行你要做的事情就行了~
    try{
       if (!result2.equals(result1))
           { ....}
        }
    catch(ExcptionType e){...}
    finally(){要做的事情}
      

  5.   

    import java.util.*;
    import java.io.*;public class FindErrors {
    private Map<String,String>errors;
    private String file;
    public FindErrors(String file){
    this.file = file;
    errors = new LinkedHashMap<String,String>();
    }

    public void find() throws IOException{
    BufferedReader br = new BufferedReader(new FileReader(file));
    int lineNum=-1;
    String line = new String();
    while((line=br.readLine())!=null){
    lineNum++;
    int ep = line.indexOf('=');
    String left = line.substring(0,ep).trim();
    String right = line.substring(ep+1).trim();
    double leftValue = Double.parseDouble(left);
    double rightValue = new CalStr(right).getResult();
    if(leftValue!=rightValue){
    errors.put(String.valueOf(lineNum),line);
    }
    }
    }


    public void showErrors(){
    System.out.println(errors);
    }

    public static void main(String args[])throws IOException{
    FindErrors fe = new FindErrors("expressions.txt");
    fe.find();
    fe.showErrors();
    }}===========================================================================
    import java.util.*;public class CalStr {
    private String src;

    /**
     * constructor
     * @param src the string(expression) to calculate
     */
    public CalStr(String src) {
    this.src = src;
    }
    /**
     * calculate to get the result
     * @return (double)result
     */
    public double getResult() {
    String postfix = getPostfix();
    Stack<String> stk = new Stack<String>();
    //System.out.println(postfix);
    String parts[] = postfix.split(" +");
    double result=0;
    for(int i=0; i<parts.length; i++){
    char tmp = parts[i].charAt(0);
    if(!isOperator(tmp)){
    stk.push(parts[i]);
    }
    else{
    double a = Double.parseDouble(stk.pop());
    double b = Double.parseDouble(stk.pop());
    //b is followed by a in the orignal expression
    result = calculate(b,a,tmp);
    stk.push(String.valueOf(result));
    }
    }
    return result;
    }
    /**
     * test if the character is an operator,such +,-,*,/
     * @param op the character to test
     * @return true if op is an operator otherwise false
     */
    private boolean isOperator(char op){
    return (op=='+'||op=='-'||op=='*'||op=='/');
    }


    /**
     * calculate an expression such (a op b)
     * @param a number 1
     * @param b number 2
     * @param op the operator
     * @return (double)(a op b)
     */
    public double calculate(double a, double b, char op) {
    switch (op) {
    case '+':
    return a + b;
    case '-':
    return a - b;
    case '*':
    return a * b;
    case '/':
    return a / b;
    }
    return -1;
    }
    /**
     * convert the suffix to postfix
     * @return the postfix as a string
     */
    private String getPostfix() {
    Stack<String> stk = new Stack<String>();
    String postfix = new String();
    char op;
    int i = 0;
    while (i < src.length()) {
    if (Character.isDigit(src.charAt(i))||src.charAt(i)=='.') {
    postfix += " ";
    do {
    postfix += src.charAt(i++);
    } while ((i < src.length())
    && (Character.isDigit(src.charAt(i))));
    postfix += " ";
    } else {
    switch (op = src.charAt(i++)) {
    case '(':
    stk.push("(");
    break;

    case ')':
    while (stk.peek() != "(") {
    String tmp = stk.pop();
    postfix += tmp;
    if(tmp.length()==1 && isOperator(tmp.charAt(0)))
    postfix += " ";
    }
    stk.pop();
    postfix += " ";
    break;

    case '+':
    case '-':
    while ((!stk.empty()) && (stk.peek() != "(")) {
    postfix += stk.pop()+" ";
    }
    stk.push(new Character(op).toString());
    break;

    case '*':
    case '/':
    while ((!stk.empty())
    && ((stk.peek() == "*") || (stk.peek() == "/"))) {
    postfix += stk.pop()+" ";
    }
    stk.push(new Character(op).toString());
    break;
    }
    }
    }
    ListIterator it = stk.listIterator(stk.size());
    while (it.hasPrevious())
    postfix += it.previous() + " ";
    return postfix.trim().replaceAll(" +\\.",".");
    } /**
     * main function
     * @param args
     */
    public static void main(String args[]) {
    System.out.println(new CalStr("((1.5+6.000)*9+9.36)*(8-8*8+8*7)").getResult());
    }
    }
      

  6.   

    楼上的人:
        ~Stack<String> stk = new Stack<String>();
        ~ ~这是c++里面的吧~大哥
      

  7.   

    private Map<String,String>errors;
    标准的cpp stl 没有见过java也可以如此
      

  8.   

    private Map<String,String>errors;
    我打了这句话会报错啊应该怎么写啊
      

  9.   

    我这个是jdk5.0的泛型写法,以前的版本可以改成如下
    private Map errors;反正所有<>这种类型的都去掉就ok了
      

  10.   

    大家好好看看jdl1.5的新特性吧!
    泛型
    foreach
    装箱拆箱
    枚举
      

  11.   

    foreach
    装箱拆箱
    =======
    是实现什么功能啊
    能不能粗略讲一下啊
      

  12.   

    if (!result2.equals(result1))
    {
        用HashMap记录错误,再将每个错误加入Vector}