Skip to content

Commit

Permalink
Merge pull request #101 from yzhanglp/master
Browse files Browse the repository at this point in the history
add the explanation and other function, round the answer up to 3 decimal
  • Loading branch information
yzhanglp authored Apr 12, 2024
2 parents 46de330 + 0e0f1e5 commit 420e7cc
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 88 deletions.
16 changes: 16 additions & 0 deletions recordList.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,19 @@
2024-04-10 16:08:33 0.29411764705882354 0.4 1784552838 36+17,53.0 19+13,32.0 49/2,24.5 28+16,44.0 76+32,108.0 25*22,550.0 34+42,76.0 28-94,-66.0 0-72,-72.0 96/7,13.714285714285714
2024-04-10 16:19:08 0.20833333333333334 0.3 1784552838 53/6,8.833333333333334 1-71,-70.0 78-57,21.0 75-54,21.0 15/2,7.5 6+59,65.0 57+81,138.0 56+2,58.0 7-49,-42.0 33+87,120.0
2024-04-12 01:10:44 75.0 0.0 198423881 27/8,3.375 66+27,93.0 78+22,100.0 70-55,15.0 13*28,364.0 18+25,43.0 95-12,83.0 62-8,54.0 32+63,95.0 40-51,-11.0
2024-04-12 19:24:36 13.043478260869565 0.3 866946869 auto-generated 53+97,150.0 49+61,110.0 96+25,121.0 85+49,134.0 89+47,136.0 69+26,95.0 35+81,116.0 42+10,52.0 52+75,127.0 62+97,159.0
2024-04-12 19:34:18 17.647058823529413 0.0 -1373810849 auto-generated 968+304+865+4,2141.0 835+98+418+788,2139.0 827+771+487+344,2429.0 821+777+111+867,2576.0 835+998+587+909,3329.0
2024-04-12 19:41:45 14.285714285714285 0.0 -1373810849 auto-generated 747+52+919+275,1993.0 926+646+879+360,2811.0 711+534+426+205,1876.0 880+902+582+478,2842.0 733+691+735+486,2645.0
2024-04-12 19:42:28 30.0 0.0 2112549518 auto-generated 620+31+604+689+760,2704.0 499+280+887+838+908,3412.0 641+298+367+50+445,1801.0 39+221+523+808+283,1874.0 659+724+717+604+442,3146.0
2024-04-12 19:44:44 12.0 0.0 1172738166 auto-generated 458*578*175*214+956,9.913914756E9 739+391*66*576+409,1.4865404E7 616*462+514*677+278,632848.0
2024-04-12 19:51:06 3.103448275862069 0.0 -1383972449 auto-generated 73+12/6/3,73.66666666666667 95/2/4+52,63.875 76+53/9/5,77.17777777777778
2024-04-12 19:54:39 18.0 0.0 -2011367393 auto-generated 52/8/4/8,0.203125 33/4/1/9,0.9166666666666666 97+20/1/1,117.0
2024-04-12 20:04:45 6.428571428571428 0.0 -2011367393 auto-generated 71+3+88/8,85.0 79+5+36+41,161.0 47+97/9+46,103.77777777777777
2024-04-12 20:06:26 16.363636363636363 0.0 -2011367393 auto-generated 30+53/5/4,32.65 20+73+42/6,100.0 18+21+73+45,157.0
2024-04-12 20:09:30 18.461538461538463 0.0 1410862850 auto-generated 570/2/4/1,71.25 820+654+699+566,2739.0 771/7+113+759,982.1428571428571 862+671/4/8,882.96875
2024-04-12 20:10:12 22.5 0.0 1585670199 auto-generated 997+851+777/5,2003.4 478+937+475+340,2230.0 376/2/8+160,183.5
2024-04-12 20:10:40 15.0 0.0 -2096538833 auto-generated 30/9/8/2,0.20833333333333334 0/5/3/8,0.0 29+80/9+78,115.88888888888889
2024-04-12 20:20:33 16.363636363636363 0.0 -2011367393 auto-generated 51+55+5/2,108.5 82+3+64+15,164.0 32+73+52+72,229.0
2024-04-12 20:21:49 25.71428571428571 0.0 -1383972449 auto-generated 2+57/4/7,4.036 36/6/8+0,0.75 53+73/7/3,56.476
2024-04-12 20:22:33 18.0 0.0 -2011367393 auto-generated 5/4/3+42,42.417 72/8/2/4,1.125 40+21/7/9,40.333
2024-04-12 20:24:09 18.0 0.0 -2011367393 auto-generated 13+58+76/3,96.333 29/5/1/7,0.829 6+24/1+44,74.0
70 changes: 68 additions & 2 deletions src/main/java/seedu/duke/Calculator.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package seedu.duke;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

