Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. //package net.codejava.swing;
  2. import java.util.NoSuchElementException;
  3. import java.util.Stack;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JTree;
  7. import javax.swing.SwingUtilities;
  8. import javax.swing.tree.DefaultMutableTreeNode;
  9. import javax.swing.tree.TreeNode;
  10. import javax.swing.JTree;
  11.  
  12.  
  13.  
  14. public class Homework1 {
  15.  
  16.  
  17.  
  18.  
  19. public static String infix(Node node) {
  20.  
  21. StringBuilder infix = new StringBuilder();
  22. infix = inorder(node);
  23.  
  24. infix.deleteCharAt(0);
  25. infix.deleteCharAt(infix.length()-1);
  26. for(int i =1;i<infix.length()-1;i++) {
  27.  
  28. if(infix.charAt(i-1) == '(' && !is_operater(infix.charAt(i)) && infix.charAt(i+1) == ')') {
  29. infix.setCharAt(i-1, ' ');
  30. infix.setCharAt(i+1, ' ');
  31. }else if(infix.charAt(i)== ' ') {
  32.  
  33. continue;
  34. }
  35.  
  36. }
  37.  
  38. for(int i=0;i<infix.length();i++) {
  39.  
  40. if(infix.charAt(i) == ' ') {
  41. infix.deleteCharAt(i);
  42.  
  43. }
  44.  
  45. }
  46.  
  47.  
  48. return infix.toString();
  49. }
  50.  
  51. public static StringBuilder inorder(Node node) {
  52.  
  53. StringBuilder infix = new StringBuilder();
  54.  
  55. subinorder(node,infix);
  56.  
  57. return infix;
  58.  
  59. }
  60.  
  61. public static void subinorder(Node node,StringBuilder infix) {
  62.  
  63. if (node != null) {
  64. infix.append("(");
  65. subinorder(node.left, infix);
  66. infix.append(node.value);
  67. subinorder(node.right, infix);
  68. infix.append(")");
  69. }
  70.  
  71. }
  72.  
  73.  
  74. public static boolean is_operater(char x) {
  75.  
  76. if(x== '+' || x=='-' || x=='*' || x=='/') {
  77.  
  78. return true;
  79.  
  80. }
  81.  
  82.  
  83. return false;
  84.  
  85. }
  86.  
  87. public static boolean ishasoperater(String text) {
  88.  
  89. for(int i=0;i<text.length();i++) {
  90.  
  91. if(is_operater(text.charAt(i))){
  92. return true;
  93. }
  94.  
  95. }
  96.  
  97. return false;
  98. }
  99.  
  100. public static int calculate(Node node) {
  101.  
  102.  
  103.  
  104.  
  105. if(node.value == '+') {
  106.  
  107. return calculate(node.left) + calculate(node.right);
  108.  
  109. }else if(node.value == '-') {
  110.  
  111. return calculate(node.left) - calculate(node.right);
  112.  
  113. }else if(node.value == '*') {
  114.  
  115. return calculate(node.left) * calculate(node.right);
  116.  
  117. }else if(node.value == '/') {
  118.  
  119. return calculate(node.left) / calculate(node.right);
  120.  
  121. }else return Integer.parseInt((String.valueOf(node.value)));
  122.  
  123.  
  124. }
  125.  
  126. public static Node bulit_tree(char posfix_array []) {
  127.  
  128. Stack<Node> data = new Stack();
  129. Node node,num1,num2;
  130.  
  131. for(int i=0;i<posfix_array.length;i++) {
  132.  
  133. if(!is_operater(posfix_array[i])) {
  134.  
  135. node = new Node(posfix_array[i]);
  136. data.push(node);
  137.  
  138. }else {
  139.  
  140. node = new Node(posfix_array[i]);
  141. num2 = data.pop();
  142. num1 = data.pop();
  143.  
  144. node.left = new Node();
  145. node.left = num1;
  146. node.right = new Node();
  147. node.right = num2;
  148.  
  149. data.push(node);
  150.  
  151.  
  152.  
  153.  
  154. }
  155.  
  156. }
  157.  
  158. node = data.peek();
  159. return node;
  160.  
  161.  
  162.  
  163. }
  164.  
  165.  
  166. public static void main(String[] args) {
  167. // Begin of arguments input sample
  168.  
  169. String posfix = "0";
  170.  
  171. try{
  172. posfix = args[0] ;
  173. }
  174. catch(ArrayIndexOutOfBoundsException e){
  175. System.out.println("No args. !");
  176. return ;
  177. }
  178.  
  179. if (args.length > 0) {
  180. String input = args[0];
  181. if (input.equalsIgnoreCase("251-*32*+")) {
  182. //System.out.println("(2*(5-1))+(3*2)=14");
  183. }
  184. }
  185.  
  186.  
  187.  
  188. char [] postfix_array = posfix.toCharArray();
  189.  
  190. Node root = bulit_tree(postfix_array);
  191.  
  192. System.out.println(infix(root) + "=" + calculate(root));
  193.  
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement