AndroidStudio高级计算器三角函数对数

用AndroidStudio编写高级计算器带三角函数对数运算功能

界面效果图:
AndroidStudio高级计算器三角函数对数_第1张图片

layout布局 activity_jisuanqi.xml代码:


<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) {
		// 处理括号里的表达式,思路,
		// 1.遇到正括号就将它在字符串里的索引保存在一个链表里<插在链表头>
		// 2.遇到第一个反括号就处理这个括号里的表达式<取出链表第一个元素,然后根据这两个索引取出表达式,会调用一个专门处理
		// log,ln,sin,cos,tan等的special_dispose函数,将反回一个自由加减乘除的表达式,在调用alg_dispose函数>
		// 3.函数处理后返回一个可能还含有log,ln,sin,cos,tan等的但没有括号的表达式;

		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) {
		// 用于处理特殊表达式;

		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右边的第一个数;
		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左边的第一个数;
		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) {
		// 将π替换为对应的数字;
		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) {
		// 处理指定运算符;
		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;
	}
}

你可能感兴趣的