Created
October 11, 2012 17:42
-
-
Save tangmae/3874207 to your computer and use it in GitHub Desktop.
Homework#2 Calculator
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
public class calculator(){ | |
private Keyboard keyboard; | |
private Screen screen; | |
private Stack operant; | |
private Stack operator; | |
private String showValue; | |
private State calState; | |
enum State{ | |
on, | |
off, | |
} | |
public static void Main(String[] args){ | |
} | |
public calculator(){ | |
this.keyboard = new Keyboard(); | |
this.screen = new Screen(); | |
this.operant = new Stack(); | |
this.operator = new Stack(); | |
showValue = null; | |
} | |
public void onpreesedKey() | |
{ | |
if(calState == State.on) | |
{ | |
Key key = keyboard.preesedKey(); | |
if(key.keytype == Number) | |
{ | |
showValue = showValue+key.value; | |
screen.getOutput(showValue); | |
} | |
else if(key.keytype == operant) | |
{ | |
// stack of operant shouldn't have size > 2 and operator's shouldn't have size > 1 | |
String keyOperator = key.value; | |
if(showValue != null &&operator.isEmpty()&&!operator.equals("=")&&!operator.equals("sqr")) | |
{ | |
//Change value from String to Double and push in Stack | |
operant.push(value); | |
operator.push(); | |
operator.push(keyOperator); | |
showValue = keyOperator; | |
screen.getOutput(showValue); | |
} | |
else if(!operator.equals("sqr")) | |
{ | |
operant1 = operant.pop(); | |
Double temp = Math.sqr(operant1); | |
operant.push(temp); | |
showValue = temp.parseToString | |
screen.getOutput(showValue); | |
} | |
else if(operator.equals("=")||!operator.isEmpty()) | |
{ | |
if(operator.size>operant.size) // pressed operation again and again pop previous operator and get new operator instrade. | |
{ | |
operator.pop(); | |
operator.push(keyOperator); | |
} | |
else | |
{ | |
Double operant2 = operant.pop(); | |
Double operant1 = operant.pop(); | |
String currentOperator = operator.pop(); | |
Double result = 0; | |
if(currentOperator.equals("+") | |
{ | |
result = operant2 + operant1; | |
//Follow the operator | |
//ex . "+" Operant | |
//... | |
} | |
else if(currentOperator.equals("-") | |
{ | |
result = operant2 - operant1; | |
//Follow the operator | |
//ex . "+" Operant | |
//... | |
} | |
operant.push(result); | |
showValue = result.parseToString; | |
screen.getOutput(showValue); | |
if(!operator.equals("=")){// continue pressed operator | |
operator.push(keyOperator); | |
showValue = keyOperator; | |
screen.getOutput(showValue); | |
} | |
} | |
} | |
else | |
{ | |
String nameKey = key.label; | |
if(nameKey == "turn_on") | |
{ | |
operant = new Stack(); | |
operator = new Stack(); | |
value = null; | |
screen.setClear(); | |
} | |
else if(nameKey == "turn_off") | |
{ | |
this.turnOff(); | |
//bye bye... | |
} | |
else if(nameKey == "back") | |
{ | |
if(showValue!=null) | |
{ | |
showValue = showValue.substring(0,showValue.lenght-1); | |
} | |
} | |
} | |
} | |
private void turnOff() | |
{ | |
} | |
} | |
public class Keyboard{ | |
private Key[]; | |
public Keyboard(){ | |
Key = {new Key("0","0",Key.KeyType.number), | |
new Key("1","1",Key.KeyType.number), | |
new Key("2","2",Key.KeyType.number), | |
new Key("3","3",Key.KeyType.number), | |
new Key("4","4",Key.KeyType.number), | |
new Key("5","5",Key.KeyType.number), | |
new Key("6","6",Key.KeyType.number), | |
new Key("7","7",Key.KeyType.number), | |
new Key("8","8",Key.KeyType.number), | |
new Key("9","9",Key.KeyType.number), | |
new Key("+","+",Key.KeyType.operator), | |
new Key("-","-",Key.KeyType.operator), | |
new Key("x","*",Key.KeyType.operator), | |
new Key("/","/",Key.KeyType.operator), | |
new Key("^","^",Key.KeyType.operator), | |
new Key("=","=",Key.KeyType.operator), | |
new Key("sqr",Key.KeyType.operator), | |
new Key("back",Key.KeyType.command), | |
new Key("turn_on",Key.KeyType.command), | |
new Key("turn_off",Key.KeyType.command) | |
}; | |
} | |
public Object preesedKey() { | |
//return Key | |
} | |
} | |
public class Key{ | |
private String label; | |
private String value; | |
private KeyType keytype; | |
enum KeyType | |
{ | |
number, | |
command, | |
operator | |
} | |
public Key(String label,String value,KeyType keytype) | |
{ | |
this.label = label; | |
this.value = value; | |
this.keytype = keytype; | |
} | |
public Key(String label,KeyType keytype) | |
{ | |
this.label = label; | |
this.keytype = keytype; | |
} | |
} | |
public class Screen{ | |
private String Output; | |
public Screen() | |
{ | |
output = null; | |
} | |
public void getOutput(String output) | |
{ | |
this.output = output; | |
} | |
public void setClear() | |
{ | |
output = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment