第一步:定义所需的字符  string.xml


<?xml version="1.0" encoding="utf-8"?>


    Calculator

    Settings

    Hello world!

    Clear

    /

    *

    Back

    7

    8

    9

   

    4

    5

    6

    +

    1

    2

    3

    .

    0

    )

    (

    =




第二步:编辑界面  activity.main.xml


<?xml version="1.0" encoding="utf-8"?>

    android:layout_width="match_parent"

    android:layout_height="match_parent" >


   


       

            android:id="@+id/text1"

            android:layout_width="320dp"

            android:layout_height="60dp"

            android:textSize="40sp" />

   


   


       

            android:id="@+id/text2"

            android:layout_width="320dp"

            android:layout_height="60dp" 

            android:textSize="40sp"/>

   


   


       

            android:id="@+id/button_clear"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/clear"

            android:textSize="20sp" />


       

            android:id="@+id/button_chu"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/chu"

            android:textSize="20sp" />


       

            android:id="@+id/button_cheng"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/cheng"

            android:textSize="20sp" />


       

            android:id="@+id/button_tui"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/tui"

            android:textSize="20sp" />

   


   


       

            android:id="@+id/button_qi"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/qi"

            android:textSize="30sp"  />


       

            android:id="@+id/button_ba"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/ba" 

            android:textSize="30sp" />


       

            android:id="@+id/button_jiu"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/jiu"

            android:textSize="30sp"  />


       

            android:id="@+id/button_jian"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/jian" 

            android:textSize="30sp" />

   


   


       

            android:id="@+id/button_si"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/si"

            android:textSize="30sp"  />


       

            android:id="@+id/button_wu"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/wu"

            android:textSize="30sp"  />


       

            android:id="@+id/button_liu"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/liu" 

            android:textSize="30sp" />


       

            android:id="@+id/button_jia"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/jia"

            android:textSize="30sp"  />

   


   


       

            android:id="@+id/button_yi"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/yi"

            android:textSize="30sp"  />


       

            android:id="@+id/button_er"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/er"

            android:textSize="30sp"  />


       

            android:id="@+id/button_san"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/san"

            android:textSize="30sp"  />


       

            android:id="@+id/button_dian"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/dian" 

            android:textSize="30sp" />

   


   


       

            android:id="@+id/button_ling"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/ling"

            android:textSize="30sp"  />


       

            android:id="@+id/button_kuohao_r"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/kuohao_r" 

            android:textSize="30sp" />


       

            android:id="@+id/button_kuohao_l"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/kuohao_l" 

            android:textSize="30sp" />


       

            android:id="@+id/button_deng"

            android:layout_width="80dp"

            android:layout_height="70dp"

            android:text="@string/deng"

            android:textSize="30sp"  />

   



第三步:编写处理四则运算(包含括号)的类 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 OPND = new Stack();

Stack OPTR = new Stack();

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


更多相关文章

  1. 高斯模糊效果移植到android系统
  2. Android(安卓)OpenGL 编写简单滤镜
  3. 如何进行Android单元测试
  4. 编写android jni代码时遇到的问题
  5. 编写android jni代码时遇到的问题
  6. Android平台上的计算器APP(付源码)
  7. Android(安卓)单独抽取 WebRtc-NS/NSX(音频降噪) 模块
  8. Android(安卓)jni的调用过程JNI_OnLoad(),利用Android(安卓)NDK
  9. Android小项目——简单计算器的实现

随机推荐

  1. Android中,在C++层使用TinyXML解析XML文件
  2. 分享自学Java,Web,Android视频教程资源(自
  3. android ListView美化-->几个比较特别的
  4. Android(安卓)应用程序窗体显示状态操作(r
  5. Android MP4取得播放时长的方法
  6. Android如何动态更换桌面图标(巨坑)
  7. android基础知识12:android自动化测试06―
  8. 第10章 Android的消息机制
  9. Android信任Https自签名证书详细教程
  10. Android AOP(二):AspectJ在Android中实现A