请教大家一个问题,我现在要从一个文件里一行一行的读取四则表达运算式并判断是否正确。
如:100=40+60
如果不正确则记录该行行号及等号左右表达式的结果,但不立即抛出异常,等所有表达式都判断完毕后再打印出哪行表达式结果有问题。
如 等号右边表达式结果为result1,左边为result2
if (!result2.equals(result1))
{....
现在是如果一遇到结果不正确就抛出异常,不能再继续检查下去,请问如何解决。
应该如何去做?
谢谢
如:100=40+60
如果不正确则记录该行行号及等号左右表达式的结果,但不立即抛出异常,等所有表达式都判断完毕后再打印出哪行表达式结果有问题。
如 等号右边表达式结果为result1,左边为result2
if (!result2.equals(result1))
{....
现在是如果一遇到结果不正确就抛出异常,不能再继续检查下去,请问如何解决。
应该如何去做?
谢谢
在判断过程中,可以
将存储四则运算原式 或 记录该行行号及等号左右表达式的结果
存储到一个对象(如Vector)中,
待判断结束后,将结果对象中的内容一并取出并显示 或 做其它处理
try{
if (!result2.equals(result1))
{ ....}
}
catch(ExcptionType e){...}
finally(){要做的事情}
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());
}
}
~Stack<String> stk = new Stack<String>();
~ ~这是c++里面的吧~大哥
标准的cpp stl 没有见过java也可以如此
我打了这句话会报错啊应该怎么写啊
private Map errors;反正所有<>这种类型的都去掉就ok了
泛型
foreach
装箱拆箱
枚举
装箱拆箱
=======
是实现什么功能啊
能不能粗略讲一下啊
{
用HashMap记录错误,再将每个错误加入Vector}