diff --git a/.idea/misc.xml b/.idea/misc.xml index 59436c9..9076de5 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,7 +3,7 @@ - + diff --git a/.idea/modules.xml b/.idea/modules.xml index 2f72ee4..e69c0ed 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -3,6 +3,7 @@ + diff --git a/.idea/vcs.xml b/.idea/vcs.xml index def6a6a..9d32e50 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,6 +2,7 @@ + diff --git a/Lab2Exercise1_Wed.iml b/Lab2Exercise1_Wed.iml new file mode 100644 index 0000000..2a02201 --- /dev/null +++ b/Lab2Exercise1_Wed.iml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/java/th/ac/tu/siit/its333/lab2exercise1/MainActivity.java b/app/src/main/java/th/ac/tu/siit/its333/lab2exercise1/MainActivity.java index f3d87ed..b51857e 100644 --- a/app/src/main/java/th/ac/tu/siit/its333/lab2exercise1/MainActivity.java +++ b/app/src/main/java/th/ac/tu/siit/its333/lab2exercise1/MainActivity.java @@ -14,6 +14,7 @@ public class MainActivity extends ActionBarActivity { // expr = the current string to be calculated StringBuffer expr; + int memory =0; @Override protected void onCreate(Bundle savedInstanceState) { @@ -28,6 +29,44 @@ public void updateExprDisplay() { tvExpr.setText(expr.toString()); } + public void memoryHandle(View v) + { + int id = v.getId(); + TextView tvAns = (TextView)findViewById(R.id.tvAns); + switch(id){ + case R.id.madd: + int memadd = Integer.parseInt(tvAns.getText().toString()); + memory+=memadd; + Toast t1 = Toast.makeText(this.getApplicationContext(), + "Memory Added, Value="+memory, Toast.LENGTH_SHORT); + t1.show(); + break; + case R.id.msub: + int memsub = Integer.parseInt(tvAns.getText().toString()); + memory-=memsub; + Toast t2 = Toast.makeText(this.getApplicationContext(), + "Memory Subtracted, Value="+memory, Toast.LENGTH_SHORT); + t2.show(); + break; + case R.id.mr: + TextView tvExpr = (TextView)findViewById(R.id.tvExpr); + tvAns.setText(Integer.toString(memory)); + Toast t3 = Toast.makeText(this.getApplicationContext(), + "Memory Showed", Toast.LENGTH_SHORT); + expr = new StringBuffer(Integer.toString(memory)); + updateExprDisplay(); + t3.show(); + break; + case R.id.mc: + memory = 0; + Toast t4 = Toast.makeText(this.getApplicationContext(), + "Memory Cleared, Value="+memory, Toast.LENGTH_SHORT); + t4.show(); + break; + default: break; + } + } + public void recalculate() { //Calculate the expression and display the output @@ -36,6 +75,40 @@ public void recalculate() { //reference: http://stackoverflow.com/questions/2206378/how-to-split-a-string-but-also-keep-the-delimiters String e = expr.toString(); String[] tokens = e.split("((?<=\\+)|(?=\\+))|((?<=\\-)|(?=\\-))|((?<=\\*)|(?=\\*))|((?<=/)|(?=/))"); + TextView tvAns = (TextView)findViewById(R.id.tvAns); + if(tokens.length == 1) + { + tvAns.setText(tokens[0]); + } + else + { + if(tokens.length%2 == 1) + { + int result = Integer.parseInt(tokens[0]); + for(int i=1;i 0) { expr.deleteCharAt(expr.length()-1); updateExprDisplay(); + recalculate(); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index a895ff2..37da49a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -73,7 +73,8 @@ android:id="@+id/madd" android:layout_row="3" android:layout_column="0" - android:textSize="28sp" /> + android:textSize="28sp" + android:onClick="memoryHandle" />