1.布局
外层采用垂直的LinearLayout布局,内层再嵌套五个水平的线性布局,通过设置weightSum和layout_weight属性来设置按钮以及各个嵌套的LinearLayout的比例。以下为activity_main.xml文件的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/text"
/>/ tools:ignore="InvalidId"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="2">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b1"
android:text="C"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b2"
android:text="del"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="4">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b3"
android:text="1"
tools:ignore="Suspicious0dp"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b4"
android:text="2"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b5"
android:text="3"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b6"
android:text="+"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="4">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b7"
android:text="4"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b8"
android:text="5"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b9"
android:text="6"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b10"
android:text="-"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="4">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b11"
android:text="7"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b12"
android:text="8"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b13"
android:text="9"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b14"
android:text="*"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="4">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b15"
android:text="."
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b16"
android:text="0"
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b17"
android:text="="
/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:id="@+id/b18"
android:text="/"
/>
</LinearLayout>
</LinearLayout>
2.逻辑处理
思路:在onClick()方法中加入一个switch循环,每个case是按钮的id,申请一个字符串,每点击一下按钮就将按钮的值存入字符串,当点击等号按钮时,将字符串中所储存的中缀表达时转为后缀表达式进行计算。代码如下:
/*MainActivity.java*/
package com.example.myapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button[] buttons = new Button[18];
private int[] ids = new int[]{R.id.b1, R.id.b2, R.id.b3, R.id.b4, R.id.b5, R.id.b6, R.id.b7, R.id.b8, R.id.b9, R.id.b10, R.id.b11, R.id.b12, R.id.b13, R.id.b14, R.id.b15, R.id.b16, R.id.b17, R.id.b18};
private TextView text;
private StringBuilder pending = new StringBuilder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < ids.length; i++) {
buttons[i] = (Button) findViewById(ids[i]);
buttons[i].setOnClickListener(this);
}
text = (TextView) findViewById(R.id.text);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.b1:
pending = pending.delete(0, pending.length());
text.setText(pending);
break;
case R.id.b2:
if (pending.length() != 0) {
pending = pending.deleteCharAt(pending.length() - 1);
}
text.setText(pending);
break;
case R.id.b3:
pending = pending.append("1");
text.setText(pending);
break;
case R.id.b4:
pending = pending.append("2");
text.setText(pending);
break;
case R.id.b5:
pending = pending.append("3");
text.setText(pending);
break;
case R.id.b6:
pending = pending.append("+");
text.setText(pending);
break;
case R.id.b7:
pending = pending.append("4");
text.setText(pending);
break;
case R.id.b8:
pending = pending.append("5");
text.setText(pending);
break;
case R.id.b9:
pending = pending.append("6");
text.setText(pending);
break;
case R.id.b10:
pending = pending.append("-");
text.setText(pending);
break;
case R.id.b11:
pending = pending.append("7");
text.setText(pending);
break;
case R.id.b12:
pending = pending.append("8");
text.setText(pending);
break;
case R.id.b13:
pending = pending.append("9");
text.setText(pending);
break;
case R.id.b14:
pending = pending.append("*");
text.setText(pending);
break;
case R.id.b15:
pending = pending.append(".");
text.setText(pending);
break;
case R.id.b16:
pending = pending.append("0");
text.setText(pending);
break;
case R.id.b17:
if (pending.length() > 1) {
Count c = new Count();
String result;
try {
String a = c.change(pending);
result = c.calculate(a);
} catch (Exception ex) {
result = "error";
}
text.setText(pending + "=" + result);
pending = pending.delete(0, pending.length());
if (Character.isDigit(result.charAt(0))) {
pending = pending.append(result);
}
}
break;
case R.id.b18:
pending = pending.append("/");
text.setText(pending);
break;
}
}
}
/*Count.java*/
package com.example.myapp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Count {
private static final Map<Character,Integer> basic =new HashMap<Character, Integer>();
static {
basic.put('-',1);
basic.put('+', 1);
basic.put('*', 2);
basic.put('/', 2);
}
public String change(StringBuilder p) {
List<String> queue = new ArrayList<String>(); //定义队列 用于存储 数字 以及最后的 后缀表达式
List<Character> stack = new ArrayList<Character>(); //定义栈 用于存储 运算符 最后stack中会被 弹空
char[] charArr = p.substring(0,p.length()).trim().toCharArray(); //字符数组 用于拆分数字或符号
String standard = "*/+-()"; //判定标准 将表达式中会出现的运算符写出来
char ch = '&'; //在循环中用来保存 字符数组的当前循环变量的 这里仅仅是初始化一个值 没有意义
int len = 0; //用于记录字符长度 【例如100*2,则记录的len为3 到时候截取字符串的前三位就是数字】
for (int i = 0; i < charArr.length; i++) { //开始迭代
ch = charArr[i]; //保存当前迭代变量
if(Character.isDigit(ch)) { //如果当前变量为 数字
len++;
}else if(ch == '.'){ //如果当前变量为 . 会出现在小数里面
len++;
}else if(standard.indexOf(ch) != -1) { //如果是上面标准中的 任意一个符号
if(len > 0) { //长度也有
queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len, i))); //说明符号之前的可以截取下来做数字
len = 0; //长度置空
}
if(ch == '(') { //如果是左括号
stack.add(ch); //将左括号 放入栈中
continue; //跳出本次循环 继续找下一个位置
}
if (!stack.isEmpty()) { //如果栈不为empty
int size = stack.size() - 1; //获取栈的大小-1 即代表栈最后一个元素的下标
boolean flag = false; //设置标志位
while (size >= 0 && ch == ')' && stack.get(size) != '(') { //若当前ch为右括号,则 栈里元素从栈顶一直弹出,直到弹出到 左括号
queue.add(String.valueOf(stack.remove(size))); //注意此处条件:ch并未入栈,所以并未插入队列中;同样直到找到左括号的时候,循环结束了,所以左括号也不会放入队列中【也就是:后缀表达式中不会出现括号】
size--; //size-- 保证下标永远在栈最后一个元素【栈中概念:指针永远指在栈顶元素】
flag = true; //设置标志位为true 表明一直在取()中的元素
}
if(ch==')'&&stack.get(size) == '('){
flag = true;
}
while (size >= 0 && !flag && basic.get(stack.get(size)) >= basic.get(ch)) { //若取得不是()内的元素,并且当前栈顶元素的优先级>=对比元素 那就出栈插入队列
queue.add(String.valueOf(stack.remove(size))); //同样 此处也是remove()方法,既能得到要获取的元素,也能将栈中元素移除掉
size--;
}
}
if(ch != ')') { //若当前元素不是右括号
stack.add(ch); //就要保证这个符号 入栈
} else { //否则就要出栈 栈内符号
stack.remove(stack.size() - 1);
}
}
if(i == charArr.length - 1) { //如果已经走到了 中缀表达式的最后一位
if(len > 0) { //如果len>0 就截取数字
queue.add(String.valueOf(Arrays.copyOfRange(charArr, i - len+1, i+1)));
}
int size = stack.size() - 1; //size表示栈内最后一个元素下标
while (size >= 0) { //一直将栈内 符号全部出栈 并且加入队列中 【最终的后缀表达式是存放在队列中的,而栈内最后会被弹空】
queue.add(String.valueOf(stack.remove(size)));
size--;
}
}
}
String a = queue.toString();
return a.substring(1,a.length()-1);
}
public String calculate(String aa) {
String [] arr = aa.split(", "); //根据, 拆分字符串
List<String> list = new ArrayList<String>(); //用于计算时 存储运算过程的集合【例如list中当前放置 100 20 5 / 则取出20/5 最终将结果4存入list 此时list中结果为 100 4 】
for (int i = 0; i < arr.length; i++) { //此处就是上面说的运算过程, 因为list.remove的缘故,所以取出最后一个数个最后两个数 都是size-2
int size = list.size();
switch (arr[i]) {
case "+": double a = Double.parseDouble(list.remove(size-2))+ Double.parseDouble(list.remove(size-2)); list.add(String.valueOf(a)); break;
case "-": double b = Double.parseDouble(list.remove(size-2))- Double.parseDouble(list.remove(size-2)); list.add(String.valueOf(b)); break;
case "*": double c = Double.parseDouble(list.remove(size-2))* Double.parseDouble(list.remove(size-2)); list.add(String.valueOf(c)); break;
case "/": double d = Double.parseDouble(list.remove(size-2))/ Double.parseDouble(list.remove(size-2)); list.add(String.valueOf(d)); break;
default: list.add(arr[i]); break; //如果是数字 直接放进list中
}
}
return list.size()== 1 ? list.get(0) : "运算失败" ;
}
}