写了一个简单的AndroidStudio的计算器程序。(带优先级的)
把代码贴一下,相互学习。
成果展示图片:
开机时:
一级运算:
二级运算:
含优先级运算:
自动更新末尾运算符:
输入错误(自动纠错):
贴代码处~:(懒得写注释了,如果有问题可以留言问一下~)
大致项目思路,就是布局-》更改ID-》创建变量-》添加事件-》填写事件!
计算器的算法思路是:自己手写一个字符栈以及数值栈,设置好优先级,在运算符的字符栈底装在最低优先级的‘#’字符,然后再待计算的算式末尾添加‘#’字符,分别用于运算符的比较以及运算的结束。
以下是代码~~
activity_XML代码,即布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.calculatororororooror.MainActivity">
<EditText
android:id="@+id/output"
android:text="What the hell???!!"
android:layout_width="300dp"
android:layout_height="60dp"
android:layout_marginTop="40dp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:editable="false"
android:background="#fffbaf"
/>
<Button
android:id="@+id/jia"
android:text="+"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:layout_marginRight="110dp"
android:layout_alignRight="@+id/jian"
android:layout_below="@id/output"
android:background="#ff0900"/>
<Button
android:id="@+id/jian"
android:text="-"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignBottom="@id/jia"
android:layout_centerHorizontal="true"
android:background="#ff0900"/>
<Button
android:id="@+id/chen"
android:text="*"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignBottom="@+id/jia"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@id/jian"
android:background="#ff0900"/>
<Button
android:id="@+id/id7"
android:text="7"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_below="@id/jia"
android:layout_marginRight="110dp"
android:layout_alignRight="@id/jian"
android:background="#01aaff"/>
<Button
android:id="@+id/id8"
android:text="8"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignBottom="@id/id7"
android:layout_centerHorizontal="true"
android:background="#01aaff"/>
<Button
android:id="@+id/id9"
android:text="9"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignBottom="@id/id7"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@id/jian"
android:background="#01aaff"/>
<Button
android:id="@+id/id4"
android:text="4"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_below="@id/id7"
android:layout_marginRight="110dp"
android:layout_alignRight="@id/jian"
android:background="#01aaff"/>
<Button
android:id="@+id/id5"
android:text="5"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignBottom="@id/id4"
android:layout_centerHorizontal="true"
android:background="#01aaff"/>
<Button
android:id="@+id/id6"
android:text="6"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignBottom="@id/id4"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@id/jian"
android:background="#01aaff"/>
<Button
android:id="@+id/id1"
android:text="1"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_below="@id/id4"
android:layout_marginRight="110dp"
android:layout_alignRight="@id/jian"
android:background="#01aaff"/>
<Button
android:id="@+id/id2"
android:text="2"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignBottom="@id/id1"
android:layout_centerHorizontal="true"
android:background="#01aaff"/>
<Button
android:id="@+id/id3"
android:text="3"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignBottom="@id/id1"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@id/jian"
android:background="#01aaff"/>
<Button
android:id="@+id/clear"
android:text="AC"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_below="@id/id1"
android:layout_marginRight="110dp"
android:layout_alignRight="@id/jian"
android:background="#ff0900"/>
<Button
android:id="@+id/id0"
android:text="0"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignBottom="@id/clear"
android:layout_centerHorizontal="true"
android:background="#01aaff"/>
<Button
android:id="@+id/cal"
android:text="="
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignBottom="@id/clear"
android:layout_marginLeft="40dp"
android:layout_toRightOf="@id/jian"
android:background="#ff8001"/>
</RelativeLayout>
MainActivity.java代码(即计算器代码):
package com.example.administrator.calculatororororooror;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//这里只能加声明,并不能使用函数
EditText output;
Button id0;
Button id1;
Button id2;
Button id3;
Button id4;
Button id5;
Button id6;
Button id7;
Button id8;
Button id9;
Button jia;
Button jian;
Button chen;
//Button chu;
//Button del;
Button clear;
Button cal;
iStack s1=new iStack(); //s1是值栈,s2是符号栈
cStack s2=new cStack();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
output=(EditText)findViewById(R.id.output);
id0=(Button)findViewById(R.id.id0);
id1=(Button)findViewById(R.id.id1);
id2=(Button)findViewById(R.id.id2);
id3=(Button)findViewById(R.id.id3);
id4=(Button)findViewById(R.id.id4);
id5=(Button)findViewById(R.id.id5);
id6=(Button)findViewById(R.id.id6);
id7=(Button)findViewById(R.id.id7);
id8=(Button)findViewById(R.id.id8);
id9=(Button)findViewById(R.id.id9);
jia=(Button)findViewById(R.id.jia);
jian=(Button)findViewById(R.id.jian);
chen=(Button)findViewById(R.id.chen);
//chu=(Button)findViewById(R.id.chu);
//del=(Button)findViewById(R.id.del);
clear=(Button)findViewById(R.id.clear);
cal=(Button)findViewById(R.id.cal);
id0.setOnClickListener(this); //为此,我的类加了个接口
id1.setOnClickListener(this);
id2.setOnClickListener(this);
id3.setOnClickListener(this);
id4.setOnClickListener(this);
id5.setOnClickListener(this);
id6.setOnClickListener(this);
id7.setOnClickListener(this);
id8.setOnClickListener(this);
id9.setOnClickListener(this);
jia.setOnClickListener(this);
jian.setOnClickListener(this);
chen.setOnClickListener(this);
//chu.setOnClickListener(this);
clear.setOnClickListener(this);
//del.setOnClickListener(this);
cal.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String str=output.getText().toString();
switch(v.getId()) {
case R.id.jia:
case R.id.jian:
case R.id.chen:
//case R.id.chu:
int len=str.length();
if(len!=0)
{
char lastcode=str.charAt(len-1);
if(lastcode<48||lastcode>57)
str=str.substring(0,len-1);
}
case R.id.id0:
case R.id.id1:
case R.id.id2:
case R.id.id3:
case R.id.id4:
case R.id.id5:
case R.id.id6:
case R.id.id7:
case R.id.id8:
case R.id.id9:
str=str+((Button)v).getText();
output.setText(str);
break;
case R.id.clear:
str="";
output.setText(str);
break;
//case R.id.del:
case R.id.cal:
getAns();
break;
}
}
public void getAns()
{
String str=output.getText().toString()+'#';
int len=str.length();
if(len==1)return;
if(str.charAt(0) < 48 || str.charAt(0) > 57)
{
str=str.substring(1,len-1);
output.setText(str);
return;
}
if(str.charAt(len-2) < 48 || str.charAt(len-2) > 57)
{
str=str.substring(0,len-2);
output.setText(str);
return;
}
s2.push('#');
int n=0; //n是表示扫描到的数字值为多少,初始化为0
boolean book=false; //没办法,用来当做n取值的有效值,true有效
for(int i=0;i<len;i++) {
char c = str.charAt(i);
if (c >= 48 && c <= 57) {
n = n * 10 + c - 48;
book = true;
} else if (c == '+' || c == '-' || c == '*' || c == '/' || c == '#' ) {
if (book == true) {
s1.push(n); //遇到符号就入栈,并清零
n = 0;
book = false;
}
if (compare(c)) { //如果当前优先级要低就开始计算
int n2 = s1.top(); //3+5,则n1为3,n2为5
s1.pop();
;
int n1 = s1.top();
s1.pop();
;
char op = s2.top();
s2.pop();
int ans = 0;
switch (op) {
case '+':
ans = n1 + n2;
break;
case '-':
ans = n1 - n2;
break;
case '*':
ans = n1 * n2;
break;
case '/':
ans = n1 / n2;
break;
}
s1.push(ans);
i--; //下一轮继续比
} else {
s2.push(c);
}
}
else {
output.setText("");
return;
}
}
str=s1.top()+"";
output.setText(str);
s1.clear();
s2.clear();
}
boolean compare(char t)
{
int n1,n2; //n1是栈顶的优先级,n2是当前扫到符号的优先级,#为0,+、-为1,*、/为2
if(t=='*'||t=='/')n2=2;
else if(t=='+'||t=='-') n2=1;
else n2=0;
t=s2.top();
if(t=='*'||t=='/')n1=2;
else if(t=='+'||t=='-') n1=1;
else n1=0;
return n2<n1; //当前符号优先级小于栈顶元素的时候开始计算
}
}
class iStack{
private int value[]=new int[100];
private int top;
iStack()
{
top=-1;
}
public void push(int c)
{
value[++top]=c;
}
public void pop()
{
top--;
}
public boolean empty()
{
return top==-1?true:false;
}
public void clear()
{
top=-1;
}
public int top()
{
return value[top];
}
};
class cStack{
private char value[]=new char[100];
private int top;
cStack()
{
top=-1;
}
public void push(char c)
{
value[++top]=c;
}
public void pop()
{
top--;
}
public boolean empty()
{
return top==-1?true:false;
}
public void clear()
{
top=-1;
}
public char top()
{
return value[top];
}
};