import static java.lang.Character.isDigit;

public class Calculator {

private static List<String> explanations = new ArrayList<>();

public static double calculate(StringBuilder sb) {
public double calculate(StringBuilder sb) {
Stack<Double> numStack = new Stack<>();
Stack<String> opStack = new Stack<>();

Expand All @@ -21,11 +24,62 @@ public static double calculate(StringBuilder sb) {
double num2 = numStack.pop();
double num1 = numStack.pop();
double result = calculateTwo(num1, num2, (String) object);
explanations.add(getExplanation(num1, num2, (String) object, result));
numStack.push(result);
}
}
assert numStack.size() == 1 : "wrong formula";
return numStack.pop();
// round to 3 decimal places
Double result = Math.round(numStack.peek() * 1000.0) / 1000.0;
return result;
}

private static String getExplanation(double num1, double num2, String op, double answer) {
String Start = "The computation of the problem: "+ String.valueOf(num1) + " " + op + " " + String.valueOf(num2) + " = " + String.valueOf(answer) + "\n\n";
List<String> explanation = new ArrayList<>();
StringBuilder builder = new StringBuilder();
String alignedProblem = "";
if (op.equals("/")){
alignedProblem = "The division of " + num1 + " and " + num2 + " is " + answer + "\n";
}
else {
String firstString = String.valueOf(num1);
String secondString = String.valueOf(num2);
String firstIntergerPart = firstString.split("\\.")[0];
String secondIntergerPart = secondString.split("\\.")[0];
if (firstIntergerPart.length() < secondIntergerPart.length()) {
String temp = firstString;
firstString = secondString;
secondString = temp;
}
if(op.equals("*")){
op = "x";
}
// align the problem to the . place
String firstDecimalPart = firstString.split("\\.")[1];
String secondDecimalPart = secondString.split("\\.")[1];

if (firstDecimalPart.length() < secondDecimalPart.length()) {
firstString = firstString + new String(new char[secondDecimalPart.length() - firstDecimalPart.length()]).replace("\0", "0");
} else {
secondString = secondString + new String(new char[firstDecimalPart.length() - secondDecimalPart.length()]).replace("\0", "0");
}

explanation.add(firstString.trim());
explanation.add(op + " " + secondString.trim());
explanation.add("---------------------------------------");
explanation.add(String.valueOf(answer));
for (String element : explanation) {
builder.append(String.format("%30s%n", element));
}
alignedProblem = builder.toString();





}
return Start + alignedProblem + "\n";
}

private static double calculateTwo(double num1, double num2, String op) {
Expand Down Expand Up @@ -132,4 +186,16 @@ private static ArrayList<Object> toFormula(StringBuilder sb) {
}
return formula;
}

public List<String> getExplanations() {
return explanations;
}

public String getExplanationsString() {
StringBuilder builder = new StringBuilder();
for (String explanation : explanations) {
builder.append(explanation);
}
return builder.toString();
}
}
89 changes: 16 additions & 73 deletions src/main/java/seedu/duke/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,85 +24,25 @@ public Checker(Test test) {
this.time = 0;
}

