Android用分析字符串的方法实现一个加减乘除的计算器

老师刚教 然后写得比较乱,代码可能有冗余的,还未修改,抽空整理



package com.dzh.jisuanqi;





import java.util.Stack;


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;




public class MainActivity extends Activity {
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,b11,a1,a2,a3,a4,a5,a6;
EditText t1;
String result=new String();
String str=new String();
String str2=new String();//结果计算时保存+ -;
String str3=new String();//计算结果时保存加数和被减数;
String flag="";//计算结果时的运算符
String firstnum=new String();//第一个操作数
String secondnum=new String();//第二个操作数
double sum=0;
String SUM=new String();
String newnum=new String();//构成新数字
Stack<String> stack=new Stack<String>();
@Override




protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);
b5=(Button)findViewById(R.id.button5);
b6=(Button)findViewById(R.id.button6);
b7=(Button)findViewById(R.id.button7);
b8=(Button)findViewById(R.id.button8);
b9=(Button)findViewById(R.id.button9);
b0=(Button)findViewById(R.id.button10);
b11=(Button)findViewById(R.id.button11);
a1=(Button)findViewById(R.id.button12);
a2=(Button)findViewById(R.id.button13);
a3=(Button)findViewById(R.id.button14);
a4=(Button)findViewById(R.id.button15);
a5=(Button)findViewById(R.id.button16);
a6=(Button)findViewById(R.id.button17);
t1=(EditText)findViewById(R.id.editText1);


b1Listener B1=new b1Listener();
b1.setOnClickListener(B1);


b2Listener B2=new b2Listener();
b2.setOnClickListener(B2);


b3Listener B3=new b3Listener();
b3.setOnClickListener(B3);


b4Listener B4=new b4Listener();
b4.setOnClickListener(B4);


b5Listener B5=new b5Listener();
b5.setOnClickListener(B5);


b6Listener B6=new b6Listener();
b6.setOnClickListener(B6);


b7Listener B7=new b7Listener();
b7.setOnClickListener(B7);


b8Listener B8=new b8Listener();
b8.setOnClickListener(B8);


b9Listener B9=new b9Listener();
b9.setOnClickListener(B9);


b0Listener B0=new b0Listener();
b0.setOnClickListener(B0);


b11Listener B11=new b11Listener();
b11.setOnClickListener(B11);


a1Listener A1=new a1Listener();
a1.setOnClickListener(A1);


a2Listener A2=new a2Listener();
a2.setOnClickListener(A2);


a3Listener A3=new a3Listener();
a3.setOnClickListener(A3);


a4Listener A4=new a4Listener();
a4.setOnClickListener(A4);


a5Listener A5=new a5Listener();
a5.setOnClickListener(A5);


a6Listener A6=new a6Listener();
a6.setOnClickListener(A6);


}
/**
* b1-11 对应0-9还有 “.”
* a1-5对应加减乘除等于号
* a6是归零
* @author 
*
*/
class b1Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+"1";
if(!stack.empty())
{
str=stack.pop();//str是当前按钮的前面那个字符,这条语句已经把前面那个字符取出来了
if(str=="*"||str=="/")
{
secondnum=secondnum+"1";
stack.push(str);
}
if(str=="+"||str=="-")
{
stack.push(str);
stack.push("1");
}
if(str!="*"&&str!="/"&&str!="+"&&str!="-")
{
newnum=str+"1";
stack.push(newnum);
}
}
else
{
stack.push("1");
}
t1.setText(result);
}
}
class b2Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+"2";
if(!stack.empty())
{
str=stack.pop();//str是当前按钮的前面那个字符,这条语句已经把前面那个字符取出来了
if(str=="*"||str=="/")
{
secondnum=secondnum+"2";
stack.push(str);
}
if(str=="+"||str=="-")
{
stack.push(str);
stack.push("2");
}
if(str!="*"&&str!="/"&&str!="+"&&str!="-")
{
newnum=str+"2";
stack.push(newnum);
}
}
else
{
stack.push("2");
}
t1.setText(result);
}
}
class b3Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+"3";
if(!stack.empty())
{
str=stack.pop();//str是当前按钮的前面那个字符,这条语句已经把前面那个字符取出来了
if(str=="*"||str=="/")
{
secondnum=secondnum+"3";
stack.push(str);
}
if(str=="+"||str=="-")
{
stack.push(str);
stack.push("3");
}
if(str!="*"&&str!="/"&&str!="+"&&str!="-")
{
newnum=str+"3";
stack.push(newnum);
}
}
else
{
stack.push("3");
}
t1.setText(result);
}
}
class b4Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+"4";
if(!stack.empty())
{
str=stack.pop();//str是当前按钮的前面那个字符,这条语句已经把前面那个字符取出来了
if(str=="*"||str=="/")
{
secondnum=secondnum+"4";
stack.push(str);
}
if(str=="+"||str=="-")
{
stack.push(str);
stack.push("4");
}
if(str!="*"&&str!="/"&&str!="+"&&str!="-")
{
newnum=str+"4";
stack.push(newnum);
}
}
else
{
stack.push("4");
}
t1.setText(result);
}
}
class b5Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+"5";
if(!stack.empty())
{
str=stack.pop();//str是当前按钮的前面那个字符,这条语句已经把前面那个字符取出来了
if(str=="*"||str=="/")
{
secondnum=secondnum+"5";
stack.push(str);
}
if(str=="+"||str=="-")
{
stack.push(str);
stack.push("5");
}
if(str!="*"&&str!="/"&&str!="+"&&str!="-")
{
newnum=str+"5";
stack.push(newnum);
}
}
else
{
stack.push("5");
}
t1.setText(result);
}
}
class b6Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+"6";
if(!stack.empty())
{
str=stack.pop();//str是当前按钮的前面那个字符,这条语句已经把前面那个字符取出来了
if(str=="*"||str=="/")
{
secondnum=secondnum+"6";
stack.push(str);
}
if(str=="+"||str=="-")
{
stack.push(str);
stack.push("6");
}
if(str!="*"&&str!="/"&&str!="+"&&str!="-")
{
newnum=str+"6";
stack.push(newnum);
}
}
else
{
stack.push("6");
}
t1.setText(result);
}
}
class b7Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+"7";
if(!stack.empty())
{
str=stack.pop();//str是当前按钮的前面那个字符,这条语句已经把前面那个字符取出来了
if(str=="*"||str=="/")
{
secondnum=secondnum+"7";
stack.push(str);
}
if(str=="+"||str=="-")
{
stack.push(str);
stack.push("7");
}
if(str!="*"&&str!="/"&&str!="+"&&str!="-")
{
newnum=str+"7";
stack.push(newnum);
}
}
else
{
stack.push("7");
}
t1.setText(result);
}
}
class b8Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+"8";
if(!stack.empty())
{
str=stack.pop();//str是当前按钮的前面那个字符,这条语句已经把前面那个字符取出来了
if(str=="*"||str=="/")
{
secondnum=secondnum+"8";
stack.push(str);
}
if(str=="+"||str=="-")
{
stack.push(str);
stack.push("8");
}
if(str!="*"&&str!="/"&&str!="+"&&str!="-")
{
newnum=str+"8";
stack.push(newnum);
}
}
else
{
stack.push("8");
}
t1.setText(result);
}
}
class b9Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+"9";
if(!stack.empty())
{
str=stack.pop();//str是当前按钮的前面那个字符,这条语句已经把前面那个字符取出来了
if(str=="*"||str=="/")
{
secondnum=secondnum+"9";
stack.push(str);
}
if(str=="+"||str=="-")
{
stack.push(str);
stack.push("9");
}
if(str!="*"&&str!="/"&&str!="+"&&str!="-")
{
newnum=str+"9";
stack.push(newnum);
}
}
else
{
stack.push("9");
}
t1.setText(result);
}
}
class b0Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+"0";
if(!stack.empty())
{
str=stack.pop();//str是当前按钮的前面那个字符,这条语句已经把前面那个字符取出来了
if(str=="*"||str=="/")
{
secondnum=secondnum+"0";
stack.push(str);
}
if(str=="+"||str=="-")
{
stack.push(str);
stack.push("0");
}
if(str!="*"&&str!="/"&&str!="+"&&str!="-")
{
newnum=str+"0";
stack.push(newnum);
}
}
else
{
stack.push("0");
}
t1.setText(result);
}
}
class b11Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+".";
str=stack.pop();
newnum=str+".";
stack.push(newnum);
t1.setText(result);
}
}
class a1Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+"+";
str=stack.pop();
if(str=="*")//乘法前碰到乘法则运算sum然后不入栈,保存为firstnum,加法前则运算sum入栈;
{
double num1=Double.parseDouble(firstnum);
double num2=Double.parseDouble(secondnum);
double sum =num1*num2;
String SUM=Double.toString(sum);
firstnum="";
secondnum="";
stack.push(SUM);
stack.push("+");
}
if(str=="/")
{
double num1=Double.parseDouble(firstnum);
double num2=Double.parseDouble(secondnum);
double sum =num1/num2;
String SUM=Double.toString(sum);
firstnum="";
secondnum="";
stack.push(SUM);
stack.push("+");
}
if(str!="*"&&str!="/")
{
stack.push(str);
stack.push("+");
}
t1.setText(result);
}
}
class a2Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
result=result+"-";
str=stack.pop();
if(str=="*")//乘法前碰到乘法则运算sum然后不入栈,保存为firstnum,加法前则运算sum入栈;
{
double num1=Double.parseDouble(firstnum);
double num2=Double.parseDouble(secondnum);
double sum =num1*num2;
String SUM=Double.toString(sum);
firstnum="";
secondnum="";
stack.push(SUM);
stack.push("-");
}
if(str=="/")
{
double num1=Double.parseDouble(firstnum);
double num2=Double.parseDouble(secondnum);
double sum =num1/num2;
String SUM=Double.toString(sum);
firstnum="";
secondnum="";
stack.push(SUM);
stack.push("-");
}
if(str!="*"&&str!="/")
{
stack.push(str);
stack.push("-");
}
t1.setText(result);
}
}
class a3Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+"*";
str=stack.pop();
if(str!="+"&&str!="-"&&str!="*"&&str!="/")
{


while(str!="+"&&str!="-"&&str!="*"&&str!="/")
{
firstnum=firstnum+str;
if(!stack.empty())
{
str=stack.pop();
}
else{
break;
}
}
if(str=="+"||str=="-"||str=="*"||str=="/")
{
stack.push(str);
}
stack.push("*");
}
if(str=="*")//乘法前碰到乘法则运算sum然后不入栈,保存为firstnum,加法前则运算sum入栈;
{
double num1=Double.parseDouble(firstnum);
double num2=Double.parseDouble(secondnum);
double sum =num1*num2;
SUM=Double.toString(sum);
firstnum="";
secondnum="";
firstnum=SUM;
stack.push("*");
}
if(str=="/")
{
double num1=Double.parseDouble(firstnum);
double num2=Double.parseDouble(secondnum);
double sum =num1/num2;
SUM=Double.toString(sum);
firstnum="";
secondnum="";
firstnum=SUM;
stack.push("/");
}
t1.setText(result);
}
}
class a4Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+"/";
str=stack.pop();
if(str!="+"&&str!="-"&&str!="*"&&str!="/")
{


while(str!="+"&&str!="-"&&str!="*"&&str!="/")
{
firstnum=firstnum+str;
if(!stack.empty())
{
str=stack.pop();
}
else{
break;
}
}
if(str=="+"||str=="-"||str=="*"||str=="/")
{
stack.push(str);
}
stack.push("/");
}
if(str=="*")//乘法前碰到乘法则运算sum然后不入栈,保存为firstnum,加法前则运算sum入栈;
{
double num1=Double.parseDouble(firstnum);
double num2=Double.parseDouble(secondnum);
double sum =num1*num2;
String SUM=Double.toString(sum);
firstnum="";
secondnum="";
firstnum=SUM;
stack.push("*");
}
if(str=="/")
{
double num1=Double.parseDouble(firstnum);
double num2=Double.parseDouble(secondnum);
double sum =num1/num2;
String SUM=Double.toString(sum);
firstnum="";
secondnum="";
firstnum=SUM;
stack.push("/");
}
t1.setText(result);
}
}
class a5Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result=result+"=";
str=stack.pop();
if(str=="*")//乘法前碰到乘法则运算sum然后不入栈,保存为firstnum,加法前则运算sum入栈;
{
double num1=Double.parseDouble(firstnum);
double num2=Double.parseDouble(secondnum);
sum =num1*num2;
SUM=Double.toString(sum);
firstnum="";
secondnum="";
stack.push(SUM);
}
if(str=="/")
{
double num1=Double.parseDouble(firstnum);
double num2=Double.parseDouble(secondnum);
sum =num1/num2;
SUM=Double.toString(sum);
firstnum="";
secondnum="";
stack.push(SUM);
}
if(str!="*"&&str!="/")
{
stack.push(str);
}




