Created
January 21, 2011 09:13
-
-
Save kor-trickster/789450 to your computer and use it in GitHub Desktop.
android - 스택 계산기(Stack 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
///////////////////////////////// CalculatorActivity.java //////////////////////////////// | |
package com.hong.Calculator; | |
import java.util.Stack; | |
import java.util.StringTokenizer; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
public class CalculatorActivity extends Activity { | |
/** Called when the activity is first created. */ | |
int num = 0; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
((Button) findViewById(R.id.Button01)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.Button02)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.Button03)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.Button04)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.Button05)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.Button06)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.Button07)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.Button08)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.Button09)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.Button00)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.plus)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.minus)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.mul)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.div)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.result)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.Lefe)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.Right)).setOnClickListener(on_Click); | |
((Button) findViewById(R.id.clear)).setOnClickListener(new Button.OnClickListener(){ | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
EditText et = ((EditText) findViewById(R.id.EditText01)); | |
et.setText("0"); | |
} | |
}); | |
} | |
private View.OnClickListener on_Click = new View.OnClickListener() { | |
public void onClick(View v) { | |
// TODO Auto-generated method stub | |
String id = null; | |
String s; | |
EditText et = ((EditText) findViewById(R.id.EditText01)); | |
if(et.getText().toString().charAt(0) == '0') et.setText(""); | |
switch(v.getId()){ | |
case R.id.Button00 : | |
id = "0"; | |
break; | |
case R.id.Button01 : | |
id = "1"; | |
break; | |
case R.id.Button02 : | |
id = "2"; | |
break; | |
case R.id.Button03 : | |
id = "3"; | |
break; | |
case R.id.Button04 : | |
id = "4"; | |
break; | |
case R.id.Button05 : | |
id = "5"; | |
break; | |
case R.id.Button06 : | |
id = "6"; | |
break; | |
case R.id.Button07 : | |
id = "7"; | |
break; | |
case R.id.Button08 : | |
id = "8"; | |
break; | |
case R.id.Button09 : | |
id = "9"; | |
break; | |
case R.id.plus : | |
id = "+"; | |
break; | |
case R.id.minus : | |
id = "-"; | |
break; | |
case R.id.mul: | |
id = "*"; | |
break; | |
case R.id.div : | |
id = "/"; | |
break; | |
case R.id.result : | |
s = et.getText() + " = " + Calc(et.getText().toString()); | |
et.setText(s); | |
return; | |
case R.id.Lefe : | |
id = "("; | |
break; | |
case R.id.Right : | |
id = ")"; | |
break; | |
default : | |
break; | |
} | |
s = et.getText() + id; | |
et.setText(s); | |
} | |
}; | |
private String Calc(String str){ | |
if(str.indexOf('(') != -1){ | |
int fs = str.indexOf('('); | |
int ls = str.lastIndexOf(')'); | |
String s = Calc(str.substring(fs + 1, ls)); | |
str = str.substring(0, fs) + s + str.substring(ls+1, str.length()); | |
} | |
int cnt = 0; | |
Stack <Integer> Stk_Num = new Stack <Integer>(); | |
StringTokenizer ST_Num = new StringTokenizer(str,"+-/* "); | |
StringTokenizer ST_Oper = new StringTokenizer(str,"1234567890 "); | |
Stk_Num.push(Integer.parseInt(ST_Num.nextToken())); | |
while(ST_Num.hasMoreTokens()){ | |
char oper = ST_Oper.nextToken().charAt(0); | |
String num = ST_Num.nextToken(); | |
int a; | |
if(oper == '*'){ | |
a = Stk_Num.pop(); | |
a *= Integer.parseInt(num); | |
Stk_Num.push(a); | |
} | |
else if(oper == '/'){ | |
a = Stk_Num.pop(); | |
a /= Integer.parseInt(num); | |
Stk_Num.push(a); | |
} | |
else if(oper == '+'){ | |
Stk_Num.push(Integer.parseInt(num)); | |
} | |
else if(oper == '-'){ | |
Stk_Num.push(-1 * (Integer.parseInt(num))); | |
} | |
} | |
while(!Stk_Num.isEmpty()){ | |
cnt += Stk_Num.pop(); | |
} | |
return Integer.toString(cnt); | |
} | |
} |
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
///////////////////////////////// main.xml //////////////////////////////// | |
<?xml version="1.0" encoding="utf-8"?> | |
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:orientation="vertical" android:layout_width="fill_parent" | |
android:layout_height="fill_parent"> | |
<EditText android:text="0" android:id="@+id/EditText01" | |
android:layout_width="wrap_content" android:layout_height="wrap_content" | |
></EditText> | |
<TableRow> | |
<Button android:text="1" android:id="@+id/Button01" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="2" android:id="@+id/Button02" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="3" android:id="@+id/Button03" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="+" android:id="@+id/plus" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="(" android:id="@+id/Lefe" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
</TableRow> | |
<TableRow> | |
<Button android:text="4" android:id="@+id/Button04" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="5" android:id="@+id/Button05" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="6" android:id="@+id/Button06" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="-" android:id="@+id/minus" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text=")" android:id="@+id/Right" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
</TableRow> | |
<TableRow> | |
<Button android:text="7" android:id="@+id/Button07" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="8" android:id="@+id/Button08" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="9" android:id="@+id/Button09" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="*" android:id="@+id/mul" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
</TableRow> | |
<TableRow> | |
<Button android:text="0" android:id="@+id/Button00" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="=" android:id="@+id/result" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="clear" android:id="@+id/clear" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
<Button android:text="/" android:id="@+id/div" | |
android:layout_width="55px" android:layout_height="55px"></Button> | |
</TableRow> | |
</TableLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment