Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class main {
  4.  
  5. //a cli calculator that takes a string ussr input and returns a value for it, update for text files
  6. //only uses ints, but will return float values. will update to use floats in future
  7.  
  8. //replace set with add
  9.  
  10. public static int calc_result;
  11.  
  12. public static void main(String[] args){
  13. //System.out.println(get_equation("123+24+3567"));
  14. calc(get_equation("123+24+3567"));
  15. System.out.println(calc_result);
  16. }
  17.  
  18. public static void parse_paren(ArrayList<String> eq){
  19. //call get_equation on deepest parenthesis
  20. //pretty much just calc but with parenthesis
  21.  
  22. ArrayList<String> new_list = new ArrayList<String>();
  23.  
  24. for (int index = 0; index < eq.size(); index++) {
  25. if(eq.get(index).equals(")")){
  26.  
  27. int index_of_close = index;
  28. int current_index = index;
  29. while (!eq.get(current_index).equals("(")){
  30. current_index--;
  31. }
  32. //make slice of arraylist also make sure calc results is 0 before doing anything
  33. ArrayList<String> calc_list = new ArrayList<String>();
  34. calc_list = new ArrayList(eq.subList(current_index+1, index));
  35. calc(calc_list);
  36.  
  37. //add on the rest of eq after this
  38. new_list.set(current_index, Integer.toString(calc_result));
  39.  
  40. ArrayList<String> placeholder = new ArrayList<String>();
  41. for(int i = 0; i < eq.size(); i++){
  42. //this should be not
  43. if((i > current_index && i < index_of_close)){
  44. placeholder.add(eq.get(i));
  45. }
  46. }
  47. new_list.addAll(placeholder);
  48.  
  49. break;
  50. }
  51. }
  52.  
  53. parse_paren(new_list);
  54. }
  55.  
  56. //make return type void, or not?
  57. public static void calc(ArrayList<String> eq) {
  58. //setup placeholder here as well, should be a lot easier
  59. int calc_result = 0;
  60.  
  61. if(eq.size() >= 1) {
  62. //recursive, simplifies equation one step at a time
  63. //or make it left to right
  64. ArrayList<String> new_eq = new ArrayList<>();
  65.  
  66. //add breaks
  67. for (int index = 0; index < eq.size(); index++) {
  68.  
  69. if (eq.get(index).equals("*")) {
  70. new_eq.add(Integer.toString(Integer.parseInt(eq.get(index - 1)) * Integer.parseInt(eq.get(index + 1))));
  71.  
  72. ArrayList<String> placeholder = new ArrayList<String>();
  73. for(int i = 0; i < eq.size(); i++){
  74. if(i > current_index && i < index_of_close){
  75. placeholder.add(eq.get(i));
  76. }
  77. }
  78.  
  79. break;
  80. }
  81. //add conditions for division
  82. else if (eq.get(index).equals("/")) {
  83. new_eq.add(Integer.toString(Integer.parseInt(eq.get(index - 1)) / Integer.parseInt(eq.get(index + 1))));
  84. break;
  85. }
  86. else if (!eq.contains("/") && !eq.contains("*")) {
  87. //System.out.println(eq.get(index)); //???
  88. if (eq.get(index).equals("+")) {
  89. new_eq.add(Integer.toString(Integer.parseInt(eq.get(index - 1)) + Integer.parseInt(eq.get(index + 1))));
  90. break;
  91. }
  92. //add conditions for division
  93. else if (eq.get(index).equals("-")) {
  94. new_eq.add(index, Integer.toString(Integer.parseInt(eq.get(index - 1)) - Integer.parseInt(eq.get(index + 1))));
  95.  
  96.  
  97.  
  98. break;
  99. }
  100. }
  101.  
  102. /*
  103. if(eq.get(index) == "*"){
  104. new_eq.set(index, Integer.toString(Integer.parseInt(eq.get(index-1))*Integer.parseInt(eq.get(index+1))));
  105. }
  106. //add conditions for division
  107. else if(eq.get(index) == "/"){
  108. new_eq.set(index, Integer.toString(Integer.parseInt(eq.get(index-1))/Integer.parseInt(eq.get(index+1))));
  109. }
  110. else if(eq.get(index) == "+"){
  111. new_eq.set(index, Integer.toString(Integer.parseInt(eq.get(index-1))+Integer.parseInt(eq.get(index+1))));
  112. }
  113. */
  114.  
  115. }
  116.  
  117. calc(new_eq);
  118. }
  119. else {
  120. calc_result = Integer.parseInt(eq.get(0));
  121. }
  122. }
  123.  
  124. public static ArrayList<String> get_equation(String equation){
  125.  
  126. //doesnt hit last term not just the element if its a number WHY?!
  127. //because on the last run THERE IS NOT EXCEPTION
  128. ArrayList<String> eq = new ArrayList<>();
  129.  
  130. String current_string = "";
  131. String new_string = "";
  132.  
  133. for(int index = 0; index < equation.length(); index++){
  134.  
  135. try {
  136.  
  137. new_string = current_string += Integer.toString(Integer.parseInt(String.valueOf(equation.charAt(index))));
  138. //if last one throw?
  139.  
  140. }
  141. catch (NumberFormatException e){
  142.  
  143. eq.add(new_string);
  144. eq.add(String.valueOf(equation.charAt(index)));
  145.  
  146. current_string = "";
  147. new_string = "";
  148.  
  149. }
  150. }
  151. eq.add(new_string);
  152. return eq;
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement