Notes
Notes - notes.io |
import java.util.*;
public class EXF2_38
{
public static class Stack<T>
{
private int top = 0;
private final static int stackMax = 25;
// highest index of stk array
private Object[] stk = new Object[stackMax+1];
public Stack()
{
}
// other methods
public boolean isEmpty()
{
if(top==0)
return true;
else
return false;
}
public void push(T el)
{
if (top == stackMax) // is stack full ?
System.out.println("Stack Push Overflow Error");
else
{
top++;
stk[top]=el;
}
}
public T pop()
{
if(isEmpty())
{
System.out.println("Stack Pop Underflow Error");
return null;
}
else
{
top= top-1;
return (T) stk[top+1];
}
}
public void clear()
{
top=0;
}
public static boolean isOperator(String x)
{
char c=x.charAt(0);
return(c=='+' || c=='-' || c=='/' || c=='*'|| c=='^');
}
public static double evaluate(double x, String o, double y)
{
char c=o.charAt(0);
double result=0;
switch(c)
{
case '+' : result=x+y; break;
case '-' : result=x-y; break;
case '*' : result=x*y; break;
case '/' : result=x/y; break;
case '^' : result=Math.pow(x,y); break;
default : break;
}
return result;
}
public static double evaluatePostfix(String e)
{
Stack<Double> s = new Stack<Double>();
String[] x=toCharArray(e);
double a=0,b=0;
for (int i = 0; i < x.length; i++)
{
if(!isOperator(x[i]))
{
s.push(Double.parseDouble(x[i]));
}
else
{
b=s.pop();
a=s.pop();
Print(a,b,x[i]);
s.push(evaluate(a,x[i],b));
}
}
return s.pop();
}
public static void Print(double b, double a, String o){
System.out.printf("%.0ft%.0fn",b , a);
evaluate(a,o,b);
}
public static String[] toCharArray(String inp){
String[] s = inp.split(" ");
return s;
}
}
public static void main(String[] args)
{
char again;
do
{
Scanner input= new Scanner(System.in);
System.out.println("Input PostFix Expression: ");
String w= input.nextLine();
double q = Stack.evaluatePostfix(w);
System.out.printf("%.0fn",q);
System.out.printf("The answer is %.0fn",q);
System.out.print("Try Again? [y/anykey]");
again= input.next().charAt(0);
}while(again=='y' || again=='Y');
}
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team