Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. package homework1;
  2.  
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Homework1 {
  7.  
  8.  
  9. static int l = -5;
  10. static Tree t = new Tree();
  11.  
  12. public static String[] inputAry;
  13.  
  14. public static void main(String[] args) {
  15. // Begin of arguments input sample
  16. System.out.print("Input: ");
  17. Scanner input = new Scanner(System.in);
  18. /* if (args.length > 0){
  19. String a = args[0];
  20. inputAry = a.split("");
  21. }*/String a = "251-*32*+";
  22. inputAry = a.split("");
  23. treeCreate(t.root);
  24. infix(t.root);
  25. System.out.println("=" + calculate(t.root));
  26. }
  27.  
  28. public static boolean oPera(String p){
  29. if(null == p)return false;
  30. else switch (p) {
  31. case "+":
  32. return true;
  33. case "-":
  34. return true;
  35. case "*":
  36. return true;
  37. case "/":
  38. return true;
  39. default:
  40. return false;
  41. }
  42.  
  43. }
  44. public void treeCreate(Node n) {
  45. l = inputAry.length - 1;
  46. t.root = new Node(inputAry[l]);
  47. n.key = inputAry[l];
  48. if(oPera(inputAry[l])){
  49. n.Right = new Node();
  50. n.Left = new Node();
  51. l--;
  52. treeCreate(n.Right);
  53. treeCreate(n.Left);
  54. }else l--;
  55. }
  56. /*public static void treeCreate(Node n) {
  57. if(l == -5 ){
  58. t.root = new Node(inputAry[l]);
  59.  
  60. l = inputAry.length - 1;
  61. }
  62. n.key = inputAry[l];
  63. if(oPera(inputAry[l])){
  64. n.Right = new Node();
  65. n.Left = new Node();
  66. l--;
  67. treeCreate(n.Right);
  68. treeCreate(n.Left);
  69. }
  70. else{
  71. l--;
  72. }
  73. } */
  74.  
  75.  
  76.  
  77.  
  78. /* t.root = new Node(inputAry[l]);
  79. n.key = inputAry[l];
  80. if(oPera(inputAry[l])){
  81. n.Right = new Node();
  82. n.Left = new Node();
  83. l--;
  84. treeCreate(n.Right);
  85. treeCreate(n.Left);
  86. }else l--;
  87. }
  88. */
  89. public static void infix(Node n){
  90. if(oPera(n.key)){
  91. if(n != t.root){
  92. System.out.print("(");
  93. }
  94. infix(n.Left);
  95. System.out.print(n.key);
  96. infix(n.Right);
  97. if(n != t.root){
  98. System.out.print(")");
  99. }
  100. }
  101. else{
  102. System.out.print(n.key);
  103. }
  104. }
  105.  
  106. public static void inorder(Node n) {
  107. if (oPera(n.key)){
  108. inorder(n.Left);
  109. System.out.print(n.key);
  110. inorder(n.Right);
  111. }else {
  112. System.out.print(n.key);
  113. }
  114.  
  115. }
  116.  
  117. public static int calculate(Node n){
  118. if(oPera(n.key)){
  119. if(null==n.key){ return Integer.parseInt(n.key);}else switch (n.key) {
  120. case "+":
  121. return calculate(n.Left) + calculate(n.Right);
  122. case "-":
  123. return calculate(n.Left) - calculate(n.Right);
  124. case "*":
  125. return calculate(n.Left) * calculate(n.Right);
  126. case "/":
  127. return calculate(n.Left) / calculate(n.Right);
  128. default:
  129. return Integer.parseInt(n.key);
  130. }
  131. }else return Integer.parseInt(n.key);
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement