Skip to content
This repository has been archived by the owner on Nov 3, 2020. It is now read-only.

post fix evaluation and prefix evalutaion #200

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions Coding/Java/PostFixEval.java
Original file line number Diff line number Diff line change
@@ -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--;
}
}
}
94 changes: 94 additions & 0 deletions Coding/Java/PreFixEval.java
Original file line number Diff line number Diff line change
@@ -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--;
}
}
}