Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class myStack
- {
- private static Stack stax = new Stack();
- public static void main (String [] args)
- {
- Scanner rice = new Scanner(System.in);
- String lawl = " ";
- System.out.println("Enter Equation: ");
- String werd = rice.nextLine();
- if (isGroupingOK(werd))
- lawl = infixToPostfix(werd);
- else System.out.println("Incorrect use of grouping signs");
- evaluator(lawl);
- /*System.out.println("Input expression to check if grouping is OK: ");
- String dawg = rice.nextLine();
- System.out.println(isGroupingOK(dawg));*/
- }
- public static String infixToPostfix (String exp)
- {
- String post = " ";
- String val = " ";
- for (int i = 0; i < exp.length(); i++)
- {
- char n = exp.charAt(i);
- if (n == ('+') || n == ('-') || n == ('*') || n == ('/'))
- {
- while (!stax.empty() && precedence(stax.peek()) >= precedence(n))
- post += stax.pop();
- stax.push(n);
- }
- else if (n == ('('))
- stax.push(n);
- else if (n == (')'))
- {
- while (!stax.peek().equals('('))
- post += stax.pop();
- stax.pop();
- }
- else if (n == ('.'))
- {
- post += n;
- int q = 0;
- for (q = i+1; q < exp.length() - i; q++)
- {
- char o = exp.charAt(q);
- if (!isOperator(o))
- post += o;
- else break;
- }
- i = q-1;
- post += ',';
- }
- else
- {
- post += n;
- int z = 0;
- for (z = i+1; z < exp.length() - i; z++)
- {
- char g = exp.charAt(z);
- if (!isOperator(g))
- post += g;
- else break;
- }
- i = z-1;
- post += ',';
- }
- }
- while (!stax.empty())
- post += stax.pop();
- System.out.println("Post fix expression equals: "+post.trim());
- return post;
- }
- public static int evaluator (String exp)
- {
- for(int i = 0; i < exp.length(); i++)
- {
- char c = exp.charAt(i);
- String a = "";
- int k = 0;
- if(c =='+')
- {
- int sum=Integer.parseInt((String)stax.pop())+Integer.parseInt((String)stax.pop());
- stax.push(Integer.toString(sum));
- }
- else if(c =='*')
- {
- int product=Integer.parseInt((String)stax.pop())*Integer.parseInt((String)stax.pop());
- stax.push(Integer.toString(product));
- }
- else if(c =='/')
- {
- int denom=Integer.parseInt((String)stax.pop());
- int quotient=Integer.parseInt((String)stax.pop())/denom;
- stax.push(Integer.toString(quotient));
- }
- else if(c =='-')
- {
- int subtractor=Integer.parseInt((String)stax.pop());
- int difference=Integer.parseInt((String)stax.pop())-subtractor;
- stax.push(Integer.toString(difference));
- }
- else
- {
- for (k = i+1; k < exp.length() - i; k++)
- {
- char g = exp.charAt(k);
- if (g == ',')
- {
- a += exp.substring(i, k-1);
- i = k+1;
- break;
- }
- }
- stax.push(a);
- System.out.println(stax.peek());
- }
- }
- System.out.println("Post fix expression evaluates to: "+stax.peek());
- return Integer.parseInt((String)stax.pop());
- }
- public static int precedence(Object x)
- {
- if(x.equals('+') || x.equals('-'))
- return 1;
- else if(x.equals('*') || x.equals('/'))
- return 2;
- else return 0;
- }
- public static boolean isGroupingOK(String infixExp)
- {
- Stack stax2 = new Stack();
- Object output = ' ';
- for (int i = 0; i < infixExp.length(); i++)
- {
- char temp = infixExp.charAt(i);
- String string = " ";
- if (isOpen(temp))
- {
- stax2.push(temp);
- }
- else if (isClose(temp))
- {
- if (stax2.isEmpty())
- return false;
- else
- {
- output = stax2.peek();
- return (isMatch(output, temp));
- }
- }
- }
- return true;
- }
- public static boolean isOpen(char c)
- {
- if(c == '(' || c == '[' || c == '{')
- return true;
- else
- return false;
- }
- public static boolean isClose(char c)
- {
- if(c == ')' || c == ']' || c == '}')
- return true;
- else
- return false;
- }
- public static boolean isMatch(Object c, char y)
- {
- if((c.equals('(') && y == ')') || (c.equals('[') && (y == ']')) || (c.equals('{') && y == '}'))
- return true;
- else
- return false;
- }
- public static boolean isOperator(char x)
- {
- if (x == ('+') || x == ('-') || x == ('*') || x == ('/') || x == (','))
- return true;
- else return false;
- }
- }
Add Comment
Please, Sign In to add comment