国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Java實現Andriod帶看括弧的計算器代碼

2020-01-02 07:00:52
字體:
來源:轉載
供稿:網友

廢話不多說了,一切盡在代碼中,具體代碼如下所示:

界面

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#ff808080"><!-- 結果顯示框 --><EditText android:id="@+id/input"android:layout_width="fill_parent"android:layout_height="wrap_content"android:cursorVisible="false"android:gravity="right"android:editable = "false"android:text="0" /><!-- 接下去采TableRow的格式進行布局設計 --><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><!-- 用于顯示存儲結果 --><TextView android:id="@+id/M"android:layout_width="53sp"android:layout_height="wrap_content"android:text=" MEM :" /><!-- 默認顯示字符串0 --><TextView android:id="@+id/mem"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="0" /></TableRow><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><!-- 顯示當前是角度還是弧度,默認是角度 --><TextView android:id="@+id/_drg"android:layout_width="53sp"android:layout_height="wrap_content"android:text=" DEG" /><!-- 清除存儲結果 --><Button android:id="@+id/mc"android:text="MC"android:layout_width="106sp"android:layout_height="wrap_content" /><!-- 清除輸出窗口的所有內容 --><Button android:id="@+id/c"android:text="C"android:layout_width="fill_parent"android:layout_height="wrap_content" /></TableRow><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><!-- 在角度和弧度之間切換 --><Button android:id="@+id/drg"android:text="DRG"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 正弦計算 --><Button android:id="@+id/sin"android:text="sin"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 余弦計算 --><Button android:id="@+id/cos"android:text="cos"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 正切計算 --><Button android:id="@+id/tan"android:text="tan"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 階乘計算 --><Button android:id="@+id/factorial"android:text="n!"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 退格鍵 --><Button android:id="@+id/bksp"android:text="Bksp"android:layout_width="53sp"android:layout_height="wrap_content" /></TableRow><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><!-- 數字7 --><Button android:id="@+id/seven"android:text="7"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 數字8 --><Button android:id="@+id/eight"android:text="8"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 數字9 --><Button android:id="@+id/nine"android:text="9"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 除號 --><Button android:id="@+id/divide"android:text="÷"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 左括號 --><Button android:id="@+id/left"android:text="("android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 右括號 --><Button android:id="@+id/right"android:text=")"android:layout_width="53sp"android:layout_height="wrap_content" /></TableRow><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><!-- 數字4 --><Button android:id="@+id/four"android:text="4"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 數字5 --><Button android:id="@+id/five"android:text="5"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 數字6 --><Button android:id="@+id/six"android:text="6"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 乘號 --><Button android:id="@+id/mul"android:text="×"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 開方 --><Button android:id="@+id/sqrt"android:text="√"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 乘方 --><Button android:id="@+id/square"android:text="^"android:layout_width="53sp"android:layout_height="wrap_content" /></TableRow><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><!-- 數字1 --><Button android:id="@+id/one"android:text="1"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 數字2 --><Button android:id="@+id/two"android:text="2"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 數字3 --><Button android:id="@+id/three"android:text="3"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 減號 --><Button android:id="@+id/sub"android:text="-"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 對數 --><Button android:id="@+id/log"android:text="log"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 自然對數 --><Button android:id="@+id/ln"android:text="ln"android:layout_width="53sp"android:layout_height="wrap_content" /></TableRow><TableLayoutandroid:layout_width="fill_parent"android:layout_height="57sp"><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><!-- 數字0 --><Button android:id="@+id/zero"android:text="0"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 小數點 --><Button android:id="@+id/dot"android:text="."android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 等號 --><Button android:id="@+id/equal"android:text="="android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 加號 --><Button android:id="@+id/add"android:text="+"android:layout_width="53sp"android:layout_height="wrap_content" /><!-- 退出計算器 --><Button android:id="@+id/exit"android:text="exit"android:layout_width="106sp"android:layout_height="wrap_content" /></TableRow></TableLayout><TableRowandroid:layout_width="fill_parent"android:layout_height="wrap_content"><!-- 用于提示,告訴用戶如何使用計算器的一些功能等 --><TextView android:id="@+id/T"android:layout_width="60sp"android:layout_height="wrap_content"android:text="reminder:" /><TextView android:id="@+id/tip"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="歡迎使用!-www.javaapk.com 提供源碼" /></TableRow></LinearLayout> 

代碼

