Android 가로 화면 전환 과학 계산기 구현
간단 한 계산기 디자인 으로 다음 과 같은 기능 을 실현 합 니 다.
1.세로 화면 시 기본 적 인 가감 승제 연산,후퇴 와 비우 기 입력 을 실현 할 수 있 습 니 다.
2.가로 화면 시 과학 계산기 로 바 뀌 어 함수 계산,진법 환산 등 기능 을 실현 한다.계산 공식 을 입력 하고 등호 키 에 따라 결 과 를 출력 합 니 다.
3.공식 입력 과 결과 표시 구역 은 팝 업 복사,붙 여 넣 기 기능 을 지원 합 니 다.
4.계산 기 를 사용 하 는 과정 에서 소프트 키 보드 를 꺼 내지 않 아야 합 니 다.
5.진도 바 를 통 해 실시 간 으로 계산 결과 에 보 존 된 소수점 뒷 자리 수 를 조정 하거나 볼 륨 키 를 통 해 같은 효 과 를 완성 할 수 있다.
6.부가 기능 실현:삼각함수,단계 곱 하기,XY,8 진법 전환,16 진법 전환,√(Y&X)와 괄호,예 를 들 어 sin(90).
세로 단순 계산기
세로 화면 인터페이스 디자인 xml 코드:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/textView"
android:layout_width="match_parent"
android:height="5dp"
android:gravity="center|end"
android:textColor="@color/black"
android:textSize="50sp"
android:text=""
android:enabled="true"
android:textCursorDrawable="@null"
android:background="@null"
android:inputType="none"
android:textIsSelectable="true"
android:layout_height="5dp" />
<SeekBar
android:id="@+id/seekBar"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="6dp"
android:layout_marginBottom="5dp"
android:progressDrawable="@drawable/bar"
android:min="1"
android:max="10"/>
<androidx.gridlayout.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:useDefaultMargins="false"
app:columnCount="4"
app:rowCount="5">
<Button
android:id="@+id/button5"
android:background="@drawable/func_btn_style"
android:textSize="24sp"
android:textColor="#0000ff"
android:text="C" />
<Button
android:id="@+id/button6"
android:background="@drawable/func_btn_style"
android:textSize="24sp"
android:textColor="#0000ff"
android:text="÷" />
<Button
android:id="@+id/button7"
android:background="@drawable/func_btn_style"
android:textSize="24sp"
android:textColor="#0000ff"
android:text="×" />
<Button
android:id="@+id/button8"
android:background="@drawable/func_btn_style"
android:textSize="24sp"
android:textColor="#0000ff"
android:text="◁" />
<Button
android:id="@+id/button9"
android:background="@drawable/buttonstyle"
android:textSize="24sp"
android:textColor="@color/black"
android:text="1" />
<Button
android:id="@+id/button10"
android:background="@drawable/buttonstyle"
android:textSize="24sp"
android:textColor="@color/black"
android:text="2" />
<Button
android:id="@+id/button11"
android:background="@drawable/buttonstyle"
android:textSize="24sp"
android:textColor="@color/black"
android:text="3" />
<Button
android:id="@+id/button12"
android:background="@drawable/func_btn_style"
android:textSize="24sp"
android:textColor="#0000ff"
android:text="-" />
<Button
android:id="@+id/button13"
android:background="@drawable/buttonstyle"
android:textSize="24sp"
android:textColor="@color/black"
android:text="4" />
<Button
android:id="@+id/button14"
android:background="@drawable/buttonstyle"
android:textSize="24sp"
android:textColor="@color/black"
android:text="5" />
<Button
android:id="@+id/button15"
android:background="@drawable/buttonstyle"
android:textSize="24sp"
android:textColor="@color/black"
android:text="6" />
<Button
android:id="@+id/button16"
android:background="@drawable/func_btn_style"
android:textSize="24sp"
android:textColor="#0000ff"
android:text="+" />
<Button
android:id="@+id/button17"
android:background="@drawable/buttonstyle"
android:textSize="24sp"
android:textColor="@color/black"
android:text="7" />
<Button
android:id="@+id/button18"
android:background="@drawable/buttonstyle"
android:textSize="24sp"
android:textColor="@color/black"
android:text="8" />
<Button
android:id="@+id/button19"
android:background="@drawable/buttonstyle"
android:textSize="24sp"
android:textColor="@color/black"
android:text="9" />
<Button
android:id="@+id/button20"
android:background="@drawable/equals_button_style"
android:textSize="24sp"
android:textColor="@color/white"
app:layout_rowSpan="2"
android:text="=" />
<Button
android:id="@+id/button21"
android:background="@drawable/buttonstyle"
android:textSize="24sp"
android:textColor="@color/black"
android:text="%" />
<Button
android:id="@+id/button22"
android:background="@drawable/buttonstyle"
android:textSize="24sp"
android:textColor="@color/black"
android:text="0" />
<Button
android:id="@+id/button23"
android:background="@drawable/buttonstyle"
android:textSize="24sp"
android:textColor="@color/black"
android:text="." />
</androidx.gridlayout.widget.GridLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
세로 효과 그림:(진도 바 를 조절 하거나 볼 륨 키 를 통 해 소수점 자릿수 를 조절 할 수 있 습 니 다!)가로 xml 코드:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context=".MainActivity">
<!-- -->
<LinearLayout
android:id="@+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/input"
android:layout_width="match_parent"
android:layout_height="75dp"
android:cursorVisible="false"
android:gravity="right|center_vertical"
android:enabled="true"
android:textCursorDrawable="@null"
android:background="@null"
android:inputType="none"
android:textSize="30sp"
android:textIsSelectable="true"/>
<!-- TableRow -->
<androidx.gridlayout.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rowCount="7"
app:columnCount="6">
<TextView
android:id="@+id/M"
android:layout_height="wrap_content"
android:gravity="center"
android:text=" Mem : " />
<TextView
android:id="@+id/mem"
android:layout_height="wrap_content"
app:layout_columnSpan="5"
android:gravity="left"
android:text=" 0" />
<SeekBar
android:id="@+id/seekBar"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:progressDrawable="@drawable/bar"
android:layout_marginBottom="5dp"
android:layout_height="6dp"
app:layout_columnSpan="6"
android:min="1"
android:max="10"/>
<TextView
android:id="@+id/_drg"
android:layout_height="wrap_content"
android:textSize="18sp"
android:gravity="center"
android:background="@drawable/func_btn_style"
android:text="DEG" />
<Button
android:id="@+id/drg"
android:layout_height="wrap_content"
android:background="@drawable/func_btn_style"
android:textSize="18sp"
android:textColor="#888"
android:text="DRG" />
<Button
android:id="@+id/mc"
android:layout_height="wrap_content"
app:layout_columnSpan="2"
android:background="@drawable/func_btn_style"
android:textColor="#888"
android:textSize="18sp"
android:text="MC" />
<Button
android:id="@+id/c"
android:layout_height="wrap_content"
app:layout_columnSpan="2"
android:background="@drawable/func_btn_style"
android:textColor="#00b5e2"
android:textSize="18sp"
android:text="C" />
<Button
android:id="@+id/sin"
android:layout_height="wrap_content"
android:text="sin"
android:background="@drawable/func_btn_style"
android:textSize="18sp"
android:textColor="#888"
android:textAllCaps="false"/>
<Button
android:id="@+id/cos"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:background="@drawable/func_btn_style"
android:textSize="18sp"
android:textColor="#888"
android:text="cos" />
<Button
android:id="@+id/tan"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textSize="18sp"
android:textColor="#888"
android:background="@drawable/func_btn_style"
android:text="tan" />
<Button
android:id="@+id/factorial"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:textSize="18sp"
android:textColor="#888"
android:background="@drawable/func_btn_style"
android:text="n!" />
<Button
android:id="@+id/bksp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:layout_columnSpan="2"
android:background="@drawable/func_btn_style"
android:textColor="#00b5e2"
android:textSize="18sp"
android:text="◁" />
<Button
android:id="@+id/one"
android:layout_height="wrap_content"
android:background="@drawable/buttonstyle"
android:textSize="18sp"
android:text="1" />
<Button
android:id="@+id/two"
android:layout_height="wrap_content"
android:background="@drawable/buttonstyle"
android:textSize="18sp"
android:text="2" />
<Button
android:id="@+id/three"
android:layout_height="wrap_content"
android:background="@drawable/buttonstyle"
android:textSize="18sp"
android:text="3" />
<Button
android:id="@+id/divide"
android:layout_height="wrap_content"
android:background="@drawable/func_btn_style"
android:textColor="#00b5e2"
android:textSize="18sp"
android:text="÷" />
<Button
android:id="@+id/left"
android:layout_height="wrap_content"
android:background="@drawable/func_btn_style"
android:text="("
android:textColor="#888"
android:textSize="18sp"/>
<Button
android:id="@+id/right"
android:layout_height="wrap_content"
android:background="@drawable/func_btn_style"
android:text=")"
android:textColor="#888"
android:textSize="18sp"/>
<Button
android:id="@+id/four"
android:layout_height="wrap_content"
android:background="@drawable/buttonstyle"
android:text="4"
android:textSize="18sp"/>
<Button
android:id="@+id/five"
android:layout_height="wrap_content"
android:background="@drawable/buttonstyle"
android:text="5"
android:textSize="18sp"/>
<Button
android:id="@+id/six"
android:layout_height="wrap_content"
android:background="@drawable/buttonstyle"
android:text="6"
android:textSize="18sp"/>
<Button
android:id="@+id/mul"
android:layout_height="wrap_content"
android:background="@drawable/func_btn_style"
android:textColor="#00b5e2"
android:text="×"
android:textSize="18sp"/>
<Button
android:id="@+id/sqrt"
android:layout_height="wrap_content"
android:background="@drawable/func_btn_style"
android:text="√"
android:textColor="#888"
android:textSize="18sp"/>
<Button
android:id="@+id/square"
android:layout_height="wrap_content"
android:background="@drawable/func_btn_style"
android:text="^"
android:textColor="#888"
android:textSize="18sp"/>
<Button
android:id="@+id/seven"
android:layout_height="wrap_content"
android:background="@drawable/buttonstyle"
android:text="7"
android:textSize="18sp"/>
<Button
android:id="@+id/eight"
android:layout_height="wrap_content"
android:background="@drawable/buttonstyle"
android:text="8"
android:textSize="18sp"/>
<Button
android:id="@+id/nine"
android:layout_height="wrap_content"
android:background="@drawable/buttonstyle"
android:text="9"
android:textSize="18sp"/>
<Button
android:id="@+id/sub"
android:layout_height="wrap_content"
android:background="@drawable/func_btn_style"
android:textColor="#00b5e2"
android:text="-"
android:textSize="18sp"/>
<Button
android:id="@+id/log"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:background="@drawable/func_btn_style"
android:text="log"
android:textColor="#888"
android:textSize="18sp"/>
<Button
android:id="@+id/ln"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:background="@drawable/func_btn_style"
android:text="ln"
android:textColor="#888"
android:textSize="18sp"/>
<Button
android:id="@+id/dot"
android:layout_height="wrap_content"
android:background="@drawable/buttonstyle"
android:text="."
android:textSize="18sp"/>
<Button
android:id="@+id/zero"
android:layout_height="wrap_content"
android:background="@drawable/buttonstyle"
android:text="0"
android:textSize="18sp"/>
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/bd"
android:layout_height="match_parent"
android:background="@drawable/buttonstyle"
android:text="D"
android:textSize="18sp"
android:layout_weight="1"
android:layout_width="match_parent" />
<Button
android:id="@+id/db"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/buttonstyle"
android:textSize="18sp"
android:layout_weight="1"
android:text="B" />
</LinearLayout>
<Button
android:id="@+id/add"
android:layout_height="wrap_content"
android:background="@drawable/func_btn_style"
android:textColor="#00b5e2"
android:text="+"
android:textSize="18sp"/>
<Button
android:id="@+id/equal"
android:layout_height="wrap_content"
android:background="@drawable/equals_button_style"
android:textColor="#ffffff"
app:layout_columnSpan="2"
android:text="="
android:textSize="18sp"/>
<TextView
android:id="@+id/T"
android:layout_height="wrap_content"
android:text=" : "
app:layout_columnSpan="6"/>
<TextView
android:id="@+id/tip"
android:layout_height="wrap_content"
android:text=" !"
app:layout_columnSpan="6"/>
</androidx.gridlayout.widget.GridLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
가로 효과 도:자바 코드 는 다음 과 같 습 니 다:
package com.zjw;
import android.app.Activity;
import android.content.ClipboardManager;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Bundle;
import android.text.Editable;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import java.math.BigDecimal;
import java.util.StringTokenizer;
public class MainActivity extends Activity {
/**
*
*/
//
public static final int[] DEF_DIV_SCALE = {17};
//
private final String[] init =new String[1];
//
private EditText input;//
//
private SeekBar seekBar;
//
private Button[] btn = new Button[10];// 0~9
private TextView mem, _drg, tip;
private Button div, mul, sub, add, equal, sin, cos, tan, log, ln, sqrt,
square, factorial, bksp, left, right, dot, bd, db, 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
// ContextMenu Id
final int Menu_1 = Menu.FIRST;
final int Menu_2 = Menu.FIRST + 1;
private ClipboardManager mClipboard = null;
public boolean onCreateOptionsMenu(Menu menu) {
//
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
//
switch (item.getItemId()) {
case R.id.menu_setting:
Toast.makeText(this, " ", Toast.LENGTH_SHORT).show();
break;
case R.id.menu_out:
// Toast.makeText(this, " ", Toast.LENGTH_SHORT).show();
android.os.Process.killProcess(android.os.Process.myPid());// PID
System.exit(0);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/**
*onCreate
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ,
if(savedInstanceState!=null){
init[0] = savedInstanceState.getString("exp");
equals_flag = false;
}
//
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
//
setContentView(R.layout.activity_main2);
InitWigdet();
AllWigdetListener();
}
else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
//
setContentView(R.layout.activity_main);
input = (EditText)findViewById(R.id.textView);
input.setText(init[0]);
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setProgress(DEF_DIV_SCALE[0]);
seekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);
Display defaultDisplay = getWindowManager().getDefaultDisplay();
Point point = new Point();
defaultDisplay.getSize(point);
Integer x = point.x;
Integer y = point.y;
input.setKeyListener(new KeyListener() {
@Override
public int getInputType() {
return 0;
}
@Override
public boolean onKeyDown(View view, Editable editable, int i, KeyEvent keyEvent) {
return false;
}
@Override
public boolean onKeyUp(View view, Editable editable, int i, KeyEvent keyEvent) {
return false;
}
@Override
public boolean onKeyOther(View view, Editable editable, KeyEvent keyEvent) {
return false;
}
@Override
public void clearMetaKeyState(View view, Editable editable, int i) {
}
});
View.OnClickListener onClearListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
input.setText("");
}
};
View.OnClickListener onClearLastListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String s = input.getText().toString();
int len = s.length();
if(len>0) {
s = s.substring(0, len - 1);
input.setText(s);
}
}
};
View.OnClickListener onEXPListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
Button temp = (Button)view;
String s = temp.getText().toString();
String show = input.getText().toString();
int len = show.length();
if(show.length()==0){
if(s.equals("+")||s.equals("×")||s.equals("÷")) return;
}
if((show.endsWith("+"))||(show.endsWith("-"))||(show.endsWith("×"))||(show.endsWith("÷"))||(show.endsWith("."))){
if(s.equals("+")||s.equals("×")||s.equals("÷")||s.equals("-")||(s.equals("."))){
show = show.substring(0,len-1) + s;
input.setText(show);
}else{
input.append(s);
}
}else{
input.append(s);
}
}
};
View.OnClickListener onEXECListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String s = input.getText().toString();
if(s.length()==0) return;
s = s.replace("×","*");
s = s.replace("÷","/");
s = s.replace("%","");
if((s.endsWith("+"))||(s.endsWith("-"))||(s.endsWith("*"))||(s.endsWith("/"))||(s.endsWith(".")))return;
try{
String re = Calculator.conversion(s);
re = round(re,DEF_DIV_SCALE[0]);
if(re.indexOf(".") > 0){
re = re.replaceAll("0+?$", "");// 0
re = re.replaceAll("[.]$", "");// .
}
input.setText(re);
}catch (Exception e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
input.setText("");
}
}
};
View.OnClickListener onPERCENTCListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
String s = input.getText().toString();
if(s.length()==0) return;
s = s.replace("×","*");
s = s.replace("÷","/");
s = s.replace("%","");
if((s.endsWith("+"))||(s.endsWith("-"))||(s.endsWith("*"))||(s.endsWith("/"))||(s.endsWith("."))) return;
s = s+"/100";
try{
String re = Calculator.conversion(s);
re = round(re,DEF_DIV_SCALE[0]);
if(re.indexOf(".") > 0){
re = re.replaceAll("0+?$", "");// 0
re = re.replaceAll("[.]$", "");// .
}
input.setText(re+"%");
}catch (Exception e){
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
input.setText("");
}
}
};
input.getLayoutParams().height = 2*y/7;
Button button5 = (Button)findViewById(R.id.button5); button5.getLayoutParams().width = x/4 ;button5.getLayoutParams().height = y/8;
Button button6 = (Button)findViewById(R.id.button6); button6.getLayoutParams().width = x/4 ;button6.getLayoutParams().height = y/8;
Button button7 = (Button)findViewById(R.id.button7); button7.getLayoutParams().width = x/4 ;button7.getLayoutParams().height = y/8;
Button button8 = (Button)findViewById(R.id.button8); button8.getLayoutParams().width = x/4 ;button8.getLayoutParams().height = y/8;
Button button9 = (Button)findViewById(R.id.button9); button9.getLayoutParams().width = x/4 ;button9.getLayoutParams().height = y/8;
Button button10 = (Button)findViewById(R.id.button10); button10.getLayoutParams().width = x/4 ;button10.getLayoutParams().height = y/8;
Button button11 = (Button)findViewById(R.id.button11); button11.getLayoutParams().width = x/4 ;button11.getLayoutParams().height = y/8;
Button button12 = (Button)findViewById(R.id.button12); button12.getLayoutParams().width = x/4 ;button12.getLayoutParams().height = y/8;
Button button13 = (Button)findViewById(R.id.button13); button13.getLayoutParams().width = x/4 ;button13.getLayoutParams().height = y/8;
Button button14 = (Button)findViewById(R.id.button14); button14.getLayoutParams().width = x/4 ;button14.getLayoutParams().height = y/8;
Button button15 = (Button)findViewById(R.id.button15); button15.getLayoutParams().width = x/4 ;button15.getLayoutParams().height = y/8;
Button button16 = (Button)findViewById(R.id.button16); button16.getLayoutParams().width = x/4 ;button16.getLayoutParams().height = y/8;
Button button17 = (Button)findViewById(R.id.button17); button17.getLayoutParams().width = x/4 ;button17.getLayoutParams().height = y/8;
Button button18 = (Button)findViewById(R.id.button18); button18.getLayoutParams().width = x/4 ;button18.getLayoutParams().height = y/8;
Button button19 = (Button)findViewById(R.id.button19); button19.getLayoutParams().width = x/4 ;button19.getLayoutParams().height = y/8;
Button button20 = (Button)findViewById(R.id.button20); button20.getLayoutParams().width = x/4 ;button20.getLayoutParams().height = 2*(y/8);
Button button21 = (Button)findViewById(R.id.button21); button21.getLayoutParams().width = x/4 ;button21.getLayoutParams().height = y/8;
Button button22 = (Button)findViewById(R.id.button22); button22.getLayoutParams().width = x/4 ;button22.getLayoutParams().height = y/8;
Button button23 = (Button)findViewById(R.id.button23); button23.getLayoutParams().width = x/4 ;button23.getLayoutParams().height = y/8;
button6.setOnClickListener(onEXPListener);button7.setOnClickListener(onEXPListener);
button9.setOnClickListener(onEXPListener);button10.setOnClickListener(onEXPListener);
button11.setOnClickListener(onEXPListener);button12.setOnClickListener(onEXPListener);
button13.setOnClickListener(onEXPListener);button14.setOnClickListener(onEXPListener);
button15.setOnClickListener(onEXPListener);button16.setOnClickListener(onEXPListener);
button17.setOnClickListener(onEXPListener);button18.setOnClickListener(onEXPListener);
button19.setOnClickListener(onEXPListener);button21.setOnClickListener(onEXPListener);
button22.setOnClickListener(onEXPListener);button23.setOnClickListener(onEXPListener);
button5.setOnClickListener(onClearListener);
button8.setOnClickListener(onClearLastListener);
button20.setOnClickListener(onEXECListener);
button21.setOnClickListener(onPERCENTCListener);
}
}
/**
*
*/
private void InitWigdet() {
//
Display defaultDisplay = getWindowManager().getDefaultDisplay();
Point point = new Point();
defaultDisplay.getSize(point);
Integer x = point.x;
Integer y = point.y - 60;
//
input = (EditText) findViewById(R.id.input);
input.setKeyListener(new KeyListener() {
@Override
public int getInputType() {
return 0;
}
@Override
public boolean onKeyDown(View view, Editable editable, int i, KeyEvent keyEvent) {
return false;
}
@Override
public boolean onKeyUp(View view, Editable editable, int i, KeyEvent keyEvent) {
return false;
}
@Override
public boolean onKeyOther(View view, Editable editable, KeyEvent keyEvent) {
return false;
}
@Override
public void clearMetaKeyState(View view, Editable editable, int i) {
}
});
input.setText(init[0]);
seekBar = (SeekBar) findViewById(R.id.seekBar); //seekBar.getLayoutParams().height = y/7/3;
seekBar.setProgress(DEF_DIV_SCALE[0]);
seekBar.setOnSeekBarChangeListener(onSeekBarChangeListener);
input.getLayoutParams().height = y/9;
mem = (TextView) findViewById(R.id.mem);
tip = (TextView) findViewById(R.id.tip);
_drg = (TextView) findViewById(R.id._drg); _drg.getLayoutParams().width = x/6 ;_drg.getLayoutParams().height = y/10-8;
btn[0] = (Button) findViewById(R.id.zero); btn[0].getLayoutParams().width = x/6 ;btn[0].getLayoutParams().height = y/10;
btn[1] = (Button) findViewById(R.id.one); btn[1].getLayoutParams().width = x/6 ;btn[1].getLayoutParams().height = y/10;
btn[2] = (Button) findViewById(R.id.two); btn[2].getLayoutParams().width = x/6 ;btn[2].getLayoutParams().height = y/10;
btn[3] = (Button) findViewById(R.id.three); btn[3].getLayoutParams().width = x/6 ;btn[3].getLayoutParams().height = y/10;
btn[4] = (Button) findViewById(R.id.four); btn[4].getLayoutParams().width = x/6 ;btn[4].getLayoutParams().height = y/10;
btn[5] = (Button) findViewById(R.id.five); btn[5].getLayoutParams().width = x/6 ;btn[5].getLayoutParams().height = y/10;
btn[6] = (Button) findViewById(R.id.six); btn[6].getLayoutParams().width = x/6 ;btn[6].getLayoutParams().height = y/10;
btn[7] = (Button) findViewById(R.id.seven); btn[7].getLayoutParams().width = x/6 ;btn[7].getLayoutParams().height = y/10;
btn[8] = (Button) findViewById(R.id.eight); btn[8].getLayoutParams().width = x/6 ;btn[8].getLayoutParams().height = y/10;
btn[9] = (Button) findViewById(R.id.nine); btn[9].getLayoutParams().width = x/6 ;btn[9].getLayoutParams().height = y/10;
div = (Button) findViewById(R.id.divide); div.getLayoutParams().width = x/6 ;div.getLayoutParams().height = y/10;
mul = (Button) findViewById(R.id.mul);mul.getLayoutParams().width = x/6 ;mul.getLayoutParams().height = y/10;
sub = (Button) findViewById(R.id.sub); sub.getLayoutParams().width = x/6 ;sub.getLayoutParams().height = y/10;
add = (Button) findViewById(R.id.add); add.getLayoutParams().width = x/6 ;add.getLayoutParams().height = y/10;
equal = (Button) findViewById(R.id.equal); equal.getLayoutParams().width = 2*x/6 ;equal.getLayoutParams().height = y/10;
sin = (Button) findViewById(R.id.sin); sin.getLayoutParams().width = x/6 ;sin.getLayoutParams().height = y/10-8;
cos = (Button) findViewById(R.id.cos); cos.getLayoutParams().width = x/6 ;cos.getLayoutParams().height = y/10-8;
tan = (Button) findViewById(R.id.tan); tan.getLayoutParams().width = x/6 ;tan.getLayoutParams().height = y/10-8;
log = (Button) findViewById(R.id.log); log.getLayoutParams().width = x/6 ;log.getLayoutParams().height = y/10;
ln = (Button) findViewById(R.id.ln); ln.getLayoutParams().width = x/6 ;ln.getLayoutParams().height = y/10;
sqrt = (Button) findViewById(R.id.sqrt); sqrt.getLayoutParams().width = x/6 ;sqrt.getLayoutParams().height = y/10;
square = (Button) findViewById(R.id.square); square.getLayoutParams().width = x/6 ;square.getLayoutParams().height = y/10;
factorial = (Button) findViewById(R.id.factorial);factorial.getLayoutParams().width = x/6 ;factorial.getLayoutParams().height = y/10-8;
bksp = (Button) findViewById(R.id.bksp);bksp.getLayoutParams().width = 2*x/6 ;bksp.getLayoutParams().height = y/10-8;
left = (Button) findViewById(R.id.left); left.getLayoutParams().width = x/6 ;left.getLayoutParams().height = y/10;
right = (Button) findViewById(R.id.right); right.getLayoutParams().width = x/6 ;right.getLayoutParams().height = y/10;
dot = (Button) findViewById(R.id.dot);dot.getLayoutParams().width = x/6 ;dot.getLayoutParams().height = y/10;
bd = (Button) findViewById(R.id.bd);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);linearLayout.getLayoutParams().width = x/6 ;linearLayout.getLayoutParams().height = y/10;
db = (Button) findViewById(R.id.db);
drg = (Button) findViewById(R.id.drg);drg.getLayoutParams().width = x/6 ;drg.getLayoutParams().height = y/10-8;
mc = (Button) findViewById(R.id.mc); mc.getLayoutParams().width = 2*x/6 ;mc.getLayoutParams().height = y/10-8;
c = (Button) findViewById(R.id.c); c.getLayoutParams().width = 2*x/6 ;c.getLayoutParams().height = y/10-8;
}
/**
*
*/
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);
bd.setOnClickListener(actionPerformed);
db.setOnClickListener(actionPerformed);
drg.setOnClickListener(actionPerformed);
mc.setOnClickListener(actionPerformed);
c.setOnClickListener(actionPerformed);
}
/**
*
*/
String[] TipCommand = new String[500];
int tip_i = 0;// TipCommand
private View.OnClickListener actionPerformed = new View.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("◁") == 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) {
// 0
input.setText("0");
vbegin = true;
tip_i = 0;
tip_lock = true;
tip.setText("welcome use the APP!");
//
} else if (command.compareTo("C") == 0) {
// 0
input.setText("0");
// true
vbegin = true;
// 0
tip_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");
// ”bd“
} else if (command.compareTo("B") == 0) {
String s = input.getText().toString();
s = s.replace(".","");
//
if(".+-x÷√^sincostanloglnn!()".indexOf(s) != -1){
input.setText("0");
}else
{
try {
int a = Integer.valueOf(s);
String re = Integer.toBinaryString(a);
input.setText(re);
}catch (Exception e){
input.setText("");
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
// ”db“
}else if(command.compareTo("D") == 0){
String s = input.getText().toString();
s = s.replace(".","");
input.setText("1234567890");
//
if("23456789+-x÷√^sincostanloglnn!().".indexOf(s) != -1){
input.setText("0");
}else{
try{
Integer re = Integer.parseInt(s,2);
input.setText(re.toString());
}catch (Exception e){
input.setText("");
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
} 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!", "!");
// true
vbegin = 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++) {
// , 1
if (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++) {
// , 1
if (TipCommand[i].compareTo("(") == 0) {
tip_right_bracket++;
}
// , 1
if (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 + "
";
break;
case 1:
tipmessage = tipcommand1 + " : /(/./-/
";
break;
case 2:
tipmessage = tipcommand1 + " :)/
";
break;
case 3:
tipmessage = tipcommand1 + " :)/ /
";
break;
case 4:
tipmessage = tipcommand1 + " :)/./ /
";
break;
case 5:
tipmessage = tipcommand1 + " :(/./ /
";
break;
case 6:
tipmessage = tipcommand1 + " :(/./
";
break;
case 7:
tipmessage = tipcommand1 + " :(/./
";
break;
case 8:
tipmessage = "
";
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 :
"
+ "DEG:sin30 = 0.5 RAD:sin1 = 0.84
"
+ " : , :
" + "sin(cos45), sincos45";
break;
case 6:
tipmessage = tipmessage + "cos :
"
+ "DEG:cos60 = 0.5 RAD:cos1 = 0.54
"
+ " : , :
" + "cos(sin45), cossin45";
break;
case 7:
tipmessage = tipmessage + "tan :
"
+ "DEG:tan45 = 1 RAD:tan1 = 1.55
"
+ " : , :
" + "tan(cos45), tancos45";
break;
case 8:
tipmessage = tipmessage + "log :
"
+ "log10 = log(5+5) = 1
" + " : , :
"
+ "log(tan45), logtan45";
break;
case 9:
tipmessage = tipmessage + "ln :
"
+ "ln10 = le(5+5) = 2.3 lne = 1
"
+ " : , :
" + "ln(tan45), lntan45";
break;
case 10:
tipmessage = tipmessage + "n! :
"
+ "n!3 = n!(1+2) = 3×2×1 = 6
" + " : , :
"
+ "n!(log1000), n!log1000";
break;
case 11:
tipmessage = tipmessage + "√ :
"
+ " :27 3 27√3 = 3
" + " : , :
"
+ "( )√( ) , (n!3)√(log100) = 2.45";
break;
case 12:
tipmessage = tipmessage + "^ :
" + " :2 3 2^3 = 8
"
+ " : , :
"
+ "( )√( ) , (n!3)^(log100) = 36";
break;
}
// tip
tip.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) {
// +- , 1
case '+':
case '-':
weightTemp = 1 + weightPlus;
break;
// x÷ , 2
case '×':
case '÷':
weightTemp = 2 + weightPlus;
break;
// sincos 3
case '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;
//
// sin
case '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;
// cos
case '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;
// tan
case '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;
// log
case 'g':
if (number[topNum - 1] <= 0) {
showError(2, str_old);
return;
}
number[topNum - 1] = Math
.log10(number[topNum - 1]);
topNum++;
break;
// ln
case '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;
// sin
case '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;
// cos
case '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;
// tan
case '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;
// log
case 'g':
if (number[topNum - 1] <= 0) {
showError(2, str_old);
return;
}
number[topNum - 1] = Math.log10(number[topNum - 1]);
topNum++;
break;
// ln
case '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.#############");
Double re = n;
String s = round(re.toString(),DEF_DIV_SCALE[0]);
return Double.parseDouble(s);
}
/*
*
*/
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 + "
" + " , C");
}
}
/**
* EditEext
*/
@Override // ,
protected void onSaveInstanceState(@NonNull Bundle outState) { // , outState
super.onSaveInstanceState(outState);
init[0] = input.getText().toString();
outState.putString("exp",init[0]);
}
/**
*
*/
public static String round(String v, int scale) {
if (scale < 0) {
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(v);
BigDecimal one = new BigDecimal("1");
BigDecimal re = b.divide(one, scale, BigDecimal.ROUND_HALF_UP);
return re.toString();
}
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.main, menu);
// return true;
// }
/**
*
*/
@Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
String s = input.getText().toString();
if(s.length()<1){
}else {
switch (keyCode) {
//
case KeyEvent.KEYCODE_VOLUME_DOWN:
if(DEF_DIV_SCALE[0]>1)
DEF_DIV_SCALE[0]--;
seekBar.setProgress(DEF_DIV_SCALE[0]);
Toast.makeText(this.getApplicationContext(), "DOWN " + DEF_DIV_SCALE[0], Toast.LENGTH_SHORT).show();
if(s.contains(".")) input.setText(round(s,DEF_DIV_SCALE[0]));
//
return true;
//
case KeyEvent.KEYCODE_VOLUME_UP:
if(DEF_DIV_SCALE[0]<40)
DEF_DIV_SCALE[0]++;
seekBar.setProgress(DEF_DIV_SCALE[0]);
Toast.makeText(this.getApplicationContext(), "UP " + DEF_DIV_SCALE[0], Toast.LENGTH_SHORT).show();
if(s.contains(".")) input.setText(round(s,DEF_DIV_SCALE[0]));
//
return true;
}
}
return super.onKeyDown (keyCode, event);
}
/**
*
*/
public SeekBar.OnSeekBarChangeListener onSeekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//txt_cur.setText(" :" + progress + " / 100 ");
String s = input.getText().toString();
if(s.length()<1){
return;
}else{
if(progress>DEF_DIV_SCALE[0]){
DEF_DIV_SCALE[0] = progress;
Toast.makeText(getApplicationContext(), "UP " + DEF_DIV_SCALE[0], Toast.LENGTH_SHORT).show();
if(s.contains(".")) input.setText(round(s,DEF_DIV_SCALE[0]));
}else if(progress<DEF_DIV_SCALE[0]){
DEF_DIV_SCALE[0] = progress;
Toast.makeText(getApplicationContext(), "DOWN " + DEF_DIV_SCALE[0], Toast.LENGTH_SHORT).show();
if(s.contains(".")) input.setText(round(s,DEF_DIV_SCALE[0]));
}
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
//Toast.makeText(mContext, " SeekBar", Toast.LENGTH_SHORT).show();
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//Toast.makeText(mContext, " SeekBar", Toast.LENGTH_SHORT).show();
}
};
/**
* 2
*/
public int BinaryToDecimal(String s){
int binaryNumber = Integer.valueOf(s);
int decimal = 0;
int p = 0;
while(true){
if(binaryNumber == 0){
break;
} else {
int temp = binaryNumber%10;
decimal += temp*Math.pow(2, p);
binaryNumber = binaryNumber/10;
p++;
}
}
return decimal;
}
/**
* 2
*/
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.