-
-
Save swarut/3874286 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) // #pon Number ถูก define ไว้ที่ Key class แล้วก็เป็น enum ด้วย ต้องเรียกด้วย Key.KeyType.number จ้า | |
{ | |
showValue = showValue+key.value; | |
screen.getOutput(showValue); | |
} | |
else if(key.keytype == operant) // #pon operant? จริง ๆ ต้องเป็น operator หรือปล่าว? เพราะงั้นก็ต้องใช้ Key.KeyType.operator นะ | |
{ | |
// 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")) | |
// เกือบดูดี ;) แต่ !operator.equals("sqr") นี่ต้องเป็น !keyOperator.equals("sqr") ไม่ใช่หรอ ;) หุหุ | |
{ | |
//Change value from String to Double and push in Stack | |
operant.push(value); | |
operator.push(); // เอ๋?? push อะไรหนอ | |
operator.push(keyOperator); | |
showValue = keyOperator; | |
screen.getOutput(showValue); | |
} | |
else if(!operator.equals("sqr")) | |
// เหมือนข้างบนเลย ต้องเป็น keyOperator นะ. ตัว operatorมันเป็น Stack ถ้าใช้ Stack.equals(String) มันจะเทียบกันยังไงน๊า??? ;P | |
{ | |
// look ok ;) | |
operant1 = operant.pop(); | |
Double temp = Math.sqr(operant1); | |
operant.push(temp); | |
showValue = temp.parseToString | |
screen.getOutput(showValue); | |
} | |
else if(operator.equals("=")||!operator.isEmpty()) // หุหุ operator อีกแว้ว ; P | |
{ | |
if(operator.size>operant.size) // pressed operation again and again pop previous operator and get new operator instrade. | |
//อันนี้ตั้งใจเขียนสำหรับกรณีที่ พิมพ์เครื่องหมายซ้ำกันสองครับ(หรือมากกว่า)ใช่มั้ย? | |
//if นี้ไม่มีทางเป็นจริงได้ เพราะเราเขียนไว้ข้างบนว่า | |
// stack of operant shouldn't have size > 2 and operator's shouldn't have size > 1 | |
// แล้วอีกอย่าง ถ้าพี่พิมพ์ operator ขึ้นมาจริง ๆ มันจะไม่เข้ามาที่นี่ แต่จะเข้าไปที่เงื่อนไขแรก ๆ แทน | |
{ | |
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; // ถ้าพี่ใส่ 5-3 มา พอมาถึงตรงนี้ กลายเป็นว่า เราคิด 3-5 ให้พี่นา :P | |
//Follow the operator | |
//ex . "+" Operant | |
//... | |
} | |
operant.push(result); | |
showValue = result.parseToString; | |
screen.getOutput(showValue); | |
if(!operator.equals("=")){// continue pressed operator | |
//เหมือนเดิม block นี้จะไม่มีทางถูก run เพราะเครื่องหมายอื่น ๆ ที่ไม่ใช่ = มันจะไปเข้าเงื่อนไขแรก ๆ แทน | |
operator.push(keyOperator); | |
showValue = keyOperator; | |
screen.getOutput(showValue); | |
} | |
} | |
} | |
} | |
else // command key | |
{ | |
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) // ใช้ setOutput จะตรงความหมายมากกว่าจ้า | |
{ | |
this.output = output; | |
} | |
public void setClear() // clear() ก็พอ ไม่ต้องมี set ก็ได้น้อ | |
{ | |
output = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment