我的Android计算器

package com.myths.counter.activity;




import java.util.regex.Matcher;
import java.util.regex.Pattern;


import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;


public class CounterActivity extends Activity implements OnClickListener{
private Button b_one;
private Button b_two;
private Button b_three;
private Button b_four;
private Button b_five;
private Button b_six;
private Button b_seven;
private Button b_eight;
private Button b_nine;
private Button b_zero;
private Button b_add;//加
private Button b_sub;//减
private Button b_mul;//乘
private Button b_exc;//除
private Button b_equ;//=
private Button b_point;//.
private Button b_c;
public static int sum=0;//点号个数判断!
public static Double double_sum=0.0;
public static int fuhao=0;//============负号标记
public int k=-1;

private TextView textview;

private void init() {
this.textview = (TextView) this.findViewById(R.id.textview);

this.b_one = (Button) this.findViewById(R.id.button_1);
this.b_two = (Button) this.findViewById(R.id.button_2);
this.b_three = (Button) this.findViewById(R.id.button_3);
this.b_four = (Button) this.findViewById(R.id.button_4);
this.b_five = (Button) this.findViewById(R.id.button_5);
this.b_six = (Button) this.findViewById(R.id.button_6);
this.b_seven = (Button) this.findViewById(R.id.button_7);
this.b_eight = (Button) this.findViewById(R.id.button_8);
this.b_nine = (Button) this.findViewById(R.id.button_9);
this.b_zero = (Button) this.findViewById(R.id.button_0);
this.b_add = (Button) this.findViewById(R.id.button_jia);
this.b_sub = (Button) this.findViewById(R.id.button_jian);
this.b_mul = (Button) this.findViewById(R.id.button_chen);
this.b_exc = (Button) this.findViewById(R.id.button_chu);
this.b_equ = (Button) this.findViewById(R.id.button_dengyu);
this.b_point = (Button) this.findViewById(R.id.button_point);
this.b_c = (Button) this.findViewById(R.id.button_c);
}

public void setListener(){
this.b_one.setOnClickListener(this);
this.b_two.setOnClickListener(this);
this.b_three.setOnClickListener(this);
this.b_four.setOnClickListener(this);
this.b_five.setOnClickListener(this);
this.b_six.setOnClickListener(this);
this.b_seven.setOnClickListener(this);
this.b_eight.setOnClickListener(this);
this.b_nine.setOnClickListener(this);
this.b_zero.setOnClickListener(this);
this.b_add.setOnClickListener(this);
this.b_sub.setOnClickListener(this);
this.b_mul.setOnClickListener(this);
this.b_exc.setOnClickListener(this);
this.b_equ.setOnClickListener(this);
this.b_point.setOnClickListener(this);
this.b_c.setOnClickListener(this);
}

private  void changeTextView(String text) {
String str = (String) this.textview.getText();
this.textview.setText(str+text);
//this.m_add(str);
}


public String changeWord(String str)  //替换 +-等符号的
{
str = str.replace(".", "d");
str = str.replace("+", "a");
str = str.replace("-", "s");
str = str.replace("×", "m");
str = str.replace("÷", "e");
return str=str.replace("=", "k");


}

public String equre_do(String str)//==等号的运算动作===
{
// String str = this.changeWord(strs);
if(!(check(str,"a")&check(str,"s")&check(str,"m")&check(str,"e")&check(str,"k")))
sum=0;

if(check(str,"k")){//如果没有等于符号就去算
if(str.charAt(0)!='s')//----------------------如果第一位不是S 就

{ System.out.println("=haoys_diyi bu shi s =====fuhao="+fuhao);
System.out.println("■■■■ "+str+" ■■■■");

if(false==check(str,"a")){
changeTextView("="+m_add(str));// +
double_sum = m_add(str);
System.out.println("■■■■ + ■■■■");
}
if(false==check(str,"s")){
System.out.println("wo qu jian ");

changeTextView("="+m_sub(str));//  -
double_sum = m_sub(str);
System.out.println("■■■■ - ■■■■");
}
if(false==check(str,"m")){
System.out.println("wo qu cheng ");

changeTextView("="+m_mul(str));//  乘
double_sum = m_mul(str);
System.out.println("■■■■ cheng ■■■■");
}
if(false==check(str,"e")){
changeTextView("="+m_exc(str));//  除
double_sum = m_exc(str);
System.out.println("■■■■ chu ■■■■");
}
}else{//第一个为符号啊

fuhao=-1;//
System.out.println("=haoys_diyi shi s =====fuhao="+fuhao);
System.out.println("====="+str+"=====");

String strmy ="-";



for (int i = 1; i < str.length(); i++) {
strmy = strmy+""+str.charAt(i);
System.out.println(str.charAt(i));
} System.out.println("strmy="+strmy);
equre_do(strmy);

}

}else{
this.textview.setText(""+double_sum);//有等于符号     保存着上次的 结果     切换界面;
if(double_sum<0.0)
fuhao=-1;
else fuhao =0;
double_sum = 0.0;
}
return str;
}



/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

public static double m_add(String str) {  //  +加
double sum_add=0;
System.out.println("=fuhao=="+ str+"===="+fuhao);

if(fuhao==-1&str.charAt(0)!='s')
{ System.out.println("=fuhao=="+ str+"===="+fuhao);
str = str.replace("s", "-");
}
String[] stres_add = str.split("a");
for (int i = 0; i < stres_add.length; i++) {

//System.out.println(stres_add[i]);
sum_add += Double.parseDouble(stres_add[i]);


System.out.println(stres_add[i]+"=长度=="+ stres_add.length+"====");
}
return sum_add;

}

public static double m_sub(String str){ //  -减
double sum_sub=0;
int k=0;
// String str_sub = str.replace("-", "s");
// System.out.println("====-=2===");
String[] stres_sub = str.split("s");

for (int i = 0; i < stres_sub.length; i++,k++) {

//System.out.println(stres_add[i]);


sum_sub = (Double.parseDouble(stres_sub[i]))-sum_sub;


System.out.println(stres_sub[i]+"===="+ sum_sub+"====");
}
if(k==1) return sum_sub;
return 0.0-sum_sub;

}

public static double m_mul(String str) { //  乘
double sum_mul=1;
if(str.charAt(0)=='s')//如果有
{ String ko="";
for(int i=1;i<str.length();i++)
{
ko = ""+str.charAt(i);
}
sum_mul = 0-m_mul(ko);
}
else{
String[] stres_mul = str.split("m");
for (int i = 0; i < stres_mul.length; i++) {

//System.out.println(stres_add[i]);
sum_mul *= Double.parseDouble(stres_mul[i]);


// System.out.println(stres_mul[i]+"===="+ sum_mul+"====");
}

}
return sum_mul;
}

public static double m_exc(String str) { // 除 
double sum_exc=1;
String[] stres_exc = str.split("e");
double exc=Double.parseDouble(stres_exc[0]);

for (int i = 0; i < stres_exc.length; i++) {
if (i==0)
sum_exc = Double.parseDouble(stres_exc[i]);
else
sum_exc = sum_exc / Double.parseDouble(stres_exc[i]);



System.out.println(stres_exc[i]+"==/="+ sum_exc+"====");
}

return sum_exc;
 
}


public static boolean check(String str,String fuhao){
 
 Pattern p = Pattern.compile(fuhao);
  Matcher m = p.matcher(str);
  boolean match = m.find();
  if(match){    
  System.out.println(fuhao+"---false");
   return false;

  }
  System.out.println(fuhao+"---true");
  return true;
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
        setListener();
        this.textview.setText("");
    }


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
@Override
public void onClick(View v) {
if(this.b_one.getId() == v.getId()) {//==按键1
if(double_sum!=0.0){

//===========

//============
this.textview.setText(""+double_sum);
double_sum = 0.0;
}
changeTextView("1");

}else if(this.b_two.getId() == v.getId()){//==按键2
if(double_sum!=0.0){
this.textview.setText(""+double_sum);
double_sum = 0.0;
}
changeTextView("2");
}else if(this.b_three.getId() == v.getId()){//==按键3
if(double_sum!=0.0){
this.textview.setText(""+double_sum);
double_sum = 0.0;
}
changeTextView("3");
}else if(this.b_four.getId() == v.getId()){//==按键4
if(double_sum!=0.0){
this.textview.setText(""+double_sum);
double_sum = 0.0;
}
changeTextView("4");
}else if(this.b_five.getId() == v.getId()){//==按键5
if(double_sum!=0.0){
this.textview.setText(""+double_sum);
double_sum = 0.0;
}
changeTextView("5");
}else if(this.b_six.getId() == v.getId()){//==按键6
if(double_sum!=0.0){
this.textview.setText(""+double_sum);
double_sum = 0.0;
}
changeTextView("6");
}else if(this.b_seven.getId() == v.getId()){//==按键7
if(double_sum!=0.0){
this.textview.setText(""+double_sum);
double_sum = 0.0;
}
changeTextView("7");
}else if(this.b_eight.getId() == v.getId()){//==按键8
if(double_sum!=0.0){
this.textview.setText(""+double_sum);
double_sum = 0.0;
}
changeTextView("8");
}else if(this.b_nine.getId() == v.getId()){//==按键9
if(double_sum!=0.0){
this.textview.setText(""+double_sum);
double_sum = 0.0;
}
changeTextView("9");
}else if(this.b_zero.getId() == v.getId()){//==按键0
if(double_sum!=0.0){
this.textview.setText(""+double_sum);
double_sum = 0.0;
}
changeTextView("0");
}else if(this.b_add.getId() == v.getId()){//===================加号判断
String mk =(String) this.textview.getText();


String str_mk = this.changeWord(mk);

if(double_sum==0.0)
{


if(str_mk=="")
changeTextView("");


else {
if(check(str_mk,"a")&check(str_mk,"s")&check(str_mk,"m")&check(str_mk,"e")&check(str_mk,"k"))
changeTextView("+");
else{
//========================
//if(str_mk=="a"|str_mk=="s"|str_mk=="m"|str_mk=="e")// 当前数据中  有!a s m e;



{
String str = (String) this.textview.getText();
System.out.println("str===="+ str+"====");
str = str.replace("+", "a");
str = str.replace("-", "s");
str = str.replace("×", "m");
str = str.replace("÷", "e");
str = str.replace("=", "k");

System.out.println("++++===="+str_mk+"====");
this.equre_do(str);
}


//=========================
}
}
}
else {
this.textview.setText(""+double_sum+"+");
double_sum = 0.0;
//changeTextView("+");
}
}else if(this.b_sub.getId() == v.getId()){//===================点了减号
String ll =(String) this.textview.getText();
String str_ll = this.changeWord(ll);
if(double_sum==0.0)
{
if(str_ll=="")
changeTextView("");
else if(check(str_ll,"a")&check(str_ll,"s")&check(str_ll,"m")&check(str_ll,"e")&check(str_ll,"k"))
changeTextView("-");
else{//=================================================有加减号!!!!!
String str = (String) this.textview.getText();
System.out.println("str===="+ str+"====");
str = str.replace("+", "a");
str = str.replace("-", "s");
str = str.replace("×", "m");
str = str.replace("÷", "e");
str = str.replace("=", "k");


this.equre_do(str);
}
}else {
if(double_sum>0.0)
{ fuhao=0;
this.textview.setText(""+double_sum);
double_sum = 0.0;
}
else{
fuhao=-1;//符号标记
this.textview.setText(""+double_sum);
double_sum = 0.0;}
changeTextView("-");
}
}else if(this.b_mul.getId() == v.getId()){//===================陈号判断
String xx =(String) this.textview.getText();
String str_xx = this.changeWord(xx);
if(double_sum==0.0)
{
if(str_xx=="")
changeTextView("");

else if(check(str_xx,"a")&check(str_xx,"s")&check(str_xx,"m")&check(str_xx,"e")&check(str_xx,"k"))
changeTextView("×");
else {
String str = (String) this.textview.getText();
System.out.println("str===="+ str+"====");
str = str.replace("+", "a");
str = str.replace("-", "s");
str = str.replace("×", "m");
str = str.replace("÷", "e");
str = str.replace("=", "k");


this.equre_do(str);
};
}else {
this.textview.setText(""+double_sum);
double_sum = 0.0;
changeTextView("×");
}

}else if(this.b_exc.getId() == v.getId()){//===================除号判断
String jj =(String) this.textview.getText();
String str_jj = this.changeWord(jj);

if(double_sum==0.0)
{
if(str_jj=="")
changeTextView("");
else if(check(str_jj,"a")&check(str_jj,"s")&check(str_jj,"m")&check(str_jj,"e")&check(str_jj,"k"))
changeTextView("÷");
else{
String str = (String) this.textview.getText();
System.out.println("str===="+ str+"====");
str = str.replace("+", "a");
str = str.replace("-", "s");
str = str.replace("×", "m");
str = str.replace("÷", "e");
str = str.replace("=", "k");


this.equre_do(str);
};// this.equre_do(str_jj);;
}else {
this.textview.setText(""+double_sum);
double_sum = 0.0;
changeTextView("÷");
}

}else if(this.b_point.getId() == v.getId()){//=================== 点号判断===================
String dd =(String) this.textview.getText();
dd = dd.replace(".", "d");
dd= dd.replace("+", "a");
dd= dd.replace("-", "s");
dd=dd.replace("×", "m");
dd=dd.replace("÷", "e");




if(double_sum==0.0)
{   System.out.println("没有结果啊=");
if(dd=="")//空数据点点  
{   //System.out.println("dd:"+dd+"===ddlase:");
changeTextView("0.");
sum++;
}//=都需要判断不能重复点击
else if(dd.length()!=0&&(dd.charAt(dd.length()-1)=='a'||dd.charAt(dd.length()-1)=='s'||dd.charAt(dd.length()-1)=='m'||dd.charAt(dd.length()-1)=='e'))//或者 当前数据最后一位是a s m e;
{
System.out.println("dd:"+dd+"===ddlase:"+dd.charAt(dd.length()-1));
changeTextView("0.");
sum++;

else if( check(dd,"d") ){ //当前数据中没有点号
changeTextView(".");
sum++;
}
else{
if(check(dd,"a")&check(dd,"s")&check(dd,"m")&check(dd,"e")){//前头有点 并且没有+ -号  --- 不再打点点
System.out.println(check(dd,"a")&check(dd,"s")&check(dd,"m")&check(dd,"e"));
}
else {
if(sum>1)
changeTextView("");
else {
changeTextView(".");
sum++;
}
}
}
}else {

this.textview.setText(""+double_sum);
double_sum = 0.0;
String aa =(String)(""+double_sum);
aa = aa.replace(".", "d");
if(check(aa,"a"))//如果double.sum有点
changeTextView("");
else//无点
changeTextView(".");
}
}//===================================================================== 点号判断===================
else if(this.b_c.getId() == v.getId()){//=================== 删除号判断
this.textview.setText("");
double_sum = 0.0;
}else if(this.b_equ.getId() == v.getId()){//=================== 等号判断

String str = (String) this.textview.getText();
System.out.println("str===="+ str+"====");
str = str.replace("+", "a");
str = str.replace("-", "s");
str = str.replace("×", "m");
str = str.replace("÷", "e");
str = str.replace("=", "k");

this.equre_do(str);

}

}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值