自己做了个简易的小计算器,目前仅实现加减乘除操作,后续还未完善,只是简单记录一下自己的学习历程
计算器制作布局很简单,只是运用的算法清晰即可制作,我是运用了中缀表达式转化成后缀表达式,然后来的出计算器的算法。
首先是
(1)若为数字直接输出,并且储存在pre_out数组中,
(2)遇到运算符,若栈为空,则进栈,不然先弹出优先级较高的运算符到pre_out,再将运算符弹入栈中
(3)当读取完字符串,就将栈里面的元素都弹出,此时pre_out就是一个后缀表达式
(4)然后对pre_out进行操作,当遇到操作数,则直接加入栈中,当遇到运算符,则从栈中弹出两个元素进行运算,但是要注意元素的先后顺序和运算符的种类,即减法和除法要注意元素弹出的顺序,后弹出的应该作为被除数和被减数。
(5)在操作过程中,如果遇到运算符但是栈中只有一个元素,那么说明无法进行运算,表达式为错误表达式,我们就用Toast提示用户重新输入
(6)对的表达式最后应该栈中只有一个元素,那个就是结果取出输出即可
1 /** 2 * author Cc_Elvis 3 */ 4 package com.hcc.my_calculate; 5 6 import java.util.LinkedList; 7 import java.util.Stack; 8 9 import android.R.integer; 10 import android.R.layout; 11 import android.app.Activity; 12 import android.os.Bundle; 13 import android.view.Menu; 14 import android.view.MenuItem; 15 import android.view.View; 16 import android.view.View.OnClickListener; 17 import android.widget.Button; 18 import android.widget.TextView; 19 import android.widget.Toast; 20 21 public class MainActivity extends Activity 22 { 23 private TextView screenView; 24 private Button zeroBtn; 25 private Button oneBtn; 26 private Button twoBtn; 27 private Button threeBtn; 28 private Button fourBtn; 29 private Button fiveBtn; 30 private Button sixBtn; 31 private Button sevenBtn; 32 private Button eightBtn; 33 private Button nineBtn; 34 private Button plusBtn; 35 private Button minusBtn; 36 private Button divideBtn; 37 private Button MultiBtn; 38 private Button equalBtn; 39 private Button resBtn; 40 private LinkedList<String> NumList=new LinkedList<String>(); 41 private String showString=""; 42 private String[] pre_out=new String[100]; 43 private int pre_out_Num=0; 44 boolean illegal_flag=true; 45 boolean isString=true; 46 @Override 47 protected void onCreate(Bundle savedInstanceState) 48 { 49 super.onCreate(savedInstanceState); 50 setContentView(R.layout.myview); 51 (oneBtn=(Button)findViewById(R.id.One)).setOnClickListener(listen); 52 (twoBtn=(Button)findViewById(R.id.Two)).setOnClickListener(listen); 53 (threeBtn=(Button)findViewById(R.id.Three)).setOnClickListener(listen); 54 (fourBtn=(Button)findViewById(R.id.Four)).setOnClickListener(listen); 55 (fiveBtn=(Button)findViewById(R.id.Five)).setOnClickListener(listen); 56 (sixBtn=(Button)findViewById(R.id.Six)).setOnClickListener(listen); 57 (sevenBtn=(Button)findViewById(R.id.Seven)).setOnClickListener(listen); 58 (eightBtn=(Button)findViewById(R.id.Eight)).setOnClickListener(listen); 59 (nineBtn=(Button)findViewById(R.id.Nine)).setOnClickListener(listen); 60 (equalBtn=(Button)findViewById(R.id.equal)).setOnClickListener(listen); 61 (divideBtn=(Button)findViewById(R.id.Division)).setOnClickListener(listen); 62 (minusBtn=(Button)findViewById(R.id.Minus)).setOnClickListener(listen); 63 (plusBtn=(Button)findViewById(R.id.plus)).setOnClickListener(listen); 64 (MultiBtn=(Button)findViewById(R.id.Multip)).setOnClickListener(listen); 65 (resBtn=(Button)findViewById(R.id.restart)).setOnClickListener(listen); 66 (zeroBtn=(Button)findViewById(R.id.Zero)).setOnClickListener(listen); 67 screenView=(TextView)findViewById(R.id.Screen); 68 Init(); 69 } 70 private OnClickListener listen=new OnClickListener() 71 { 72 73 @Override 74 public void onClick(View v) 75 { 76 Button btn=(Button)v; 77 if( btn.getText().equals("1") || btn.getText().equals("2") || 78 btn.getText().equals("3") || btn.getText().equals("4") || 79 btn.getText().equals("5") || btn.getText().equals("6") || 80 btn.getText().equals("7") || btn.getText().equals("8") || 81 btn.getText().equals("9") || btn.getText().equals("0"))//如果为数字,直接输出并储存在pre_out数组里以转化成后缀表达式 82 { 83 String c=btn.getText().toString(); 84 addString(c);//add to TextEdit 85 if(isString) 86 { 87 pre_out[pre_out_Num]+=c;//add to Stack 88 isString=true; 89 } 90 else 91 { 92 pre_out_Num++; 93 pre_out[pre_out_Num]=c; 94 isString=true; 95 } 96 } 97 else if(btn.getText().equals("AC"))//reset 98 { 99 restart();100 }101 else if(btn.getText().equals("="))//遇到‘=’则对栈开始进行清空并且输出到pre_out里面102 {103 while(!NumList.isEmpty())104 {105 pre_out[++pre_out_Num]=NumList.removeFirst();106 }107 if(!GetResult())//judge if result true?108 {109 Toast.makeText(MainActivity.this, "输入不合法", Toast.LENGTH_LONG).show();110 }111 else112 {113 String restString=screenView.getText().toString();114 restart();115 screenView.setText(restString);116 }117 }118 else if (btn.getText().equals("+") || btn.getText().equals("-") ||119 btn.getText().equals("*") || btn.getText().equals("/"))120 {121 isString=false;122 String c=btn.getText().toString();123 addString(c);//输出到TextView上面124 if(NumList.isEmpty())//若栈为空则先将运算符压入125 {126 NumList.addFirst(c);127 }128 else129 {130 while(NumList.size()>=1&&get_prior(c)<=get_prior(NumList.getFirst().toString()))////先弹出优先级较高的运算符131 {132 if(pre_out_Num>0)133 pre_out[++pre_out_Num]=NumList.removeFirst();//转为后缀表达式序列134 else pre_out[pre_out_Num]=NumList.removeFirst();135 }136 NumList.addFirst(c);//再将此运算符入栈137 }138 }139 }140 };141 private void restart()//重新开始142 {143 Init();144 NumList.clear();145 pre_out_Num=0;146 screenView.setText(""+0);147 showString="";148 }149 private void addString(String c)//显示结果框的字符串150 {151 showString+=c;152 screenView.setText(showString);153 }154 private boolean GetResult()//获取结果155 {156 int length=0;157 while(pre_out[length]!="")158 {159 String c=pre_out[length];160 if(get_prior(c)==0)//数字直接压入栈161 {162 NumList.addFirst(c);163 length++;164 }165 else166 {167 compute(c);168 length++;169 }170 }171 if(NumList.size()>1||get_prior(NumList.getFirst())!=0||illegal_flag==false)172 return false;173 if(!NumList.isEmpty())174 {175 int result=Integer.parseInt(NumList.removeFirst());176 screenView.setText(""+result);177 return true;178 }179 else return false;180 }181 private int get_prior(String c)182 {183 if(c.equals("+")||c.equals("-"))184 return 1;185 else if(c.equals("*")||c.equals("/"))186 return 2;187 else return 0;188 }189 private void compute(String c)//计算每一次的结果值190 {191 int sum =0,a,b;192 if(NumList.size()>=2)//如果只剩下一个数,那么肯定不合法193 {194 a=Integer.parseInt(NumList.removeFirst());195 b=Integer.parseInt(NumList.removeFirst());196 }197 else 198 {199 illegal_flag=false;200 return ;201 } ;202 if(c.equals("+"))203 {204 sum=a+b;205 } 206 else if(c.equals("*"))207 {208 sum=a*b;209 }210 else if(c.equals("-"))211 {212 sum=b-a;213 }214 else if(c.equals("/"))215 {216 sum=b/a;217 }218 String s=""+sum;219 NumList.addFirst(s);220 }221 private void Init()//初始化表达式222 {223 for(int i=0;i<100;i++)224 pre_out[i]="";225 }226 }
1 <? 2 <LinearLayout 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" 5 android:orientation="vertical" > 6 <TextView 7 android:layout_marginTop="40dp" 8 android:layout_width="fill_parent" 9 android:layout_height="50dp" 10 android:textSize="40dp" 11 android:text="0" 12 android:gravity="right" 13 android:id="@+id/Screen" 14 /> 15 <GridLayout 16 android:layout_marginLeft="10dp" 17 android:layout_marginTop="50dp" 18 android:layout_height="wrap_content" 19 android:layout_width="wrap_content" 20 android:columnCount="4" 21 android:rowCount="4"> 22 <Button 23 android:layout_marginLeft="10dp" 24 android:layout_marginTop="10dp" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:text="7" 28 android:id="@+id/Seven" 29 /> 30 <Button 31 android:layout_marginLeft="10dp" 32 android:layout_marginTop="10dp" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:text="8" 36 android:id="@+id/Eight" 37 /> 38 <Button 39 android:layout_marginLeft="10dp" 40 android:layout_marginTop="10dp" 41 android:layout_width="wrap_content" 42 android:layout_height="wrap_content" 43 android:text="9" 44 android:id="@+id/Nine" 45 /> 46 <Button 47 android:layout_marginLeft="10dp" 48 android:layout_marginTop="10dp" 49 android:layout_width="wrap_content" 50 android:layout_height="wrap_content" 51 android:text="/" 52 android:id="@+id/Division" 53 /> 54 <Button 55 android:layout_marginLeft="10dp" 56 android:layout_marginTop="10dp" 57 android:layout_width="wrap_content" 58 android:layout_height="wrap_content" 59 android:text="4" 60 android:id="@+id/Four" 61 /> 62 <Button 63 android:layout_marginLeft="10dp" 64 android:layout_marginTop="10dp" 65 android:layout_width="wrap_content" 66 android:layout_height="wrap_content" 67 android:text="5" 68 android:id="@+id/Five" 69 /> 70 <Button 71 android:layout_marginLeft="10dp" 72 android:layout_marginTop="10dp" 73 android:layout_width="wrap_content" 74 android:layout_height="wrap_content" 75 android:text="6" 76 android:id="@+id/Six" 77 /> 78 <Button 79 android:layout_marginLeft="10dp" 80 android:layout_marginTop="10dp" 81 android:layout_width="wrap_content" 82 android:layout_height="wrap_content" 83 android:text="@string/Multi" 84 android:id="@+id/Multip" 85 /> 86 <Button 87 android:layout_marginLeft="10dp" 88 android:layout_marginTop="10dp" 89 android:layout_width="wrap_content" 90 android:layout_height="wrap_content" 91 android:text="1" 92 android:id="@+id/One" 93 /> 94 <Button 95 android:layout_marginLeft="10dp" 96 android:layout_marginTop="10dp" 97 android:layout_width="wrap_content" 98 android:layout_height="wrap_content" 99 android:text="2"100 android:id="@+id/Two"101 />102 <Button103 android:layout_marginLeft="10dp"104 android:layout_marginTop="10dp"105 android:layout_width="wrap_content"106 android:layout_height="wrap_content"107 android:text="3"108 android:id="@+id/Three"109 />110 <Button111 android:layout_marginLeft="10dp"112 android:layout_marginTop="10dp"113 android:layout_width="wrap_content"114 android:layout_height="wrap_content"115 android:text="-"116 android:id="@+id/Minus"117 />118 <Button119 android:layout_marginLeft="10dp"120 android:layout_marginTop="10dp"121 android:layout_width="wrap_content"122 android:layout_height="wrap_content"123 android:text="0"124 android:id="@+id/Zero"125 />126 <Button127 android:layout_marginLeft="10dp"128 android:layout_marginTop="10dp"129 android:id="@+id/equal"130 android:layout_gravity="fill"131 android:text="=" 132 />133 <Button134 android:layout_marginLeft="10dp"135 android:layout_marginTop="10dp"136 android:id="@+id/restart"137 android:text="@string/ac" 138 />139 140 <Button141 android:layout_marginLeft="10dp"142 android:layout_marginTop="10dp"143 android:layout_width="wrap_content"144 android:layout_height="wrap_content"145 android:text="+"146 android:id="@+id/plus"147 />148 </GridLayout>149 150 <TextView151 android:layout_width="fill_parent"152 android:layout_height="wrap_content"153 android:layout_marginBottom="10dp"154 android:text="This is CC's Calculator" />155 156 </LinearLayout>
代码量较小,健壮性还有待完善。。。。
---恢复内容结束---
原标题:安卓简易编辑器
关键词: