-
-
Save anonymous/a010695d59f16d566bf6 to your computer and use it in GitHub Desktop.
This file contains 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
// A+ Computer Science - www.apluscompsci.com | |
//Name - Sudeep Ghantasala | |
//Date - January 14, 2015 | |
//Class - AP Computer Science | |
//Lab - ArrayLists | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
import static java.lang.Integer.*; | |
import static java.lang.System.*; | |
public class ExpressionSolver | |
{ | |
//add in instance variables | |
private String finale; | |
private int Mult, Div, Add, Sub; | |
private ArrayList<String>lol; | |
public ExpressionSolver(String s) | |
{ | |
finale = s; | |
} | |
public void setExpression(String s) | |
{ | |
finale = s; | |
} | |
public void solveExpression() | |
{ | |
Scanner string = new Scanner(finale); | |
while(string.hasNext()) { | |
lol.add(string.next()); | |
} | |
int Div = lol.indexOf("/"); | |
int Mult = lol.indexOf("*"); | |
int Add = lol.indexOf("+"); | |
int Sub = lol.indexOf("-"); | |
while(Mult > -1 || Div > -1){ | |
if((Mult > Div && Div != -1) || (Div > -1 && Mult == -1 && Div != -1)){ | |
int quotient = Integer.parseInt(lol.get(Div-1)) / Integer.parseInt(lol.get(Div+1)); | |
String lString = quotient + ""; | |
lol.remove(Div-1); | |
lol.remove(Div-1); | |
lol.remove(Div-1); | |
lol.add(Div - 1, lString); | |
Mult = lol.indexOf("*"); | |
Div = lol.indexOf("/"); | |
Add = lol.indexOf("+"); | |
Sub = lol.indexOf("-"); | |
} | |
else if((Mult < Div && Mult != -1) || (Mult > -1 && Div == -1 && Mult != -1)){ | |
int product = Integer.parseInt(lol.get(Mult-1)) * Integer.parseInt(lol.get(Mult+1)); | |
String lString = product + ""; | |
lol.remove(Mult-1); | |
lol.remove(Mult-1); | |
lol.remove(Mult-1); | |
lol.add(Mult - 1, lString); | |
Mult = lol.indexOf("*"); | |
Div = lol.indexOf("/"); | |
Add = lol.indexOf("+"); | |
Sub = lol.indexOf("-"); | |
} | |
} | |
while (Add > -1 || Sub > -1) { | |
if((Add < Sub && Add != -1) || (Add > -1 && Sub == -1)){ | |
int Num = Integer.parseInt(lol.get(Add-1)) + Integer.parseInt(lol.get(Add+1)); | |
String lString = Num + ""; | |
lol.remove(Add-1); | |
lol.remove(Add - 1); | |
lol.remove(Add - 1); | |
lol.add(Add-1, lString); | |
Mult = lol.indexOf("*"); | |
Div = lol.indexOf("/"); | |
Add = lol.indexOf("+"); | |
Sub = lol.indexOf("-"); | |
} | |
else if((Mult < Div && Mult != -1) || (Mult > -1 && Div == -1 && Mult != -1)){ | |
int product = Integer.parseInt(lol.get(Mult-1)) * Integer.parseInt(lol.get(Mult+1)); | |
String lString = product + ""; | |
lol.remove(Mult-1); | |
lol.remove(Mult-1); | |
lol.remove(Mult-1); | |
lol.add(Mult - 1, lString); | |
Mult = lol.indexOf("*"); | |
Div = lol.indexOf("/"); | |
Add = lol.indexOf("+"); | |
Sub = lol.indexOf("-"); | |
} | |
} | |
} | |
public String toString( ) | |
{ | |
return finale + " = " + lol.get(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment