Android计算器编程实现-创新互联

Android 计算器编程实现

创新互联成立于2013年,是专业互联网技术服务公司,拥有项目做网站、成都做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元新会做网站,已为上家服务,为新会各地企业和个人服务,联系电话:18982081108

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

  Calculator

  Settings

  Hello world!

  Clear

  /

  *

  Back

  7

  8

  9

  

  4

  5

  6

  +

  1

  2

  3

  .

  0

  )

  (

  =

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

  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

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章题目:Android计算器编程实现-创新互联
本文地址:http://cdiso.cn/article/cdcgde.html

其他资讯