Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.NoSuchElementException;
  4. public class ArithmeticExpression {
  5.  
  6. // funkcija za presmetuvanje na izrazot pocnuvajki
  7. // od indeks l, zavrsuvajki vo indeks r
  8. static int presmetaj(char c[], int l, int r) {
  9. StringBuilder sb= new StringBuilder();
  10. for(int i =l; i<=r; i++)
  11. {
  12. sb.append(c[i]);
  13. }
  14. String s=sb.toString();
  15. ArrayStack<String> numbers= new ArrayStack<String>(r);
  16. ArrayStack<String> operators= new ArrayStack<String>(r);
  17. for(int i=0; i<s.length(); i++){
  18. char ch=s.charAt(i);
  19.  
  20. if(Character.isDigit(ch))
  21. {
  22. numbers.push(Character.toString(ch));
  23.  
  24. }
  25. else if(ch=='+' || ch=='-' || ch=='/' || ch=='*')
  26. {
  27. operators.push(Character.toString(ch));
  28.  
  29. }
  30. else if(c[i]==')'){
  31. int num2=Integer.parseInt(numbers.pop());
  32. int num1=Integer.parseInt(numbers.pop());
  33. char oper=operators.pop().charAt(0);
  34.  
  35. if(oper=='+')
  36. {
  37. numbers.push(Integer.toString(num1+num2));
  38. }
  39. else if(oper=='-')
  40. {
  41. numbers.push(Integer.toString(num1-num2));
  42. }
  43.  
  44. }
  45. }
  46. return Integer.parseInt(numbers.pop());
  47. }
  48.  
  49. public static void main(String[] args) throws Exception {
  50. int i,j,k;
  51.  
  52. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  53.  
  54. String expression = br.readLine();
  55. char exp[] = expression.toCharArray();
  56.  
  57. int rez = presmetaj(exp, 0, exp.length-1);
  58. System.out.println(rez);
  59.  
  60. br.close();
  61.  
  62. }
  63.  
  64. }
  65.  
  66.  
  67. class ArrayStack<E> implements Stack<E> {
  68. private E[] elems;
  69. private int depth;
  70.  
  71. @SuppressWarnings("unchecked")
  72. public ArrayStack (int maxDepth) {
  73. // Creating new empty stack
  74. elems = (E[]) new Object[maxDepth];
  75. depth = 0;
  76. }
  77.  
  78.  
  79. public boolean isEmpty () {
  80. // Returns true only if the stack is empty.
  81.  
  82. return (depth == 0);
  83. }
  84.  
  85.  
  86. public E peek () {
  87. // Returns the element on the top od the stack.
  88. if (depth == 0)
  89. throw new NoSuchElementException();
  90. return elems[depth-1];
  91. }
  92.  
  93.  
  94. public void clear () {
  95. // Clears the stack.
  96. for (int i = 0; i < depth; i++) elems[i] = null;
  97. depth = 0;
  98. }
  99.  
  100.  
  101. public void push (E x) {
  102. // Adds x on the top of the stack.
  103. elems[depth++] = x;
  104. }
  105.  
  106.  
  107. public E pop () {
  108. // Removes and returns the element on the top.
  109. if (depth == 0)
  110. throw new NoSuchElementException();
  111. E topmost = elems[--depth];
  112. elems[depth] = null;
  113. return topmost;
  114. }
  115.  
  116.  
  117.  
  118. }
  119.  
  120.  
  121.  
  122. interface Stack<E> {
  123.  
  124. // The elements of the Stack are any kind of objects
  125.  
  126. // Access methods:
  127.  
  128. public boolean isEmpty ();
  129. // Returns true only if the stack is empty.
  130.  
  131. public E peek ();
  132. // Returns the element on the top od the stack.
  133.  
  134. // Transformation methods:
  135.  
  136. public void clear ();
  137. // Clears the stack.
  138.  
  139. public void push (E x);
  140. // Adds x on the top of the stack.
  141.  
  142. public E pop ();
  143. // Removes and returns the element on the top.
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement