Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ExpressionParser {
- public static String[] operators = new String[] {"", "+", "-"};
- public static void main(String []args){
- String numString = "123";
- int sum = 0;
- int length = numString.length();
- // numString on its own is positive
- recursion(0, numString, length, sum);
- String negNumString = operators[2] + numString;
- }
- public static void recursion(int currentIndex, String sub, int length, int sum)
- {
- // Base case
- if (sub.charAt(0) == '-' && length == 2 || length == 1)
- {
- //if (parse(sub) == sum) { System.out.println(sub); }
- // just append _sub, +sub, and -sub, no recursion call
- // and parse them all
- }
- else
- {
- String newSub = sub.substring(currentIndex+1, length);
- // append _, +, - in front of newSub in new blank, plus, minus strings
- // parse each one to see if right value
- // then do recursion(currentIndex+1, whateversub, whateversub.length(), 0);
- String spaceSub = operators[0] + newSub;
- recursion(currentIndex+1, spaceSub, spaceSub.length(), sum);
- String addSub = operators[1] + newSub;
- recursion(currentIndex+1, addSub, addSub.length(), sum); // length + 1?
- String subSub = operators[2] + newSub;
- recursion(currentIndex+1, subSub, subSub.length(), sum); // length + 1?
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement