Android编写计算器(Java语言)真菜鸟食用
双非大学大二菜鸟在老师和男朋友的帮助下,艰难的写出了一个勉强能用的计算器,虽然写的很烂,但比较“适合菜鸟理解”,希望能够帮助到向我一样菜到不行的人。
一、计算器的编写思想
我身边的同学有使用双栈编写的,但是我的数据结构学习的很差,所以采用了男朋友教的“动态数组”编写。
1.创建3组ArrayList,分别用于存储数字、运算符、数字+运算符(创建2组当然更好了,一组存储数字+运算符,另一组用于计算,但是我菜啊zzz)
2.使用num_add()、ope_add()方法将数字、运算符分类操作,存入num_List、ope_List、mix_List。
3.使用show()在TextView中显示mix_ope信息。
4.使用caculate()按照“先乘除后加减”原则,对mix_List内的数据进行运算,并重新存入。
5.点击“=”后,使用equ()调用caculate()、show(),计算并显示运算后的信息。
6.编写其他方法,如cln()清空、back()退格等。
二、构建步骤
1.先从创建项目开始吧
我用的IDEA,不过用啥都能做。
我选择的是Empty Activity
文件的位置一定要存好,千万别乱存。虽然技术差,但是编程的好习惯一定要养好。
Language别选成Kotlin
2.再搞搞UI小脸蛋
外行人看热闹,内行人看门道。作为菜鸟来说,程序有一张好脸就胜过千言万语了。
我的计算器长这样,结构和配色查一查就行。(男朋友说计算器的颜色应该取一些冷色调,显得理智,狗男人事真多)
布局代码:
提示:类似 android:background="@drawable/round_tv" 代码表示为在drawable文件夹下创建一个xml文件,内部设置按钮、TextView等样式,再在布局文件中作为背景调用。
具体见:
https://blog.csdn.net/m0_46651408/article/details/117043479?spm=1001.2014.3001.5501
布局说明:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#377375">
<!-- 文字部分-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ll_top"
android:layout_marginTop="50dp"
android:orientation="vertical">
<TextView
android:id="@+id/demo_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="60sp"
android:height="260dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="right"
android:background="@drawable/round_tv"
/>
</LinearLayout>
<!-- 按钮部分-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ll_btm"
android:layout_below="@id/ll_top"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/btn_c"
android:text="AC"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="40dp"
android:background="@drawable/round2_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_back"
android:text="▶"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="40dp"
android:background="@drawable/round2_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_pow"
android:text="x²"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="40dp"
android:background="@drawable/round2_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_ptg"
android:text="%"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="40dp"
android:background="@drawable/round2_button"
android:layout_marginRight="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp">
<Button
android:id="@+id/btn_7"
android:text="7"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_8"
android:text="8"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_9"
android:text="9"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_div"
android:text="÷"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp">
<Button
android:id="@+id/btn_4"
android:text="4"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_5"
android:text="5"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_6"
android:text="6"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_mult"
android:text="×"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp">
<Button
android:id="@+id/btn_1"
android:text="1"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_2"
android:text="2"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_3"
android:text="3"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_minus"
android:text="-"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp">
<Button
android:id="@+id/btn_0"
android:text="0"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_point"
android:text="·"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_equal"
android:text="="
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/btn_plus"
android:text="+"
android:textSize="20dp"
android:layout_width="85dp"
android:layout_height="85dp"
android:background="@drawable/round_button"
android:layout_marginRight="10dp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
3.编写Activity内的智慧结晶
输入数字有三种情况需要考虑,如输入最开始的数字,输入运算符后的数字,输入一个两位数及以上的数字。
输入运算符有两种情况需要考虑,如输数字后的运算符,输入+后按下-自动更改
水平太差,也只实现了简单的加减乘除,至于小数点等其他功能暂没有开发,而且不停地在转换数值,多多包涵
package com.example.demo;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
private TextView demo_tv;
//creat num\ope\mix_List
private ArrayList<Long> num_List = new ArrayList<Long>();
private ArrayList<Character> ope_List = new ArrayList<Character>();
private ArrayList<String> mix_List = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.caculator_layout);
//设置监听事件
{
findViewById(R.id.btn_0).setOnClickListener(this);
findViewById(R.id.btn_1).setOnClickListener(this);
findViewById(R.id.btn_2).setOnClickListener(this);
findViewById(R.id.btn_3).setOnClickListener(this);
findViewById(R.id.btn_4).setOnClickListener(this);
findViewById(R.id.btn_5).setOnClickListener(this);
findViewById(R.id.btn_6).setOnClickListener(this);
findViewById(R.id.btn_7).setOnClickListener(this);
findViewById(R.id.btn_8).setOnClickListener(this);
findViewById(R.id.btn_9).setOnClickListener(this);
findViewById(R.id.btn_c).setOnClickListener(this);
findViewById(R.id.btn_minus).setOnClickListener(this);
findViewById(R.id.btn_plus).setOnClickListener(this);
findViewById(R.id.btn_ptg).setOnClickListener(this);
findViewById(R.id.btn_equal).setOnClickListener(this);
findViewById(R.id.btn_div).setOnClickListener(this);
findViewById(R.id.btn_mult).setOnClickListener(this);
findViewById(R.id.btn_back).setOnClickListener(this);
findViewById(R.id.btn_point).setOnClickListener(this);
findViewById(R.id.btn_pow).setOnClickListener(this);
demo_tv = (TextView) findViewById(R.id.demo_tv);
demo_tv.setMovementMethod(ScrollingMovementMethod.getInstance());//设置文本域可以滑动
}
}
@Override
public void onClick(View v) {
//getID ->distribute num/ope_add + other
switch (v.getId()) {
case R.id.btn_0:
num_add(0L);
break;
case R.id.btn_1:
num_add(1L);
break;
case R.id.btn_2:
num_add(2L);
break;
case R.id.btn_3:
num_add(3L);
break;
case R.id.btn_4:
num_add(4L);
break;
case R.id.btn_5:
num_add(5L);
break;
case R.id.btn_6:
num_add(6L);
break;
case R.id.btn_7:
num_add(7L);
break;
case R.id.btn_8:
num_add(8L);
break;
case R.id.btn_9:
num_add(9L);
break;
case R.id.btn_plus:
ope_add('+');
break;
case R.id.btn_minus:
ope_add('-');
break;
case R.id.btn_mult:
ope_add('×');
break;
case R.id.btn_div:
ope_add('÷');
break;
case R.id.btn_ptg:
ope_add('%');
break;
case R.id.btn_equal:
equ();
break;
case R.id.btn_c:
cln();
break;
case R.id.btn_back:
back();
break;
case R.id.btn_point: //虚假的功能
break;
case R.id.btn_pow: //虚假的功能
break;
}
}
public void num_add(long num) {
if (num_List.size() == 0 || ope_List.size() == num_List.size()) {// _1 +1
num_List.add(num);
mix_List.add(String.valueOf(num));
}
else{// 11
long numplus = num_List.get(num_List.size() - 1) * 10;
num_List.set(num_List.size() - 1, numplus + num);
mix_List.set(mix_List.size() - 1, String.valueOf(numplus + num));
}
show(mix_List);
}
public void ope_add(char ope) {
if (num_List.size()==0)
;
else {
if (ope_List.size() < num_List.size()) {//1+
ope_List.add(ope);
mix_List.add(String.valueOf(ope));
} else if (ope_List.size() == num_List.size()) {//+ -> -
ope_List.set(ope_List.size() - 1, ope);
mix_List.set(mix_List.size() - 1, String.valueOf(ope));
}
}
show(mix_List);
}
public String calculate() {
for (int j = 0; j < mix_List.size(); j++){//多嵌套一层for循环,有利于运算,说不上来为啥,但是少了这个for偶尔运算步骤太多一口气处理不完
for (int i = 0; i < mix_List.size(); i++) {//先乘除
if (mix_List.get(i).equals("×")) {
String con = String.valueOf (Long.parseLong(mix_List.get(i - 1)) * Long.parseLong(mix_List.get(i + 1)));
mix_List.set(i-1,con);
mix_List.remove(i);
mix_List.remove(i);//delete i -> now (i) == before(i+1)
}else if (mix_List.get(i).equals("÷")){
if (mix_List.get(i+1).equals("0")){// cannot execute a/0
break;
}else {
String con = String.valueOf(Long.parseLong(mix_List.get(i - 1)) / Long.parseLong(mix_List.get(i + 1)));
mix_List.set(i-1,con);
mix_List.remove(i);
mix_List.remove(i);
}
}else if (mix_List.get(i).equals("%")){
String con = String.valueOf(Long.parseLong(mix_List.get(i - 1)) % Long.parseLong(mix_List.get(i + 1)));
mix_List.set(i-1,con);
mix_List.remove(i);
mix_List.remove(i);
}
}
for (int i = 0; i < mix_List.size(); i++) {//后加减
if (mix_List.get(i).equals("+")) {
String con = String.valueOf(Long.parseLong(mix_List.get(i - 1)) + Long.parseLong(mix_List.get(i + 1)));
mix_List.set(i-1,con);
mix_List.remove(i);
mix_List.remove(i);
}else if (mix_List.get(i).equals("-")) {
String con = String.valueOf(Long.parseLong(mix_List.get(i - 1)) - Long.parseLong(mix_List.get(i + 1)));
mix_List.set(i - 1, con);
mix_List.remove(i);
mix_List.remove(i);
}
}
}
return (mix_List.toString());
}
public void cln(){//清空
num_List.clear();
ope_List.clear();
mix_List.clear();
demo_tv.setText("0");
}
public void back(){//退格
if(mix_List.size()==0)
;
else {
if (num_List.size()==ope_List.size()){//退运算符 1+ -> 1
ope_List.remove(ope_List.size()-1);
mix_List.remove(mix_List.size()-1);
} else if (num_List.get(num_List.size()-1)>=10){//退个位数 11 -> 1
long str = num_List.get(num_List.size()-1)/10;
num_List.set(num_List.size()-1,str);
mix_List.set(mix_List.size()-1, String.valueOf(str));
} else{//退数字 1+1 -> 1+
num_List.remove(num_List.size()-1);
mix_List.remove(mix_List.size()-1);
}
}
show(mix_List);
}
public void equ(){//等于
if (ope_List.size()==0){ //only _1
return;
}
if (num_List.size()==ope_List.size()){ // 1+1+ -> 1+1
mix_List.remove(mix_List.size()-1);
ope_List.remove(ope_List.size()-1);
}
calculate();
show(mix_List);
}
public void show(ArrayList list){
if (list.size()==0){
demo_tv.setText("0");
}else {
StringBuffer sb = new StringBuffer();
for (int i = 0;i<mix_List.size();i++)
sb.append(mix_List.get(i));
demo_tv.setText(sb);
}
}
}
总结
好不容易对编程产生了兴趣,一定要好好坚持!!