android计算器源码

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

这是我本人做的Android 简单计算器源代码。

可以实现普通四则运算2,10,16进制转换。

常见异常处理,可以实现activity屏幕跳转。

算法用逆波兰实现。

本项目所有代码不涉及任何知识产权问题,完全由自己学习android时候写的,所以各位可以放心下载。

AndroidManifest.xml代码
<?xml version="1.0"encoding="utf-8"?>
<manifest xmlns:android="/apk/res/android"
package="my.calu"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/meinv"
android:label="@string/app_name">
<activity android:name=".Main"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category
android:name="UNCHER"/>
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="4"/>
</manifest>
//layout 代码
<?xml version="1.0"encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="/apk/res/android">
<Button
android:id="@+id/binary_sign"
android:layout_width="73px"
android:layout_height="52px"
android:text="10/2"
android:layout_x="14dp"
android:layout_y="148dp"/>
<Button
android:id="@+id/sexadecimal_sign"
android:layout_width="73px"
android:layout_height="52px"
android:text="10/16"
android:layout_x="88dp"
android:layout_y="148dp"/>
<Button
android:id="@+id/add_sign"
android:layout_width="73px"
android:layout_height="52px"
android:text="+"
android:layout_x="240dp"
android:layout_y="382dp"/>
<EditText
android:id="@+id/editview"
android:layout_width="295px"
android:layout_height="76px"
android:text=""
android:textSize="18sp"
android:layout_x="8dp"
android:layout_y="14dp"/>
<Button
android:id="@+id/number0"
android:layout_width="73px"
android:layout_height="52px"
android:text="0"
android:layout_x="89dp"
android:layout_y="382dp"/> <Button
android:id="@+id/point_sign"
android:layout_width="73px"
android:layout_height="52px"
android:text="."
android:layout_x="10dp"
android:layout_y="382dp"/> <Button
android:id="@+id/sub_sign"
android:layout_width="73px"
android:layout_height="52px"
android:text="-"
android:layout_x="240dp"
android:layout_y="322dp"/> <Button
android:id="@+id/mult_sign"
android:layout_width="73px"
android:layout_height="52px"
android:text="*"
android:layout_x="240dp"
android:layout_y="262dp"/> <Button
android:id="@+id/dev_sign"
android:layout_width="73px"
android:layout_height="52px"
android:text="÷"
android:layout_x="240dp"
android:layout_y="202dp"/> <Button
android:id="@+id/number3"
android:layout_width="73px"
android:layout_height="52px"
android:text="3"
android:layout_x="170dp"
android:layout_y="322dp"/> <Button
android:id="@+id/number6"
android:layout_width="73px"
android:layout_height="52px"
android:text="6"
android:layout_x="170dp"
android:layout_y="262dp"/> <Button
android:id="@+id/equal_sign"
android:layout_width="73px"
android:layout_height="52px"
android:text="="
android:layout_x="167dp"
android:layout_y="382dp"/> <Button
android:id="@+id/number1"
android:layout_width="73px"
android:layout_height="52px"
android:text="1"
android:layout_x="10dp"
android:layout_y="322dp"/> <Button
android:id="@+id/number5"
android:layout_width="73px"
android:layout_height="52px"
android:text="5"
android:layout_x="90dp"
android:layout_y="262dp"/> <Button
android:id="@+id/number4"
android:layout_width="73px"
android:layout_height="52px"
android:text="4"
android:layout_x="10dp"
android:layout_y="262dp"/> <Button
android:id="@+id/number9"
android:layout_width="73px"
android:layout_height="52px"
android:text="9"
android:layout_x="170dp"
android:layout_y="202dp"/>
<Button
android:id="@+id/number8"
android:layout_width="73px"
android:layout_height="52px"
android:text="8"
android:layout_x="90dp"
android:layout_y="202dp"/>
<Button
android:id="@+id/number7"
android:layout_width="73px"
android:layout_height="52px"
android:text="7"
android:layout_x="10dp"
android:layout_y="202dp"/>
<Button
android:id="@+id/number2"
android:layout_width="73px"
android:layout_height="52px"
android:text="2"
android:layout_x="90dp"
android:layout_y="322dp"/>
<Button
android:id="@+id/clear_sign"
android:layout_width="138px"
android:layout_height="wrap_content"
android:text="C"
android:layout_x="174dp"
android:layout_y="148dp"/>
</AbsoluteLayout>
主代码
package my.calu;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
//package expression;
import java.util.ArrayList;
import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main extends Activity implements OnClickListener {
private static final char Str = 0;
private Button
number0,number1,number2,number3,number4,number5,number6,number7,numbe r8,number9;
private Button
binary_sign,sexadecimal_sign,clear_sign,point_sign,add_sign,sub_sign, mult_sign,dev_sign,equal_sign;
private EditText edittext;
private String buffer="";
private boolean first_is_number=false ,
after_equal=false,flage_10zhijing=true,flage_16zhijing=true;
//初始化事件
public void init() {
this.edittext=(EditText) findViewById(R.id.editview);
this.edittext.setOnClickListener(this);
this.number0=(Button) findViewById(R.id.number0);
this.number0.setOnClickListener(this);
this.number1=(Button) findViewById(R.id.number1);
this.number1.setOnClickListener(this);
this.number2=(Button) findViewById(R.id.number2);
this.number2.setOnClickListener(this);
this.number3=(Button) findViewById(R.id.number2);
this.number0.setOnClickListener(this);
this.number3=(Button) findViewById(R.id.number3);
this.number3.setOnClickListener(this);
this.number4=(Button) findViewById(R.id.number4);
this.number4.setOnClickListener(this);
this.number5=(Button) findViewById(R.id.number5);
this.number5.setOnClickListener(this);
this.number6=(Button) findViewById(R.id.number6);
this.number6.setOnClickListener(this);
this.number7=(Button) findViewById(R.id.number7);
this.number7.setOnClickListener(this);
this.number8=(Button) findViewById(R.id.number8);
this.number8.setOnClickListener(this);
this.number9=(Button) findViewById(R.id.number9);
this.number9.setOnClickListener(this);
this.point_sign=(Button) findViewById(R.id.point_sign);
this.point_sign.setOnClickListener(this);
this.equal_sign=(Button) findViewById(R.id.equal_sign);
this.equal_sign.setOnClickListener(this);
this.add_sign=(Button) findViewById(R.id.add_sign);
this.add_sign.setOnClickListener(this);
this.sub_sign=(Button) findViewById(R.id.sub_sign);
this.sub_sign.setOnClickListener(this);
this.mult_sign=(Button) findViewById(R.id.mult_sign);
this.mult_sign.setOnClickListener(this);
this.dev_sign=(Button) findViewById(R.id.dev_sign);
this.dev_sign.setOnClickListener(this);
this.clear_sign=(Button) findViewById(R.id.clear_sign);
this.clear_sign.setOnClickListener(this);
this.binary_sign=(Button) findViewById(R.id.binary_sign);
this.binary_sign.setOnClickListener(this);
this.sexadecimal_sign=(Button)
findViewById(R.id.sexadecimal_sign);
this.sexadecimal_sign.setOnClickListener(this);
}
//判断字符串是否为全为0-9的整形数
public boolean is10Numeric(String str){
Pattern pattern = pile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
//判断字符串是否为全为0-1的整形数
public boolean is2Numeric(String str){
Pattern pattern = pile("[0-1]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
//判断字符串是否为全为0-f的整形数
public boolean is16Numeric(String str){
Pattern pattern = pile("[0-f]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}
//判断最后一位和第一位是否为数字
public boolean isnumber ( String s) {
int i=s.length();
char c=s.charAt(i-1);
char k=s.charAt(0);
// System.out.println("---firstnumber--"+k);
// System.out.println("---70c=charAt--"+c);
if(s.equals(null)) {
return false;
}
if((c >= '0' && c <= '9')&& (k >='0' && k <= '9') ){
return true;
}else
return false;
}
//判断是否有运算符
public boolean have_signal(String s){
after_equal=false;
boolean have=s.contains("+");
if(s.contains("+")||s.contains("-") || s.contains("*") || s.contains("/"))
return true;
else
return false;
}
//-------堆栈算法---------
public void deal_equal (String s)
{
// System.out.println("Hello World!");
//中缀 => 后缀表达式SSSssss
// String s = "8-3*5"; //中缀
String S = ""; //后缀
char[] Operators = new char[s.length()];
int Top = -1;
for (int i = 0; i < s.length(); i++)
{
char C = s.charAt(i);
// System.out.print(C);
switch(C)
{
case' ' :
b reak;
case'+' : //操作符
case'-' :
while (Top >= 0) //栈不为空时
{
char c = Operators[Top--]; //pop Operator
if (c == '(')
{
Operators[++Top] = c; //push Operator
break;
}
else
{
S = S + c;
}
}
O perators[++Top] = C; //push Operator
S += " ";
break;
case'*' : //操作符
case'/' :
w hile (Top >= 0) //栈不为空时
{
char c = Operators[Top--]; //pop Operator
if (c == '(')
{
Operators[++Top] = c; //push Operator
break;
}
else
{
if (c == '+' || c == '-')
{
Operators[++Top] = c; //push Operator
break;
}
else
{
S = S + c;
}
}
}
O perators[++Top] = C; //push Operator
S += " ";
b reak;
case'(' : //操作符
Operators[++Top] = C;
S += " ";
break;
case')' : //操作符
while (Top >= 0) //栈不为空时
{
char c = Operators[Top--]; //pop Operator
if (c == '(')
{
break;
}
else
{
S = S + c;
}
}
S += " ";
break;
default : //操作数
S = S + C;
break;
}
}
while (Top >= 0)
{
S = S + Operators[Top--]; //pop Operator
}
// System.out.print(Operators);
// System.out.println(S); //后缀
//后缀表达式计算
double[] Operands = new double[S.length()];
// System.out.println(S.length());
double x, y, v;
Top = - 1;
String Operand = "";
for (int i = 0; i < S.length(); i++)
{
char c = S.charAt(i);
if ((c >= '0' && c <= '9') || c == '.')
{
Operand += c;
// System.out.print(Operand+" ");
}
if ((c == ' ' && Operand != "") || i == S.length()) {
Operands[++Top] = ng.Double.parseDouble(Operand) ; //push Operands
Operand = "";
}
if (c == '+' || c == '-' || c == '*' || c == '/')
{
if ((Operand != ""))
{
Operands[++Top] = ng.Double.parseDouble(Operand) ;//push Operands
// System.out.println(Operators);
Operand = "";
}
y = Operands[Top--]; //pop 双目运算符的第二操作数 (后进先出)注意操作数顺序对除法的影响
x = Operands[Top--]; //pop 双目运算符的第一操作数
switch (c)
{
case'+' :
v = x + y;
break;
case'-' :
v = x - y;
break;
case'*' :
v = x * y;
break;
case'/' :
v = x / y; // 第一操作数 / 第二操作数注意操作数顺序对除法的影响break;
default :
v = 0;
break;
}
Operands[++Top] = v; //push 中间结果再次入栈
}
}
v = Operands[Top--]; //pop 最终结果
// System.out.println("-------result-------"+v);
String result=v+"";
this.edittext.setText(result);
}
/** Called when the activity is first created. */
//------------------函数入口--wangwei-------------
//@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(yout.main);
this.init();
//跳转
// Intent intent=new Intent();
//
// Intent intent=TwoActivity.this.getlntent(); // Bundle bundle=intent.getExtras();
// String name=bundle.getString(buffer);
//
}
//触发事件后的动作
// @Override
public void onClick(View v) {
try {
// TODO Auto-generated method stub
if(v.getId()==this.number0.getId()) {
buffer=buffer+0;
this.edittext.setText(buffer);
// first_is_number=true;
}else if(v.getId()==this.number1.getId()) { buffer=buffer+1;
this.edittext.setText(buffer);
// first_is_number=true;
}else if(v.getId()==this.number2.getId()) { buffer=buffer+2;
this.edittext.setText(buffer);
}else if(v.getId()==this.number3.getId()) { buffer=buffer+3;
this.edittext.setText(buffer);
// first_is_number=true;
}else if(v.getId()==this.number4.getId()) { buffer=buffer+4;
this.edittext.setText(buffer);
// first_is_number=true;
}else if(v.getId()==this.number5.getId()) { buffer=buffer+5;
this.edittext.setText(buffer);
}else if(v.getId()==this.number6.getId()) {
buffer=buffer+6;
this.edittext.setText(buffer);
// first_is_number=true;
}else if(v.getId()==this.number7.getId()) {
buffer=buffer+7;
this.edittext.setText(buffer);
// first_is_number=true;
}else if(v.getId()==this.number8.getId()) {
buffer=buffer+8;
this.edittext.setText(buffer);
first_is_number=true;
}else if(v.getId()==this.number9.getId()) {
buffer=buffer+9;
this.edittext.setText(buffer);
// first_is_number=true;
//---------------2进制的转换-----------
}else if(v.getId()==this.binary_sign.getId()) { boolean is10=is10Numeric(buffer);
boolean is2=is2Numeric(buffer);
if(is10&&(flage_10zhijing==true))
{
int num=Integer.parseInt(buffer);
flage_10zhijing=false;
buffer=Integer.toBinaryString(num)+"" ;
System.out.println("is10"+flage_10zhijing);
}else if (is2&&(flage_10zhijing==false)){
flage_10zhijing=true;
buffer=Integer.valueOf(buffer,2).toString()+"" ;
}else {
//System.out.println(" 不是10进制也不是二进制");
//buffer="0000000000";
}
this.edittext.setText(buffer);
//----------------16进制的转换--------------
}else if(v.getId()==this.sexadecimal_sign.getId()) { boolean is16=false,is10=false;
is16=is16Numeric(buffer);
is10=is10Numeric(buffer);
System.out.println("is16 --- "+is16);
System.out.println("flage_16zhijing"+flage_16zhijing);
if(is10&&flage_16zhijing)
{
int num=Integer.parseInt(buffer);
flage_16zhijing=false;
buffer=Integer.toHexString(num) +"" ;
System.out.println("jinruis10 "+flage_16zhijing);
}else if ((is16==true)&&(flage_16zhijing==false)){
System.out.println("jinruis16 "+flage_16zhijing);
flage_16zhijing=true;
buffer=Integer.valueOf(buffer,16).toString() +"" ;
//buffer="0000000000";
}else {
//System.out.println(" 不是16进制也不是1 0进制");
//buffer="0000000000";
}
this.edittext.setText(buffer);
//-------------四则运算-------------
}else if(v.getId()==this.point_sign.getId()) {
boolean isnumber;
isnumber= isnumber ( buffer);
if(isnumber)
buffer=buffer+".";
this.edittext.setText(buffer);
}else if(v.getId()==this.sub_sign.getId()) {
boolean isnumber;
isnumber= isnumber ( buffer);
if(isnumber)
buffer=buffer+"-";
this.edittext.setText(buffer);
}else if(v.getId()==this.mult_sign.getId()) {
boolean isnumber;
isnumber= isnumber ( buffer);
if(isnumber)
buffer=buffer+"*";
this.edittext.setText(buffer);
}else if(v.getId()==this.dev_sign.getId()) {
boolean isnumber;
isnumber= isnumber ( buffer);
if(isnumber)
buffer=buffer+"/";
this.edittext.setText(buffer);
}else if(v.getId()==this.add_sign.getId()) {
boolean isnumber;
isnumber= isnumber ( buffer);
if(isnumber){
buffer=buffer+"+";
this.edittext.setText(buffer);
}else {
//buffer="000000000000";
this.edittext.setText(buffer);
}
//-------------------清除-------------------
}else if(v.getId()==this.clear_sign.getId()) {
// if(after_equal) {
// int len=buffer.length();
// if(len >=1) {
// buffer= (String )buffer.subSequence(0, len-1); // }
//
// } else {
// buffer="";
// }
buffer="";
this.edittext.setText(buffer);
//--------------计算结果------------------------}else if(v.getId()==this.equal_sign.getId()) {
boolean isnumber;
if(buffer.equals(null)) {
buffer=" ";
this.edittext.setText(buffer);
}else {
isnumber= isnumber ( buffer);
boolean have_signal=have_signal(buffer);
if(isnumber&&have_signal){
after_equal=true;
deal_equal(buffer);
}
}
}
} catch (Exception e) {
// TODO: handle exception
}
// System.out.println("-----327------"+buffer);
}
}。

相关文档
最新文档