首页 > 代码库 > Android 计算器编程实现
Android 计算器编程实现
第一步:定义所需的字符 string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Calculator</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="clear">Clear</string>
<string name="chu">/</string>
<string name="cheng">*</string>
<string name="tui">Back</string>
<string name="qi">7</string>
<string name="ba">8</string>
<string name="jiu">9</string>
<string name="jian">—</string>
<string name="si">4</string>
<string name="wu">5</string>
<string name="liu">6</string>
<string name="jia">+</string>
<string name="yi">1</string>
<string name="er">2</string>
<string name="san">3</string>
<string name="dian">.</string>
<string name="ling">0</string>
<string name="kuohao_l">)</string>
<string name="kuohao_r">(</string>
<string name="deng">=</string>
</resources>
第二步:编辑界面 activity.main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout>
<TextView
android:id="@+id/text1"
android:layout_width="320dp"
android:layout_height="60dp"
android:textSize="40sp" />
</TableLayout>
<TableLayout>
<TextView
android:id="@+id/text2"
android:layout_width="320dp"
android:layout_height="60dp"
android:textSize="40sp"/>
</TableLayout>
<TableRow>
<Button
android:id="@+id/button_clear"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/clear"
android:textSize="20sp" />
<Button
android:id="@+id/button_chu"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/chu"
android:textSize="20sp" />
<Button
android:id="@+id/button_cheng"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/cheng"
android:textSize="20sp" />
<Button
android:id="@+id/button_tui"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/tui"
android:textSize="20sp" />
</TableRow>
<TableRow>
<Button
android:id="@+id/button_qi"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/qi"
android:textSize="30sp" />
<Button
android:id="@+id/button_ba"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/ba"
android:textSize="30sp" />
<Button
android:id="@+id/button_jiu"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/jiu"
android:textSize="30sp" />
<Button
android:id="@+id/button_jian"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/jian"
android:textSize="30sp" />
</TableRow>
<TableRow>
<Button
android:id="@+id/button_si"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/si"
android:textSize="30sp" />
<Button
android:id="@+id/button_wu"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/wu"
android:textSize="30sp" />
<Button
android:id="@+id/button_liu"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/liu"
android:textSize="30sp" />
<Button
android:id="@+id/button_jia"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/jia"
android:textSize="30sp" />
</TableRow>
<TableRow>
<Button
android:id="@+id/button_yi"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/yi"
android:textSize="30sp" />
<Button
android:id="@+id/button_er"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/er"
android:textSize="30sp" />
<Button
android:id="@+id/button_san"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/san"
android:textSize="30sp" />
<Button
android:id="@+id/button_dian"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/dian"
android:textSize="30sp" />
</TableRow>
<TableRow>
<Button
android:id="@+id/button_ling"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/ling"
android:textSize="30sp" />
<Button
android:id="@+id/button_kuohao_r"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/kuohao_r"
android:textSize="30sp" />
<Button
android:id="@+id/button_kuohao_l"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/kuohao_l"
android:textSize="30sp" />
<Button
android:id="@+id/button_deng"
android:layout_width="80dp"
android:layout_height="70dp"
android:text="@string/deng"
android:textSize="30sp" />
</TableRow>
</TableLayout>
第三步:编写处理四则运算(包含括号)的类 Yuansuan.class
package com.malakana.cal;
import android.annotation.SuppressLint;
import java.util.*;
public class Yunsuan {
Yunsuan(String str1) {
EvaluateExpression(str1);
}
@SuppressLint("UseValueOf")
public static String EvaluateExpression(String str) {
char[] a;
int i = 0;
a = str.toCharArray();
Stack<Float> OPND = new Stack<Float>();
Stack<Character> OPTR = new Stack<Character>();
OPTR.push(‘=‘);
float number = 0;
int decimalnum = 1;
boolean integer = false;
boolean decimal = false;
while (true) {
if (i == str.length())
break;
if (In(a[i]) == -1) {
number = number * 10 + (a[i] - 48);
integer = true;
if (decimal)
decimalnum = decimalnum * 10;
i++;
} else if (a[i] == ‘.‘) {
if (decimal)
return "ERROR";
decimal = integer = true;
i++;
} else if (In(a[i]) > -1 && In(a[i]) < 7) {
if (In(a[i]) == 1 && (i == 0 || In(a[i - 1]) == 4))
OPND.push(new Float(0));
if (integer) {
OPND.push(new Float(number / decimalnum));
number = 0;
decimalnum = 1;
integer = decimal = false;
}
switch (Precede(In(OPTR.peek()), In(a[i]))) {
case 2:
if (OPND.empty())
return "ERROR";
float x = OPND.pop();
if (OPND.empty())
return "ERROR";
float y = OPND.pop();
char theta = OPTR.pop();
if (In(theta) == 3 && x == 0)
return "ERROR";
OPND.push(new Float(Operate(y, theta, x)));
break;
case 1:
OPTR.pop();
i++;
break;
case 0:
OPTR.push(new Character(a[i]));
i++;
break;
case -1:
return "ERROR";
}
}
}
if (OPND.empty())
return "ERROR";
else
return ("" + OPND.peek());
}
public static int In(char t) {
int i = 0;
if (t > 47 && t < 58)
return -1;
switch (t) {
case ‘+‘:
i = 0;
break;
case ‘-‘:
i = 1;
break;
case ‘*‘:
i = 2;
break;
case ‘/‘:
i = 3;
break;
case ‘(‘:
i = 4;
break;
case ‘)‘:
i = 5;
break;
case ‘=‘:
i = 6;
break;
}
return i;
}
public static int Precede(int t1, int t2) {
int relationship[][] = { { 2, 2, 0, 0, 0, 2, 2 },
{ 2, 2, 0, 0, 0, 2, 2 }, { 2, 2, 2, 2, 0, 2, 2 },
{ 2, 2, 2, 2, 0, 2, 2 }, { 0, 0, 0, 0, 0, 1, -1 },
{ 2, 2, 2, 2, -1, 2, 2 }, { 0, 0, 0, 0, 0, -1, 1 } };
return relationship[t1][t2];
}
public static float Operate(float a, char theta, float b) {
float i = 0;
switch (theta) {
case ‘+‘:
i = a + b;
break;
case ‘-‘:
i = a - b;
break;
case ‘*‘:
i = a * b;
break;
case ‘/‘:
i = a / b;
break;
}
return i;
}
public static void main(String[] args) {
new Yunsuan(null);
}
}
第四部: 编写主类 MianActivity.java
package com.malakana.cal;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class MainActivity extends Activity implements OnClickListener {
String str = "";
String str1 = "";
String suanfa;
TextView text1;
TextView text2;
Button button_jia;
Button button_jian;
Button button_cheng;
Button button_chu;
Button button_clear;
Button button_tui;
Button button_deng;
Button button_kuohao_l;
Button button_kuohao_r;
Button button_dian;
Button button_yi;
Button button_er;
Button button_san;
Button button_si;
Button button_wu;
Button button_liu;
Button button_qi;
Button button_ba;
Button button_jiu;
Button button_ling;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1 = (TextView) findViewById(R.id.text1);
text2 = (TextView) findViewById(R.id.text2);
text1.setTextSize(20);
text2.setTextSize(20);
button_jia = (Button) findViewById(R.id.button_jia);
button_jia.setTextSize(17);
button_jian = (Button) findViewById(R.id.button_jian);
button_jian.setTextSize(17);
button_cheng = (Button) findViewById(R.id.button_cheng);
button_cheng.setTextSize(17);
button_chu = (Button) findViewById(R.id.button_chu);
button_chu.setTextSize(17);
button_dian = (Button) findViewById(R.id.button_dian);
button_dian.setTextSize(17);
button_deng = (Button) findViewById(R.id.button_deng);
button_clear = (Button) findViewById(R.id.button_clear);
button_tui = (Button) findViewById(R.id.button_tui);
button_jian = (Button) findViewById(R.id.button_jian);
button_kuohao_l = (Button) findViewById(R.id.button_kuohao_l);
button_kuohao_r = (Button) findViewById(R.id.button_kuohao_r);
button_yi = (Button) findViewById(R.id.button_yi);
button_er = (Button) findViewById(R.id.button_er);
button_san = (Button) findViewById(R.id.button_san);
button_si = (Button) findViewById(R.id.button_si);
button_wu = (Button) findViewById(R.id.button_wu);
button_liu = (Button) findViewById(R.id.button_liu);
button_qi = (Button) findViewById(R.id.button_qi);
button_ba = (Button) findViewById(R.id.button_ba);
button_jiu = (Button) findViewById(R.id.button_jiu);
button_ling = (Button) findViewById(R.id.button_ling);
button_jia.setOnClickListener(this);
button_jian.setOnClickListener(this);
button_cheng.setOnClickListener(this);
button_chu.setOnClickListener(this);
button_dian.setOnClickListener(this);
button_clear.setOnClickListener(this);
button_tui.setOnClickListener(this);
button_deng.setOnClickListener(this);
button_kuohao_l.setOnClickListener(this);
button_kuohao_r.setOnClickListener(this);
button_yi.setOnClickListener(this);
button_er.setOnClickListener(this);
button_san.setOnClickListener(this);
button_si.setOnClickListener(this);
button_wu.setOnClickListener(this);
button_liu.setOnClickListener(this);
button_qi.setOnClickListener(this);
button_ba.setOnClickListener(this);
button_jiu.setOnClickListener(this);
button_ling.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button listener = (Button) v;
if (listener.getText().equals("1") || listener.getText().equals("2")
|| listener.getText().equals("3")
|| listener.getText().equals("4")
|| listener.getText().equals("5")
|| listener.getText().equals("6")
|| listener.getText().equals("7")
|| listener.getText().equals("8")
|| listener.getText().equals("9")
|| listener.getText().equals("0")
|| listener.getText().equals(".")) {
str = str + listener.getText();
} else if (listener.getText().equals("+")
|| listener.getText().equals("-")
|| listener.getText().equals("*")
|| listener.getText().equals("/")
|| listener.getText().equals("(")
|| listener.getText().equals(")")) {
if (str1.endsWith("=")) {
str1 = "";
}
str = str + listener.getText();
str1 = str1 + str;
str = "";
} else if (listener.getText().equals("=")) {
if(str1.endsWith("=")){
str1="";
}
str = str + listener.getText();
str1 = str1 + str;
str=Yunsuan.EvaluateExpression(str1);
//double m = 0;
//m = CalcStr.calc(str1);
//str1 = str1 + "=";
//str = m + "";
}
else if (listener.getText().equals("Back")) {
if (str.length() >= 1) {
str = str.substring(0, str.length() - 1);
} else {
str1 = "";
str = "";
}
} else if (listener.getText().equals("Clear")) {
str = "";
str1 = "";
}
text2.setText(str);
text1.setText(str1);
text1.setTextSize(TRIM_MEMORY_BACKGROUND);
text2.setTextSize(TRIM_MEMORY_BACKGROUND);
}
}
最后编译实现 run for Android Application
Android 计算器编程实现