//重头戏开始 最后一步 计算最后加减字符串的值
while(!stack.empty())
{


str=stack.pop();
if(stack.empty())
{
t1.setText(str);
}
else
{
str2=stack.pop();
str3=stack.pop();
firstnum=str;
secondnum=str3;
if(str2=="+")
{
double num1=Double.parseDouble(firstnum);
double num2=Double.parseDouble(secondnum);
double sum =num1+num2;
SUM=Double.toString(sum);
firstnum="";
secondnum="";
if(stack.empty())
{
t1.setText(SUM);
break;
}
else
stack.push(SUM);
}
if(str2=="-")
{
double num1=Double.parseDouble(firstnum);
double num2=Double.parseDouble(secondnum);
double sum =num2-num1;
SUM=Double.toString(sum);
firstnum="";
secondnum="";
if(stack.empty())
{
t1.setText(SUM);
break;
}
else
stack.push(SUM);
}
}
}
}
}
class a6Listener implements OnClickListener{


@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
result="";
firstnum="";
secondnum="";
newnum="";
sum=0;
stack.clear();
t1.setText(result);
}
}






@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;
}
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package ymq.demo03; import android.app.Activity; import android.os.Bundle; import android.view.*; import android.widget.*; public class demo03 extends Activity { /** Called when the activity is first created. */ String str=""; EditText et; int c=0,flag=0; double b=0.0,g=0.0,f=0.0; View vi; public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add(0, 1, 1, "退出"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub if(item.getItemId()==1){finish();} return super.onOptionsItemSelected(item); } //计算方法 public double calculater(){ switch(c){ case 0:f=g;break; case 1:f=b+g;break; case 2:f=b-g;break; case 3:f=b*g;break; case 4:f=b/g;break; } b=f; c=0; return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获得按键 final Button number[]=new Button[10]; final Button fuhao[]=new Button[11]; fuhao[0]=(Button)findViewById(R.id.button01); fuhao[1]=(Button)findViewById(R.id.button02); fuhao[2]=(Button)findViewById(R.id.button03); fuhao[3]=(Button)findViewById(R.id.button04); fuhao[4]=(Button)findViewById(R.id.button05); fuhao[5]=(Button)findViewById(R.id.button06); fuhao[6]=(Button)findViewById(R.id.buttonce); fuhao[7]=(Button)findViewById(R.id.buttonc); fuhao[8]=(Button)findViewById(R.id.zheng); fuhao[9]=(Button)findViewById(R.id.kaifang); fuhao[10]=(Button)findViewById(R.id.pingfang); number[0]=(Button)findViewById(R.id.button0); number[1]=(Button)findViewById(R.id.button1); number[2]=(Button)findViewById(R.id.button2); number[3]=(Button)findViewById(R.id.button3); number[4]=(Button)findViewById(R.id.button4); number[5]=(Button)findViewById(R.id.button5); number[6]=(Button)findViewById(R.id.button6); number[7]=(Butto
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值