Created
September 19, 2014 14:25
-
-
Save rubaiyat6370/4961d012d7d4dd3809d0 to your computer and use it in GitHub Desktop.
Assignment of OOP1 on Prefix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package edu.univdhaka.iit.prefix; | |
import java.util.Stack; | |
import java.util.Vector; | |
public class AdvancedCalculator extends Calculator{ | |
private String temp; | |
private String mainInputString ; | |
Vector <Float> myVector = new Vector<>(); | |
Vector <content> postFix = new Vector<>(); | |
Vector <content> infix = new Vector<content>(); | |
public AdvancedCalculator (){ | |
} | |
public AdvancedCalculator(String string){ | |
this.mainInputString = string; | |
} | |
private boolean lowerPrecedence(char infixSymbol , char stackSymbol){ | |
if((infixSymbol=='*'||infixSymbol=='/' || infixSymbol == '^') && (stackSymbol=='+'||stackSymbol=='-')) return true; | |
else return false; | |
} | |
private content makeContentWithNumber( float numberObtained){ | |
content node = new content(); | |
node.number = numberObtained; | |
node.flag = true; | |
return node; | |
} | |
private content makeContentWithSymbol( char symbolFromInputString){ | |
content node = new content(); | |
node.symbol = symbolFromInputString; | |
node.flag = false; | |
return node; | |
} | |
private void ParsingInfixString(){ | |
this.temp = this.mainInputString; | |
String tempForParsingToFloat = temp.replaceAll("[+^*/-]"," ").replaceAll("\\)" ,"").replaceAll("\\(", ""); | |
this.temp = temp.replaceAll("[+^*/-]"," ").replaceAll("\\)" ," ").replaceAll("\\(", " "); | |
if(tempForParsingToFloat.charAt(0) == ' ') tempForParsingToFloat.replaceFirst(" ", ""); | |
String [] numberObtained = tempForParsingToFloat.split("[ ]"); | |
for(int i = 0 ; i < numberObtained.length ; i++){ | |
myVector.add(Float.parseFloat(numberObtained[i])); | |
} | |
for(int i=0; i<temp.length(); i++){ | |
if(temp.charAt(i) == ' '){ | |
if(i != 0){ | |
if(temp.charAt(i-1) != ' '){ | |
content node = new content(); | |
node = makeContentWithNumber(myVector.elementAt(0)); | |
infix.add(node); | |
myVector.remove(0); | |
node = makeContentWithSymbol(mainInputString.charAt(i)); | |
infix.add(node); | |
} | |
else{ | |
content node = new content(); | |
node = makeContentWithSymbol( mainInputString.charAt(i)); | |
infix.add(node); | |
} | |
} | |
else{ | |
content node = new content(); | |
node = makeContentWithSymbol( mainInputString.charAt(i)); | |
infix.add(node); | |
} | |
} | |
} | |
if(!myVector.isEmpty()){ | |
content node = new content(); | |
node = makeContentWithNumber(myVector.elementAt(0)); | |
infix.add(node); | |
myVector.remove(0); | |
} | |
} | |
private void infixToPostfix(Vector <content> infix){ | |
Stack <content> myStack = new Stack<>(); | |
content node = new content(); | |
node = makeContentWithSymbol('('); | |
myStack.push(node); | |
node = makeContentWithSymbol( ')'); | |
infix.add(node); | |
for(int i=0; i<infix.size(); i++){ | |
if(infix.elementAt(i).flag){ | |
postFix.add(infix.elementAt(i)); | |
} | |
else{ | |
if(infix.elementAt(i).symbol == '('){ | |
myStack.push(infix.elementAt(i)); | |
} | |
else if(infix.elementAt(i).symbol == ')'){ | |
while(true){ | |
content tempNode = new content(); | |
tempNode = myStack.lastElement(); | |
if(tempNode.symbol == '('){ | |
myStack.pop(); | |
break; | |
} | |
else{ | |
postFix.add(tempNode); | |
myStack.pop(); | |
} | |
} | |
} | |
else{ | |
while(true){ | |
content tempNode1 = new content(); | |
tempNode1 = myStack.lastElement(); | |
if(lowerPrecedence(infix.elementAt(i).symbol, tempNode1.symbol)|| tempNode1.symbol == '(') break; | |
postFix.add(tempNode1); | |
myStack.pop(); | |
} | |
myStack.push(infix.elementAt(i)); | |
} | |
} | |
} | |
} | |
private float evaluateBasicArithmetic(float a, float b, char symbol){ | |
switch(symbol){ | |
case '+': return addition(a, b); | |
case '-': return substraction(b, a); | |
case '*': return multiplication(a, b); | |
case '^': return power(b, a); | |
default: return division(b, a); | |
} | |
} | |
private float evaluatePostfix(Vector <content> postfix){ | |
Stack <Float> myStack = new Stack <>(); | |
for(int i=0; i<postfix.size(); i++){ | |
if(postfix.elementAt(i).flag){ | |
myStack.push(postfix.elementAt(i).number); | |
} | |
else{ | |
float stackTop0, stackTop1, result; | |
stackTop0 = myStack.lastElement(); | |
myStack.pop(); | |
stackTop1 = myStack.lastElement(); | |
myStack.pop(); | |
result = evaluateBasicArithmetic(stackTop0, stackTop1, postfix.elementAt(i).symbol); | |
myStack.push(result); | |
} | |
} | |
return myStack.lastElement(); | |
} | |
public void Result(){ | |
ParsingInfixString(); | |
infixToPostfix(infix); | |
System.out.println(evaluatePostfix(postFix)); | |
} | |
private class content{ | |
public float number=0; | |
public char symbol; | |
boolean flag = true; | |
public content(){ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment