Android?Studio實(shí)現(xiàn)帶三角函數(shù)對(duì)數(shù)運(yùn)算功能的高級(jí)計(jì)算器
用AndroidStudio編寫高級(jí)計(jì)算器帶三角函數(shù)對(duì)數(shù)運(yùn)算功能
界面效果圖:
layout布局 activity_jisuanqi.xml代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="fill_parent" ? ? android:layout_height="fill_parent" ? ? android:orientation="vertical" ? ? android:padding="5dp" > ? ? <LinearLayout ? ? ? ? android:layout_width="fill_parent" ? ? ? ? android:layout_height="fill_parent" ? ? ? ? android:layout_weight="2" ? ? ? ? android:orientation="vertical" > ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? android:layout_height="100dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:orientation="vertical" ? ? ? ? ? ? android:paddingBottom="5dp" > ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:id="@+id/text0" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1.2" ? ? ? ? ? ? ? ? android:gravity="right" ? ? ? ? ? ? ? ? android:scrollHorizontally="true" ? ? ? ? ? ? ? ? android:textColor="#ff000000" ? ? ? ? ? ? ? ? android:textSize="25sp" /> ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:id="@+id/text1" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:gravity="right" ? ? ? ? ? ? ? ? android:scrollHorizontally="true" ? ? ? ? ? ? ? ? android:text="0" ? ? ? ? ? ? ? ? android:textColor="#ff000000" ? ? ? ? ? ? ? ? android:textSize="30sp" /> ? ? ? ? </LinearLayout> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="21dp" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:orientation="vertical" > ? ? ? ? </LinearLayout> ? ? </LinearLayout> ? ? <LinearLayout ? ? ? ? android:layout_width="fill_parent" ? ? ? ? android:layout_height="fill_parent" ? ? ? ? android:layout_weight="1" ? ? ? ? android:orientation="vertical"> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:orientation="horizontal"> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/factorial" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="!" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/power" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="^" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/square_root" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="√" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/pai" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:textAllCaps="false" ? ? ? ? ? ? ? ? android:text="π" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/c" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:textAllCaps="false" ? ? ? ? ? ? ? ? android:text="C" ? ? ? ? ? ? ? ? android:textColor="#ffff0000" /> ? ? ? ? </LinearLayout> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:orientation="horizontal"> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/sin" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:textAllCaps="false" ? ? ? ? ? ? ? ? android:text="sin" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/brackets1" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="(" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/brackets2" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text=")" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/e" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:textAllCaps="false" ? ? ? ? ? ? ? ? android:text="e" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/del" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="DEL" /> ? ? ? ? </LinearLayout> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:orientation="horizontal"> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/cos" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:textAllCaps="false" ? ? ? ? ? ? ? ? android:text="cos" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/seven" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="7" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/eight" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="8" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/nine" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="9" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/division" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="÷" /> ? ? ? ? </LinearLayout> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:orientation="horizontal"> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/tan" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:textAllCaps="false" ? ? ? ? ? ? ? ? android:text="tan" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/four" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="4" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/five" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="5" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/six" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="6" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/multiple" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="×" /> ? ? ? ? </LinearLayout> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:orientation="horizontal"> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/ln" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:textAllCaps="false" ? ? ? ? ? ? ? ? android:text="ln" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/one" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="1" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/two" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="2" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/three" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="3" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/subtract" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="-" /> ? ? ? ? </LinearLayout> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? android:orientation="horizontal"> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/log" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:textAllCaps="false" ? ? ? ? ? ? ? ? android:text="log" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/zero" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="0" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/point" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="." /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/add" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="+" /> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/eq" ? ? ? ? ? ? ? ? android:layout_width="fill_parent" ? ? ? ? ? ? ? ? android:layout_height="fill_parent" ? ? ? ? ? ? ? ? android:layout_weight="1" ? ? ? ? ? ? ? ? android:text="=" /> ? ? ? ? </LinearLayout> ? ? </LinearLayout> </LinearLayout>
java類JisuanqiActivity代碼:
package com.xueba.jsq; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.MenuItem.OnMenuItemClickListener; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class JisuanqiActivity extends Activity implements OnClickListener { ?? ?/** Called when the activity is first created. */ ?? ?private String string = "0"; ?? ?private Button btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btn0; ?? ?private Button btnadd, btnsub, btnmul, btndiv, btnsin, btncos, btntan; ?? ?private Button btnln, btnlog, btne, btneq, btnpoint, btndel, btnpower; ?? ?private Button btnsq, btnpai, btnc, btnb1, btnb2, btnfa; ?? ?private TextView btnt0, btnt1; ?? ?private boolean flag = false; ?? ?private GetValue getValue = new GetValue(); ?? ?private Judge judge = new Judge(); ?? ?private void init() { ?? ??? ?btn1 = (Button) this.findViewById(R.id.one); ?? ??? ?btn2 = (Button) this.findViewById(R.id.two); ?? ??? ?btn3 = (Button) this.findViewById(R.id.three); ?? ??? ?btn4 = (Button) this.findViewById(R.id.four); ?? ??? ?btn5 = (Button) this.findViewById(R.id.five); ?? ??? ?btn6 = (Button) this.findViewById(R.id.six); ?? ??? ?btn7 = (Button) this.findViewById(R.id.seven); ?? ??? ?btn8 = (Button) this.findViewById(R.id.eight); ?? ??? ?btn9 = (Button) this.findViewById(R.id.nine); ?? ??? ?btn0 = (Button) this.findViewById(R.id.zero); ?? ??? ?btnadd = (Button) this.findViewById(R.id.add); ?? ??? ?btnsub = (Button) this.findViewById(R.id.subtract); ?? ??? ?btnmul = (Button) this.findViewById(R.id.multiple); ?? ??? ?btndiv = (Button) this.findViewById(R.id.division); ?? ??? ?btnsin = (Button) this.findViewById(R.id.sin); ?? ??? ?btncos = (Button) this.findViewById(R.id.cos); ?? ??? ?btntan = (Button) this.findViewById(R.id.tan); ?? ??? ?btnln = (Button) this.findViewById(R.id.ln); ?? ??? ?btnlog = (Button) this.findViewById(R.id.log); ?? ??? ?btne = (Button) this.findViewById(R.id.e); ?? ??? ?btneq = (Button) this.findViewById(R.id.eq); ?? ??? ?btnpoint = (Button) this.findViewById(R.id.point); ?? ??? ?btndel = (Button) this.findViewById(R.id.del); ?? ??? ?btnpower = (Button) this.findViewById(R.id.power); ?? ??? ?btnfa = (Button) this.findViewById(R.id.factorial); ?? ??? ?btnsq = (Button) this.findViewById(R.id.square_root); ?? ??? ?btnpai = (Button) this.findViewById(R.id.pai); ?? ??? ?btnc = (Button) this.findViewById(R.id.c); ?? ??? ?btnb1 = (Button) this.findViewById(R.id.brackets1); ?? ??? ?btnb2 = (Button) this.findViewById(R.id.brackets2); ?? ??? ?btnt0 = (TextView) this.findViewById(R.id.text0); ?? ??? ?btnt1 = (TextView) this.findViewById(R.id.text1); ?? ??? ?this.btn0.setOnClickListener(this); ?? ??? ?this.btn1.setOnClickListener(this); ?? ??? ?this.btn2.setOnClickListener(this); ?? ??? ?this.btn3.setOnClickListener(this); ?? ??? ?this.btn4.setOnClickListener(this); ?? ??? ?this.btn5.setOnClickListener(this); ?? ??? ?this.btn6.setOnClickListener(this); ?? ??? ?this.btn7.setOnClickListener(this); ?? ??? ?this.btn8.setOnClickListener(this); ?? ??? ?this.btn9.setOnClickListener(this); ?? ??? ?this.btnadd.setOnClickListener(this); ?? ??? ?this.btnb1.setOnClickListener(this); ?? ??? ?this.btnb2.setOnClickListener(this); ?? ??? ?this.btnc.setOnClickListener(this); ?? ??? ?this.btncos.setOnClickListener(this); ?? ??? ?this.btndel.setOnClickListener(this); ?? ??? ?this.btndiv.setOnClickListener(this); ?? ??? ?this.btne.setOnClickListener(this); ?? ??? ?this.btneq.setOnClickListener(this); ?? ??? ?this.btnfa.setOnClickListener(this); ?? ??? ?this.btnln.setOnClickListener(this); ?? ??? ?this.btnlog.setOnClickListener(this); ?? ??? ?this.btnmul.setOnClickListener(this); ?? ??? ?this.btnpai.setOnClickListener(this); ?? ??? ?this.btnpoint.setOnClickListener(this); ?? ??? ?this.btnpower.setOnClickListener(this); ?? ??? ?this.btnsin.setOnClickListener(this); ?? ??? ?this.btnsq.setOnClickListener(this); ?? ??? ?this.btnsub.setOnClickListener(this); ?? ??? ?this.btntan.setOnClickListener(this); ?? ?} ?? ?@Override ?? ?public void onCreate(Bundle savedInstanceState) { ?? ??? ?super.onCreate(savedInstanceState); ?? ??? ?setContentView(R.layout.activity_jisuanqi); ?? ??? ?this.init(); ?? ?} ?? ?public boolean onCreateOptionsMenu(Menu menu) { ?? ??? ?MenuItem exit = menu.add("EXIT"); ?? ??? ?exit.setOnMenuItemClickListener(new OnMenuItemClickListener() { ?? ??? ??? ?@Override ?? ??? ??? ?public boolean onMenuItemClick(MenuItem item) { ?? ??? ??? ??? ?finish(); ?? ??? ??? ??? ?return true; ?? ??? ??? ?} ?? ??? ?}); ?? ??? ?return true; ?? ?} ?? ?@Override ?? ?public void onClick(View v) { ?? ??? ?if ("error".equals(btnt1.getText().toString()) ?? ??? ??? ??? ?|| "∞".equals(btnt1.getText().toString())) { ?? ??? ??? ?string = "0"; ?? ??? ?} ?? ??? ?if (v == this.btn0) { ?? ??? ??? ?string = judge.digit_judge(string, "0", flag); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btn1) { ?? ??? ??? ?string = judge.digit_judge(string, "1", flag); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btn2) { ?? ??? ??? ?string = judge.digit_judge(string, "2", flag); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btn3) { ?? ??? ??? ?string = judge.digit_judge(string, "3", flag); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btn4) { ?? ??? ??? ?string = judge.digit_judge(string, "4", flag); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btn5) { ?? ??? ??? ?string = judge.digit_judge(string, "5", flag); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btn6) { ?? ??? ??? ?string = judge.digit_judge(string, "6", flag); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btn7) { ?? ??? ??? ?string = judge.digit_judge(string, "7", flag); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btn8) { ?? ??? ??? ?string = judge.digit_judge(string, "8", flag); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btn9) { ?? ??? ??? ?string = judge.digit_judge(string, "9", flag); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btneq) { ?? ??? ??? ?btnt0.setText(string + "="); ?? ??? ??? ?string = getValue.advanced_dispose(string); ?? ??? ??? ?string = judge.digit_dispose(string); ?? ??? ??? ?flag = true; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btnadd) { ?? ??? ??? ?string = judge.judge(string, "+"); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btnsub) { ?? ??? ??? ?string = judge.judge(string, "-"); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btnmul) { ?? ??? ??? ?string = judge.judge(string, "×"); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btndiv) { ?? ??? ??? ?string = judge.judge(string, "÷"); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btnb1) { ?? ??? ??? ?if ("0".equals(string) || flag) { ?? ??? ??? ??? ?string = "("; ?? ??? ??? ?} else ?? ??? ??? ??? ?string += "("; ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btnb2) { ?? ??? ??? ?if ("0".equals(string) || flag) { ?? ??? ??? ??? ?string = ")"; ?? ??? ??? ?} else ?? ??? ??? ??? ?string += ")"; ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btne) { ?? ??? ??? ?if ("0".equals(string)) { ?? ??? ??? ??? ?string = "e"; ?? ??? ??? ?} else ?? ??? ??? ??? ?string += "e"; ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btnpower) { ?? ??? ??? ?string += "^"; ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btnfa) { ?? ??? ??? ?string += "!"; ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btnsq) { ?? ??? ??? ?if ("0".equals(string) || flag) ?? ??? ??? ??? ?string = ""; ?? ??? ??? ?string += "√"; ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btnpai) { ?? ??? ??? ?if ("0".equals(string) || flag) { ?? ??? ??? ??? ?string = "π"; ?? ??? ??? ?} else ?? ??? ??? ??? ?string += "π"; ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btnsin) { ?? ??? ??? ?if ("0".equals(string) || flag) { ?? ??? ??? ??? ?string = "sin("; ?? ??? ??? ?} else ?? ??? ??? ??? ?string += "sin("; ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btncos) { ?? ??? ??? ?if ("0".equals(string) || flag) { ?? ??? ??? ??? ?string = "cos("; ?? ??? ??? ?} else ?? ??? ??? ??? ?string += "cos("; ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btntan) { ?? ??? ??? ?if ("0".equals(string) || flag) { ?? ??? ??? ??? ?string = "tan("; ?? ??? ??? ?} else ?? ??? ??? ??? ?string += "tan("; ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btnln) { ?? ??? ??? ?if ("0".equals(string) || flag) { ?? ??? ??? ??? ?string = "ln("; ?? ??? ??? ?} else ?? ??? ??? ??? ?string += "ln("; ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btnpoint) { ?? ??? ??? ?string = judge.judge1(string); ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btnlog) { ?? ??? ??? ?if ("0".equals(string) || flag) { ?? ??? ??? ??? ?string = "log("; ?? ??? ??? ?} else ?? ??? ??? ??? ?string += "log("; ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btndel) { ?? ??? ??? ?if (flag) { ?? ??? ??? ??? ?string = ""; ?? ??? ??? ??? ?btnt0.setText(string); ?? ??? ??? ??? ?string = "0"; ?? ??? ??? ??? ?btnt1.setText(string); ?? ??? ??? ?} ?? ??? ??? ?if (!"0".equals(string)) { ?? ??? ??? ??? ?string = string.substring(0, string.length() - 1); ?? ??? ??? ??? ?if (0 == string.length()) ?? ??? ??? ??? ??? ?string = "0"; ?? ??? ??? ?} ?? ??? ??? ?flag = false; ?? ??? ??? ?btnt1.setText(string); ?? ??? ?} else if (v == this.btnc) { ?? ??? ??? ?string = ""; ?? ??? ??? ?btnt0.setText(string); ?? ??? ??? ?string = "0"; ?? ??? ??? ?btnt1.setText(string); ?? ??? ??? ?flag = false; ?? ??? ?} ?? ?} }
java類GetValue代碼:
package com.xueba.jsq; import java.util.LinkedList; import java.util.List; public class GetValue { ?? ?public String bracke_dispose(String string) { ?? ??? ?// 處理括號(hào)里的表達(dá)式,思路, ?? ??? ?// 1.遇到正括號(hào)就將它在字符串里的索引保存在一個(gè)鏈表里<插在鏈表頭> ?? ??? ?// 2.遇到第一個(gè)反括號(hào)就處理這個(gè)括號(hào)里的表達(dá)式<取出鏈表第一個(gè)元素,然后根據(jù)這兩個(gè)索引取出表達(dá)式,會(huì)調(diào)用一個(gè)專門處理 ?? ??? ?// log,ln,sin,cos,tan等的special_dispose函數(shù),將反回一個(gè)自由加減乘除的表達(dá)式,在調(diào)用alg_dispose函數(shù)> ?? ??? ?// 3.函數(shù)處理后返回一個(gè)可能還含有l(wèi)og,ln,sin,cos,tan等的但沒有括號(hào)的表達(dá)式; ?? ??? ?int flag = 0, flag1 = 0, i = 0; ?? ??? ?int leng = string.length(); ?? ??? ?List<Integer> list = new LinkedList<Integer>(); ?? ??? ?Character c; ?? ??? ?String str = "", tmp = "", tmp3 = "", tmp4 = ""; ?? ??? ?while (true) { ?? ??? ??? ?if (i >= leng) ?? ??? ??? ??? ?break; ?? ??? ??? ?c = string.charAt(i); ?? ??? ??? ?if ('(' == c) ?? ??? ??? ??? ?list.add(0, i); ?? ??? ??? ?if (')' == c) { ?? ??? ??? ??? ?if (0 == list.size()) { ?? ??? ??? ??? ??? ?System.out.println(")error"); ?? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?str = string.substring(list.get(0) + 1, i); ?? ??? ??? ??? ?tmp = this.special_dispose(str); ?? ??? ??? ??? ?tmp = this.alg_dispose(str); ?? ??? ??? ??? ?if ("error".equals(tmp)) ?? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ?tmp3 = string.substring(0, list.get(0)); ?? ??? ??? ??? ?tmp4 = string.substring(i + 1, leng); ?? ??? ??? ??? ?string = tmp3 + tmp + tmp4; ?? ??? ??? ??? ?flag = str.length() + 2; ?? ??? ??? ??? ?flag1 = tmp.length(); ?? ??? ??? ??? ?leng += (flag1 - flag); ?? ??? ??? ??? ?i += (flag1 - flag); ?? ??? ??? ??? ?list.remove(0); ?? ??? ??? ?} ?? ??? ??? ?i++; ?? ??? ?} ?? ??? ?if (list.size() != 0) { ?? ??? ??? ?System.out.println("(error"); ?? ??? ??? ?return "error"; ?? ??? ?} ?? ??? ?return string; ?? ?} ?? ?public String special_dispose(String string) { ?? ??? ?// 用于處理特殊表達(dá)式; ?? ??? ?if ("error".equals(string)) { ?? ??? ??? ?return "error"; ?? ??? ?} ?? ??? ?string = dispose(string, "sin"); ?? ??? ?string = dispose(string, "cos"); ?? ??? ?string = dispose(string, "tan"); ?? ??? ?string = dispose(string, "log"); ?? ??? ?string = dispose(string, "ln"); ?? ??? ?string = dispose(string, "!"); ?? ??? ?string = dispose(string, "^"); ?? ??? ?string = dispose(string, "√"); ?? ??? ?string = dispose(string, "e"); ?? ??? ?return string; ?? ?} ?? ?public String alg_dispose(String string) { ?? ??? ?// 處理加減乘除 ?? ??? ?string = special_dispose(string); ?? ??? ?if ("error".equals(string)) { ?? ??? ??? ?return "error"; ?? ??? ?} ?? ??? ?Character c; ?? ??? ?String str = ""; ?? ??? ?List<Double> list = new LinkedList<Double>(); ?? ??? ?double tmp = 0; ?? ??? ?boolean add_flag = false; ?? ??? ?for (int i = 0; i < string.length(); i++) { ?? ??? ??? ?c = string.charAt(i); ?? ??? ??? ?if (!Judge.paiduan(c)) { ?? ??? ??? ??? ?str += c; ?? ??? ??? ?} else { ?? ??? ??? ??? ?if (str.length() != 0) ?? ??? ??? ??? ??? ?list.add(new Double(str)); ?? ??? ??? ??? ?str = ""; ?? ??? ??? ??? ?switch (c) { ?? ??? ??? ??? ??? ?case '+': ?? ??? ??? ??? ??? ??? ?if (2 == list.size()) { ?? ??? ??? ??? ??? ??? ??? ?if (add_flag) { ?? ??? ??? ??? ??? ??? ??? ??? ?tmp = list.get(0) + list.get(1); ?? ??? ??? ??? ??? ??? ??? ??? ?list.clear(); ?? ??? ??? ??? ??? ??? ??? ??? ?list.add(tmp); ?? ??? ??? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ??? ??? ?tmp = list.get(0) - list.get(1); ?? ??? ??? ??? ??? ??? ??? ??? ?list.clear(); ?? ??? ??? ??? ??? ??? ??? ??? ?list.add(tmp); ?? ??? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?add_flag = true; ?? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ??? ?case '-': ?? ??? ??? ??? ??? ??? ?if (2 == list.size()) { ?? ??? ??? ??? ??? ??? ??? ?if (add_flag) { ?? ??? ??? ??? ??? ??? ??? ??? ?tmp = list.get(0) + list.get(1); ?? ??? ??? ??? ??? ??? ??? ??? ?list.clear(); ?? ??? ??? ??? ??? ??? ??? ??? ?list.add(tmp); ?? ??? ??? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ??? ??? ?tmp = list.get(0) - list.get(1); ?? ??? ??? ??? ??? ??? ??? ??? ?list.clear(); ?? ??? ??? ??? ??? ??? ??? ??? ?list.add(tmp); ?? ??? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ??? ?add_flag = false; ?? ??? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ??? ?case '×': ?? ??? ??? ??? ??? ??? ?if (i == (string.length() - 1)) ?? ??? ??? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ??? ??? ?while (!(i == (string.length() - 1)) ?? ??? ??? ??? ??? ??? ??? ??? ?&& !Judge.paiduan((c = string.charAt(i + 1)))) { ?? ??? ??? ??? ??? ??? ??? ?str += c; ?? ??? ??? ??? ??? ??? ??? ?i++; ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?if (str.length() != 0) ?? ??? ??? ??? ??? ??? ??? ?list.add(new Double(str)); ?? ??? ??? ??? ??? ??? ?str = ""; ?? ??? ??? ??? ??? ??? ?if (list.size() == 2) { ?? ??? ??? ??? ??? ??? ??? ?tmp = list.get(0) * list.get(1); ?? ??? ??? ??? ??? ??? ??? ?list.clear(); ?? ??? ??? ??? ??? ??? ??? ?list.add(0, tmp); ?? ??? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ??? ?tmp = list.get(2) * list.get(1); ?? ??? ??? ??? ??? ??? ??? ?list.remove(2); ?? ??? ??? ??? ??? ??? ??? ?list.remove(1); ?? ??? ??? ??? ??? ??? ??? ?list.add(tmp); ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ??? ?case '÷': ?? ??? ??? ??? ??? ??? ?if (i == (string.length() - 1)) ?? ??? ??? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ??? ??? ?while (!(i == (string.length() - 1)) ?? ??? ??? ??? ??? ??? ??? ??? ?&& !Judge.paiduan((c = string.charAt(i + 1)))) { ?? ??? ??? ??? ??? ??? ??? ?str += c; ?? ??? ??? ??? ??? ??? ??? ?i++; ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?if (str.length() != 0) ?? ??? ??? ??? ??? ??? ??? ?list.add(new Double(str)); ?? ??? ??? ??? ??? ??? ?str = ""; ?? ??? ??? ??? ??? ??? ?if (list.size() == 2) { ?? ??? ??? ??? ??? ??? ??? ?tmp = list.get(0) / list.get(1); ?? ??? ??? ??? ??? ??? ??? ?list.clear(); ?? ??? ??? ??? ??? ??? ??? ?list.add(0, tmp); ?? ??? ??? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ??? ??? ?tmp = list.get(2) / list.get(1); ?? ??? ??? ??? ??? ??? ??? ?list.remove(2); ?? ??? ??? ??? ??? ??? ??? ?list.remove(1); ?? ??? ??? ??? ??? ??? ??? ?list.add(tmp); ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ??? ?default: ?? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if (str.length() != 0) ?? ??? ??? ?list.add(new Double(str)); ?? ??? ?if (2 == list.size()) { ?? ??? ??? ?if (add_flag) { ?? ??? ??? ??? ?tmp = list.get(0) + list.get(1); ?? ??? ??? ??? ?list.clear(); ?? ??? ??? ??? ?list.add(tmp); ?? ??? ??? ?} else { ?? ??? ??? ??? ?tmp = list.get(0) - list.get(1); ?? ??? ??? ??? ?list.clear(); ?? ??? ??? ??? ?list.add(tmp); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if (list.size() != 0) ?? ??? ??? ?string = "" + list.get(0); ?? ??? ?return string; ?? ?} ?? ?public String get_r_digit(String string, int index) { ?? ??? ?// 得到index右邊的第一個(gè)數(shù); ?? ??? ?String tmp = ""; ?? ??? ?int length = string.length(), j = index; ?? ??? ?Character character; ?? ??? ?if (index >= length) ?? ??? ??? ?tmp = "error"; ?? ??? ?while (true) { ?? ??? ??? ?if (j == length) ?? ??? ??? ??? ?break; ?? ??? ??? ?character = string.charAt(j); ?? ??? ??? ?if (Character.isDigit(character) || (character == '.')) { ?? ??? ??? ??? ?tmp += character; ?? ??? ??? ?} else ?? ??? ??? ??? ?break; ?? ??? ??? ?j++; ?? ??? ?} ?? ??? ?if (0 == tmp.length()) ?? ??? ??? ?tmp = "error"; ?? ??? ?System.out.println(tmp); ?? ??? ?return tmp; ?? ?} ?? ?public String get_l_digit(String string, int index) { ?? ??? ?// 得到index左邊的第一個(gè)數(shù); ?? ??? ?String tmp = ""; ?? ??? ?int j = index; ?? ??? ?Character c; ?? ??? ?if (index < 0) ?? ??? ??? ?tmp = "error"; ?? ??? ?while (true) { ?? ??? ??? ?if (j < 0) ?? ??? ??? ??? ?break; ?? ??? ??? ?c = string.charAt(j); ?? ??? ??? ?if (Character.isDigit(c) || (c == '.')) { ?? ??? ??? ??? ?tmp = "" + c + tmp; ?? ??? ??? ?} else ?? ??? ??? ??? ?break; ?? ??? ??? ?j--; ?? ??? ?} ?? ??? ?if (0 == tmp.length()) ?? ??? ??? ?tmp = "error"; ?? ??? ?return tmp; ?? ?} ?? ?public String dispose1(String string) { ?? ??? ?// 將π替換為對(duì)應(yīng)的數(shù)字; ?? ??? ?int index, leng = string.length(); ?? ??? ?double tmp = Math.PI; ?? ??? ?while (leng != 0) { ?? ??? ??? ?index = string.indexOf("π"); ?? ??? ??? ?if (-1 == index) ?? ??? ??? ??? ?break; ?? ??? ??? ?if ((index != 0 && (index + 1) != leng) ?? ??? ??? ??? ??? ?&& Character.isDigit(string.charAt(index + 1)) ?? ??? ??? ??? ??? ?&& Character.isDigit(string.charAt(index - 1))) { ?? ??? ??? ??? ?string = string.replaceFirst("π", "×" + tmp + "×"); ?? ??? ??? ?} else if (index != 0 ?? ??? ??? ??? ??? ?&& Character.isDigit(string.charAt(index - 1))) { ?? ??? ??? ??? ?string = string.replaceFirst("π", "×" + tmp); ?? ??? ??? ?} else if (index != 0 ?? ??? ??? ??? ??? ?&& Character.isDigit(string.charAt(index + 1))) { ?? ??? ??? ??? ?string = string.replaceFirst("π", "" + tmp + "×"); ?? ??? ??? ?} else { ?? ??? ??? ??? ?string = string.replaceFirst("π", "" + tmp); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?return string; ?? ?} ?? ?public String dispose(String string, String dispose) { ?? ??? ?// 處理指定運(yùn)算符;<log,ln,sin,cos,tan....> ?? ??? ?int index, flag = dispose.length(), flag1, flag2; ?? ??? ?String tmpString = "", tmpString1 = "", tmpString2 = ""; ?? ??? ?Double tmp, tmp1; ?? ??? ?while (string.length() != 0) { ?? ??? ??? ?index = string.indexOf(dispose); ?? ??? ??? ?if (-1 == index) ?? ??? ??? ??? ?break; ?? ??? ??? ?if ("!".equals(dispose)) { ?? ??? ??? ??? ?tmpString = get_l_digit(string, index - flag); ?? ??? ??? ??? ?if (!"error".equals(tmpString) && !"error".equals(tmpString1)) { ?? ??? ??? ??? ??? ?tmp = new Double(tmpString); ?? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?tmp1 = 1.0; ?? ??? ??? ??? ?for (int i = 1; i <= tmp; i++) { ?? ??? ??? ??? ??? ?tmp1 *= i; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?tmpString2 = "" + tmp1; ?? ??? ??? ??? ?string = string.replaceFirst(tmpString + dispose, tmpString2); ?? ??? ??? ?} else if ("e".equals(dispose)) { ?? ??? ??? ??? ?tmpString = get_r_digit(string, index + flag); ?? ??? ??? ??? ?tmpString1 = get_l_digit(string, index - flag); ?? ??? ??? ??? ?if (!"error".equals(tmpString) && !"error".equals(tmpString1)) { ?? ??? ??? ??? ??? ?tmp = new Double(tmpString); ?? ??? ??? ??? ??? ?tmp1 = new Double(tmpString1); ?? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?tmp = Math.pow(10, tmp); ?? ??? ??? ??? ?tmp1 *= tmp; ?? ??? ??? ??? ?tmpString2 = "" + tmp1; ?? ??? ??? ??? ?string = string.replaceFirst(tmpString1 + dispose + tmpString, ?? ??? ??? ??? ??? ??? ?tmpString2); ?? ??? ??? ??? ?System.out.println(string); ?? ??? ??? ?} else if ("^".equals(dispose)) { ?? ??? ??? ??? ?tmpString = get_r_digit(string, index + flag); ?? ??? ??? ??? ?tmpString1 = get_l_digit(string, index - flag); ?? ??? ??? ??? ?flag1 = tmpString.length(); ?? ??? ??? ??? ?flag2 = tmpString1.length(); ?? ??? ??? ??? ?if (!"error".equals(tmpString) && !"error".equals(tmpString1)) { ?? ??? ??? ??? ??? ?tmp = new Double(tmpString); ?? ??? ??? ??? ??? ?tmp1 = new Double(tmpString1); ?? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?tmp = Math.pow(tmp1, tmp); ?? ??? ??? ??? ?tmpString2 = "" + tmp; ?? ??? ??? ??? ?flag1 = index + flag1 + 1; ?? ??? ??? ??? ?flag2 = index - flag2; ?? ??? ??? ??? ?if (0 == flag2) { ?? ??? ??? ??? ??? ?tmpString = ""; ?? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ?tmpString = string.substring(0, flag2); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?if (string.length() == flag1) { ?? ??? ??? ??? ??? ?tmpString1 = ""; ?? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ?tmpString1 = string.substring(flag1, string.length()); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?string = tmpString + tmpString2 + tmpString1; ?? ??? ??? ?} else { ?? ??? ??? ??? ?tmpString = get_r_digit(string, index + flag); ?? ??? ??? ??? ?flag1 = tmpString.length(); ?? ??? ??? ??? ?if (!"error".equals(tmpString)) ?? ??? ??? ??? ??? ?tmp = new Double(tmpString); ?? ??? ??? ??? ?else { ?? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?tmpString2 = tmpString; ?? ??? ??? ??? ?if ("sin".equals(dispose)) { ?? ??? ??? ??? ??? ?if ("error".equals(tmp)) ?? ??? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ??? ?tmp = Math.sin(tmp); ?? ??? ??? ??? ?} else if ("cos".equals(dispose)) { ?? ??? ??? ??? ??? ?if ("error".equals(tmp)) ?? ??? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ??? ?tmp = Math.cos(tmp); ?? ??? ??? ??? ?} else if ("tan".equals(dispose)) { ?? ??? ??? ??? ??? ?if ("error".equals(tmp)) ?? ??? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ??? ?tmp = Math.tan(tmp); ?? ??? ??? ??? ?} else if ("log".equals(dispose)) { ?? ??? ??? ??? ??? ?if ("error".equals(tmp)) ?? ??? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ??? ?tmp = Math.log10(tmp); ?? ??? ??? ??? ?} else if ("ln".equals(dispose)) { ?? ??? ??? ??? ??? ?if ("error".equals(tmp)) ?? ??? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ??? ?tmp = Math.log(tmp); ?? ??? ??? ??? ?} else if ("√".equals(dispose)) { ?? ??? ??? ??? ??? ?if ("error".equals(tmp)) ?? ??? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ??? ?tmp = Math.sqrt(tmp); ?? ??? ??? ??? ?} else if ("log".equals(dispose)) { ?? ??? ??? ??? ??? ?if ("error".equals(tmp)) ?? ??? ??? ??? ??? ??? ?return "error"; ?? ??? ??? ??? ??? ?tmp = Math.log(tmp); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?if (index != 0 ?? ??? ??? ??? ??? ??? ?&& Character.isDigit(string.charAt(index - 1))) { ?? ??? ??? ??? ??? ?tmpString = "×" + tmp; ?? ??? ??? ??? ?} else { ?? ??? ??? ??? ??? ?tmpString = "" + tmp; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?string = string.replaceFirst(dispose + tmpString2, tmpString); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?return string; ?? ?} ?? ?public String advanced_dispose(String string) { ?? ??? ?string = this.dispose1(string); ?? ??? ?string = this.bracke_dispose(string); ?? ??? ?string = this.alg_dispose(string); ?? ??? ?return string; ?? ?} }
java類Judge代碼:
package com.xueba.jsq; public class Judge { ?? ?public String judge(String string, String c) { ?? ??? ?switch (string.charAt(string.length() - 1)) { ?? ??? ??? ?case '+': ?? ??? ??? ?case '-': ?? ??? ??? ?case '×': ?? ??? ??? ?case '÷': ?? ??? ??? ??? ?string = string.substring(0, string.length() - 1) + c; ?? ??? ??? ??? ?break; ?? ??? ??? ?default: ?? ??? ??? ??? ?string += c; ?? ??? ??? ??? ?break; ?? ??? ?} ?? ??? ?return string; ?? ?} ?? ?public static String dispose(String string) { ?? ??? ?int leng = string.length() - 1; ?? ??? ?Character character; ?? ??? ?if (0 == leng) { ?? ??? ??? ?return "error"; ?? ??? ?} ?? ??? ?for (int i = 0; i < leng; i++) { ?? ??? ??? ?character = string.charAt(i); ?? ??? ??? ?if (Character.isLetter(character)) { ?? ??? ??? ??? ?return "error"; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?return string; ?? ?} ?? ?public String judge1(String string) { ?? ??? ?int p = string.length() - 1; ?? ??? ?boolean flag = true; ?? ??? ?Character tmp = string.charAt(p); ?? ??? ?if (0 == p) ?? ??? ??? ?string += "."; ?? ??? ?if (Character.isDigit(tmp) && 0 != p) { ?? ??? ??? ?while (flag) { ?? ??? ??? ??? ?if (!Character.isDigit(tmp)) { ?? ??? ??? ??? ??? ?flag = false; ?? ??? ??? ??? ??? ?if (tmp != '.') ?? ??? ??? ??? ??? ??? ?string += "."; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?if (0 == --p && (tmp != '.')) { ?? ??? ??? ??? ??? ?string += "."; ?? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?tmp = string.charAt(p); ?? ??? ??? ?} ?? ??? ?} ?? ??? ?return string; ?? ?} ?? ?public static boolean paiduan(Character c) { ?? ??? ?switch (c) { ?? ??? ??? ?case '+': ?? ??? ??? ?case '-': ?? ??? ??? ?case '×': ?? ??? ??? ?case '÷': ?? ??? ??? ??? ?return true; ?? ??? ??? ?default: ?? ??? ??? ??? ?return false; ?? ??? ?} ?? ?} ?? ?public String digit_judge(String string, String c, boolean flag) { ?? ??? ?if ("0".equals(string)) { ?? ??? ??? ?string = c; ?? ??? ?} else if (flag) { ?? ??? ??? ?string = c; ?? ??? ?} else ?? ??? ??? ?string += c; ?? ??? ?return string; ?? ?} ?? ?public String digit_dispose(String string) { ?? ??? ?if ("error".equals(string)) { ?? ??? ??? ?return string; ?? ??? ?} ?? ??? ?Double double1 = new Double(string); ?? ??? ?if(double1>999999999999999.0) ?? ??? ??? ?return "∞"; ?? ??? ?long l = (long) (double1 * 1e4); ?? ??? ?double1 = (Double) (l / 1e4); ?? ??? ?string = "" + double1; ?? ??? ?return string; ?? ?} }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 點(diǎn)擊屏幕空白處收起輸入法軟鍵盤(手動(dòng)打開)
很多時(shí)候,我們?cè)谑褂脩?yīng)用時(shí),會(huì)出現(xiàn)輸入法軟鍵盤彈出的問題,通常情況下,我們默認(rèn)會(huì)使用戶點(diǎn)擊返回鍵或者下一步對(duì)軟鍵盤進(jìn)行隱藏。為了更好的體驗(yàn),我們可以實(shí)現(xiàn)當(dāng)用戶使用完畢軟鍵盤時(shí)。點(diǎn)擊屏幕空白即可實(shí)現(xiàn)收起輸入法軟鍵盤2016-12-12Android移動(dòng)開發(fā)recycleView的頁面點(diǎn)擊跳轉(zhuǎn)設(shè)計(jì)實(shí)現(xiàn)
這篇文章主要介紹了Android移動(dòng)開發(fā)recycleView的頁面點(diǎn)擊跳轉(zhuǎn)設(shè)計(jì)實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Android關(guān)于SeekBar無法點(diǎn)擊到最大值問題解決方法記錄(推薦)
這篇文章主要介紹了Android關(guān)于SeekBar無法點(diǎn)擊到最大值問題解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析
StrictMode意思為嚴(yán)格模式,是用來檢測(cè)程序中違例情況的開發(fā)者工具。最常用的場(chǎng)景就是檢測(cè)主線程中本地磁盤和網(wǎng)絡(luò)讀寫等耗時(shí)的操作。這篇文章給大家介紹Android性能調(diào)優(yōu)利器StrictMode應(yīng)用分析,感興趣的朋友一起看看吧2018-01-01android判斷一個(gè)Activity是否處于棧頂?shù)膶?shí)例
下面小編就為大家分享一篇android判斷一個(gè)Activity是否處于棧頂?shù)膶?shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03Android中ActionBar以及menu的代碼設(shè)置樣式
這篇文章主要介紹了Android中ActionBar以及menu的代碼設(shè)置樣式的相關(guān)資料,需要的朋友可以參考下2015-07-07Android實(shí)現(xiàn)字幕滾動(dòng)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)字幕滾動(dòng)的方法,很實(shí)用的功能,需要的朋友可以參考下2014-07-07Android微信搶紅包功能的實(shí)現(xiàn)原理淺析
快到新年了,微信紅包越來越多,那么基于程序是怎么實(shí)現(xiàn)的呢?今天小編給大家分享Android微信搶紅包功能的實(shí)現(xiàn)原理淺析,一起看看吧2017-01-01