fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. import javax.script.ScriptEngineManager;
  6. import javax.script.ScriptEngine;
  7.  
  8. class Ideone
  9. {
  10. // single : x -> [x]
  11. public static String expandSingle (String input)
  12. {
  13. String out = "";
  14. for (String str : input.split(" "))
  15. {
  16. out += " ";
  17. if(str.matches("[0-9]+"))
  18. {
  19. out += "["+str+"]";
  20. }
  21. else
  22. {
  23. out += str;
  24. }
  25. }
  26. return out.substring(1);
  27. }
  28.  
  29. // range : {start,end,step} -> [x,..,y]
  30. public static String expandRange (String input)
  31. {
  32. String out = "";
  33. int a,b,c;
  34. int i=0;
  35. for (String str : input.split(" "))
  36. {
  37. out += " ";
  38. if(str.matches("\\{[0-9]+,[0-9]+,[0-9]+\\}"))
  39. {
  40. str = str.replaceAll("[\\{\\}]","");
  41. a = Integer.parseInt(str.split(",")[0]);
  42. b = Integer.parseInt(str.split(",")[1]);
  43. c = Integer.parseInt(str.split(",")[2]);
  44.  
  45. out += "["+a;
  46. while ((a+=c) <= b) out += ","+a;
  47. out += "]";
  48. }
  49. else
  50. {
  51. out += str;
  52. }
  53. }
  54. return out.substring(1);
  55. }
  56.  
  57. public static void main (String[] args) throws java.lang.Exception
  58. {
  59. String input = "3 * [3,2] / {0,2,1}";
  60. System.out.println(" input = "+input);
  61. input = expandSingle(input);
  62. input = expandRange(input);
  63. System.out.println(" expand = "+input);
  64. evaluate(input);
  65. }
  66.  
  67. public static void evaluate (String input) throws java.lang.Exception
  68. {
  69. int i = 0, j = 0;
  70. String t = "";
  71. ArrayList<String[]> set = new ArrayList<String[]>();
  72. ArrayList<String> in = new ArrayList<String>();
  73. ArrayList<String> out = new ArrayList<String>();
  74.  
  75. // map sets
  76. for (String str : input.split(" "))
  77. {
  78. if(str.matches("\\[[0-9,]+\\]"))
  79. {
  80. set.add(str.replaceAll("[\\[\\]]","").split(","));
  81. t+=" $"+i++;
  82. }
  83. else t+=" "+str;
  84. }
  85. in.add(t.substring(1));
  86.  
  87. // generate expressions
  88. while (j<i)
  89. {
  90. out.clear();
  91. for (String exp : in)
  92. {
  93. for (String sub : set.get(j))
  94. {
  95. out.add(exp.replace("$"+j,sub));
  96. }
  97. }
  98. in.clear();
  99. in.addAll(out);
  100. j++;
  101. }
  102.  
  103. ScriptEngineManager mgr = new ScriptEngineManager();
  104. ScriptEngine engine = mgr.getEngineByName("JavaScript");
  105.  
  106. // print expressions
  107. for (String exp : in)
  108. {
  109. System.out.println(" "+exp+" = "+engine.eval(exp).toString().replace("Infinity","NaN"));
  110. }
  111. }
  112. }
  113.  
Success #stdin #stdout 1.43s 326528KB
stdin
Standard input is empty
stdout
 input = 3 * [3,2] / {0,2,1}
 expand = [3] * [3,2] / [0,1,2]
 3 * 3 / 0 = NaN
 3 * 3 / 1 = 9
 3 * 3 / 2 = 4.5
 3 * 2 / 0 = NaN
 3 * 2 / 1 = 6
 3 * 2 / 2 = 3