Created
September 19, 2014 15:52
-
-
Save shafi2263/01a8b3118fd415970969 to your computer and use it in GitHub Desktop.
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
import java.util.Stack; | |
import java.util.Vector; | |
public class AdvanceCalculator extends Calculator{ | |
private String temp; | |
private String mainInputString ; | |
Vector <Float> vec = new Vector<>(); | |
Vector <content> postFix = new Vector<>(); | |
Vector <content> infix = new Vector<content>(); | |
public AdvanceCalculator (){ | |
} | |
public AdvanceCalculator(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++){ | |
vec.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(vec.elementAt(0)); | |
infix.add(node); | |
vec.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(!vec.isEmpty()){ | |
content node = new content(); | |
node = makeContentWithNumber(vec.elementAt(0)); | |
infix.add(node); | |
vec.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 add(a, b); | |
case '-': return substract(b, a); | |
case '*': return multiply(a, b); | |
case '^': return power(b, a); | |
default: return divide(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 showResult(){ | |
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