Hi all,
I want to create a program to show the graph of an mathematikal formula but I don't know how I can get an input string into a formula java can use to calculate.
Has anyone an idea how I could do this?


private String process(String str) { // some code if (someCondition) process(str2) // some code }
public class Parser { /** * Parses simple equations such as: * "2*3 + 3*4" * * ~Duta */ public static int parseEquation(String equation) { // Remove any spaces. for(int i = 0; i < equation.length(); i++) { if(equation.charAt(i) == ' ') { String leftSide = ""; String rightSide = ""; for(int i2 = 0; i2 < i; i2++) { leftSide += equation.charAt(i2); } for(int i2 = i + 1; i2 < equation.length(); i2++) { rightSide += equation.charAt(i2); } equation = leftSide + rightSide; i--; } } // Parse the equation. if(equation.length() == 0) { return 0; } return parseAdditive(equation); } private static int parseAdditive(String equation) { // Try to pattern match it to: // multiplicative + additive // If that fails, match it to: // multiplicative int plusIndex = equation.indexOf('+'); if(plusIndex != -1) { String leftSide = ""; String rightSide = ""; for(int i2 = 0; i2 < plusIndex; i2++) { leftSide += equation.charAt(i2); } for(int i2 = plusIndex + 1; i2 < equation.length(); i2++) { rightSide += equation.charAt(i2); } return parseMultiplicative(leftSide) + parseAdditive(rightSide); } else { return parseMultiplicative(equation); } } private static int parseMultiplicative(String equation) { // Try to pattern match it to: // integer * multiplicative // If that fails, match it to: // integer int timesIndex = equation.indexOf('*'); if(timesIndex != -1) { String leftSide = ""; String rightSide = ""; for(int i2 = 0; i2 < timesIndex; i2++) { leftSide += equation.charAt(i2); } for(int i2 = timesIndex + 1; i2 < equation.length(); i2++) { rightSide += equation.charAt(i2); } return parseInteger(leftSide) * parseMultiplicative(rightSide); } else { return parseInteger(equation); } } private static int parseInteger(String equation) { return Integer.parseInt(equation); } }
public class Parser { /** * Parses simple equations such as: * "(2 + (3 + 3)*4)/-0.5" * (to produce -52) * * ~Duta */ public static double parseEquation(String equation) { // Remove any spaces. for(int i = 0; i < equation.length(); i++) { if(equation.charAt(i) == ' ') { String leftSide = ""; String rightSide = ""; for(int i2 = 0; i2 < i; i2++) { leftSide += equation.charAt(i2); } for(int i2 = i + 1; i2 < equation.length(); i2++) { rightSide += equation.charAt(i2); } equation = leftSide + rightSide; i--; } } // Parse the equation. if(equation.length() == 0) { return 0; } return parseAdd(equation); } private static double parseAdd(String equation) { // Try to pattern match it to: // multiplicative + additive // If that fails, match it to: // multiplicative int plusIndex = getIndex(equation, '+'); if(plusIndex != -1) { String leftSide = equation.substring(0, plusIndex); String rightSide = equation.substring(plusIndex + 1, equation.length()); return parseTimesDivide(leftSide) + parseAdd(rightSide); } else { return parseTimesDivide(equation); } } private static double parseTimesDivide(String equation) { // Try to pattern match it to: // parentheses * timesdivide // If that fails, match it to: // parentheses / timesdivide // If that fails, match it to: // parentheses int timesIndex = getIndex(equation, '*'); if(timesIndex != -1) { String leftSide = equation.substring(0, timesIndex); String rightSide = equation.substring(timesIndex + 1, equation.length()); return parseParentheses(leftSide) * parseTimesDivide(rightSide); } else { int divIndex = getIndex(equation, '/'); if(divIndex != -1) { String leftSide = equation.substring(0, divIndex); String rightSide = equation.substring(divIndex + 1, equation.length()); return parseParentheses(leftSide) / parseTimesDivide(rightSide); } else { return parseParentheses(equation); } } } private static double parseParentheses(String equation) { // Try to pattern match it to: // (additive) // If that fails, match it to: // number if(equation.length() > 2 && equation.charAt(0) == '(' && equation.charAt(equation.length() - 1) == ')') { String middle = equation.substring(1, equation.length() - 1); return parseAdd(middle); } else { return parseNum(equation); } } private static double parseNum(String equation) { return Double.parseDouble(equation); } private static int getIndex(String equation, char chr) { int chIndex; int fromPoint = 0; while(true) { chIndex = equation.indexOf(chr, fromPoint); if(chIndex == -1) break; int leftOpenCount = 0; int leftCloseCount = 0; int rightOpenCount = 0; int rightCloseCount = 0; for(int i2 = 0; i2 < chIndex; i2++) { char ch = equation.charAt(i2); if(ch == '(') leftOpenCount++; else if(ch == ')') leftCloseCount++; } for(int i2 = chIndex + 1; i2 < equation.length(); i2++) { char ch = equation.charAt(i2); if(ch == '(') rightOpenCount++; else if(ch == ')') rightCloseCount++; } if(leftOpenCount == leftCloseCount && rightOpenCount == rightCloseCount) { break; } else { fromPoint = chIndex + 1; } } return chIndex; } }
public class Parser { /** * Parses equations such as: * "(2 + (3 + 3)*4)/(-0.25^0.5)" * (to produce -52) * * ~Duta */ public static double parseEquation(String equation) { // Remove any spaces. for(int i = 0; i < equation.length(); i++) { if(equation.charAt(i) == ' ') { String leftSide = ""; String rightSide = ""; for(int i2 = 0; i2 < i; i2++) { leftSide += equation.charAt(i2); } for(int i2 = i + 1; i2 < equation.length(); i2++) { rightSide += equation.charAt(i2); } equation = leftSide + rightSide; i--; } } // Parse the equation. if(equation.length() == 0) { return 0; } return parseAddSubtract(equation); } private static double parseAddSubtract(String equation) { // Try to pattern match it to: // multiplicative + addsubtract // If that fails, match it to: // multiplicative int plusIndex = getIndex(equation, '+'); if(plusIndex != -1) { String leftSide = equation.substring(0, plusIndex); String rightSide = equation.substring(plusIndex + 1, equation.length()); return parseTimesDivide(leftSide) + parseAddSubtract(rightSide); } else { int minusIndex = getIndex(equation, '-'); if(minusIndex != -1) { String leftSide = equation.substring(0, minusIndex); String rightSide = equation.substring(minusIndex + 1, equation.length()); leftSide = leftSide.equals("") ? "0" : leftSide; return parseTimesDivide(leftSide) - parseAddSubtract(rightSide); } else { return parseTimesDivide(equation); } } } private static double parseTimesDivide(String equation) { // Try to pattern match it to: // power * timesdivide // If that fails, match it to: // power / timesdivide // If that fails, match it to: // power int timesIndex = getIndex(equation, '*'); if(timesIndex != -1) { String leftSide = equation.substring(0, timesIndex); String rightSide = equation.substring(timesIndex + 1, equation.length()); return parsePower(leftSide) * parseTimesDivide(rightSide); } else { int divIndex = getIndex(equation, '/'); if(divIndex != -1) { String leftSide = equation.substring(0, divIndex); String rightSide = equation.substring(divIndex + 1, equation.length()); return parsePower(leftSide) / parseTimesDivide(rightSide); } else { return parsePower(equation); } } } private static double parsePower(String equation) { // Try to pattern match it to: // parentheses ^ parentheses // If that fails, match it to: // parentheses int powerIndex = getIndex(equation, '^'); if(powerIndex != -1) { String leftSide = equation.substring(0, powerIndex); String rightSide = equation.substring(powerIndex + 1, equation.length()); return Math.pow(parseParentheses(leftSide), parseParentheses(rightSide)); } else { return parseParentheses(equation); } } private static double parseParentheses(String equation) { // Try to pattern match it to: // (additive) // If that fails, match it to: // number if(equation.length() > 2 && equation.charAt(0) == '(' && equation.charAt(equation.length() - 1) == ')') { String middle = equation.substring(1, equation.length() - 1); return parseAddSubtract(middle); } else { return parseNum(equation); } } private static double parseNum(String equation) { return Double.parseDouble(equation); } private static int getIndex(String equation, char chr) { int chIndex; int fromPoint = 0; while(true) { chIndex = equation.indexOf(chr, fromPoint); if(chIndex == -1) break; int leftOpenCount = 0; int leftCloseCount = 0; int rightOpenCount = 0; int rightCloseCount = 0; for(int i2 = 0; i2 < chIndex; i2++) { char ch = equation.charAt(i2); if(ch == '(') leftOpenCount++; else if(ch == ')') leftCloseCount++; } for(int i2 = chIndex + 1; i2 < equation.length(); i2++) { char ch = equation.charAt(i2); if(ch == '(') rightOpenCount++; else if(ch == ')') rightCloseCount++; } if(leftOpenCount == leftCloseCount && rightOpenCount == rightCloseCount) { break; } else { fromPoint = chIndex + 1; } } return chIndex; } }
public class Parser { /** * Parses equations such as: * "(sin(90) - -1 + (3 + 3)*4)/(-0.25^0.5)" * (to produce -52) * * ~Duta */ public static double parseEquation(String equation) { // Remove any spaces. for(int i = 0; i < equation.length(); i++) { if(equation.charAt(i) == ' ') { String leftSide = equation.substring(0, i); String rightSide = equation.substring(i + 1, equation.length()); equation = leftSide + rightSide; i--; } } // Convert any "--"s to "+"s for(int i = 1; i < equation.length(); i++) { if(equation.charAt(i-1) == '-' && equation.charAt(i) == '-') { String leftSide = equation.substring(0, i-1); String rightSide = equation.substring(i + 1, equation.length()); equation = leftSide + '+' + rightSide; i--; } } // Parse the equation. if(equation.length() == 0) { return 0; } try { return parseAddSubtract(equation); } catch(Exception e) { System.out.println("Something went wrong."); return 0; } } private static double parseAddSubtract(String equation) throws Exception { // Try to pattern match it to: // timesdivide + addsubtract // If that fails, match it to: // timesdivide - addsubtract // If that fails, match it to: // timesdivide int plusIndex = getIndex(equation, '+'); if(plusIndex != -1) { String leftSide = equation.substring(0, plusIndex); String rightSide = equation.substring(plusIndex + 1, equation.length()); return parseTimesDivide(leftSide) + parseAddSubtract(rightSide); } else { int minusIndex = getIndex(equation, '-'); if(minusIndex != -1) { String leftSide = equation.substring(0, minusIndex); String rightSide = equation.substring(minusIndex + 1, equation.length()); leftSide = leftSide.equals("") ? "0" : leftSide; return parseTimesDivide(leftSide) - parseAddSubtract(rightSide); } else { return parseTimesDivide(equation); } } } private static double parseTimesDivide(String equation) throws Exception { // Try to pattern match it to: // power * timesdivide // If that fails, match it to: // power / timesdivide // If that fails, match it to: // power int timesIndex = getIndex(equation, '*'); if(timesIndex != -1) { String leftSide = equation.substring(0, timesIndex); String rightSide = equation.substring(timesIndex + 1, equation.length()); return parsePower(leftSide) * parseTimesDivide(rightSide); } else { int divIndex = getIndex(equation, '/'); if(divIndex != -1) { String leftSide = equation.substring(0, divIndex); String rightSide = equation.substring(divIndex + 1, equation.length()); return parsePower(leftSide) / parseTimesDivide(rightSide); } else { return parsePower(equation); } } } private static double parsePower(String equation) throws Exception { // Try to pattern match it to: // parentheses ^ parentheses // If that fails, match it to: // parentheses int powerIndex = getIndex(equation, '^'); if(powerIndex != -1) { String leftSide = equation.substring(0, powerIndex); String rightSide = equation.substring(powerIndex + 1, equation.length()); return Math.pow(parseParentheses(leftSide), parseParentheses(rightSide)); } else { return parseParentheses(equation); } } private static double parseParentheses(String equation) throws Exception { // Try to pattern match it to: // (additive) // If that fails, match it to: // func if(equation.length() > 2 && equation.charAt(0) == '(' && equation.charAt(equation.length() - 1) == ')') { String middle = equation.substring(1, equation.length() - 1); return parseAddSubtract(middle); } else { return parseFunc(equation); } } private static double parseFunc(String equation) throws Exception { // Try to pattern match it to: // sin(additive) // If that fails, match it to: // cos(additive) // If that fails, match it to: // tan(additive) // If that fails, match it to: // num if(equation.length() > 5 && equation.substring(0, 4).equalsIgnoreCase("sin(") && equation.charAt(equation.length() - 1) == ')') { String middle = equation.substring(4, equation.length() - 1); return Math.sin(Math.toRadians(parseAddSubtract(middle))); } else if(equation.length() > 5 && equation.substring(0, 4).equalsIgnoreCase("cos(") && equation.charAt(equation.length() - 1) == ')') { String middle = equation.substring(4, equation.length() - 1); return Math.cos(Math.toRadians(parseAddSubtract(middle))); } else if(equation.length() > 5 && equation.substring(0, 4).equalsIgnoreCase("tan(") && equation.charAt(equation.length() - 1) == ')') { String middle = equation.substring(4, equation.length() - 1); return Math.tan(Math.toRadians(parseAddSubtract(middle))); } else { return parseNum(equation); } } private static double parseNum(String equation) throws Exception { return Double.parseDouble(equation); } private static int getIndex(String equation, char chr) { int chIndex; int fromPoint = 0; while(true) { chIndex = equation.indexOf(chr, fromPoint); if(chIndex == -1) break; int leftOpenCount = 0; int leftCloseCount = 0; int rightOpenCount = 0; int rightCloseCount = 0; for(int i2 = 0; i2 < chIndex; i2++) { char ch = equation.charAt(i2); if(ch == '(') leftOpenCount++; else if(ch == ')') leftCloseCount++; } for(int i2 = chIndex + 1; i2 < equation.length(); i2++) { char ch = equation.charAt(i2); if(ch == '(') rightOpenCount++; else if(ch == ')') rightCloseCount++; } if(leftOpenCount == leftCloseCount && rightOpenCount == rightCloseCount) { break; } else { fromPoint = chIndex + 1; } } return chIndex; } }