package buildpoly;
import computepoly.Simplify;
import factor.Cons;
import factor.Power;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class PolyChecker {
private Stack<String> opStack = new Stack<>();
private String blankChecker(String line) {
String wrong1 = ".*((\\d+\\s+\\d+)|" +
"([+\\-]\\s*[+\\-]\\s*[+\\-]\\s+\\d+)|" +
"(\\*\\s*[+\\-]\\s+\\d+)|" +
"(\\^\\s*[+\\-]\\s+\\d+)|" +
"(s\\s+i\\s*n)|" +
"(s\\s*i\\s+n)|" +
"(c\\s+o\\s*s)|" +
"(c\\s*o\\s+s)).*";
boolean isLegal1 = Pattern.matches(wrong1, line);
String newLine;
newLine = line.replaceAll("[ \t]", "");
String wrong2 = "\\s";
boolean isLegal2 = Pattern.matches(wrong2, newLine);
boolean isLegal3 = newLine.isEmpty();
Error.formatError(isLegal1 | isLegal2 | isLegal3);
return newLine;
}
private void charChecker(String line) {
String wrong = ".*[^0-9xcosin*+\\-()^]";
Error.formatError(Pattern.matches(wrong, line));
}
private void indexNegateChecker(String line) {
boolean isLegal = line.contains("^-");
Error.formatError(isLegal);
}
private void opChecker(String line) {
String wrong1 = ".*([+\\-]{3,}[^0-9]).*";
boolean isLegal1 = Pattern.matches(wrong1, line);
String wrong2 = ".*([+\\-]{4,}).*";
boolean isLegal2 = Pattern.matches(wrong2, line);
String wrong3 = ".*(\\d[xcs(]).*";
boolean isLegal3 = Pattern.matches(wrong3, line);
Error.formatError(isLegal1 | isLegal2 | isLegal3);
}
private ArrayList<PolyTree> termChecker(String line) {
ArrayList<PolyTree> poly = new ArrayList<>();
String str;
if (line.charAt(0) == '+') {
str = line.substring(1);
} else if (line.charAt(0) == '-') {
str = "0-" + line.substring(1);
} else {
str = line;
}
str = str.replaceAll("\\*", "*#");
str = str.replaceAll("#\\(", "(");
int i = 0;
while (i < str.length()) {
PolyTree newNode = new PolyTree();
switch (str.charAt(i)) {
case '+':
newNode.setOp(Operate.ADD);
i++;
break;
case '-':
newNode.setOp(Operate.SUB);
i++;
break;
case '*':
newNode.setOp(Operate.MUL);
i++;
break;
case '(':
newNode.setOp(Operate.LEFT);
opStack.push(Operate.LEFT);
i++;
break;
default:
if (str.charAt(i) == '#') { i++; }
String newPoly = str.substring(i);
Object[] obj = addPoly(newPoly, i);
newNode = (PolyTree) obj[0];
i = (Integer) obj[1];
}
poly.add(newNode);
}
Error.formatError(!opStack.empty());
return poly;
}
private Object[] addPoly(String str, int oldI) {
Pattern co = Pattern.compile("^([+\\-]?\\d+)");
Matcher cons = co.matcher(str);
Pattern pow = Pattern.compile("^(x(\\^(\\+)?\\d+)?)");
Matcher power = pow.matcher(str);
Pattern sinLe = Pattern.compile("^sin\\(");
Matcher sinLeft = sinLe.matcher(str);
Pattern cosLe = Pattern.compile("^cos\\(");
Matcher cosLeft = cosLe.matcher(str);
Pattern rig = Pattern.compile("^(\\)(\\^[+-]?\\d+)?)");
Matcher right = rig.matcher(str);
PolyTree newNode = new PolyTree();
int i = oldI;
if (cons.find()) {
newNode.setOp(Operate.NOT);
Cons acons = new Cons(new BigInteger(cons.group()));
newNode.setFactor(acons);
i += cons.end();
} else if (power.find()) {
newNode.setOp(Operate.NOT);
if (power.group().length() == 1) {
Power apower = new Power(BigInteger.ONE);
newNode.setFactor(apower);
} else {
newNode = splitCaret(power, newNode);
}
i += power.group().length();
} else if (sinLeft.find()) {
newNode.setOp(Operate.SIN);
opStack.push(Operate.SIN);
i += sinLeft.end();
} else if (cosLeft.find()) {
newNode.setOp(Operate.COS);
opStack.push(Operate.COS);
i += cosLeft.end();
} else if (right.find()) {
if (right.group().length() == 1) {
newNode.setOp(Operate.RIGHT);
opStack.pop();
Power apower = new Power(BigInteger.valueOf(1));
newNode.setFactor(apower);
} else {
Error.formatError(opStack.pop().equals(Operate.LEFT));
newNode.setOp(Operate.RIGHT);
newNode = splitCaret(right, newNode);
}
i += right.group().length();
} else {
Error.formatError(true);
}
return new Object[]{newNode, i};
}
private PolyTree splitCaret(Matcher matcher, PolyTree newNode) {
String ind = matcher.group().substring(2);
int index = Integer.parseInt(ind);
Error.formatError(index <= 0 |
index > 10000);
Power apower = new Power(BigInteger.valueOf(index));
newNode.setFactor(apower);
return newNode;
}
private void factorChecker(String line) {
String wrong1 = ".*(sin\\(\\d+[+\\-*].*\\)).*";
boolean isLegal1 = Pattern.matches(wrong1, line);
String wrong2 = ".*(cos\\(\\d+[+\\-*].*\\)).*";
boolean isLegal2 = Pattern.matches(wrong2, line);
String wrong3 = ".*(sin\\([+\\-][^0-9].*\\)).*";
boolean isLegal3 = Pattern.matches(wrong3, line);
String wrong4 = ".*(cos\\([+\\-][^0-9].*\\)).*";
boolean isLegal4 = Pattern.matches(wrong4, line);
Error.formatError(isLegal1 | isLegal2 | isLegal3 | isLegal4);
Pattern tri1 = Pattern.compile("^((sin)|(cos))\\(\\(.+\\)\\).*");
Matcher triMat1;
Pattern tri2 = Pattern.compile("^((sin)|(cos))\\([+\\-]?\\d+\\).*");
Matcher triMat2;
Pattern tri3 =
Pattern.compile("^((sin)|(cos))\\(x(\\^(\\+)?\\d+)?\\).*");
Matcher triMat3;
Pattern tri4 =
Pattern.compile("^((sin)|(cos))\\(((sin)|(cos)).*\\).*");
Matcher triMat4;
String str;
for (int i = 0; i < line.length(); i++) {
if ((line.charAt(i) == 's' || line.charAt(i) == 'c') &&
i < line.length() - 1) {
if (line.charAt(i + 1) == 'i' || line.charAt(i) == 'o') {
str = line.substring(i);
triMat1 = tri1.matcher(str);
triMat2 = tri2.matcher(str);
triMat3 = tri3.matcher(str);
triMat4 = tri4.matcher(str);
isLegal1 = triMat1.find();
isLegal2 = triMat2.find();
isLegal3 = triMat3.find();
isLegal4 = triMat4.find();
boolean isLegal = isLegal1 | isLegal2 | isLegal3 | isLegal4;
Error.formatError(!isLegal);
}
}
}
}
public ArrayList<PolyTree> fomatCheck(String str) {
Error.formatError(str.isEmpty());
String newStr = blan
没有合适的资源?快使用搜索试试~ 我知道了~
Java_OO作业:包含简单幂函数和简单正余弦函数的复合导函数的求解
共15个文件
java:14个
md:1个
需积分: 10 14 下载量 10 浏览量
2019-04-01
19:47:02
上传
评论 3
收藏 13KB RAR 举报
温馨提示
输入中,包含且仅包含一行,表示一个表达式 关于输出,首先程序需要对输入数据的合法性进行判定 - 如果是一组合法的输入数据(即符合上述的表达式基本规则),则应当输出一行,表示求出的导函数。格式同样需要满足上述的表达式基本格式规则。 - 如果是一组不合法的输入数据,则应当输出一行`WRONG FORMAT!`
资源推荐
资源详情
资源评论
收起资源包目录
OO.rar (15个子文件)
src
buildpoly
PolyTree.java 1KB
PolyChecker.java 8KB
Error.java 213B
PolyBuilder.java 5KB
Operate.java 489B
factor
Sin.java 572B
Factor.java 416B
Cons.java 493B
Cos.java 581B
Power.java 746B
computepoly
PrintPoly.java 2KB
Simplify.java 1KB
DiffPoly.java 4KB
Main.java 856B
Unit1 Differential Homework3.md 18KB
共 15 条
- 1
资源评论
debugzhang
- 粉丝: 10
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功