Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Stack;
- import java.util.Vector;
- public class OP{
- static void printStack(Stack st){
- for(int i=0; i<st.size(); i++)
- System.out.print("["+ st.get(i) +"]");
- System.out.println();
- }
- static double add(double a, double b){
- return b+a;
- }
- static double sub(double a, double b){
- return b-a;
- }
- static double mlt(double a, double b){
- return b*a;
- }
- static double div(double a, double b){
- if (a!=0)
- return b/a;
- else
- System.exit(1);
- return 0;
- }
- static double getDouble(Object o){
- return Double.parseDouble(o.toString());
- }
- static int pri(String s){
- char c = s.charAt(0);
- switch(c){
- case '+':
- case '-':
- return 1;
- case '*':
- case '/':
- return 2;
- }
- return 0;
- }
- static double examine(String exp){
- Stack stack = new Stack();
- String strArr[] = exp.split(" ");
- for (int i=0; i<strArr.length; i++){
- if(strArr[i].equals("+")){
- stack.push(add(getDouble(stack.pop()), getDouble(stack.pop())));
- }else if(strArr[i].equals("-")){
- stack.push(sub(getDouble(stack.pop()), getDouble(stack.pop())));
- }else if(strArr[i].equals("*")){
- stack.push(mlt(getDouble(stack.pop()), getDouble(stack.pop())));
- }else if(strArr[i].equals("/")){
- stack.push(div(getDouble(stack.pop()), getDouble(stack.pop())));
- }else{
- stack.push(strArr[i]);
- }
- }
- return getDouble(stack.pop());
- }
- static String pre(String in){
- Stack stack = new Stack();
- String inArr[] = in.split(" ");
- String out="";
- for (int i=0; i<inArr.length; i++){
- if (inArr[i].equals("(")){
- stack.push("(");
- }else if (inArr[i].equals(")")){
- while(true){
- if(stack.empty()){
- System.out.println("Parentheses mismatch!");
- System.exit(1);
- }
- String opp = stack.pop().toString();
- if(opp.equals("("))
- break;
- else
- out += opp+" ";
- }
- if(!stack.empty())
- out += stack.pop().toString()+" ";
- }else if(inArr[i].equals("+")){
- while(stack.size() > 0 && pri("+") <= pri(stack.get(stack.size()-1).toString()))
- out+=stack.pop().toString()+" ";
- stack.push("+");
- }else if(inArr[i].equals("-")){
- while(stack.size() > 0 && pri("-") <= pri(stack.get(stack.size()-1).toString()))
- out+=stack.pop().toString()+" ";
- stack.push("-");
- }else if(inArr[i].equals("*")){
- while(stack.size() > 0 && pri("*") <= pri(stack.get(stack.size()-1).toString()))
- out+=stack.pop().toString()+" ";
- stack.push("*");
- }else if(inArr[i].equals("/")){
- while(stack.size() > 0 && pri("/") <= pri(stack.get(stack.size()-1).toString()))
- out+=stack.pop().toString()+" ";
- stack.push("/");
- }else{
- out+=inArr[i]+" ";
- }
- }
- while(!stack.empty()){
- String opp = stack.pop().toString();
- if(opp.equals("(")){
- System.out.println("Parentheses mismatch!");
- System.exit(1);
- }
- out+=opp+" ";
- }
- return out;
- }
- public static void main(String args[]){
- Stack stack = new Stack();
- String exp = "( 56 + 3 ) * 2 / 2 * 42 / ( 2 * ( 2 ) + 3 ) - 194";
- String out = pre(exp);
- System.out.println(exp);
- System.out.println(out);
- System.out.println(examine(out));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment