参照的博客:
https://blog.csdn.net/sakurakider/article/details/76283801
https://blog.csdn.net/h992109898/article/details/53025706
O(∩_∩)O哈哈~,第一次写博客不会写,可能有点丑,请见谅。
初始化 StringBuilder pending = new StringBuilder(); 使用StringBuilder()这个类可以进行字符串操作: 使用 String s=""+pending 将 StringBuilder 转换成字符串。
pending.append(c) | 在字符串后加入字符‘c' |
pending.insert(3,c) | 在第三位后加入字符’c' |
pending.delete(0,3) | 删除从0到3位的字符 |
up_btn.xml
down_btn.xml
login_button_selector.xml
(全部都用 LinearLayout 来控制每个控件的位置,没什么技术含量,就是复制粘贴)
package com.example.pc.counter;
//https://blog.csdn.net/sakurakider/article/details/76283801
//https://blog.csdn.net/h992109898/article/details/53025706
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import static com.example.pc.counter.ExpressionToDouble.StringToRes;
import static com.example.pc.counter.jinzhi.eight_to_ten;
import static com.example.pc.counter.jinzhi.sixteen_to_ten;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn_A;
Button btn_B;
Button btn_C;
Button btn_D;
Button btn_E;
Button btn_F;
Button btn_0;
Button btn_1;
Button btn_2;
Button btn_3;
Button btn_4;
Button btn_5;
Button btn_6;
Button btn_7;
Button btn_8;
Button btn_9;
Button btn_add;
Button btn_delect;
Button btn_divide;
Button btn_equal;
Button btn_multiply;
Button btn_zero;
Button btn_sub;
Button btn_ten_two;
Button btn_ten_sixteen;
Button btn_ten_eight;
Button btn_two_ten;
Button btn_sixteen_ten;
Button btn_eight_ten;
Button btn_dialog;
TextView tv;
private StringBuilder pending = new StringBuilder();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
btn_A=(Button)findViewById(R.id.btn_A);
btn_B=(Button)findViewById(R.id.btn_B);
btn_C=(Button)findViewById(R.id.btn_C);
btn_D=(Button)findViewById(R.id.btn_D);
btn_E=(Button)findViewById(R.id.btn_E);
btn_F=(Button)findViewById(R.id.btn_F);
btn_0=(Button)findViewById(R.id.btn_0);
btn_1=(Button)findViewById(R.id.btn_1);
btn_2=(Button)findViewById(R.id.btn_2);
btn_3=(Button)findViewById(R.id.btn_3);
btn_4=(Button)findViewById(R.id.btn_4);
btn_5=(Button)findViewById(R.id.btn_5);
btn_6=(Button)findViewById(R.id.btn_6);
btn_7=(Button)findViewById(R.id.btn_7);
btn_8=(Button)findViewById(R.id.btn_8);
btn_9=(Button)findViewById(R.id.btn_9);
btn_add=(Button)findViewById(R.id.btn_add);
btn_delect=(Button)findViewById(R.id.btn_delect);
btn_divide=(Button)findViewById(R.id.btn_divide);
btn_equal=(Button)findViewById(R.id.btn_equal);
btn_multiply=(Button)findViewById(R.id.btn_multiply);
btn_zero=(Button)findViewById(R.id.btn_zero);
btn_sub=(Button)findViewById(R.id.btn_sub);
btn_ten_two=(Button)findViewById(R.id.btn_ten_two);
btn_ten_sixteen=(Button)findViewById(R.id.btn_ten_sixteen);
btn_ten_eight=(Button)findViewById(R.id.btn_ten_eight);
btn_two_ten=(Button)findViewById(R.id.btn_two_ten);
btn_sixteen_ten=(Button)findViewById(R.id.btn_sixteen_ten);
btn_eight_ten=(Button)findViewById(R.id.btn_eight_ten);
btn_dialog=(Button)findViewById(R.id.dialog);
tv=(TextView)findViewById(R.id.textview);
btn_A.setOnClickListener(this);
btn_B.setOnClickListener(MainActivity.this);
btn_C.setOnClickListener(MainActivity.this);
btn_D.setOnClickListener(MainActivity.this);
btn_E.setOnClickListener(MainActivity.this);
btn_F.setOnClickListener(MainActivity.this);
btn_0.setOnClickListener(this);
btn_1.setOnClickListener(MainActivity.this);
btn_2.setOnClickListener(MainActivity.this);
btn_3.setOnClickListener(MainActivity.this);
btn_4.setOnClickListener(MainActivity.this);
btn_5.setOnClickListener(MainActivity.this);
btn_6.setOnClickListener(MainActivity.this);
btn_7.setOnClickListener(MainActivity.this);
btn_8.setOnClickListener(MainActivity.this);
btn_9.setOnClickListener(MainActivity.this);
btn_add.setOnClickListener(MainActivity.this);
btn_delect.setOnClickListener(MainActivity.this);
btn_divide.setOnClickListener(MainActivity.this);
btn_equal.setOnClickListener(MainActivity.this);
btn_multiply.setOnClickListener(MainActivity.this);
btn_zero.setOnClickListener(MainActivity.this);
btn_sub.setOnClickListener(MainActivity.this);
btn_ten_two.setOnClickListener(MainActivity.this);
btn_ten_sixteen.setOnClickListener(MainActivity.this);
btn_ten_eight.setOnClickListener(MainActivity.this);
btn_two_ten.setOnClickListener(MainActivity.this);
btn_sixteen_ten.setOnClickListener(MainActivity.this);
btn_eight_ten.setOnClickListener(MainActivity.this);
btn_dialog.setOnClickListener(MainActivity.this);
}
@SuppressLint("SetTextI18n")
public void onClick(View v){
int last = 0;
if(pending.length()!=0)
{
last = pending.codePointAt(pending.length()-1);
}
String str= (String) tv.getText();
switch (v.getId()){
case R.id.btn_A:
pending = pending.append("A");
tv.setText( pending);
break;
case R.id.btn_B:
pending = pending.append("B");
tv.setText( pending);
break;
case R.id.btn_C:
pending = pending.append("C");
tv.setText( pending);
break;
case R.id.btn_D:
pending = pending.append("D");
tv.setText( pending);
break;
case R.id.btn_E:
pending = pending.append("E");
tv.setText( pending);
break;
case R.id.btn_F:
pending = pending.append("F");
tv.setText( pending);
break;
case R.id.btn_0:
pending = pending.append("0");
tv.setText( pending);
break;
case R.id.btn_1:
pending = pending.append("1");
tv.setText( pending);
break;
case R.id.btn_2:
pending = pending.append("2");
tv.setText( pending);
break;
case R.id.btn_3:
pending = pending.append("3");
tv.setText( pending);
break;
case R.id.btn_4:
pending = pending.append("4");
tv.setText( pending);
break;
case R.id.btn_5:
pending = pending.append("5");
tv.setText( pending);
break;
case R.id.btn_6:
pending = pending.append("6");
tv.setText( pending);
break;
case R.id.btn_7:
pending = pending.append("7");
tv.setText( pending);
break;
case R.id.btn_8:
pending = pending.append("8");
tv.setText( pending);
break;
case R.id.btn_9:
pending = pending.append("9");
tv.setText( pending);
break;
case R.id.btn_add:
pending = pending.append("+");
tv.setText( pending);
break;
case R.id.btn_delect:
pending = pending.append("-");
tv.setText( pending);
break;
case R.id.btn_divide:
pending = pending.append("÷");
tv.setText( pending);
break;
case R.id.btn_multiply:
pending = pending.append("×");
tv.setText( pending);
break;
case R.id.btn_sub:
if (pending.length() != 0) {
pending = pending.delete(pending.length() - 1, pending.length());
tv.setText( pending);
}
break;
case R.id.btn_zero:
pending = pending.delete(0, pending.length());
tv.setText( "");
break;
case R.id.btn_ten_two:
int a = Integer.parseInt(str);
tv.setText(str+ "\n"+Integer.toBinaryString(a));
pending = pending.delete(0, pending.length());
break;
case R.id.btn_ten_eight:
int b = Integer.parseInt(str);
tv.setText(str+"\n"+ Integer.toOctalString(b));
pending = pending.delete(0, pending.length());
break;
case R.id.btn_ten_sixteen:
int c = Integer.parseInt(str);
tv.setText( str+"\n"+Integer.toHexString(c));
pending = pending.delete(0, pending.length());
break;
case R.id.btn_two_ten:
tv.setText( str+"\n"+Integer.parseInt(str,2));
pending = pending.delete(0, pending.length());
break;
case R.id.btn_eight_ten:
tv.setText( str+"\n"+eight_to_ten(str));
pending = pending.delete(0, pending.length());
break;
case R.id.btn_sixteen_ten:
tv.setText(str+"\n"+ sixteen_to_ten(str));
pending = pending.delete(0, pending.length());
break;
case R.id.btn_equal:
pending = pending.append("=");
String total=""+pending;
tv.setText(StringToRes(total));
pending = pending.delete(0, pending.length());
break;
case R.id.dialog:
showDialog();
}
}
private void showDialog(){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setIcon(R.drawable.counter_bg);
builder.setTitle("分享二维码");
builder.setPositiveButton("我知道了",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setView(R.layout.layout);
AlertDialog dialog=builder.create();
dialog.show();
}
}
这部分是抄别人的代码,里面的注释是我自己的理解,可能是错的。
如果想正确点可以看原博客主,我已经附在上面了。
package com.example.pc.counter;
import java.util.Stack;
public class ExpressionToDouble {
private static boolean isNumber(char ch){
return ch == '.' || (ch >= '0' && ch <= '9');
}
//遇到数字入栈(包括小数点)
private static void hasAddOrSub(Stack s, char opration){
StringBuilder number = new StringBuilder();
while(true){
char cur = s.peek(); //peek()获取栈顶部元素
if (cur == opration)
break;
//当读到运算符时打断
number.insert(0, cur);
s.pop(); //pop()删除数组中最后的数据,数组长度会改变
}
s.pop();
double right = Double.parseDouble(number.toString());
number.replace(0, number.length(), "");
while(!s.isEmpty()){
char cur = (char)s.peek();
number.insert(0, cur);
s.pop();
}
double left = Double.parseDouble(number.toString());
number.replace(0, number.length(), "");
double res = 0;
if (opration == '+')
res = left + right;
else
res = left -right;
String str = res + "";
for(char chr : str.toCharArray()){
s.push(chr);
}
}
//判断是否有加(+)或减(-)
private static void hasMulOrDiv(Stack s, char opration){
StringBuilder number = new StringBuilder();
while(true){
char cur = s.peek();
if (cur == opration)
break;
number.insert(0, cur);
s.pop();
}
s.pop();
double right = Double.parseDouble(number.toString());
number.replace(0, number.length(), "");
while(!s.isEmpty()){
char cur = s.peek();
if (cur == '+' || cur == '-')
break;
number.insert(0, cur);
s.pop();
}
double left = Double.parseDouble(number.toString());
number.replace(0, number.length(), "");
double res = 0;
if (opration == '×')
res = left * right;
else
res = left / right;
String str = res + "";
for(char chr : str.toCharArray()){
s.push(chr);
}
}
//判断是否有乘(*)或除(/)
private static boolean checkInfo(char []datas){
for(int i = 0; i < datas.length-1; i++){
if (!isNumber(datas[i]) && !isNumber(datas[i+1]))
return false;
if (datas[i] == '.' && datas[i+1] == '.')
return false;
}
return true;
}
public static String StringToRes(String info){
Stack s = new Stack();
char []datas = info.toCharArray();
if (!checkInfo(datas))
return "";
boolean hasAdd = false;
boolean hasSub = false;
boolean hasMul = false;
boolean hasDiv = false;
for (char ch : datas){
if (isNumber(ch)){
s.push(ch);
}
else{
switch(ch){
case '+':
if (hasMul){
hasMulOrDiv(s, '×');
hasMul = false;
}
if (hasDiv){
hasMulOrDiv(s, '÷');
hasDiv = false;
}
if (hasAdd){
hasAddOrSub(s, '+');
hasAdd = false;
}
if (hasSub){
hasAddOrSub(s, '-');
hasSub = false;
}
s.push(ch);
hasAdd = true;
break;
case '-':
if (hasMul){
hasMulOrDiv(s, '×');
hasMul = false;
}
if (hasDiv){
hasMulOrDiv(s, '÷');
hasDiv = false;
}
if (hasAdd){
hasAddOrSub(s, '+');
hasAdd = false;
}
if (hasSub){
hasAddOrSub(s, '-');
hasSub = false;
}
s.push(ch);
hasSub = true;
break;
case '×':
if (hasMul){
hasMulOrDiv(s, '×');
hasMul = false;
}
if (hasDiv){
hasMulOrDiv(s, '÷');
hasDiv = false;
}
s.push(ch);
hasMul = true;
break;
case '÷':
if (hasMul){
hasMulOrDiv(s, '×');
hasMul = false;
}
if (hasDiv){
hasMulOrDiv(s, '÷');
hasDiv = false;
}
s.push(ch);
hasDiv = true;
break;
case '=':
if (hasMul){
hasMulOrDiv(s, '×');
hasMul = false;
}
if (hasDiv){
hasMulOrDiv(s, '÷');
hasDiv = false;
}
if (hasAdd){
hasAddOrSub(s, '+');
hasAdd = false;
}
if (hasSub){
hasAddOrSub(s, '-');
hasSub = false;
}
StringBuilder number = new StringBuilder();
while(!s.isEmpty()){
char cur = (char)s.peek();
number.insert(0, cur);
s.pop();
}
return number.toString();
}
}
}
return "";
}
// public static void main(String[] args) {
// System.out.println(StringToRes("78.54-24.34×54+24÷23="));
// }
}
//https://blog.csdn.net/h992109898/article/details/53025706
这正是找不到Java现成的代码,就只好自己写了
package com.example.pc.counter;
public class jinzhi {
public static int sixteen_to_ten(String s){
int num=0;
for(int i=0; i= 'A' && s.charAt(i) <= 'F') {
num = num * 16 + s.charAt(i) - 'A' + 10;
}else {
num = num * 16 + +s.charAt(i) - '0';
}
}
return num;
}
public static int eight_to_ten(String s){
int num = 0;
for(int i=0;i
我的代码还没完善好,有很多bug,不过可以运行。
是不是看了和没看一样,哈哈!