Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. public class ExpressionParser {
  2.  
  3. public static String[] operators = new String[] {"", "+", "-"};
  4.  
  5. public static void main(String []args){
  6. String numString = "123";
  7. int sum = 0;
  8. int length = numString.length();
  9.  
  10. // numString on its own is positive
  11. recursion(0, numString, length, sum);
  12.  
  13. String negNumString = operators[2] + numString;
  14.  
  15. }
  16.  
  17. public static void recursion(int currentIndex, String sub, int length, int sum)
  18. {
  19.  
  20. // Base case
  21. if (sub.charAt(0) == '-' && length == 2 || length == 1)
  22. {
  23. //if (parse(sub) == sum) { System.out.println(sub); }
  24.  
  25. // just append _sub, +sub, and -sub, no recursion call
  26. // and parse them all
  27. }
  28. else
  29. {
  30. String newSub = sub.substring(currentIndex+1, length);
  31. // append _, +, - in front of newSub in new blank, plus, minus strings
  32. // parse each one to see if right value
  33. // then do recursion(currentIndex+1, whateversub, whateversub.length(), 0);
  34. String spaceSub = operators[0] + newSub;
  35. recursion(currentIndex+1, spaceSub, spaceSub.length(), sum);
  36. String addSub = operators[1] + newSub;
  37. recursion(currentIndex+1, addSub, addSub.length(), sum); // length + 1?
  38. String subSub = operators[2] + newSub;
  39. recursion(currentIndex+1, subSub, subSub.length(), sum); // length + 1?
  40.  
  41. }
  42.  
  43. }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement