From 31243c405d960775a5d9c0162db38aec25278e69 Mon Sep 17 00:00:00 2001 From: satvik Date: Thu, 8 Oct 2020 21:36:25 +0530 Subject: [PATCH 1/2] Create PostFixEval.java --- Coding/Java/PostFixEval.java | 94 ++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Coding/Java/PostFixEval.java diff --git a/Coding/Java/PostFixEval.java b/Coding/Java/PostFixEval.java new file mode 100644 index 0000000..e64306a --- /dev/null +++ b/Coding/Java/PostFixEval.java @@ -0,0 +1,94 @@ +import java.util.*; + +class CQStack +{ + public int maxSize; // size of stack array + public int[] stackArray; + public int top; // top of stack + + public CQStack(int s) // constructor + { + maxSize = s; // set array size + stackArray = new int[maxSize]; // create array + top = -1; // no items yet + } + public boolean isEmpty() // true if stack is empty + { + return (top == -1); + } + public boolean isFull() // true if stack is full + { + return (top == maxSize-1); + } + + public void push(int j) // put item on top of stack + { + if(isFull()) + { + } + else + { + stackArray[++top] = j; // increment top, insert item + } + } + public int pop() // take item from top of stack + { + if (isEmpty()) + { + return -1; + } + else + { + int temp=stackArray[top--]; + return temp; // access item, decrement top + } + } +} + +public class PostFixEval +{ + + static int evalPostfix(CQStack s, String exp) +{ + int i, op1, op2, answer; + for (i = 0; i < exp.length(); ++i) + { + + char c = exp.charAt(i); + if (Character.isDigit(c)) + s.push(c - '0'); + + else + { + op2 = s.pop(); + op1 = s.pop(); + switch(c) + { + case '+': s.push(op1 + op2); break; + case '-': s.push(op1 - op2); break; + case '*': s.push(op1 * op2); break; + case '/': s.push(op1 / op2); break; + case '^': s.push((int)Math.pow(op1,op2)); break; + } + } + } + answer = s.pop(); + return answer; +} + + +public static void main(String[] args) + { + CQStack theStack = new CQStack(100); // make new stack + Scanner s=new Scanner(System.in); + int t, n, q1, q2; + String st; + t = Integer.parseInt(s.nextLine()); + while(t>0) + { + st = s.nextLine(); + System.out.println(evalPostfix(theStack, st)); + t--; + } + } +} \ No newline at end of file From e0511ba0e7eb8f7a85a941282c6abb461d7b2419 Mon Sep 17 00:00:00 2001 From: satvik Date: Thu, 8 Oct 2020 21:36:36 +0530 Subject: [PATCH 2/2] Create PreFixEval.java --- Coding/Java/PreFixEval.java | 94 +++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Coding/Java/PreFixEval.java diff --git a/Coding/Java/PreFixEval.java b/Coding/Java/PreFixEval.java new file mode 100644 index 0000000..1609749 --- /dev/null +++ b/Coding/Java/PreFixEval.java @@ -0,0 +1,94 @@ +import java.util.*; + +class CQStack +{ + public int maxSize; // size of stack array + public int[] stackArray; + public int top; // top of stack + + public CQStack(int s) // constructor + { + maxSize = s; // set array size + stackArray = new int[maxSize]; // create array + top = -1; // no items yet + } + public boolean isEmpty() // true if stack is empty + { + return (top == -1); + } + public boolean isFull() // true if stack is full + { + return (top == maxSize-1); + } + + public void push(int j) // put item on top of stack + { + if(isFull()) + { + } + else + { + stackArray[++top] = j; // increment top, insert item + } + } + public int pop() // take item from top of stack + { + if (isEmpty()) + { + return -1; + } + else + { + int temp=stackArray[top--]; + return temp; // access item, decrement top + } + } +} + +class Result { + static int evalPrefix(CQStack s, String exp) { + int i, op1, op2, answer; + for (i = exp.length()-1; i >= 0 ; i--) + { + // If the scanned character is an operand (number here), push it to the stack. + char c = exp.charAt(i); + if (Character.isDigit(c)) + s.push(c - '0'); + + // If the scanned character is an operator, pop two elements from stack apply the operator + else + { + op1 = s.pop(); + op2 = s.pop(); + switch(c) + { + case '+': s.push(op1 + op2); break; + case '-': s.push(op1 - op2); break; + case '*': s.push(op1 * op2); break; + case '/': s.push(op1 / op2); break; + case '^': s.push((int)Math.pow(op1,op2)); break; + } + } + } + answer = s.pop(); + return answer; + } +} + +class PreFixEval +{ + public static void main(String[] args) + { + CQStack theStack = new CQStack(100); // make new stack + Scanner s=new Scanner(System.in); + int t, n, q1, q2; + String st; + t = Integer.parseInt(s.nextLine()); + while(t>0) + { + st = s.nextLine(); + System.out.println(Result.evalPrefix(theStack, st)); + t--; + } + } +} \ No newline at end of file