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?


1 2 3 4 5 6 | private String process(String str) { // some code if (someCondition) process(str2) // some code } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | 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); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | 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; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | 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; } } |