Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions Lab2Exercise1_Wed.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="java-gradle" name="Java-Gradle">
<configuration>
<option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/build/classes/main" />
<output-test url="file://$MODULE_DIR$/build/classes/test" />
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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

Expand All @@ -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<tokens.length;i+=2)
{
String operator = tokens[i];
int operand = Integer.parseInt(tokens[i+1]);
if(operator.equals("+"))
{
result+=operand;
}
if(operator.equals("-"))
{
result-=operand;
}
if(operator.equals("*"))
{
result*=operand;
}
if(operator.equals("/"))
{
result/=operand;
}
}
tvAns.setText(Integer.toString(result));
}
}
}

public void digitClicked(View v) {
Expand All @@ -51,14 +124,33 @@ public void digitClicked(View v) {

public void operatorClicked(View v) {
//IF the last character in expr is not an operator and expr is not "",
//THEN append the clicked operator and updateExprDisplay,
//ELSE do nothing
if(expr.length()!=0)
{
char last = expr.charAt(expr.length() - 1);
if (last != '+' && last != '-' && last != '*' && last != '/')
{
//THEN append the clicked operator and updateExprDisplay,
String d = ((TextView) v).getText().toString();
expr.append(d);
updateExprDisplay();
}
}
}

public void equalClicked(View v){
TextView tvAns = (TextView)findViewById(R.id.tvAns);
String result = tvAns.getText().toString();
expr = new StringBuffer(result);
updateExprDisplay();
tvAns.setText("0");
}

public void ACClicked(View v) {
//Clear expr and updateExprDisplay
expr = new StringBuffer();
updateExprDisplay();
TextView tvAns = (TextView)findViewById(R.id.tvAns);
tvAns.setText(expr.toString());
//Display a toast that the value is cleared
Toast t = Toast.makeText(this.getApplicationContext(),
"All cleared", Toast.LENGTH_SHORT);
Expand All @@ -70,6 +162,7 @@ public void BSClicked(View v) {
if (expr.length() > 0) {
expr.deleteCharAt(expr.length()-1);
updateExprDisplay();
recalculate();
}
}

Expand Down
12 changes: 8 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<Button
android:layout_width="wrap_content"
Expand All @@ -82,7 +83,8 @@
android:id="@+id/msub"
android:layout_row="3"
android:layout_column="1"
android:textSize="28sp" />
android:textSize="28sp"
android:onClick="memoryHandle" />

<Button
android:layout_width="wrap_content"
Expand All @@ -91,7 +93,8 @@
android:id="@+id/mr"
android:layout_row="3"
android:layout_column="2"
android:textSize="28sp" />
android:textSize="28sp"
android:onClick="memoryHandle" />

<Button
android:layout_width="wrap_content"
Expand All @@ -100,7 +103,8 @@
android:id="@+id/mc"
android:layout_row="3"
android:layout_column="3"
android:textSize="28sp" />
android:textSize="28sp"
android:onClick="memoryHandle" />

<Button
android:layout_width="wrap_content"
Expand Down