android 단순 계산기 기능 구현

18348 단어 android계산기
본 논문 의 사례 는 안 드 로 이 드 가 간단 한 계산 기 를 실현 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.

//   :2014/9/26
 
//   ,             。      ,                  
 
////////////////////////////////////////////////////////////////////////////////////////
//        
// 1.             ,     。( :        )
// 2.              。( 4.5.6)
//       bug,       
 
////////////////////////////////////////////////////////////////////////////////////////
//       。          。
//           ,   。(st)
 
 
package com.example.countea;
 
import android.os.Bundle;          //        
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
 
import android.widget.Toast;         //       
import java.util.Iterator;         //    
import java.util.LinkedList;         //     
 
public class MainActivity extends Activity {
 private EditText content;
 private EditText operaline;
 private Double first_num = 0.0;       //       
 private Double sec_num = 0.0;       //       
 //    bool   ?????
 private static int equal_flg = 0;       //      :FALSE       ,TRUE        
 private double negative_mark = 0;       //      
 LinkedList<String> Infix = new LinkedList<String>();  //  content    ,           
 LinkedList<String> Suffix = new LinkedList<String>();  //           
 LinkedList<Double> Suffix_Num = new LinkedList<Double>(); //             
 LinkedList<String> OP = new LinkedList<String>();   // 1.            ;2.           
 
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 
 //   "c"      
 Button clear = (Button) findViewById(R.id.clear);
 clear.setOnClickListener(new OnClickListener() {
 
 @Override
 public void onClick(View v) 
 {
 // TODO Auto-generated method stub
 content = (EditText) findViewById(R.id.content);
 content.setText("");
 equal_flg = 0;
 
 first_num = 0.0;
 sec_num = 0.0;
 OP.clear();
 Suffix.clear();
 Suffix_Num.clear();
 operaline.setText("");
 operaline.setSelection(operaline.getText().length());
 }
 });
 
 //   CE          
 Button current_clear = (Button) findViewById(R.id.current_clear);
  current_clear.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) 
 {
 // TODO Auto-generated method stub
 content = (EditText) findViewById(R.id.content);
 content.setText("");
 equal_flg = 0;
 
 first_num = 0.0;
 sec_num = 0.0;
 OP.clear();
 Suffix.clear();
 Suffix_Num.clear();
 }
 });
 
 //   back      
 Button back = (Button) findViewById(R.id.back);
 back.setOnClickListener(new OnClickListener() {
 
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 content = (EditText) findViewById(R.id.content);
 String str = content.getText().toString();
 if (content.getText().length() != 0)
  content.setText(str.substring(0, str.length() - 1));
  content.setSelection(content.getText().length()); //         ,           ,           
 }
 });
 
 //                   
 //    "0123456789.+-*/()"     ;  "="                 onCreate()      
 OnClickListener mylistener = new OnClickListener() 
 {
 @Override
 public void onClick(View v) {
 // TODO Auto-generated method stub
 Button num_btn = (Button) v;
 content = (EditText) findViewById(R.id.content);
 
 //        
 String content_str = content.getText().toString();
 
 //          ,   “   ”       
 if((1==equal_flg)/*&&(num_btn.getText().toString().equals("1")
  ||num_btn.getText().toString().equals("2")
  ||num_btn.getText().toString().equals("3")
  ||num_btn.getText().toString().equals("4")
  ||num_btn.getText().toString().equals("5")
  ||num_btn.getText().toString().equals("6")
  ||num_btn.getText().toString().equals("7")
  ||num_btn.getText().toString().equals("8")
  ||num_btn.getText().toString().equals("9")
  ||num_btn.getText().toString().equals("0"))*/)
 {
  content.setText("0");
  content.setSelection(content.getText().length());
  Toast.makeText(MainActivity.this,"     !", Toast.LENGTH_LONG).show();
  equal_flg=0; 
 }
 
 //        、    “.”   
 if(("+".equals(content_str.substring(content_str.length()-1,content_str.length()))
  ||"-".equals(content_str.substring(content_str.length()-1,content_str.length()))
  ||"*".equals(content_str.substring(content_str.length()-1,content_str.length()))
  ||"/".equals(content_str.substring(content_str.length()-1,content_str.length()))
  ||".".equals(content_str.substring(content_str.length()-1,content_str.length())))&&((num_btn.getText().toString().equals("+")
  ||num_btn.getText().toString().equals("-")
  ||num_btn.getText().toString().equals("*")
  ||num_btn.getText().toString().equals("/")
  ||num_btn.getText().toString().equals("."))))
 {
  content_str=content_str.substring(0, content_str.length()-1);
  //Toast.makeText(MainActivity.this,"    ?", Toast.LENGTH_LONG).show();
 } 
 
 //    “.”   
 /*if(num_btn.getText().toString().equals("."))
 {
  //         ,          
  if("0"!=content_str)
  {
  judge_str = turn_mark(judge_str);  
  content.setText(judge_str);
  content.setSelection(content.getText().length());
  // Toast.makeText(MainActivity.this,"GET", Toast.LENGTH_LONG).show();
  }
 }*/ 
 
 //      
 //      0,      0     
 if("0".equals(content.getText().toString())
  &&!(num_btn.getText().toString().equals("+"))
  &&!(num_btn.getText().toString().equals("-"))
  &&!(num_btn.getText().toString().equals("*"))
  &&!(num_btn.getText().toString().equals("/"))
  &&!(num_btn.getText().toString().equals(".")))
 {
  //            ??????
  content_str = (String) num_btn.getText();
  //Toast.makeText(MainActivity.this,num_btn.getText(), Toast.LENGTH_LONG).show();
 }
 
 //     
 //      0            
 else
 {
  content_str += num_btn.getText();
  //Toast.makeText(MainActivity.this,content_str, Toast.LENGTH_LONG).show();
 }
 content.setText(content_str);
 content.setSelection(content.getText().length());
 }
 };
 
 //               
 Button num1 = (Button) findViewById(R.id.num_1);
 num1.setOnClickListener(mylistener);
 Button num2 = (Button) findViewById(R.id.num_2);
 num2.setOnClickListener(mylistener);
 Button num3 = (Button) findViewById(R.id.num_3);
 num3.setOnClickListener(mylistener);
 Button num4 = (Button) findViewById(R.id.num_4);
 num4.setOnClickListener(mylistener);
 Button num5 = (Button) findViewById(R.id.num_5);
 num5.setOnClickListener(mylistener);
 Button num6 = (Button) findViewById(R.id.num_6);
 num6.setOnClickListener(mylistener);
 Button num7 = (Button) findViewById(R.id.num_7);
 num7.setOnClickListener(mylistener);
 Button num8 = (Button) findViewById(R.id.num_8);
 num8.setOnClickListener(mylistener);
 Button num9 = (Button) findViewById(R.id.num_9);
 num9.setOnClickListener(mylistener);
 Button point = (Button) findViewById(R.id.point);
 point.setOnClickListener(mylistener);
 Button left = (Button) findViewById(R.id.left);
 left.setOnClickListener(mylistener);
 Button right = (Button) findViewById(R.id.right);
 right.setOnClickListener(mylistener);
 Button plus = (Button) findViewById(R.id.plus);
 plus.setOnClickListener(mylistener);
 Button subtract = (Button) findViewById(R.id.subs);
 subtract.setOnClickListener(mylistener);
 Button multiply = (Button) findViewById(R.id.multiply);
 multiply.setOnClickListener(mylistener);
 Button divide = (Button) findViewById(R.id.division);
 divide.setOnClickListener(mylistener);
 
 
 //    0   
 Button num0 = (Button) findViewById(R.id.num_0);
 num0.setOnClickListener(new OnClickListener() 
 {
 @Override
 public void onClick(View v) 
 {
 content = (EditText) findViewById(R.id.content);
 //          0
 if ("0".equals(content.getText().toString())||""== content.getText().toString())
 {
  content.setText("0");
 }
 //       0  
 else
 {
  String str = content.getText().toString();
   str += "0";
   content.setText(str);
 }
 content.setSelection(content.getText().length());
 // Toast.makeText(MainActivity.this,"GET", Toast.LENGTH_LONG).show();
 } 
 });
 
 //  “-/+”   
 Button mark_sign = (Button) findViewById(R.id.mark_sign);
 mark_sign.setOnClickListener(new OnClickListener() 
 {
 @Override
 public void onClick(View v) 
 {
 
 content = (EditText) findViewById(R.id.content);
 String judge_str = content.getText().toString();
 
 //         ,          
 if("0"!=judge_str&&((-1==judge_str.indexOf("+"))
  ||(-1==judge_str.indexOf("-")))
  ||(-1==judge_str.indexOf("*"))
  ||(-1==judge_str.indexOf("/"))
  ||(-1==judge_str.indexOf("("))
  ||(-1==judge_str.indexOf(")")))
 {
  judge_str = turn_mark(judge_str);  
  content.setText(judge_str);
  content.setSelection(content.getText().length());
  // Toast.makeText(MainActivity.this,"GET", Toast.LENGTH_LONG).show();
 }
 } 
 });
 //   "="     
 Button equal = (Button) findViewById(R.id.equal);
 equal.setOnClickListener(new OnClickListener() 
 {
 @Override
 public void onClick(View v) 
 {
 // TODO Auto-generated method stub
 content = (EditText) findViewById(R.id.content);
 operaline = (EditText) findViewById(R.id.operatline);
 // str_Infix          
 String str_Infix = content.getText().toString();
 //          
 String equate = str_Infix;
 equate += "=";
 operaline.setText(equate);
 operaline.setSelection(content.getText().length());
 //   Analysis   content    
 MainActivity.this.Analysis(str_Infix);   
 System.out.println(Infix);
 //   ,         Infix   
 
 Iterator<String> it = Infix.iterator();
 while (it.hasNext()) 
 {  
  String tmp_str = it.next();
  if (isNum(tmp_str)) 
  {
   //               Suffix  ;
  Suffix.addLast(tmp_str);
  }
  //             ;
  else 
  {
  int OP_level = OP.isEmpty() ? 0 : getLevel(OP.getLast());
  // tmp_str OP           OP
  if (getLevel(tmp_str) > OP_level) 
  {
  OP.addLast(tmp_str);
  }
  // tmp_str OP         
  else 
  {
  // tmp_str ")",  OP        "("
  if (getLevel(tmp_str) == -1) 
  {
  String temp_OP = OP.removeLast();
  while (getLevel(temp_OP) != -2) {
   Suffix.addLast(temp_OP);
   temp_OP = OP.removeLast();
  }
  }
  // tmp_str "(",    OP
  else if (getLevel(tmp_str) == -2) 
  {
  OP.addLast(tmp_str);
  }
  // tmp_str OP_level       "(" ")",
  //  OP      OP   tmp_str OP_level    
  else 
  {
  String str2 = OP.removeLast();
  while (getLevel(str2) >= OP_level) 
  {
   Suffix.addLast(str2);
   if (OP.isEmpty()) 
   {
   break;
   }
   str2 = OP.removeLast();
  }
  OP.addLast(tmp_str);
  }
  }
  }
 }
 Infix.clear();//   Infix  
 // OP       OP  Suffix
 while (!OP.isEmpty()) 
 {
  Suffix.addLast(OP.removeLast());
 }
 System.out.println(Suffix);
 //   ,                Suffix
 
 //           ???           0
 while (!(Suffix.isEmpty())) 
 {
  String count_str = Suffix.removeFirst();
  if (isOP(count_str)) 
  {
  char compare_ch = count_str.charAt(0);
  first_num = Suffix_Num.removeLast();
  sec_num = Suffix_Num.removeLast();
  switch (compare_ch) {
  case '*':
  Suffix_Num.addLast(sec_num * first_num);
  break;
  case '/':
  //              
  if (first_num != 0) 
  {
  Suffix_Num.addLast(sec_num / first_num);
  break;
  } else 
  {
  content = (EditText) findViewById(R.id.content);
  // ?????       ,    
  content.setText("∞");
  content.setSelection(content.getText().length());
  }
  case '+':
  Suffix_Num.addLast(sec_num + first_num);
  break;
  case '-':
  Suffix_Num.addLast(sec_num - first_num);
  break;
  }
  } 
  else 
  {
  Suffix_Num.addLast(Double.parseDouble(count_str));
  }
 }
 //   ,       Suffix_Num   
 
 //         result    Suffix_Num        ,           。
 Double result=Suffix_Num.removeFirst();
 String res_str=Double.toString(result);
 
 if("0".equals(res_str.substring(res_str.length()-1,res_str.length())))
 {
  if(".".equals(res_str.substring(res_str.length()-2,res_str.length()-1)))
  {
  res_str=res_str.substring(0,res_str.length()-2);
  //Toast.makeText(MainActivity.this,res_str, Toast.LENGTH_LONG).show();
  };
 };
  content.setText(res_str);
  content.setSelection(content.getText().length());
  equal_flg = 1;
  if("-".equals(res_str.substring(0,1)))
  res_str=turn_mark(res_str);
 }
 });
 //      ?????android       ?
 first_num = 0.0;
 sec_num = 0.0;
 OP.clear();
 Suffix.clear();
 Suffix_Num.clear();
 }
 
 //    isNum()            
 public boolean isNum(String str) {
 int num = 0;
 for (int i = 0; i < str.length(); i++) {
 String strr = str.substring(i, i + 1);
 if (strr.equals("0") || strr.equals("1") || strr.equals("2")
  || strr.equals("3") || strr.equals("4") || strr.equals("5")
  || strr.equals("6") || strr.equals("7") || strr.equals("8")
  || strr.equals("9") || strr.equals("."))
 num = num + 1;
 }
 if (num == str.length())
 return true;
 else
 return false;
 }
 
 //    isOP()     Suffix           
 public boolean isOP(String strr) {
 if (strr.equals("+") || strr.equals("-") || strr.equals("*")
 || strr.equals("/"))
 return true;
 else
 return false;
 }
 
 //         
 public int getLevel(String str) {
 if (str.equals("*") || str.equals("/")) {
 return 2;
 } else if (str.equals("+") || str.equals("-")) {
 return 1;
 } else if (str.equals("(")) {
 return -2;
 } else if (str.equals(")")) {
 return -1;
 } else {
 return 0;
 }
 }
 
 //      
 public String turn_mark(String str) 
 {
 String temp = "(";
 temp += "-";
 temp += str;
 temp += ")";
 str = temp;
 return str;
 }
 
 //                    
 public void Analysis(String str) {
 String sub = "";
 
 for (int i = 0; i < str.length(); i++) 
 {
 //  substring         
 String strr = str.substring(i, i + 1);
 if (isNum(strr)) 
 {
 sub += strr;
 }
 else 
 {
 if (sub != "")
 {
  Infix.addLast(sub); //   sub Infix
  sub = "";    //  sub  
 }
 Infix.addLast(strr);  // "+-*/" "(" ")"     Infix 
 }
 }
 // ?????  for   IF                
 if (isNum(str.substring(str.length() - 1))) {
 Infix.addLast(sub);   //   sub Infix
 sub = "";      //  sub  
 }
 
 }
 
 @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);
 */
 menu.add(0, 1, 1, "  ");
 menu.add(0, 2, 2, "  ");
 menu.add(0, 3, 3, "  ");
 return super.onCreateOptionsMenu(menu);
 /* return true; */
 }
 
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
 // TODO Auto-generated method stub
 //finish();
 if (item.getItemId() == 2) 
 {
 //   Toast       
 Toast.makeText(MainActivity.this,"  :g   :[email protected]", Toast.LENGTH_LONG).show();
 }
 if (item.getItemId() == 3) 
 {
 //   Toast       
 Toast.makeText(MainActivity.this,"         !", Toast.LENGTH_LONG).show();
 }
 
 return super.onOptionsItemSelected(item);
 };
 
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기