这个java程序看不懂 有些问题想请教一下
packagecom.project.sample;importcom.project.base.ArithmeticExperssion;importcom.proje...
package com.project.sample;
import com.project.base.ArithmeticExperssion;
import com.project.base.InvalidTokenException;
import com.project.base.Operator;
import com.project.base.OperatorNotFoundException;
import com.project.base.WrongNumberOfTokensException;
public class IntArithmaticExperession implements ArithmeticExperssion {
private IntOperand left;
private IntOperand right;
private Operator op;
@Override
public String eval(String s) throws OperatorNotFoundException, WrongNumberOfTokensException, InvalidTokenException {
// TODO 自动生成的方法存根
String[] tokens = s.split(SEPERATOR);
if(tokens.length != 3) {
throw new WrongNumberOfTokensException();
}
left = new IntOperand(tokens[0]);
if(tokens[1].length() != 1) {
throw new InvalidTokenException();
}
op = new Operator(tokens[1].charAt(0));
right = new IntOperand(tokens[2]);
return String.valueOf(calc());
}
private int calc() throws OperatorNotFoundException {
int result = 0;
switch(op.getSymbol()) {
case PLUS:
result = left.getOperand() + right.getOperand();
break;
case MINUS:
result = left.getOperand() - right.getOperand();
break;
case MUTIPLY:
result = left.getOperand() * right.getOperand();
break;
case DIVIDE:
result = left.getOperand() / right.getOperand();
break;
default:
throw new OperatorNotFoundException(op);
}
return result;
}
@Override
public String toString() {
return left.toString() + SEPERATOR + op.toString() + SEPERATOR + right.toString();
}
}
请问这句String[] tokens = s.split(SEPERATOR);里的SEPERATOR有什么用? 展开
import com.project.base.ArithmeticExperssion;
import com.project.base.InvalidTokenException;
import com.project.base.Operator;
import com.project.base.OperatorNotFoundException;
import com.project.base.WrongNumberOfTokensException;
public class IntArithmaticExperession implements ArithmeticExperssion {
private IntOperand left;
private IntOperand right;
private Operator op;
@Override
public String eval(String s) throws OperatorNotFoundException, WrongNumberOfTokensException, InvalidTokenException {
// TODO 自动生成的方法存根
String[] tokens = s.split(SEPERATOR);
if(tokens.length != 3) {
throw new WrongNumberOfTokensException();
}
left = new IntOperand(tokens[0]);
if(tokens[1].length() != 1) {
throw new InvalidTokenException();
}
op = new Operator(tokens[1].charAt(0));
right = new IntOperand(tokens[2]);
return String.valueOf(calc());
}
private int calc() throws OperatorNotFoundException {
int result = 0;
switch(op.getSymbol()) {
case PLUS:
result = left.getOperand() + right.getOperand();
break;
case MINUS:
result = left.getOperand() - right.getOperand();
break;
case MUTIPLY:
result = left.getOperand() * right.getOperand();
break;
case DIVIDE:
result = left.getOperand() / right.getOperand();
break;
default:
throw new OperatorNotFoundException(op);
}
return result;
}
@Override
public String toString() {
return left.toString() + SEPERATOR + op.toString() + SEPERATOR + right.toString();
}
}
请问这句String[] tokens = s.split(SEPERATOR);里的SEPERATOR有什么用? 展开
2个回答
展开全部
就String这个类来说,split()方法的参数就是将一个字符串按照SEPERATOR指定的字符或正则表达式且分成多个字符串组成的一个字符串数组。
比如SEPERATOR为冒号":"时,"boo:and:foo" -->{ "boo", "and", "foo" },
看你这个程序,大概是实现了加减乘除四则运算吧,你跟踪进去看一下,应该SEPERATOR是一个空格吧,否则没法区分运算符和操作数。有问题再补充啊
比如SEPERATOR为冒号":"时,"boo:and:foo" -->{ "boo", "and", "foo" },
看你这个程序,大概是实现了加减乘除四则运算吧,你跟踪进去看一下,应该SEPERATOR是一个空格吧,否则没法区分运算符和操作数。有问题再补充啊
更多追问追答
追问
再请问一下这句left = new IntOperand(tokens[0]);是什么意思?
追答
IntOperand你没贴出来,看名字应该是一个整型操作数吧,比如1+2里的1
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询