java用户界面项目_结对项目(带图型用户界面)Java实现【柴政-陈起廷】
對分數及整數的計算
/***
* 相加操作
*/
ADD("+") {
@Override
public String calculate(String a, String b) {
boolean flagA = a.contains("/");
boolean flagB = b.contains("/");
//兩個都是分數
if (flagA && flagB) {
int[] anInt = ResolveUtil.analysis(a);
int[] bnInt = ResolveUtil.analysis(b);
//以AB為分母
int denominator = anInt[1] * bnInt[1];
//相加后的分子
int molecule = anInt[0] * bnInt[1] + anInt[1] * bnInt[0];
return ResolveUtil.createFraction(molecule, denominator);
} else if (flagA) {
int[] anInt = ResolveUtil.analysis(a);
//直接更新分子便可
anInt[0] += Integer.parseInt(b) * anInt[1];
return ResolveUtil.createFraction(anInt[0], anInt[1]);
} else if (flagB) {
int[] bnInt = ResolveUtil.analysis(b);
//直接更新分子便可
bnInt[0] += Integer.parseInt(a) * bnInt[1];
return ResolveUtil.createFraction(bnInt[0], bnInt[1]);
} else {
return String.valueOf(Integer.parseInt(a) + Integer.parseInt(b));
}
}
},
/***
* 相乘操作
*/
MULTIPLY("×") {
@Override
public String calculate(String a, String b) {
boolean flagA = a.contains("/");
boolean flagB = b.contains("/");
if (flagA && flagB) {
int[] anInt = ResolveUtil.analysis(a);
int[] bnInt = ResolveUtil.analysis(b);
//以AB為分母
int denominator = anInt[1] * bnInt[1];
//分子相乘
int molecule = anInt[0] * bnInt[0];
return ResolveUtil.createFraction(molecule, denominator);
} else if (flagA) {
int[] anInt = ResolveUtil.analysis(a);
return ResolveUtil.createFraction(anInt[0] * Integer.parseInt(b), anInt[1]);
} else if (flagB) {
int[] bnInt = ResolveUtil.analysis(b);
return ResolveUtil.createFraction(bnInt[0] * Integer.parseInt(a), bnInt[1]);
} else {
return String.valueOf(Integer.parseInt(a) * Integer.parseInt(b));
}
}
},
/***
* 相除操作
*/
DIVIDE("÷") {
@Override
public String calculate(String a, String b) {
//除法,從另外一種角度來說,是乘法的倒轉,所以,只需要把b分子分母倒過來用乘法就行了
boolean flag = b.contains("/");
//新的數b的字符串
String newB;
//判斷b是否為分數
if (flag) {
int[] bnInt = ResolveUtil.analysis(b);
newB = ResolveUtil.createFraction(bnInt[1], bnInt[0]);
} else {
newB = 1 + "/" + b;
}
return Symbol.MULTIPLY.calculate(a, newB);
}
},
/***
* 相減操作
*/
SUB("-") {
@Override
public String calculate(String a, String b) {
//減是加的特例,把b弄成-就可以了
return Symbol.ADD.calculate(a, "-" + b);
}
},
中綴轉后綴表達式
/***
* 中綴表達式轉換成后綴表達式
* @param expression 表達式
* @return 數組
*/
private String[] middleToAfter(String expression) {
//用來轉換的棧
Stack stack = new Stack<>();
//表達式每個字符前后都會生成一個空格
String[] strings = expression.split("\\s");
//返回的list
List stringList = new ArrayList<>(strings.length);
for (int index = 0; index < strings.length; index++) {
if ('0' <= strings[index].charAt(0) && strings[index].charAt(0) <= '9') {
//數字直接輸出
stringList.add(strings[index]);
} else if (strings[index].equals(Symbol.BEGIN.getSymbol())) {
//開始括號壓進棧
stack.push(strings[index]);
} else if (strings[index].equals(Symbol.END.getSymbol())) {
//把所有運算符都出棧
while (!stack.peek().equals(Symbol.BEGIN.getSymbol())) {
stringList.add(stack.pop());
}
//出棧開始括號
stack.pop();
} else if (strings[index].equals(Symbol.MULTIPLY.getSymbol())
|| strings[index].equals(Symbol.DIVIDE.getSymbol())) {
//判斷上一級符號是什么
boolean flag = !stack.isEmpty() && (stack.peek().equals(Symbol.MULTIPLY.getSymbol())
|| stack.peek().equals(Symbol.DIVIDE.getSymbol()));
if (flag) {
stringList.add(stack.pop());
}
stack.push(strings[index]);
} else if (strings[index].equals(Symbol.SUB.getSymbol())
|| strings[index].equals(Symbol.ADD.getSymbol())) {
//此處應該為+,-號
boolean flag = !stack.isEmpty() && (stack.peek().equals(Symbol.ADD.getSymbol())
|| stack.peek().equals(Symbol.SUB.getSymbol()));
if (flag) {
stringList.add(stack.pop());
}
stack.push(strings[index]);
} else {
//有其他符號,直接跳出,可能是=號
break;
}
}
while (!stack.isEmpty()) {
stringList.add(stack.pop());
}
//返回數組
return stringList.toArray(new String[0]);
}
后綴計算
/***
* 計算表達式
* @param expression 表達式
* @param permit 允許存在負數的運算過程
* @return 結果
*/
public String calculate(String expression, boolean permit) {
if (expression == null) {
return null;
}
//先生成后綴表達式數組,然后手動控制數組進行操作
String[] afterExp = middleToAfter(expression);
Stack stack = new Stack<>();
try {
for (int index = 0; index < afterExp.length; index++) {
if (afterExp[index].matches("[0-9/']+")) {
stack.push(afterExp[index]);
} else {
String b = stack.pop();
String a = stack.pop();
String result = Symbol.value(afterExp[index]).calculate(a, b);
//計算過程中存在負數,重新生成表達式
if (result.startsWith("-") && !permit) {
return null;
}
stack.push(result);
}
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("存在表達式不合法");
}
return stack.pop();
}
生成表達式
/***
* 生成表達式
* @return 生成表達式
*/
private String generateExpression() {
//隨機運算符大小
int operatorSize = (int) (Math.random() * MAX_OPERATOR_SIZE) + 1;
int numberSize = operatorSize + 1;
//判斷是否需要生成括號,1/4的概率
boolean flag = (int) (Math.random() * MAX_OPERATOR_SIZE) == 0;
//標記(產生的位置)
int mark = -1;
if (flag) {
//隨機插入括號的位置
mark = (int) (Math.random() * operatorSize);
}
StringBuilder expression = new StringBuilder();
//遍歷產生數字和符號,你一下我一下
for (int i = 0; i < numberSize; i++) {
if (mark == i) {
myAppend(expression, "(");
}
//生成數字
myAppend(expression, (int) (Math.random() * 2) == 0 ? generateFraction() : generateInt());
//判斷是否加入結束符號,判斷是否結尾
if (mark >= 0 && mark < i) {
//已經到了表達式結尾, 此時必須結束
if (i == operatorSize) {
myAppend(expression, ")");
break;
}
//判斷是否需要結束
flag = (int) (Math.random() * 2) == 0;
if (flag) {
myAppend(expression, ")");
mark = -1;
}
}
if (i < operatorSize) {
//然后生成一個操作符
myAppend(expression, generateOperator());
}
}
//最后補充等號
expression.append("=");
return expression.toString();
}
去重校驗
/***
* 檢查表達式是否已經存在或者重復
* @param expression 表達式
* @param result 結果
* @return 是否重復
*/
private boolean checkExpressionExistAndResultIllegal(String expression, String result) {
if (Objects.isNull(result)) {
return true;
}
//當前沒有表達式
if (nowExpressionSize == 0) {
return false;
}
//API的一些操作也是循環,效率低下,手動循環
for (int i = 0, j = nowExpressionSize - 1; i <= j; i++, j--) {
if (expressionList.get(i).equals(expression) || expressionList.get(j).equals(expression)) {
return true;
}
//查看是否答案有相同的
if (answerList.get(i).equals(result)) {
return checkCharEquals(expressionList.get(i), expression);
} else if (answerList.get(j).equals(result)) {
return checkCharEquals(expressionList.get(j), expression);
}
}
return false;
}
/***
*
* @param oldExpression 存在的表達式
* @param newExpression 還沒存進去的表達式
* @return 相同,不相同
*/
private boolean checkCharEquals(String oldExpression, String newExpression) {
String[] oldExpressionArrays = oldExpression.split("\\s+");
String[] newExpressionArrays = newExpression.split("\\s+");
int oldExpressionNumber = 0;
int equalsNumber = 0;
//是否為數字
boolean flag;
//開始遍歷
for (String oldString : oldExpressionArrays) {
flag = oldExpression.matches("[0-9'/]+");
if (flag) {
oldExpressionNumber++;
}
//比對
for (String newString : newExpressionArrays) {
if (oldString.equals(newString)) {
equalsNumber++;
}
}
}
答案校對
//保存正確/錯誤題號的隊列
private List correctList = new ArrayList<>();
private List wrongList = new ArrayList<>();
/**
* 校驗待檢測文件狀態
* @param exersicesFile
* @param myAnswer
* @Author Naren
*/
public void checkFile(String exersicesFile,String myAnswer) {
//存儲表達式的文件
File expFile = new File(exersicesFile);//Exercise002.txt
//待校驗答案的文件
File myAnsFile = new File(myAnswer);//myAnswers001.txt
//待校驗答案文件不存在
if(!myAnsFile.exists()){
//System.out.println("未找到待檢驗答案文件。");
new Tips().displayTips("noExpTip.png");
return;
}
//如果表達式文件不存在
if(!expFile.exists()) {
//System.out.println("未找到指定題目文件。");
new Tips().displayTips("noAnsTip.png");
return;
}
//如果全部文件名都正確,檢測待校對題目文件是否存在于系統生成歷史中
String id = exersicesFile.substring(9,12);
String sysAnsFile = "Answers" + id + ".txt"; //Myapp.exe -e Exercises001.txt -a myAnswers001.txt
File ansFile = new File(sysAnsFile);//Answers002.txt(不存在)
//若系統中不存在與題目文件相符合的答案文件
if(!ansFile.exists()){
try {
FileReader fr = new FileReader(expFile);
BufferedReader br = new BufferedReader(fr);
String content = null;
ArrayList questionList = new ArrayList<>();
while((content = br.readLine()) != null){//(?m)^.*$
content = content.split("\\.")[1];
questionList.add(content);
}
//調用起廷方法獲得答案隊列
Expression ex = new Expression(new Calculate());
List answerList = ex.getCorrectAnswerList(questionList);
//比對
checkAnswer(myAnsFile,answerList);
} catch (Exception e) {
System.out.println("Class:AnswerChecking,Method:checkFile(String,String) is wrong!");
}
}else{
//調用本類檢驗方法比對答案文件
checkAnswer(myAnsFile,ansFile);
}
}
/**
* 將待校驗答案文件與現場計算出的答案隊列進行比對
* @param myAnswer
* @param answerList
* @author Naren
*/
private void checkAnswer(File myAnswer, List answerList){
try {
//待檢測答案文件
FileReader fr1 = new FileReader(myAnswer);
BufferedReader br1 = new BufferedReader(fr1);
LinkedHashMap map1 = new LinkedHashMap<>();
String content = "";
while((content = br1.readLine()) != null){
content = content.replaceAll(" +", "").replaceAll("\uFEFF", "");
//map1待校驗答案:key:序號,value:答案
map1.put(Integer.valueOf(content.split("\\.")[0]),content.split("\\.")[1]);
}
//開始比對
for (int i = 0; i < answerList.size(); i++) {
if(map1.containsKey(i + 1)){
if(map1.get(i + 1).equals(answerList.get(i))) {
correctList.add(i + 1);//正確題號添加進隊列
}
else{
wrongList.add(i + 1);//錯誤題號添加進隊列
}
}else{
//漏寫
wrongList.add(i + 1);
}
}
//將校驗結果寫入文件
//System.out.println("檢驗信息已寫入Grade.txt文件。");
new Tips().displayTips("checkSuccess.png");
new DataStorage().storeCheckInfo(correctList,wrongList);
} catch (Exception e) {
System.out.println("Class:AnswerChecking,Method:checkAnswer(File,List) is wrong!");
}
}
/**
* 【重載】將待校驗答案文件與本地答案文件進行比對
* @param myAnswer 待校驗答案文件
* @param ansFile 正確答案文件
* @author Naren
*/
private void checkAnswer(File myAnswer, File ansFile) {
try {
FileReader fr1 = new FileReader(myAnswer);
FileReader fr2 = new FileReader(ansFile);
BufferedReader br1 = new BufferedReader(fr1);
BufferedReader br2 = new BufferedReader(fr2);
LinkedHashMap map1 = new LinkedHashMap<>();//待檢測 key:序號,value:答案
LinkedHashMap map2 = new LinkedHashMap<>();//正 確 key:序號,value:答案
//分別按行讀出答案
String content = "";
while((content = br1.readLine()) != null){
content = content.replaceAll(" +", "").replaceAll("\uFEFF", "");
map1.put(Integer.valueOf(content.split("\\.")[0]),content.split("\\.")[1]);
}
while((content = br2.readLine()) != null){
content = content.replaceAll(" +", "").replaceAll("\uFEFF", "");
map2.put(Integer.valueOf(content.split("\\.")[0]),content.split("\\.")[1]);
}
//開始比對
for (int i = 0; i < map2.size(); i++) {
if(map1.containsKey(i + 1)){
if(map1.get(i + 1).equals(map2.get(i + 1))) {
correctList.add(i + 1);//正確題號添加進隊列
}
else{
wrongList.add(i + 1);//錯誤題號添加進隊列
}
}else{
//漏寫
wrongList.add(i + 1);
}
}
//將校驗結果寫入文件
//System.out.println("檢驗信息已寫入Grade.txt文件。");
new Tips().displayTips("checkSuccess.png");
new DataStorage().storeCheckInfo(correctList,wrongList);
} catch (Exception e) {
System.out.println("Class:AnswerChecking,Method:checkAnswer(File,File) is wrong!");
}
}
總結
以上是生活随笔為你收集整理的java用户界面项目_结对项目(带图型用户界面)Java实现【柴政-陈起廷】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux远程计算,如何使用Linux应
- 下一篇: c++ mysql 配置文件_C++操作