package com.example.yanlei.mytk;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.Spinner;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.view.animation.AnimationUtils;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.Gallery;import android.widget.Gallery.LayoutParams;import android.widget.ImageSwitcher;import android.widget.ImageView;import android.widget.ViewSwitcher;import java.text.DecimalFormat;import java.util.StringTokenizer;import android.os.Bundle;import android.R.integer;import android.app.Activity;import android.util.Log;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends AppCompatActivity {// 定義變量private Button[] btn = new Button[10];// 0~9十個數字private EditText input;// 用于顯示輸出結果private TextView mem, _drg, tip;private Button div, mul, sub, add, equal, sin, cos, tan, log, ln, sqrt,square, factorial, bksp, left, right, dot, exit, drg, mc, c;public String str_old;public String str_new;public boolean vbegin = true;// 控制輸入,true為重新輸入,false為接著輸入public boolean drg_flag = true;// true為角度,false為弧度public double pi = 4 * Math.atan(1);// π值public boolean tip_lock = true;// true為正確,可以繼續輸入,false錯誤,輸入鎖定public boolean equals_flag = true;// 是否在按下=之后輸入,true為之前,false為之后@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);InitWigdet();AllWigdetListener();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.menu_main, menu);return true;}/*** 初始化所有的組件*/private void InitWigdet() {// 獲取界面元素input = (EditText) findViewById(R.id.input);mem = (TextView) findViewById(R.id.mem);tip = (TextView) findViewById(R.id.tip);_drg = (TextView) findViewById(R.id._drg);btn[0] = (Button) findViewById(R.id.zero);btn[1] = (Button) findViewById(R.id.one);btn[2] = (Button) findViewById(R.id.two);btn[3] = (Button) findViewById(R.id.three);btn[4] = (Button) findViewById(R.id.four);btn[5] = (Button) findViewById(R.id.five);btn[6] = (Button) findViewById(R.id.six);btn[7] = (Button) findViewById(R.id.seven);btn[8] = (Button) findViewById(R.id.eight);btn[9] = (Button) findViewById(R.id.nine);div = (Button) findViewById(R.id.divide);mul = (Button) findViewById(R.id.mul);sub = (Button) findViewById(R.id.sub);add = (Button) findViewById(R.id.add);equal = (Button) findViewById(R.id.equal);sin = (Button) findViewById(R.id.sin);cos = (Button) findViewById(R.id.cos);tan = (Button) findViewById(R.id.tan);log = (Button) findViewById(R.id.log);ln = (Button) findViewById(R.id.ln);sqrt = (Button) findViewById(R.id.sqrt);square = (Button) findViewById(R.id.square);factorial = (Button) findViewById(R.id.factorial);bksp = (Button) findViewById(R.id.bksp);left = (Button) findViewById(R.id.left);right = (Button) findViewById(R.id.right);dot = (Button) findViewById(R.id.dot);exit = (Button) findViewById(R.id.exit);drg = (Button) findViewById(R.id.drg);mc = (Button) findViewById(R.id.mc);c = (Button) findViewById(R.id.c);}/*** 為所有按鍵綁定監聽器*/private void AllWigdetListener() {// 數字鍵for (int i = 0; i < 10; i++) {btn[i].setOnClickListener(actionPerformed);}// 為+-x÷等按鍵綁定監聽器div.setOnClickListener(actionPerformed);mul.setOnClickListener(actionPerformed);sub.setOnClickListener(actionPerformed);add.setOnClickListener(actionPerformed);equal.setOnClickListener(actionPerformed);sin.setOnClickListener(actionPerformed);cos.setOnClickListener(actionPerformed);tan.setOnClickListener(actionPerformed);log.setOnClickListener(actionPerformed);ln.setOnClickListener(actionPerformed);sqrt.setOnClickListener(actionPerformed);square.setOnClickListener(actionPerformed);factorial.setOnClickListener(actionPerformed);bksp.setOnClickListener(actionPerformed);left.setOnClickListener(actionPerformed);right.setOnClickListener(actionPerformed);dot.setOnClickListener(actionPerformed);exit.setOnClickListener(actionPerformed);drg.setOnClickListener(actionPerformed);mc.setOnClickListener(actionPerformed);c.setOnClickListener(actionPerformed);}/*** 鍵盤命令捕捉*/String[] TipCommand = new String[500];int tip_i = 0;// TipCommand的指針private OnClickListener actionPerformed = new OnClickListener() {public void onClick(View v) {// 按鍵上的命令獲取String command = ((Button) v).getText().toString();// 顯示器上的字符串String str = input.getText().toString();// 檢測輸入是否合法if (equals_flag == false&& "0123456789.()sincostanlnlogn!+-×÷√^".indexOf(command) != -1) {// 檢測顯示器上的字符串是否合法if (right(str)) {if ("+-×÷√^)".indexOf(command) != -1) {for (int i = 0; i < str.length(); i++) {TipCommand[tip_i] = String.valueOf(str.charAt(i));tip_i++;}vbegin = false;}} else {input.setText("0");vbegin = true;tip_i = 0;tip_lock = true;tip.setText("welcome use the APP!");}equals_flag = true;}if (tip_i > 0)TipChecker(TipCommand[tip_i - 1], command);else if (tip_i == 0) {TipChecker("#", command);}if ("0123456789.()sincostanlnlogn!+-×÷√^".indexOf(command) != -1&& tip_lock) {TipCommand[tip_i] = command;tip_i++;}// 若輸入正確,則將輸入信息顯示到顯示器上if ("0123456789.()sincostanlnlogn!+-×÷√^".indexOf(command) != -1&& tip_lock) { // 共25個按鍵print(command);// 若果點擊了“DRG”,則切換當前弧度角度制,并將切換后的結果顯示到按鍵上方。} else if (command.compareTo("DRG") == 0 && tip_lock) {if (drg_flag == true) {drg_flag = false;_drg.setText(" RAD");} else {drg_flag = true;_drg.setText(" DEG");}// 如果輸入時退格鍵,并且是在按=之前} else if (command.compareTo("Bksp") == 0 && equals_flag) {// 一次刪除3個字符if (TTO(str) == 3) {if (str.length() > 3)input.setText(str.substring(0, str.length() - 3));else if (str.length() == 3) {input.setText("0");vbegin = true;tip_i = 0;tip.setText("welcome use the APP!");}// 依次刪除2個字符} else if (TTO(str) == 2) {if (str.length() > 2)input.setText(str.substring(0, str.length() - 2));else if (str.length() == 2) {input.setText("0");vbegin = true;tip_i = 0;tip.setText("welcome use the APP!");}// 依次刪除一個字符} else if (TTO(str) == 1) {// 若之前輸入的字符串合法則刪除一個字符if (right(str)) {if (str.length() > 1)input.setText(str.substring(0, str.length() - 1));else if (str.length() == 1) {input.setText("0");vbegin = true;tip_i = 0;tip.setText("welcome use the APP!");}// 若之前輸入的字符串不合法則刪除全部字符} else {input.setText("0");vbegin = true;tip_i = 0;tip.setText("welcome use the APP!");}}if (input.getText().toString().compareTo("-") == 0|| equals_flag == false) {input.setText("0");vbegin = true;tip_i = 0;tip.setText("welcome use the APP!");}tip_lock = true;if (tip_i > 0)tip_i--;// 如果是在按=之后輸入退格鍵} else if (command.compareTo("Bksp") == 0 && equals_flag == false) {// 將顯示器內容設置為0input.setText("0");vbegin = true;tip_i = 0;tip_lock = true;tip.setText("welcome use the APP!");// 如果輸入的是清除鍵} else if (command.compareTo("C") == 0) {// 將顯示器內容設置為0input.setText("0");// 重新輸入標志置為truevbegin = true;// 緩存命令位數清0tip_i = 0;// 表明可以繼續輸入tip_lock = true;// 表明輸入=之前equals_flag = true;tip.setText("welcome use the APP!");// 如果輸入的是”MC“,則將存儲器內容清0} else if (command.compareTo("MC") == 0) {mem.setText("0");// 如果按”exit“則退出程序} else if (command.compareTo("exit") == 0) {System.exit(0);// 如果輸入的是=號,并且輸入合法} else if (command.compareTo("=") == 0 && tip_lock && right(str)&& equals_flag) {tip_i = 0;// 表明不可以繼續輸入tip_lock = false;// 表明輸入=之后equals_flag = false;// 保存原來算式樣子str_old = str;// 替換算式中的運算符,便于計算str = str.replaceAll("sin", "s");str = str.replaceAll("cos", "c");str = str.replaceAll("tan", "t");str = str.replaceAll("log", "g");str = str.replaceAll("ln", "l");str = str.replaceAll("n!", "!");// 重新輸入標志設置truevbegin = true;// 將-1x轉換成-str_new = str.replaceAll("-", "-1×");// 計算算式結果new calc().process(str_new);}// 表明可以繼續輸入tip_lock = true;}};/** 檢測函數,對str進行前后語法檢測 為Tip的提示方式提供依據,與TipShow()配合使用 編號 字符 其后可以跟隨的合法字符 1 (* 數字|(|-|.|函數 2 ) 算符|)|√ ^ 3 . 數字|算符|)|√ ^ 4 數字 .|數字|算符|)|√ ^ 5 算符* 數字|(|.|函數 6 √ ^ ( |. | 數字 7 函數 數字|(|.** 小數點前后均可省略,表示0 數字第一位可以為0*/private void TipChecker(String tipcommand1, String tipcommand2) {// Tipcode1表示錯誤類型,Tipcode2表示名詞解釋類型int Tipcode1 = 0, Tipcode2 = 0;// 表示命令類型int tiptype1 = 0, tiptype2 = 0;// 括號數int bracket = 0;// “+-x÷√^”不能作為第一位if (tipcommand1.compareTo("#") == 0&& (tipcommand2.compareTo("÷") == 0|| tipcommand2.compareTo("×") == 0|| tipcommand2.compareTo("+") == 0|| tipcommand2.compareTo(")") == 0|| tipcommand2.compareTo("√") == 0 || tipcommand2.compareTo("^") == 0)) {Tipcode1 = -1;}// 定義存儲字符串中最后一位的類型else if (tipcommand1.compareTo("#") != 0) {if (tipcommand1.compareTo("(") == 0) {tiptype1 = 1;} else if (tipcommand1.compareTo(")") == 0) {tiptype1 = 2;} else if (tipcommand1.compareTo(".") == 0) {tiptype1 = 3;} else if ("0123456789".indexOf(tipcommand1) != -1) {tiptype1 = 4;} else if ("+-×÷".indexOf(tipcommand1) != -1) {tiptype1 = 5;} else if ("√^".indexOf(tipcommand1) != -1) {tiptype1 = 6;} else if ("sincostanloglnn!".indexOf(tipcommand1) != -1) {tiptype1 = 7;}// 定義欲輸入的按鍵類型if (tipcommand2.compareTo("(") == 0) {tiptype2 = 1;} else if (tipcommand2.compareTo(")") == 0) {tiptype2 = 2;} else if (tipcommand2.compareTo(".") == 0) {tiptype2 = 3;} else if ("0123456789".indexOf(tipcommand2) != -1) {tiptype2 = 4;} else if ("+-×÷".indexOf(tipcommand2) != -1) {tiptype2 = 5;} else if ("√^".indexOf(tipcommand2) != -1) {tiptype2 = 6;} else if ("sincostanloglnn!".indexOf(tipcommand2) != -1) {tiptype2 = 7;}switch (tiptype1) {case 1:// 左括號后面直接接右括號,“+x÷”(負號“-”不算),或者"√^"if (tiptype2 == 2|| (tiptype2 == 5 && tipcommand2.compareTo("-") != 0)|| tiptype2 == 6)Tipcode1 = 1;break;case 2:// 右括號后面接左括號,數字,“+-x÷sin^...”if (tiptype2 == 1 || tiptype2 == 3 || tiptype2 == 4|| tiptype2 == 7)Tipcode1 = 2;break;case 3:// “.”后面接左括號或者“sincos...”if (tiptype2 == 1 || tiptype2 == 7)Tipcode1 = 3;// 連續輸入兩個“.”if (tiptype2 == 3)Tipcode1 = 8;break;case 4:// 數字后面直接接左括號或者“sincos...”if (tiptype2 == 1 || tiptype2 == 7)Tipcode1 = 4;break;case 5:// “+-x÷”后面直接接右括號,“+-x÷√^”if (tiptype2 == 2 || tiptype2 == 5 || tiptype2 == 6)Tipcode1 = 5;break;case 6:// “√^”后面直接接右括號,“+-x÷√^”以及“sincos...”if (tiptype2 == 2 || tiptype2 == 5 || tiptype2 == 6|| tiptype2 == 7)Tipcode1 = 6;break;case 7:// “sincos...”后面直接接右括號“+-x÷√^”以及“sincos...”if (tiptype2 == 2 || tiptype2 == 5 || tiptype2 == 6|| tiptype2 == 7)Tipcode1 = 7;break;}}// 檢測小數點的重復性,Tipconde1=0,表明滿足前面的規則if (Tipcode1 == 0 && tipcommand2.compareTo(".") == 0) {int tip_point = 0;for (int i = 0; i < tip_i; i++) {// 若之前出現一個小數點點,則小數點計數加1if (TipCommand[i].compareTo(".") == 0) {tip_point++;}// 若出現以下幾個運算符之一,小數點計數清零if (TipCommand[i].compareTo("sin") == 0|| TipCommand[i].compareTo("cos") == 0|| TipCommand[i].compareTo("tan") == 0|| TipCommand[i].compareTo("log") == 0|| TipCommand[i].compareTo("ln") == 0|| TipCommand[i].compareTo("n!") == 0|| TipCommand[i].compareTo("√") == 0|| TipCommand[i].compareTo("^") == 0|| TipCommand[i].compareTo("÷") == 0|| TipCommand[i].compareTo("×") == 0|| TipCommand[i].compareTo("-") == 0|| TipCommand[i].compareTo("+") == 0|| TipCommand[i].compareTo("(") == 0|| TipCommand[i].compareTo(")") == 0) {tip_point = 0;}}tip_point++;// 若小數點計數大于1,表明小數點重復了if (tip_point > 1) {Tipcode1 = 8;}}// 檢測右括號是否匹配if (Tipcode1 == 0 && tipcommand2.compareTo(")") == 0) {int tip_right_bracket = 0;for (int i = 0; i < tip_i; i++) {// 如果出現一個左括號,則計數加1if (TipCommand[i].compareTo("(") == 0) {tip_right_bracket++;}// 如果出現一個右括號,則計數減1if (TipCommand[i].compareTo(")") == 0) {tip_right_bracket--;}}// 如果右括號計數=0,表明沒有響應的左括號與當前右括號匹配if (tip_right_bracket == 0) {Tipcode1 = 10;}}// 檢查輸入=的合法性if (Tipcode1 == 0 && tipcommand2.compareTo("=") == 0) {// 括號匹配數int tip_bracket = 0;for (int i = 0; i < tip_i; i++) {if (TipCommand[i].compareTo("(") == 0) {tip_bracket++;}if (TipCommand[i].compareTo(")") == 0) {tip_bracket--;}}// 若大于0,表明左括號還有未匹配的if (tip_bracket > 0) {Tipcode1 = 9;bracket = tip_bracket;} else if (tip_bracket == 0) {// 若前一個字符是以下之一,表明=號不合法if ("√^sincostanloglnn!".indexOf(tipcommand1) != -1) {Tipcode1 = 6;}// 若前一個字符是以下之一,表明=號不合法if ("+-×÷".indexOf(tipcommand1) != -1) {Tipcode1 = 5;}}}// 若命令式以下之一,則顯示相應的幫助信息if (tipcommand2.compareTo("MC") == 0)Tipcode2 = 1;if (tipcommand2.compareTo("C") == 0)Tipcode2 = 2;if (tipcommand2.compareTo("DRG") == 0)Tipcode2 = 3;if (tipcommand2.compareTo("Bksp") == 0)Tipcode2 = 4;if (tipcommand2.compareTo("sin") == 0)Tipcode2 = 5;if (tipcommand2.compareTo("cos") == 0)Tipcode2 = 6;if (tipcommand2.compareTo("tan") == 0)Tipcode2 = 7;if (tipcommand2.compareTo("log") == 0)Tipcode2 = 8;if (tipcommand2.compareTo("ln") == 0)Tipcode2 = 9;if (tipcommand2.compareTo("n!") == 0)Tipcode2 = 10;if (tipcommand2.compareTo("√") == 0)Tipcode2 = 11;if (tipcommand2.compareTo("^") == 0)Tipcode2 = 12;// 顯示幫助和錯誤信息TipShow(bracket, Tipcode1, Tipcode2, tipcommand1, tipcommand2);}/** 反饋Tip信息,加強人機交互,與TipChecker()配合使用*/private void TipShow(int bracket, int tipcode1, int tipcode2,String tipcommand1, String tipcommand2) {String tipmessage = "";if (tipcode1 != 0)tip_lock = false;// 表明輸入有誤switch (tipcode1) {case -1:tipmessage = tipcommand2 + " 不能作為第一個算符/n";break;case 1:tipmessage = tipcommand1 + " 后應輸入:數字/(/./-/函數 /n";break;case 2:tipmessage = tipcommand1 + " 后應輸入:)/算符 /n";break;case 3:tipmessage = tipcommand1 + " 后應輸入:)/數字/算符 /n";break;case 4:tipmessage = tipcommand1 + " 后應輸入:)/./數字 /算符 /n";break;case 5:tipmessage = tipcommand1 + " 后應輸入:(/./數字/函數 /n";break;case 6:tipmessage = tipcommand1 + " 后應輸入:(/./數字 /n";break;case 7:tipmessage = tipcommand1 + " 后應輸入:(/./數字 /n";break;case 8:tipmessage = "小數點重復/n";break;case 9:tipmessage = "不能計算,缺少 " + bracket + " 個 )";break;case 10:tipmessage = "不需要 )";break;}switch (tipcode2) {case 1:tipmessage = tipmessage + "[MC 用法: 清除記憶 MEM]";break;case 2:tipmessage = tipmessage + "[C 用法: 歸零]";break;case 3:tipmessage = tipmessage + "[DRG 用法: 選擇 DEG 或 RAD]";break;case 4:tipmessage = tipmessage + "[Bksp 用法: 退格]";break;case 5:tipmessage = tipmessage + "sin 函數用法示例:/n"+ "DEG:sin30 = 0.5 RAD:sin1 = 0.84/n"+ "注:與其他函數一起使用時要加括號,如:/n" + "sin(cos45),而不是sincos45";break;case 6:tipmessage = tipmessage + "cos 函數用法示例:/n"+ "DEG:cos60 = 0.5 RAD:cos1 = 0.54/n"+ "注:與其他函數一起使用時要加括號,如:/n" + "cos(sin45),而不是cossin45";break;case 7:tipmessage = tipmessage + "tan 函數用法示例:/n"+ "DEG:tan45 = 1 RAD:tan1 = 1.55/n"+ "注:與其他函數一起使用時要加括號,如:/n" + "tan(cos45),而不是tancos45";break;case 8:tipmessage = tipmessage + "log 函數用法示例:/n"+ "log10 = log(5+5) = 1/n" + "注:與其他函數一起使用時要加括號,如:/n"+ "log(tan45),而不是logtan45";break;case 9:tipmessage = tipmessage + "ln 函數用法示例:/n"+ "ln10 = le(5+5) = 2.3 lne = 1/n"+ "注:與其他函數一起使用時要加括號,如:/n" + "ln(tan45),而不是lntan45";break;case 10:tipmessage = tipmessage + "n! 函數用法示例:/n"+ "n!3 = n!(1+2) = 3×2×1 = 6/n" + "注:與其他函數一起使用時要加括號,如:/n"+ "n!(log1000),而不是n!log1000";break;case 11:tipmessage = tipmessage + "√ 用法示例:開任意次根號/n"+ "如:27開3次根為 27√3 = 3/n" + "注:與其他函數一起使用時要加括號,如:/n"+ "(函數)√(函數) , (n!3)√(log100) = 2.45";break;case 12:tipmessage = tipmessage + "^ 用法示例:開任意次平方/n" + "如:2的3次方為 2^3 = 8/n"+ "注:與其他函數一起使用時要加括號,如:/n"+ "(函數)√(函數) , (n!3)^(log100) = 36";break;}// 將提示信息顯示到tiptip.setText(tipmessage);}/*** 將信息顯示在顯示屏上*/private void print(String str) {// 清屏后輸出if (vbegin) {input.setText(str);} else {input.append(str);}vbegin = false;}/** 檢測函數,返回值為3、2、1 表示應當一次刪除幾個? Three+Two+One = TTO 為Bksp按鈕的刪除方式提供依據* 返回3,表示str尾部為sin、cos、tan、log中的一個,應當一次刪除3個 返回2,表示str尾部為ln、n!中的一個,應當一次刪除2個* 返回1,表示為除返回3、2外的所有情況,只需刪除一個(包含非法字符時要另外考慮:應清屏)*/private int TTO(String str) {if ((str.charAt(str.length() - 1) == 'n'&& str.charAt(str.length() - 2) == 'i' && str.charAt(str.length() - 3) == 's')|| (str.charAt(str.length() - 1) == 's'&& str.charAt(str.length() - 2) == 'o' && str.charAt(str.length() - 3) == 'c')|| (str.charAt(str.length() - 1) == 'n'&& str.charAt(str.length() - 2) == 'a' && str.charAt(str.length() - 3) == 't')|| (str.charAt(str.length() - 1) == 'g'&& str.charAt(str.length() - 2) == 'o' && str.charAt(str.length() - 3) == 'l')) {return 3;} else if ((str.charAt(str.length() - 1) == 'n' && str.charAt(str.length() - 2) == 'l')|| (str.charAt(str.length() - 1) == '!' && str.charAt(str.length() - 2) == 'n')) {return 2;} else {return 1;}}/** 判斷一個str是否是合法的,返回值為true、false* 只包含0123456789.()sincostanlnlogn!+-×÷√^的是合法的str,返回true* 包含了除0123456789.()sincostanlnlogn!+-×÷√^以外的字符的str為非法的,返回false*/private boolean right(String str) {int i = 0;for (i = 0; i < str.length(); i++) {if (str.charAt(i) != '0' && str.charAt(i) != '1'&& str.charAt(i) != '2' && str.charAt(i) != '3'&& str.charAt(i) != '4' && str.charAt(i) != '5'&& str.charAt(i) != '6' && str.charAt(i) != '7'&& str.charAt(i) != '8' && str.charAt(i) != '9'&& str.charAt(i) != '.' && str.charAt(i) != '-'&& str.charAt(i) != '+' && str.charAt(i) != '×'&& str.charAt(i) != '÷' && str.charAt(i) != '√'&& str.charAt(i) != '^' && str.charAt(i) != 's'&& str.charAt(i) != 'i' && str.charAt(i) != 'n'&& str.charAt(i) != 'c' && str.charAt(i) != 'o'&& str.charAt(i) != 't' && str.charAt(i) != 'a'&& str.charAt(i) != 'l' && str.charAt(i) != 'g'&& str.charAt(i) != '(' && str.charAt(i) != ')'&& str.charAt(i) != '!')break;}if (i == str.length()) {return true;} else {return false;}}/** 整個計算核心,* 只要將表達式的整個字符串傳入calc().process()就可以實行計算了 算法包括以下幾部分:* 1、計算部分* process(String str) 當然,這是建立在查錯無錯誤的情況下* 2、數據格式化 FP(double n) 使數據有相當的精確度* 3、階乘算法 N(double n) 計算n!,將結果返回* 4、錯誤提示 showError(int code ,String str)* 將錯誤返回*/public class calc {public calc() {}final int MAXLEN = 500;/** 計算表達式 從左向右掃描,數字入number棧,運算符入operator棧* +-基本優先級為1,* ×÷基本優先級為2,* log ln sin cos tan n!基本優先級為3,* √^基本優先級為4* 括號內層運算符比外層同級運算符優先級高4* 當前運算符優先級高于棧頂壓棧,* 低于棧頂彈出一個運算符與兩個數進行運算* 重復直到當前運算符大于棧頂* 掃描完后對剩下的運算符與數字依次計算*/public void process(String str) {int weightPlus = 0, topOp = 0, topNum = 0, flag = 1, weightTemp = 0;// weightPlus為同一()下的基本優先級,weightTemp臨時記錄優先級的變化// topOp為weight[],operator[]的計數器;topNum為number[]的計數器// flag為正負數的計數器,1為正數,-1為負數int weight[]; // 保存operator棧中運算符的優先級,以topOp計數double number[]; // 保存數字,以topNum計數char ch, ch_gai, operator[];// operator[]保存運算符,以topOp計數String num;// 記錄數字,str以+-×÷()sctgl!√^分段,+-×÷()sctgl!√^字符之間的字符串即為數字weight = new int[MAXLEN];number = new double[MAXLEN];operator = new char[MAXLEN];String expression = str;StringTokenizer expToken = new StringTokenizer(expression,"+-×÷()sctgl!√^");int i = 0;while (i < expression.length()) {ch = expression.charAt(i);// 判斷正負數if (i == 0) {if (ch == '-')flag = -1;} else if (expression.charAt(i - 1) == '(' && ch == '-')flag = -1;// 取得數字,并將正負符號轉移給數字if (ch <= '9' && ch >= '0' || ch == '.' || ch == 'E') {num = expToken.nextToken();ch_gai = ch;Log.e("guojs", ch + "--->" + i);// 取得整個數字while (i < expression.length()&& (ch_gai <= '9' && ch_gai >= '0' || ch_gai == '.' || ch_gai == 'E')) {ch_gai = expression.charAt(i++);Log.e("guojs", "i的值為:" + i);}// 將指針退回之前的位置if (i >= expression.length())i -= 1;else {i -= 2;}if (num.compareTo(".") == 0)number[topNum++] = 0;// 將正負符號轉移給數字else {number[topNum++] = Double.parseDouble(num) * flag;flag = 1;}}// 計算運算符的優先級if (ch == '(')weightPlus += 4;if (ch == ')')weightPlus -= 4;if (ch == '-' && flag == 1 || ch == '+' || ch == '×'|| ch == '÷' || ch == 's' || ch == 'c' || ch == 't'|| ch == 'g' || ch == 'l' || ch == '!' || ch == '√'|| ch == '^') {switch (ch) {// +-的優先級最低,為1case '+':case '-':weightTemp = 1 + weightPlus;break;// x÷的優先級稍高,為2case '×':case '÷':weightTemp = 2 + weightPlus;break;// sincos之類優先級為3case 's':case 'c':case 't':case 'g':case 'l':case '!':weightTemp = 3 + weightPlus;break;// 其余優先級為4// case '^':// case '√':default:weightTemp = 4 + weightPlus;break;}// 如果當前優先級大于堆棧頂部元素,則直接入棧if (topOp == 0 || weight[topOp - 1] < weightTemp) {weight[topOp] = weightTemp;operator[topOp] = ch;topOp++;// 否則將堆棧中運算符逐個取出,直到當前堆棧頂部運算符的優先級小于當前運算符} else {while (topOp > 0 && weight[topOp - 1] >= weightTemp) {switch (operator[topOp - 1]) {// 取出數字數組的相應元素進行運算case '+':number[topNum - 2] += number[topNum - 1];break;case '-':number[topNum - 2] -= number[topNum - 1];break;case '×':number[topNum - 2] *= number[topNum - 1];break;// 判斷除數為0的情況case '÷':if (number[topNum - 1] == 0) {showError(1, str_old);return;}number[topNum - 2] /= number[topNum - 1];break;case '√':if (number[topNum - 1] == 0|| (number[topNum - 2] < 0 && number[topNum - 1] % 2 == 0)) {showError(2, str_old);return;}number[topNum - 2] = Math.pow(number[topNum - 2],1 / number[topNum - 1]);break;case '^':number[topNum - 2] = Math.pow(number[topNum - 2], number[topNum - 1]);break;// 計算時進行角度弧度的判斷及轉換// sincase 's':if (drg_flag == true) {number[topNum - 1] = Math.sin((number[topNum - 1] / 180)* pi);} else {number[topNum - 1] = Math.sin(number[topNum - 1]);}topNum++;break;// coscase 'c':if (drg_flag == true) {number[topNum - 1] = Math.cos((number[topNum - 1] / 180)* pi);} else {number[topNum - 1] = Math.cos(number[topNum - 1]);}topNum++;break;// tancase 't':if (drg_flag == true) {if ((Math.abs(number[topNum - 1]) / 90) % 2 == 1) {showError(2, str_old);return;}number[topNum - 1] = Math.tan((number[topNum - 1] / 180)* pi);} else {if ((Math.abs(number[topNum - 1]) / (pi / 2)) % 2 == 1) {showError(2, str_old);return;}number[topNum - 1] = Math.tan(number[topNum - 1]);}topNum++;break;// logcase 'g':if (number[topNum - 1] <= 0) {showError(2, str_old);return;}number[topNum - 1] = Math.log10(number[topNum - 1]);topNum++;break;// lncase 'l':if (number[topNum - 1] <= 0) {showError(2, str_old);return;}number[topNum - 1] = Math.log(number[topNum - 1]);topNum++;break;// 階乘case '!':if (number[topNum - 1] > 170) {showError(3, str_old);return;} else if (number[topNum - 1] < 0) {showError(2, str_old);return;}number[topNum - 1] = N(number[topNum - 1]);topNum++;break;}// 繼續取堆棧的下一個元素進行判斷topNum--;topOp--;}// 將運算符如堆棧weight[topOp] = weightTemp;operator[topOp] = ch;topOp++;}}i++;}// 依次取出堆棧的運算符進行運算while (topOp > 0) {// +-x直接將數組的后兩位數取出運算switch (operator[topOp - 1]) {case '+':number[topNum - 2] += number[topNum - 1];break;case '-':number[topNum - 2] -= number[topNum - 1];break;case '×':number[topNum - 2] *= number[topNum - 1];break;// 涉及到除法時要考慮除數不能為零的情況case '÷':if (number[topNum - 1] == 0) {showError(1, str_old);return;}number[topNum - 2] /= number[topNum - 1];break;case '√':if (number[topNum - 1] == 0|| (number[topNum - 2] < 0 && number[topNum - 1] % 2 == 0)) {showError(2, str_old);return;}number[topNum - 2] = Math.pow(number[topNum - 2],1 / number[topNum - 1]);break;case '^':number[topNum - 2] = Math.pow(number[topNum - 2],number[topNum - 1]);break;// sincase 's':if (drg_flag == true) {number[topNum - 1] = Math.sin((number[topNum - 1] / 180) * pi);} else {number[topNum - 1] = Math.sin(number[topNum - 1]);}topNum++;break;// coscase 'c':if (drg_flag == true) {number[topNum - 1] = Math.cos((number[topNum - 1] / 180) * pi);} else {number[topNum - 1] = Math.cos(number[topNum - 1]);}topNum++;break;// tancase 't':if (drg_flag == true) {if ((Math.abs(number[topNum - 1]) / 90) % 2 == 1) {showError(2, str_old);return;}number[topNum - 1] = Math.tan((number[topNum - 1] / 180) * pi);} else {if ((Math.abs(number[topNum - 1]) / (pi / 2)) % 2 == 1) {showError(2, str_old);return;}number[topNum - 1] = Math.tan(number[topNum - 1]);}topNum++;break;// 對數logcase 'g':if (number[topNum - 1] <= 0) {showError(2, str_old);return;}number[topNum - 1] = Math.log10(number[topNum - 1]);topNum++;break;// 自然對數lncase 'l':if (number[topNum - 1] <= 0) {showError(2, str_old);return;}number[topNum - 1] = Math.log(number[topNum - 1]);topNum++;break;// 階乘case '!':if (number[topNum - 1] > 170) {showError(3, str_old);return;} else if (number[topNum - 1] < 0) {showError(2, str_old);return;}number[topNum - 1] = N(number[topNum - 1]);topNum++;break;}// 取堆棧下一個元素計算topNum--;topOp--;}// 如果是數字太大,提示錯誤信息if (number[0] > 7.3E306) {showError(3, str_old);return;}// 輸出最終結果input.setText(String.valueOf(FP(number[0])));tip.setText("計算完畢,要繼續請按歸零鍵 C");mem.setText(str_old + "=" + String.valueOf(FP(number[0])));}/** FP = floating point 控制小數位數,達到精度 否則會出現* 0.6-0.2=0.39999999999999997的情況,用FP即可解決,使得數為0.4 本格式精度為15位*/public double FP(double n) {// NumberFormat format=NumberFormat.getInstance(); //創建一個格式化類f// format.setMaximumFractionDigits(18); //設置小數位的格式DecimalFormat format = new DecimalFormat("0.#############");return Double.parseDouble(format.format(n));}/** 階乘算法*/public double N(double n) {int i = 0;double sum = 1;// 依次將小于等于n的值相乘for (i = 1; i <= n; i++) {sum = sum * i;}return sum;}/** 錯誤提示,按了"="之后,若計算式在process()過程中,出現錯誤,則進行提示*/public void showError(int code, String str) {String message = "";switch (code) {case 1:message = "零不能作除數";break;case 2:message = "函數格式錯誤";break;case 3:message = "值太大了,超出范圍";}input.setText("/"" + str + "/"" + ": " + message);tip.setText(message + "/n" + "計算完畢,要繼續請按歸零鍵 C");}}}

菜單menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:id="@+id/action_settings"android:orderInCategory="100"android:showAsAction="never"android:title="@string/action_settings"/></menu>

以上代碼是小編給大家介紹的Java實現Andriod帶看括弧的計算器代碼,代碼有哪里寫的不好,還請各位大俠多多提出寶貴意見,小編會及時和大家取得聯系的。同時也非常感謝大家一直以來對武林網網站的支持。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 临泽县| 屏东县| 景德镇市| 宣化县| 聂拉木县| 宜春市| 张北县| 开平市| 禄劝| 营口市| 鱼台县| 普安县| 青州市| 揭阳市| 普兰县| 铜川市| 织金县| 澄江县| 壤塘县| 花莲县| 阳西县| 株洲县| 安远县| 北安市| 逊克县| 合川市| 呼伦贝尔市| 浦县| 新晃| 军事| 博罗县| 台东县| 高平市| 东安县| 浪卡子县| 青海省| 贺兰县| 南丹县| 肇源县| 黔南| 宜宾县|