public static String getExplanation(Problem problem) {
String description = problem.getDescription();
String start = "Let's explain this problem by the following format: " + "\n\n";
List<String> explanation = new ArrayList<>();
StringBuilder builder = new StringBuilder();
if (description.contains("+")) {
String[] parser = description.split("\\+");
double firstNumber = Double.parseDouble(parser[0]);
double secondNumber = Double.parseDouble(parser[1]);
String answerString = String.valueOf(problem.getAnswer());
String firstString = String.valueOf(firstNumber);
String secondString = String.valueOf(secondNumber);
//Shift the longer number to the first
if (firstString.length() < secondString.length()) {
String temp = firstString;
firstString = secondString;
secondString = temp;
}
explanation.add(firstString);
explanation.add("+" + " " + secondString);

} else if (description.contains("-")) {
String[] parser = description.split("-");
double firstNumber = Double.parseDouble(parser[0]);
double secondNumber = Double.parseDouble(parser[1]);
String answerString = String.valueOf(problem.getAnswer());
String firstString = String.valueOf(firstNumber);
String secondString = String.valueOf(secondNumber);
//Shift the longer number to the first
if (firstString.length() < secondString.length()) {
String temp = firstString;
firstString = secondString;
secondString = temp;
}
explanation.add(firstString);
explanation.add("-" + " " + secondString);

} else if (description.contains("*")) {
String[] parser = description.split("\\*");
double firstNumber = Double.parseDouble(parser[0]);
double secondNumber = Double.parseDouble(parser[1]);
String answerString = String.valueOf(problem.getAnswer());
String firstString = String.valueOf(firstNumber);
String secondString = String.valueOf(secondNumber);
//Shift the longer number to the first
if (firstString.length() < secondString.length()) {
String temp = firstString;
firstString = secondString;
secondString = temp;
}
explanation.add(firstString);
explanation.add("X" + " " + secondString);
} else if (description.contains("/")) {
return "We do not support the explanation with division now!";
} else {
return "The format of the problem do not provide any explanation now!";
}
explanation.add("---------------------------------------");
explanation.add(String.valueOf(problem.getAnswer()));
for (String element : explanation) {
builder.append(String.format("%" + 20 + "s%n", element));
}
String alignedProblem = builder.toString();
String end = "You can compare your answer with the above function\n";
return start + alignedProblem + "\n" + end;
}
public static void showExplanation(Problem problem) {
String explanations = problem.getExplanations();
Ui ui = new Ui("");
ui.print("The explanation of the problem: " + problem.solved());
ui.print("Let us caculate it step by step:");
ui.print(explanations);

ui.print("From all the steps above, we can get the answer: " + problem.solved()+"\n");

public static void testExplanation() {
Problem testProblem = new Problem("99+9", 108);
System.out.println(getExplanation(testProblem));
}


Boolean checkCorrectness(Problem problem, double answer) {
return Math.abs(problem.getAnswer() - answer) < 0.01;
}

void getUserAnswer() {
long startTime = System.currentTimeMillis();
ui.print("Press Enter to start answering the questions...");
ui.print("Press Enter to start answering the questions, you can type \"exit\" to quit the test when answering the question...");
String userInput = ui.readCommand();

for (int i = 0; i < test.getNumber(); i++) {
Expand All @@ -112,16 +52,19 @@ void getUserAnswer() {
userAnswer[i] = userInput;
double answer = Double.NEGATIVE_INFINITY;
boolean isValid = false;
if (userInput.equals("exit")) {
ui.print("Exit the test! All the test not finished will be marked as wrong!");
break;
}
while (!isValid) {

try {
answer = Double.parseDouble(userInput);
isValid = true;
} catch (NumberFormatException e) {

ui.print("Invalid Input, please enter a number");
//wrongAnswer.add(userInput);
//wrongProblem.add(problem);
//continue;
ui.print(problem.unsolved());
userInput = ui.readCommand();

}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/seedu/duke/DIYProblemSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public void addDIYProblemSet(Ui ui) {
String description;
String correctAnswer;
double answer = 0.0;
String explanations = "";
String quit = "";
while (!quit.equals("y")) {
ui.print("input the description of the problem (e.g. 1+2*3): ");
Expand All @@ -26,7 +27,7 @@ public void addDIYProblemSet(Ui ui) {
} catch (NumberFormatException e) {
ui.print("Invalid answer! Please input a number.");
}
Problem problem = new Problem(description,answer);
Problem problem = new Problem(description,answer,explanations);
problemSet.add(problem);
ui.print("Do you finish adding problems? y/n: ");
quit = scanner.nextLine();
Expand All @@ -40,6 +41,7 @@ public void addDIYProblemSet(Ui ui) {
ui.print("\nSuccessfully save the DIY problem set!");
record.print(true);
ui.print("\n");
scanner.close();
}


Expand Down
11 changes: 7 additions & 4 deletions src/main/java/seedu/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,16 @@ public static void solveProbSet(Test test, Ui ui, boolean retry, int id) {

for (int i = 0; i < wrongProblem.size(); i++) {
Problem problem = wrongProblem.get(i);
ui.print("The "+ String.valueOf(i+1)+"th wrong answer of you: ");
ui.print("Your answer: " + problem.getDescription() + " = " + wrongAnswer.get(i));
ui.print("Correct Answer: " + problem.solved());
// need further implementation for 3 more operators
// ui.print("If you want to see the explanation, type exp or explanation, else just type enter");
// String userInput = ui.readCommand();
// if(userInput.equals("exp")||userInput.equals("explanation"))
// System.out.println(Checker.getExplanation(problem));
ui.print("If you want to see the explanation, type exp or explanation, else just type enter, type exit to stop showing the answer");
String userInput = ui.readCommand();
if(userInput.equals("exit"))
break;
if(userInput.equals("exp")||userInput.equals("explanation"))
Checker.showExplanation(problem);
}

// Storage write to file
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/seedu/duke/Problem.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package seedu.duke;

import java.util.List;

public class Problem {

private final String description;
private final double answer;
private String explanations;

public Problem(String description, double answer) {
public Problem(String description, double answer, String explanations2) {
this.description = description;
this.answer = answer;
this.explanations = explanations2;


}

public String solved() {
Expand All @@ -25,4 +31,8 @@ public double getAnswer() {
public String getDescription() {
return description;
}

public String getExplanations() {
return explanations;
}
}
10 changes: 7 additions & 3 deletions src/main/java/seedu/duke/ProblemGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static java.lang.Character.isDigit;

Expand Down Expand Up @@ -176,6 +177,7 @@ private Test generate(HashMap<String, String> parameter) {

StringBuilder descriptionBuilder = new StringBuilder();
double answer;
String explanations;
int max = (int) Math.pow(MAXIMUM_LENGTH, maxDigit);

for (int j = 0; j < length; j++) {
Expand All @@ -189,12 +191,14 @@ private Test generate(HashMap<String, String> parameter) {
}

descriptionBuilder = division_check(descriptionBuilder);

answer = Calculator.calculate(descriptionBuilder);
Calculator calculator = new Calculator();
answer = calculator.calculate(descriptionBuilder);
explanations = calculator.getExplanationsString();
String description = descriptionBuilder.toString();


Problem p = new Problem(description, answer);

Problem p = new Problem(description, answer, explanations);
System.out.println((i + 1) + ". " + p.unsolved());
test.addToTest(p);

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static void processLine(String line) throws Exception {

for (int i = minimumLength + 1; i < words.length; i++) {
String[] term = words[i].split(",");
probSet.add(new Problem(term[0], Double.parseDouble(term[1])));
probSet.add(new Problem(term[0], Double.parseDouble(term[1]), null));
}


Expand Down Expand Up @@ -139,6 +139,7 @@ public static void writeFile() {
writer.flush();
System.out.println("Record successfully saved!");
Ui.showLine();
writer.close();

} catch (IOException e) {
System.out.println("Error when saving record!");
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/seedu/duke/CheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ public void sampleTest() {

@org.junit.jupiter.api.Test
public void testCheckCorrectness() {
Problem problem = new Problem("2 + 2", 4.0);
Problem problem = new Problem("2 + 2", 4.0, null);
Checker checker = new Checker(new Test("+", 1, 1, 1));
boolean result = checker.checkCorrectness(problem, 4.0);
assertTrue(result, "Expected true for correct answer");
}

@org.junit.jupiter.api.Test
public void testCheckCorrectness_withIncorrectAnswer() {
Problem problem = new Problem("5 - 3", 2.0);
Problem problem = new Problem("5 - 3", 2.0, null);
Checker checker = new Checker(new Test("+", 1, 1, 1));
boolean result = checker.checkCorrectness(problem, 4.0);
assertFalse(result, "Expected false for incorrect answer");
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/seedu/duke/ProblemGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void calculateTest() {
formula.append(5);
formula.append("*");
formula.append(3);
assertEquals(Calculator.calculate(formula),85);
Calculator calculator = new Calculator();
assertEquals(calculator.calculate(formula),85);
}
}

0 comments on commit 420e7cc

Please sign in to comment.