Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions functions.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
x + sin(x)
x + sin(x)
2x^2 + 9x
3x^7 - cos(x)
3x^7 - cos(x)
2x^3 - 9
4x^2 + 6
3x^5 - 21
Expand All @@ -25,4 +25,26 @@ x^2 + 2x - 1
9x^4 - 8x
42x^5 + cos(x)
5x^3 + sin(x)
56x^3 - cos(x)
56x^3 - cos(x)
8x^3 + 3x^2 - 7x
12x^2 + 5x + 3
6x^5 - 4x^3
7x^4 + 2x^2 - 10
15x^3 + sin(x)
20x^2 - 7x + 4
x^3 + x^2 + x
11x^2 - cos(x)
4x^6 - 3x
2x^5 + 8x^3 - 1
9x^3 + 14x
33x^2 + sin(x)
x^4 - 6x^2 + 9
7x^3 - 5x + 2
18x^4 + cos(x)
50x^3 - 25x
6x^2 + 12x - 8
x^5 - sin(x)
14x^3 + 7x^2
8x^4 - cos(x)
3x^6 + 2x
100x^2 - 50x + 1
3 changes: 1 addition & 2 deletions src/BoardState.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static String[][] getGrid(String f, int numDerivs, int numOptions) {
continue;
}

if (correctDerivative == "0") {
if (correctDerivative.equals("0")) {
tempChoices.add(Integer.toString((int) (Math.random() * 9) + 1));
continue;
}
Expand All @@ -78,7 +78,6 @@ public static String[][] getGrid(String f, int numDerivs, int numOptions) {

}

System.out.println("this is temp choices:" + tempChoices);

List<String> choicesShuffle = new ArrayList<String>(tempChoices);
Collections.shuffle(choicesShuffle);
Expand Down
28 changes: 14 additions & 14 deletions src/Differentiate.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package src;

import java.util.*;
import javax.swing.tree.TreeModel;

/**
* Performs differentiating calculations.
Expand Down Expand Up @@ -38,31 +37,27 @@ public String differentiateString(String function) {

// "3x^2 + cos(x)" => ["3x^2", "+", "cos(x)"]
// "3x^2 - cos(x)" => ["3x^2", "-", "cos(x)"]
String[] terms = function.split(" ");
String[] terms = function.trim().split(" ");
String answer = "";
boolean uselessAndOnlyForConstAtBeginningCase = false;
boolean skipNextSign = false;
for (int i = 0; i < terms.length; i++) {

String termOrSign = terms[i];

System.out.println("this is termor sign: " + terms[i]);

// CORNER CASE: ONLY A CONSTANT
if (terms.length == 1 && terms[i].indexOf("x") < 0) {
System.out.println("this is terms[i]: " + terms[i]);
answer = "0";
break;
}

else if (uselessAndOnlyForConstAtBeginningCase) {
uselessAndOnlyForConstAtBeginningCase = false;
else if (skipNextSign) {
skipNextSign = false;
if ((termOrSign.equals("+") || termOrSign.equals("-"))) {
answer += termOrSign + " ";
}
continue;
}

System.out.println("this is termor sign: " + termOrSign);
if (!(termOrSign.equals("+") || termOrSign.equals("-"))) {
if (termOrSign.equals("x") && termOrSign.length() == 1) {
answer += "1 ";
Expand All @@ -73,7 +68,7 @@ else if (uselessAndOnlyForConstAtBeginningCase) {
if (!termOrSign.contains("x")) {
// 4 + x
if (answer.length() == 0) {
uselessAndOnlyForConstAtBeginningCase = true;
skipNextSign = true;
}
// x + 4 of x + 4 + x
else if (answer.length() > 0) {
Expand Down Expand Up @@ -103,12 +98,18 @@ else if (answer.length() > 0) {
}

int derivCoefficient = 1;
if (!derivativeTerm.substring(0, 1).equals("x")) {
derivCoefficient = Integer.parseInt(derivativeTerm.substring(0, 1));
// Parse multi-digit derivative coefficients
int derivCoeffEnd = 0;
while (derivCoeffEnd < derivativeTerm.length()
&& Character.isDigit(derivativeTerm.charAt(derivCoeffEnd))) {
derivCoeffEnd++;
}
if (derivCoeffEnd > 0) {
derivCoefficient = Integer.parseInt(derivativeTerm.substring(0, derivCoeffEnd));
}

String finalCoefficient = (derivCoefficient * preCoefficient) + "";
String finalExpression = finalCoefficient + derivativeTerm.substring(1);
String finalExpression = finalCoefficient + derivativeTerm.substring(derivCoeffEnd);
answer += finalExpression + " ";
}

Expand Down Expand Up @@ -216,7 +217,6 @@ public String[] correctAnswers(String function, int numOrders) {
public static void main(String[] args) {
Differentiate diff = new Differentiate();
System.out.println(diff.differentiateString("15120 - sin(x)"));
System.out.println("damn memer omment".hashCode());
}

}
Loading